Skip to content

Commit

Permalink
unify inforschema logic for rename tables
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyfhust committed Oct 27, 2023
1 parent 30288c7 commit 9d00706
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
11 changes: 2 additions & 9 deletions pkg/ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 8 additions & 5 deletions pkg/infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 9d00706

Please sign in to comment.