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
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 @@ -1177,10 +1178,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
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 add comments for this logic?

job.Args[0] = job.SchemaID
Copy link
Contributor

@zimulala zimulala Jun 12, 2023

Choose a reason for hiding this comment

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

I feel that the effect of create table in apply is diff.TableID why do we update job.SchemaID?
It can make drop the created table in the first apply.

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 @@ -1201,10 +1212,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
2 changes: 1 addition & 1 deletion infoschema/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ go_test(
],
embed = [":infoschema"],
flaky = True,
shard_count = 9,
shard_count = 10,
deps = [
"//ddl/placement",
"//domain",
Expand Down
20 changes: 20 additions & 0 deletions infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,3 +822,23 @@ func TestIssue42400(t *testing.T) {
tk.MustQuery("show create table information_schema.ddl_jobs").CheckContain("`QUERY` text")
tk.MustQuery("select length(query) from information_schema.ddl_jobs;") // No error
}

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