Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl: support recover truncate table #15398

Merged
merged 3 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 24 additions & 17 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,17 +480,23 @@ func (s *testSerialSuite) TestRecoverTableByJobID(c *C) {
tk.MustExec("insert into t_recover values (1),(2),(3)")
tk.MustExec("drop table t_recover")

rs, err := tk.Exec("admin show ddl jobs")
c.Assert(err, IsNil)
rows, err := session.GetRows4Test(context.Background(), tk.Se, rs)
c.Assert(err, IsNil)
row := rows[0]
c.Assert(row.GetString(1), Equals, "test_recover")
c.Assert(row.GetString(3), Equals, "drop table")
jobID := row.GetInt64(0)
getDDLJobID := func(table, tp string) int64 {
rs, err := tk.Exec("admin show ddl jobs")
c.Assert(err, IsNil)
rows, err := session.GetRows4Test(context.Background(), tk.Se, rs)
c.Assert(err, IsNil)
for _, row := range rows {
if row.GetString(1) == table && row.GetString(3) == tp {
return row.GetInt64(0)
}
}
c.Errorf("can't find %s table of %s", tp, table)
return -1
}
jobID := getDDLJobID("test_recover", "drop table")

// if GC safe point is not exists in mysql.tidb
_, err = tk.Exec(fmt.Sprintf("recover table by job %d", jobID))
_, err := tk.Exec(fmt.Sprintf("recover table by job %d", jobID))
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "can not get 'tikv_gc_safe_point'")
// set GC safe point
Expand Down Expand Up @@ -539,14 +545,7 @@ func (s *testSerialSuite) TestRecoverTableByJobID(c *C) {

tk.MustExec("delete from t_recover where a > 1")
tk.MustExec("drop table t_recover")
rs, err = tk.Exec("admin show ddl jobs")
c.Assert(err, IsNil)
rows, err = session.GetRows4Test(context.Background(), tk.Se, rs)
c.Assert(err, IsNil)
row = rows[0]
c.Assert(row.GetString(1), Equals, "test_recover")
c.Assert(row.GetString(3), Equals, "drop table")
jobID = row.GetInt64(0)
jobID = getDDLJobID("test_recover", "drop table")

tk.MustExec(fmt.Sprintf("recover table by job %d", jobID))

Expand All @@ -556,6 +555,14 @@ func (s *testSerialSuite) TestRecoverTableByJobID(c *C) {
tk.MustExec("insert into t_recover values (7),(8),(9)")
tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "7", "8", "9"))

// Test for recover truncate table.
tk.MustExec("truncate table t_recover")
tk.MustExec("rename table t_recover to t_recover_new")
jobID = getDDLJobID("test_recover", "truncate table")
tk.MustExec(fmt.Sprintf("recover table by job %d", jobID))
tk.MustExec("insert into t_recover values (10)")
tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "7", "8", "9", "10"))

gcEnable, err := gcutil.CheckGCEnable(tk.Se)
c.Assert(err, IsNil)
c.Assert(gcEnable, Equals, false)
Expand Down
4 changes: 2 additions & 2 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ func (e *DDLExec) getRecoverTableByJobID(s *ast.RecoverTableStmt, t *meta.Meta,
if job == nil {
return nil, nil, admin.ErrDDLJobNotFound.GenWithStackByArgs(s.JobID)
}
if job.Type != model.ActionDropTable {
return nil, nil, errors.Errorf("Job %v type is %v, not drop table", job.ID, job.Type)
if job.Type != model.ActionDropTable && job.Type != model.ActionTruncateTable {
return nil, nil, errors.Errorf("Job %v type is %v, not dropped/truncated table", job.ID, job.Type)
}

// Check GC safe point for getting snapshot infoSchema.
Expand Down
7 changes: 7 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4638,6 +4638,13 @@ func (s *testRecoverTable) TestRecoverTable(c *C) {
tk.MustExec("insert into t_recover values (7),(8),(9)")
tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "7", "8", "9"))

// Recover truncate table.
tk.MustExec("truncate table t_recover")
tk.MustExec("rename table t_recover to t_recover_new")
crazycs520 marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("recover table t_recover")
tk.MustExec("insert into t_recover values (10)")
tk.MustQuery("select * from t_recover;").Check(testkit.Rows("1", "7", "8", "9", "10"))

gcEnable, err := gcutil.CheckGCEnable(tk.Se)
c.Assert(err, IsNil)
c.Assert(gcEnable, Equals, false)
Expand Down