Skip to content

Commit 71e6696

Browse files
authored
infoschema: update raw args to make it drop correct table in RENAME TABLE (#44585) (#44594)
close #43714
1 parent 3e9e4e4 commit 71e6696

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

ddl/table.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package ddl
1616

1717
import (
1818
"context"
19+
"encoding/json"
1920
"fmt"
2021
"strconv"
2122
"sync/atomic"
@@ -1166,10 +1167,20 @@ func finishJobRenameTable(d *ddlCtx, t *meta.Meta, job *model.Job) (int64, error
11661167
job.State = model.JobStateCancelled
11671168
return 0, errors.Trace(err)
11681169
}
1170+
// Before updating the schema version, we need to reset the old schema ID to new schema ID, so that
1171+
// the table info can be dropped normally in `ApplyDiff`. This is because renaming table requires two
1172+
// schema versions to complete.
1173+
oldRawArgs := job.RawArgs
1174+
job.Args[0] = job.SchemaID
1175+
job.RawArgs, err = json.Marshal(job.Args)
1176+
if err != nil {
1177+
return 0, errors.Trace(err)
1178+
}
11691179
ver, err := updateSchemaVersion(d, t, job)
11701180
if err != nil {
11711181
return ver, errors.Trace(err)
11721182
}
1183+
job.RawArgs = oldRawArgs
11731184
job.FinishTableJob(model.JobStateDone, model.StatePublic, ver, tblInfo)
11741185
return ver, nil
11751186
}
@@ -1190,10 +1201,21 @@ func finishJobRenameTables(d *ddlCtx, t *meta.Meta, job *model.Job,
11901201
}
11911202
tblInfos = append(tblInfos, tblInfo)
11921203
}
1204+
// Before updating the schema version, we need to reset the old schema ID to new schema ID, so that
1205+
// the table info can be dropped normally in `ApplyDiff`. This is because renaming table requires two
1206+
// schema versions to complete.
1207+
var err error
1208+
oldRawArgs := job.RawArgs
1209+
job.Args[0] = newSchemaIDs
1210+
job.RawArgs, err = json.Marshal(job.Args)
1211+
if err != nil {
1212+
return 0, errors.Trace(err)
1213+
}
11931214
ver, err := updateSchemaVersion(d, t, job)
11941215
if err != nil {
11951216
return ver, errors.Trace(err)
11961217
}
1218+
job.RawArgs = oldRawArgs
11971219
job.FinishMultipleTableJob(model.JobStateDone, model.StatePublic, ver, tblInfos)
11981220
return ver, nil
11991221
}

infoschema/BUILD.bazel

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ go_library(
5959

6060
go_test(
6161
name = "infoschema_test",
62-
timeout = "short",
6362
srcs = [
6463
"cache_test.go",
6564
"cluster_tables_test.go",
@@ -69,8 +68,6 @@ go_test(
6968
"tables_test.go",
7069
],
7170
embed = [":infoschema"],
72-
flaky = True,
73-
shard_count = 50,
7471
deps = [
7572
"//config",
7673
"//ddl/placement",

infoschema/infoschema_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,3 +813,23 @@ func TestIndexComment(t *testing.T) {
813813
tk.MustExec("create table t1 (c1 VARCHAR(10) NOT NULL COMMENT 'Abcdefghijabcd', c2 INTEGER COMMENT 'aBcdefghijab',c3 INTEGER COMMENT '01234567890', c4 INTEGER, c5 INTEGER, c6 INTEGER, c7 INTEGER, c8 VARCHAR(100), c9 CHAR(50), c10 DATETIME, c11 DATETIME, c12 DATETIME,c13 DATETIME, INDEX i1 (c1) COMMENT 'i1 comment',INDEX i2(c2) ) COMMENT='ABCDEFGHIJabc';")
814814
tk.MustQuery("SELECT index_comment,char_length(index_comment),COLUMN_NAME FROM information_schema.statistics WHERE table_name='t1' ORDER BY index_comment;").Check(testkit.Rows(" 0 c2", "i1 comment 10 c1"))
815815
}
816+
817+
func TestInfoSchemaRenameTable(t *testing.T) {
818+
store := testkit.CreateMockStore(t)
819+
820+
tk := testkit.NewTestKit(t, store)
821+
tk.MustExec("use test")
822+
tk.MustExec("create table test.t1 (id int primary key, a text);")
823+
tk.MustExec("insert test.t1 values(1,'334'),(4,'3443435'),(5,'fdf43t536653');")
824+
tk.MustExec("rename table test.t1 to mysql.t1;")
825+
tk.MustQuery("SELECT count(*) FROM information_schema.TABLES WHERE (TABLE_SCHEMA = 'mysql') AND (TABLE_NAME = 't1');").
826+
Check(testkit.Rows("1"))
827+
828+
tk.MustExec("create table test.t2 (id int primary key, a text);")
829+
tk.MustExec("insert test.t2 values(1,'334'),(4,'3443435'),(5,'fdf43t536653');")
830+
tk.MustExec("create table test.t3 (id int primary key, a text);")
831+
tk.MustExec("insert test.t3 values(1,'334'),(4,'3443435'),(5,'fdf43t536653');")
832+
tk.MustExec("rename table test.t2 to mysql.t2, test.t3 to mysql.t3;")
833+
tk.MustQuery("SELECT count(*) FROM information_schema.TABLES WHERE (TABLE_SCHEMA = 'mysql') AND (TABLE_NAME = 't3');").
834+
Check(testkit.Rows("1"))
835+
}

0 commit comments

Comments
 (0)