Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema_v2: fix rename table #52151

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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