diff --git a/ddl/table.go b/ddl/table.go index 6c498cdbce3cf..c42822663c618 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -16,6 +16,7 @@ package ddl import ( "context" + "encoding/json" "fmt" "strconv" "sync/atomic" @@ -1166,10 +1167,20 @@ func finishJobRenameTable(d *ddlCtx, t *meta.Meta, job *model.Job) (int64, error job.State = model.JobStateCancelled return 0, errors.Trace(err) } + // Before updating the schema version, we need to reset the old schema ID to new schema ID, so that + // the table info can be dropped normally in `ApplyDiff`. This is because renaming table requires two + // schema versions to complete. + oldRawArgs := job.RawArgs + job.Args[0] = job.SchemaID + job.RawArgs, err = json.Marshal(job.Args) + if err != nil { + return 0, errors.Trace(err) + } ver, err := updateSchemaVersion(d, t, job) if err != nil { return ver, errors.Trace(err) } + job.RawArgs = oldRawArgs job.FinishTableJob(model.JobStateDone, model.StatePublic, ver, tblInfo) return ver, nil } @@ -1190,10 +1201,21 @@ func finishJobRenameTables(d *ddlCtx, t *meta.Meta, job *model.Job, } tblInfos = append(tblInfos, tblInfo) } + // Before updating the schema version, we need to reset the old schema ID to new schema ID, so that + // the table info can be dropped normally in `ApplyDiff`. This is because renaming table requires two + // schema versions to complete. + var err error + oldRawArgs := job.RawArgs + job.Args[0] = newSchemaIDs + job.RawArgs, err = json.Marshal(job.Args) + if err != nil { + return 0, errors.Trace(err) + } ver, err := updateSchemaVersion(d, t, job) if err != nil { return ver, errors.Trace(err) } + job.RawArgs = oldRawArgs job.FinishMultipleTableJob(model.JobStateDone, model.StatePublic, ver, tblInfos) return ver, nil } diff --git a/infoschema/BUILD.bazel b/infoschema/BUILD.bazel index 53bf35a2d43e3..f5d1563552cdc 100644 --- a/infoschema/BUILD.bazel +++ b/infoschema/BUILD.bazel @@ -59,7 +59,6 @@ go_library( go_test( name = "infoschema_test", - timeout = "short", srcs = [ "cache_test.go", "cluster_tables_test.go", @@ -69,8 +68,6 @@ go_test( "tables_test.go", ], embed = [":infoschema"], - flaky = True, - shard_count = 50, deps = [ "//config", "//ddl/placement", diff --git a/infoschema/infoschema_test.go b/infoschema/infoschema_test.go index 66bd8cc33ef00..59c0a093b7cac 100644 --- a/infoschema/infoschema_test.go +++ b/infoschema/infoschema_test.go @@ -813,3 +813,23 @@ func TestIndexComment(t *testing.T) { 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';") 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")) } + +func TestInfoSchemaRenameTable(t *testing.T) { + store := testkit.CreateMockStore(t) + + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table test.t1 (id int primary key, a text);") + tk.MustExec("insert test.t1 values(1,'334'),(4,'3443435'),(5,'fdf43t536653');") + tk.MustExec("rename table test.t1 to mysql.t1;") + tk.MustQuery("SELECT count(*) FROM information_schema.TABLES WHERE (TABLE_SCHEMA = 'mysql') AND (TABLE_NAME = 't1');"). + Check(testkit.Rows("1")) + + tk.MustExec("create table test.t2 (id int primary key, a text);") + tk.MustExec("insert test.t2 values(1,'334'),(4,'3443435'),(5,'fdf43t536653');") + tk.MustExec("create table test.t3 (id int primary key, a text);") + tk.MustExec("insert test.t3 values(1,'334'),(4,'3443435'),(5,'fdf43t536653');") + tk.MustExec("rename table test.t2 to mysql.t2, test.t3 to mysql.t3;") + tk.MustQuery("SELECT count(*) FROM information_schema.TABLES WHERE (TABLE_SCHEMA = 'mysql') AND (TABLE_NAME = 't3');"). + Check(testkit.Rows("1")) +}