Skip to content

Commit

Permalink
infoschema: update raw args to make it drop correct table in `RENAME …
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot committed Jun 29, 2023
1 parent 04ad9d4 commit 666df2d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ddl

import (
"context"
"encoding/json"
"fmt"
"strconv"
"sync/atomic"
Expand Down Expand Up @@ -1028,10 +1029,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
}
Expand All @@ -1052,10 +1063,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
}
Expand Down
21 changes: 21 additions & 0 deletions infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,24 @@ 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, clean := testkit.CreateMockStore(t)
defer clean()

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"))
}

0 comments on commit 666df2d

Please sign in to comment.