Skip to content

Commit

Permalink
*: fix the issue of executing DDL after executing SQL failure in txn (#…
Browse files Browse the repository at this point in the history
…8044)

* ddl, executor: fix the issue of executing DDL after executing SQL failure in txn
  • Loading branch information
zimulala authored Oct 25, 2018
1 parent b556638 commit 38274c3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 0 additions & 5 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,6 @@ func (d *ddl) asyncNotifyWorker(jobTp model.ActionType) {
}

func (d *ddl) doDDLJob(ctx sessionctx.Context, job *model.Job) error {
// For every DDL, we must commit current transaction.
if err := ctx.NewTxn(); err != nil {
return errors.Trace(err)
}

// Get a global job ID and put the DDL job in the queue.
err := d.addDDLJob(ctx, job)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func (s *testForeighKeySuite) testCreateForeignKey(c *C, tblInfo *model.TableInf
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{fkInfo},
}
err := s.d.doDDLJob(s.ctx, job)
err := s.ctx.NewTxn()
c.Assert(err, IsNil)
err = s.d.doDDLJob(s.ctx, job)
c.Assert(err, IsNil)
return job
}
Expand Down
5 changes: 5 additions & 0 deletions executor/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (e *DDLExec) Next(ctx context.Context, chk *chunk.Chunk) (err error) {
return nil
}
e.done = true

// For each DDL, we should commit the previous transaction and create a new transaction.
if err = e.ctx.NewTxn(); err != nil {
return errors.Trace(err)
}
defer func() { e.ctx.GetSessionVars().StmtCtx.IsDDLJobInQueue = false }()

switch x := e.stmt.(type) {
Expand Down
17 changes: 17 additions & 0 deletions executor/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ func (s *testSuite) TestTruncateTable(c *C) {
result.Check(nil)
}

// TestInTxnExecDDLFail tests the following case:
// 1. Execute the SQL of "begin";
// 2. A SQL that will fail to execute;
// 3. Execute DDL.
func (s *testSuite) TestInTxnExecDDLFail(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t (i int key);")
tk.MustExec("insert into t values (1);")
tk.MustExec("begin;")
tk.MustExec("insert into t values (1);")
_, err := tk.Exec("truncate table t;")
c.Assert(err.Error(), Equals, "[kv:1062]Duplicate entry '1' for key 'PRIMARY'")
result := tk.MustQuery("select count(*) from t")
result.Check(testkit.Rows("1"))
}

func (s *testSuite) TestCreateTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down

0 comments on commit 38274c3

Please sign in to comment.