Skip to content

Commit

Permalink
infoschema_v2: fix rename table (#52151)
Browse files Browse the repository at this point in the history
  • Loading branch information
GMHDBJD committed Mar 28, 2024
1 parent 538352c commit 8bca3dd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
5 changes: 2 additions & 3 deletions pkg/infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,18 @@ func dropTableForUpdate(b *Builder, newTableID, oldTableID int64, dbInfo *model.
// TODO: Check how this would work with ADD/REMOVE Partitioning,
// which may have AutoID not connected to tableID
// TODO: can there be _tidb_rowid AutoID per partition?
oldAllocs, _ := allocByID(b.infoSchema, oldTableID)
oldAllocs, _ := allocByID(b, oldTableID)
newAllocs = filterAllocators(diff, oldAllocs)
}

tmpIDs := tblIDs
if (diff.Type == model.ActionRenameTable || diff.Type == model.ActionRenameTables) && diff.OldSchemaID != diff.SchemaID {
oldRoDBInfo, ok := b.infoSchema.SchemaByID(diff.OldSchemaID)
oldDBInfo, ok := oldSchemaInfo(b, diff)
if !ok {
return nil, newAllocs, ErrDatabaseNotExists.GenWithStackByArgs(
fmt.Sprintf("(Schema ID %d)", diff.OldSchemaID),
)
}
oldDBInfo := b.getSchemaAndCopyIfNecessary(oldRoDBInfo.Name.L)
tmpIDs = applyDropTable(b, diff, oldDBInfo, oldTableID, tmpIDs)
} else {
tmpIDs = applyDropTable(b, diff, dbInfo, oldTableID, tmpIDs)
Expand Down
9 changes: 0 additions & 9 deletions pkg/infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,6 @@ func (is *infoSchema) SchemaTableInfos(schema model.CIStr) []*model.TableInfo {
return getTableInfoList(is.SchemaTables(schema))
}

// allocByID returns the Allocators of a table.
func allocByID(is InfoSchema, id int64) (autoid.Allocators, bool) {
tbl, ok := is.TableByID(id)
if !ok {
return autoid.Allocators{}, false
}
return tbl.Allocators(nil), true
}

// AllSchemaNames returns all the schemas' names.
func AllSchemaNames(is InfoSchema) (names []string) {
schemas := is.AllSchemaNames()
Expand Down
27 changes: 27 additions & 0 deletions pkg/infoschema/infoschema_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,33 @@ func updateInfoSchemaBundles(b *Builder) {
}
}

func oldSchemaInfo(b *Builder, diff *model.SchemaDiff) (*model.DBInfo, bool) {
if b.enableV2 {
return b.infoschemaV2.SchemaByID(diff.OldSchemaID)
}

oldRoDBInfo, ok := b.infoSchema.SchemaByID(diff.OldSchemaID)
if ok {
oldRoDBInfo = b.getSchemaAndCopyIfNecessary(oldRoDBInfo.Name.L)
}
return oldRoDBInfo, ok
}

// allocByID returns the Allocators of a table.
func allocByID(b *Builder, id int64) (autoid.Allocators, bool) {
var is InfoSchema
if b.enableV2 {
is = &b.infoschemaV2
} else {
is = b.infoSchema
}
tbl, ok := is.TableByID(id)
if !ok {
return autoid.Allocators{}, false
}
return tbl.Allocators(nil), true
}

// TODO: more UT to check the correctness.
func (b *Builder) applyTableUpdateV2(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) {
oldDBInfo, ok := b.infoschemaV2.SchemaByID(diff.SchemaID)
Expand Down

0 comments on commit 8bca3dd

Please sign in to comment.