Skip to content

Commit

Permalink
upgrades: modify InjectLegacyTable to handle dynamically assigned IDs
Browse files Browse the repository at this point in the history
This patch modifies the `InjectLegacyTable` function, which is used by
upgrade tests to inject an old version of a table descriptor, to handle
the case where the table has a dynamically assigned ID.

Release note: None
  • Loading branch information
andyyang890 committed Jan 18, 2023
1 parent 4066912 commit 77732e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/upgrade/upgrades/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ go_test(
"//pkg/sql/catalog/descbuilder",
"//pkg/sql/catalog/descpb",
"//pkg/sql/catalog/descs",
"//pkg/sql/catalog/schemadesc",
"//pkg/sql/catalog/schematelemetry/schematelemetrycontroller",
"//pkg/sql/catalog/systemschema",
"//pkg/sql/catalog/tabledesc",
Expand Down
27 changes: 22 additions & 5 deletions pkg/upgrade/upgrades/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemadesc"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/systemschema"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -76,12 +78,27 @@ func InjectLegacyTable(
err := s.InternalDB().(descs.DB).DescsTxn(ctx, func(
ctx context.Context, txn descs.Txn,
) error {
id := table.GetID()
tab, err := txn.Descriptors().MutableByID(txn.KV()).Table(ctx, id)
if err != nil {
return err
deprecatedDesc := getDeprecatedDescriptor()
var tab *tabledesc.Mutable
switch id := table.GetID(); id {
// If the table descriptor does not have a valid ID, it must be a system
// table with a dynamically-allocated ID.
case descpb.InvalidID:
var err error
tab, err = txn.Descriptors().MutableByName(txn.KV()).Table(ctx,
systemschema.SystemDB, schemadesc.GetPublicSchema(), table.GetName())
if err != nil {
return err
}
deprecatedDesc.ID = tab.GetID()
default:
var err error
tab, err = txn.Descriptors().MutableByID(txn.KV()).Table(ctx, id)
if err != nil {
return err
}
}
builder := tabledesc.NewBuilder(getDeprecatedDescriptor())
builder := tabledesc.NewBuilder(deprecatedDesc)
if err := builder.RunPostDeserializationChanges(); err != nil {
return err
}
Expand Down

0 comments on commit 77732e1

Please sign in to comment.