diff --git a/ddl/serial_test.go b/ddl/serial_test.go index 640a3f8a1acf4..9ff3125996148 100644 --- a/ddl/serial_test.go +++ b/ddl/serial_test.go @@ -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 @@ -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)) @@ -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) diff --git a/executor/ddl.go b/executor/ddl.go index 1b058737fb517..dfa4abc5d84c9 100644 --- a/executor/ddl.go +++ b/executor/ddl.go @@ -426,8 +426,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. diff --git a/executor/executor_test.go b/executor/executor_test.go index cb1ae583ca8c1..3c1b3df2b543c 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -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") + 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)