diff --git a/pkg/ddl/ddl_worker.go b/pkg/ddl/ddl_worker.go index 12bfeef693ed2..39d198dbb4281 100644 --- a/pkg/ddl/ddl_worker.go +++ b/pkg/ddl/ddl_worker.go @@ -1366,22 +1366,15 @@ func updateSchemaVersion(d *ddlCtx, t *meta.Meta, job *model.Job, multiInfos ... if err != nil { return 0, errors.Trace(err) } - affects := make([]*model.AffectedOption, len(newSchemaIDs)-1) + affects := make([]*model.AffectedOption, len(newSchemaIDs)) for i, newSchemaID := range newSchemaIDs { - // Do not add the first table to AffectedOpts. Related issue tidb#47064. - if i == 0 { - continue - } - affects[i-1] = &model.AffectedOption{ + affects[i] = &model.AffectedOption{ SchemaID: newSchemaID, TableID: tableIDs[i], OldTableID: tableIDs[i], OldSchemaID: oldSchemaIDs[i], } } - diff.TableID = tableIDs[0] - diff.SchemaID = newSchemaIDs[0] - diff.OldSchemaID = oldSchemaIDs[0] diff.AffectedOpts = affects case model.ActionExchangeTablePartition: // From start of function: diff.SchemaID = job.SchemaID diff --git a/pkg/infoschema/builder.go b/pkg/infoschema/builder.go index 54f8d8a85dc58..747c6ed08fbde 100644 --- a/pkg/infoschema/builder.go +++ b/pkg/infoschema/builder.go @@ -223,8 +223,8 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro return b.applyDropTableOrPartition(m, diff) case model.ActionRecoverTable: return b.applyRecoverTable(m, diff) - case model.ActionCreateTables: - return b.applyCreateTables(m, diff) + case model.ActionCreateTables, model.ActionRenameTables: + return b.applyCreateOrRenameTables(m, diff) case model.ActionReorganizePartition, model.ActionRemovePartitioning, model.ActionAlterTablePartitioning: return b.applyReorganizePartition(m, diff) @@ -237,18 +237,21 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro } } -func (b *Builder) applyCreateTables(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) { +func (b *Builder) applyCreateOrRenameTables(m *meta.Meta, diff *model.SchemaDiff) ([]int64, error) { tblIDs := make([]int64, 0, len(diff.AffectedOpts)) if diff.AffectedOpts != nil { for _, opt := range diff.AffectedOpts { affectedDiff := &model.SchemaDiff{ Version: diff.Version, - Type: model.ActionCreateTable, SchemaID: opt.SchemaID, TableID: opt.TableID, OldSchemaID: opt.OldSchemaID, OldTableID: opt.OldTableID, } + affectedDiff.Type = model.ActionCreateTable + if diff.Type == model.ActionRenameTables { + affectedDiff.Type = model.ActionRenameTable + } affectedIDs, err := b.ApplyDiff(m, affectedDiff) if err != nil { return nil, errors.Trace(err) @@ -764,7 +767,7 @@ func (b *Builder) applyRecoverSchema(m *meta.Meta, diff *model.SchemaDiff) ([]in dbInfo: di, tables: make(map[string]table.Table, len(diff.AffectedOpts)), } - return b.applyCreateTables(m, diff) + return b.applyCreateOrRenameTables(m, diff) } func (b *Builder) copySortedTablesBucket(bucketIdx int) {