From 84708849c837104f1e4691e8a5a01d3f12baf4de Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Tue, 19 Sep 2023 17:53:07 +0800 Subject: [PATCH 1/4] fix duplicate inforschema information of rename tables --- ddl/ddl_worker.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index 30782fa694985..5c62d670e2f87 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -1352,9 +1352,12 @@ 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{ + if i == 0 { + continue + } + affects[i-1] = &model.AffectedOption{ SchemaID: newSchemaID, TableID: tableIDs[i], OldTableID: tableIDs[i], From 046de23a246f830474d312c81962dd724c9a014d Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Tue, 19 Sep 2023 18:12:15 +0800 Subject: [PATCH 2/4] add test --- ddl/db_rename_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ddl/db_rename_test.go b/ddl/db_rename_test.go index d2a33c2556734..0bbe0eaea7e3e 100644 --- a/ddl/db_rename_test.go +++ b/ddl/db_rename_test.go @@ -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")) +} From 292d41daa14381b59ec080bb3f20da9e162cc02b Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Wed, 20 Sep 2023 14:49:39 +0800 Subject: [PATCH 3/4] add comment --- ddl/ddl_worker.go | 1 + 1 file changed, 1 insertion(+) diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index 5c62d670e2f87..9b447af86eba5 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -1354,6 +1354,7 @@ func updateSchemaVersion(d *ddlCtx, t *meta.Meta, job *model.Job, multiInfos ... } affects := make([]*model.AffectedOption, len(newSchemaIDs)-1) for i, newSchemaID := range newSchemaIDs { + // Do not add the first table to AffectedOpts. Related issue tidb#47064. if i == 0 { continue } From c034c2203d3e19e24314896580c14134d09864b4 Mon Sep 17 00:00:00 2001 From: jiyfhust Date: Wed, 20 Sep 2023 17:02:43 +0800 Subject: [PATCH 4/4] fix --- ddl/fktest/foreign_key_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/fktest/foreign_key_test.go b/ddl/fktest/foreign_key_test.go index b3af6a87050e7..c739e40121118 100644 --- a/ddl/fktest/foreign_key_test.go +++ b/ddl/fktest/foreign_key_test.go @@ -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")