Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ddl/db_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,15 @@ func TestRenameMultiTables(t *testing.T) {
tk.MustExec("drop database test1")
tk.MustExec("drop database test")
}

func TestRenameMultiTablesIssue47064(t *testing.T) {
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(a int)")
tk.MustExec("create table t2(a int)")
tk.MustExec("create database test1")
tk.MustExec("rename table test.t1 to test1.t1, test.t2 to test1.t2")
tk.MustQuery("select column_name from information_schema.columns where table_name = 't1'").Check(testkit.Rows("a"))
}
8 changes: 6 additions & 2 deletions ddl/ddl_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,9 +1377,13 @@ 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))
affects := make([]*model.AffectedOption, len(newSchemaIDs)-1)
for i, newSchemaID := range newSchemaIDs {
affects[i] = &model.AffectedOption{
// Do not add the first table to AffectedOpts. Related issue tidb#47064.
if i == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we not change these codes and remove line1393-1295 to fix it?

Copy link
Contributor Author

@jiyfhust jiyfhust Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am afaid it can't work if we just remove line 1393-1395, because the infoschema builder will applyTableUpdate on a not normal "SchemaDiff". It will give applyTableUpdate a risk, is it?

Copy link
Contributor

@zimulala zimulala Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be fixed, but it does have potential risks. We may need to remove "applyTableUpdate" like "applyCreateTables" for safety.

Or we can do this according to your PR first, and then do unification.

Copy link
Contributor Author

@jiyfhust jiyfhust Sep 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do unification later and i'm pleased to do that. We can choose the second method first, because it is a simple and reliable way to fix the problem.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We can fix the bug first.
Thank you very much for the follow-up help to unify this logic.

continue
}
affects[i-1] = &model.AffectedOption{
SchemaID: newSchemaID,
TableID: tableIDs[i],
OldTableID: tableIDs[i],
Expand Down
2 changes: 1 addition & 1 deletion ddl/tests/fk/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ func TestRenameTablesWithForeignKey(t *testing.T) {
// check the schema diff
diff := getLatestSchemaDiff(t, tk)
require.Equal(t, model.ActionRenameTables, diff.Type)
require.Equal(t, 3, len(diff.AffectedOpts))
require.Equal(t, 2, len(diff.AffectedOpts))

// check referred foreign key information.
t1ReferredFKs := getTableInfoReferredForeignKeys(t, dom, "test", "t1")
Expand Down