diff --git a/new_session_test.go b/new_session_test.go index 09dd314c7cee0..9490d8c235948 100644 --- a/new_session_test.go +++ b/new_session_test.go @@ -511,7 +511,7 @@ func (s *testSessionSuite) TestLastInsertID(c *C) { c.Assert(lastInsertID+2, Equals, currLastInsertID) } -func (s *testSessionSuite) TestPrimaryKeyAutoincrement(c *C) { +func (s *testSessionSuite) TestPrimaryKeyAutoIncrement(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("drop table if exists t") tk.MustExec("create table t (id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL, name varchar(255) UNIQUE NOT NULL, status int)") @@ -573,6 +573,99 @@ func (s *testSessionSuite) TestAutoIncrementID(c *C) { tk.MustQuery("select last_insert_id()").Check(testkit.Rows(fmt.Sprint(20))) } +func (s *testSessionSuite) TestAutoIncrementWithRetry(c *C) { + // test for https://github.com/pingcap/tidb/issues/827 + + tk := testkit.NewTestKitWithInit(c, s.store) + tk1 := testkit.NewTestKitWithInit(c, s.store) + + tk.MustExec("create table t (c2 int, c1 int not null auto_increment, PRIMARY KEY (c1))") + tk.MustExec("insert into t (c2) values (1), (2), (3), (4), (5)") + + // insert values + lastInsertID := tk.Se.LastInsertID() + tk.MustExec("begin") + tk.MustExec("insert into t (c2) values (11), (12), (13)") + tk.MustQuery("select c1 from t where c2 = 11").Check(testkit.Rows("6")) + tk.MustExec("update t set c2 = 33 where c2 = 1") + + tk1.MustExec("update t set c2 = 22 where c2 = 1") + + tk.MustExec("commit") + + tk.MustQuery("select c1 from t where c2 = 11").Check(testkit.Rows("6")) + currLastInsertID := tk.Se.GetSessionVars().PrevLastInsertID + c.Assert(lastInsertID+5, Equals, currLastInsertID) + + // insert set + lastInsertID = currLastInsertID + tk.MustExec("begin") + tk.MustExec("insert into t set c2 = 31") + tk.MustQuery("select c1 from t where c2 = 31").Check(testkit.Rows("9")) + tk.MustExec("update t set c2 = 44 where c2 = 2") + + tk1.MustExec("update t set c2 = 55 where c2 = 2") + + tk.MustExec("commit") + + tk.MustQuery("select c1 from t where c2 = 31").Check(testkit.Rows("9")) + currLastInsertID = tk.Se.GetSessionVars().PrevLastInsertID + c.Assert(lastInsertID+3, Equals, currLastInsertID) + + // replace + lastInsertID = currLastInsertID + tk.MustExec("begin") + tk.MustExec("insert into t (c2) values (21), (22), (23)") + tk.MustQuery("select c1 from t where c2 = 21").Check(testkit.Rows("10")) + tk.MustExec("update t set c2 = 66 where c2 = 3") + + tk1.MustExec("update t set c2 = 77 where c2 = 3") + + tk.MustExec("commit") + + tk.MustQuery("select c1 from t where c2 = 21").Check(testkit.Rows("10")) + currLastInsertID = tk.Se.GetSessionVars().PrevLastInsertID + c.Assert(lastInsertID+1, Equals, currLastInsertID) + + // update + lastInsertID = currLastInsertID + tk.MustExec("begin") + tk.MustExec("insert into t set c2 = 41") + tk.MustExec("update t set c1 = 0 where c2 = 41") + tk.MustQuery("select c1 from t where c2 = 41").Check(testkit.Rows("0")) + tk.MustExec("update t set c2 = 88 where c2 = 4") + + tk1.MustExec("update t set c2 = 99 where c2 = 4") + + tk.MustExec("commit") + + tk.MustQuery("select c1 from t where c2 = 41").Check(testkit.Rows("0")) + currLastInsertID = tk.Se.GetSessionVars().PrevLastInsertID + c.Assert(lastInsertID+3, Equals, currLastInsertID) + + // prepare + lastInsertID = currLastInsertID + tk.MustExec("begin") + tk.MustExec("prepare stmt from 'insert into t (c2) values (?)'") + tk.MustExec("set @v1=100") + tk.MustExec("set @v2=200") + tk.MustExec("set @v3=300") + tk.MustExec("execute stmt using @v1") + tk.MustExec("execute stmt using @v2") + tk.MustExec("execute stmt using @v3") + tk.MustExec("deallocate prepare stmt") + tk.MustQuery("select c1 from t where c2 = 12").Check(testkit.Rows("7")) + tk.MustExec("update t set c2 = 111 where c2 = 5") + + tk1.MustExec("update t set c2 = 222 where c2 = 5") + + tk.MustExec("commit") + + tk.MustQuery("select c1 from t where c2 = 12").Check(testkit.Rows("7")) + currLastInsertID = tk.Se.GetSessionVars().PrevLastInsertID + c.Assert(lastInsertID+3, Equals, currLastInsertID) +} + func (s *testSessionSuite) TestPrepare(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) tk.MustExec("create table t(id TEXT)") diff --git a/session_test.go b/session_test.go index 7254b0ea3f6ad..7053f919cdef3 100644 --- a/session_test.go +++ b/session_test.go @@ -104,166 +104,6 @@ func (s *testSessionSuite) TestSchemaCheckerSimple(c *C) { c.Assert(result, Equals, domain.ResultUnknown) } -func (s *testSessionSuite) TestIssue827(c *C) { - defer testleak.AfterTest(c)() - dbName := "test_issue827" - dropDBSQL := fmt.Sprintf("drop database %s;", dbName) - se := newSession(c, s.store, dbName) - se1 := newSession(c, s.store, dbName) - - mustExecSQL(c, se, "drop table if exists t1") - c.Assert(se.(*session).txn, IsNil) - mustExecSQL(c, se, "create table t1 (c2 int, c3 int, c1 int not null auto_increment, PRIMARY KEY (c1))") - mustExecSQL(c, se, "insert into t1 set c2 = 30") - - mustExecSQL(c, se, "drop table if exists t") - c.Assert(se.(*session).txn, IsNil) - mustExecSQL(c, se, "create table t (c2 int, c1 int not null auto_increment, PRIMARY KEY (c1))") - mustExecSQL(c, se, "insert into t (c2) values (1), (2), (3), (4), (5)") - - // insert values - lastInsertID := se.LastInsertID() - mustExecSQL(c, se, "begin") - mustExecSQL(c, se, "insert into t (c2) values (11), (12), (13)") - rs, err := exec(se, "select c1 from t where c2 = 11") - c.Assert(err, IsNil) - expect, err := GetRows(rs) - c.Assert(err, IsNil) - _, err = exec(se, "update t set c2 = 33 where c2 = 1") - c.Assert(err, IsNil) - - mustExecSQL(c, se1, "begin") - mustExecSQL(c, se1, "update t set c2 = 22 where c2 = 1") - mustExecSQL(c, se1, "commit") - - _, err = exec(se, "commit") - c.Assert(err, IsNil) - - rs, err = exec(se, "select c1 from t where c2 = 11") - c.Assert(err, IsNil) - r, err := GetRows(rs) - c.Assert(err, IsNil) - c.Assert(r, DeepEquals, expect) - currLastInsertID := se.GetSessionVars().PrevLastInsertID - c.Assert(lastInsertID+5, Equals, currLastInsertID) - - // insert set - lastInsertID = currLastInsertID - mustExecSQL(c, se, "begin") - mustExecSQL(c, se, "insert into t set c2 = 31") - rs, err = exec(se, "select c1 from t where c2 = 31") - c.Assert(err, IsNil) - expect, err = GetRows(rs) - c.Assert(err, IsNil) - _, err = exec(se, "update t set c2 = 44 where c2 = 2") - c.Assert(err, IsNil) - - mustExecSQL(c, se1, "begin") - mustExecSQL(c, se1, "update t set c2 = 55 where c2 = 2") - mustExecSQL(c, se1, "commit") - - _, err = exec(se, "commit") - c.Assert(err, IsNil) - - rs, err = exec(se, "select c1 from t where c2 = 31") - c.Assert(err, IsNil) - r, err = GetRows(rs) - c.Assert(err, IsNil) - c.Assert(r, DeepEquals, expect) - currLastInsertID = se.GetSessionVars().PrevLastInsertID - c.Assert(lastInsertID+3, Equals, currLastInsertID) - - // replace - lastInsertID = currLastInsertID - mustExecSQL(c, se, "begin") - mustExecSQL(c, se, "insert into t (c2) values (21), (22), (23)") - rs, err = exec(se, "select c1 from t where c2 = 21") - c.Assert(err, IsNil) - expect, err = GetRows(rs) - c.Assert(err, IsNil) - _, err = exec(se, "update t set c2 = 66 where c2 = 3") - c.Assert(err, IsNil) - - mustExecSQL(c, se1, "begin") - mustExecSQL(c, se1, "update t set c2 = 77 where c2 = 3") - mustExecSQL(c, se1, "commit") - - _, err = exec(se, "commit") - c.Assert(err, IsNil) - - rs, err = exec(se, "select c1 from t where c2 = 21") - c.Assert(err, IsNil) - r, err = GetRows(rs) - c.Assert(err, IsNil) - c.Assert(r, DeepEquals, expect) - currLastInsertID = se.GetSessionVars().PrevLastInsertID - c.Assert(lastInsertID+1, Equals, currLastInsertID) - - // update - lastInsertID = currLastInsertID - mustExecSQL(c, se, "begin") - mustExecSQL(c, se, "insert into t set c2 = 41") - mustExecSQL(c, se, "update t set c1 = 0 where c2 = 41") - rs, err = exec(se, "select c1 from t where c2 = 41") - c.Assert(err, IsNil) - expect, err = GetRows(rs) - c.Assert(err, IsNil) - _, err = exec(se, "update t set c2 = 88 where c2 = 4") - c.Assert(err, IsNil) - - mustExecSQL(c, se1, "begin") - mustExecSQL(c, se1, "update t set c2 = 99 where c2 = 4") - mustExecSQL(c, se1, "commit") - - _, err = exec(se, "commit") - c.Assert(err, IsNil) - - rs, err = exec(se, "select c1 from t where c2 = 41") - c.Assert(err, IsNil) - r, err = GetRows(rs) - c.Assert(err, IsNil) - c.Assert(r, DeepEquals, expect) - currLastInsertID = se.GetSessionVars().PrevLastInsertID - c.Assert(lastInsertID+3, Equals, currLastInsertID) - - // prepare - lastInsertID = currLastInsertID - mustExecSQL(c, se, "begin") - mustExecSQL(c, se, "prepare stmt from 'insert into t (c2) values (?)'") - mustExecSQL(c, se, "set @v1=100") - mustExecSQL(c, se, "set @v2=200") - mustExecSQL(c, se, "set @v3=300") - mustExecSQL(c, se, "execute stmt using @v1") - mustExecSQL(c, se, "execute stmt using @v2") - mustExecSQL(c, se, "execute stmt using @v3") - mustExecSQL(c, se, "deallocate prepare stmt") - rs, err = exec(se, "select c1 from t where c2 = 12") - c.Assert(err, IsNil) - expect, err = GetRows(rs) - c.Assert(err, IsNil) - _, err = exec(se, "update t set c2 = 111 where c2 = 5") - c.Assert(err, IsNil) - - mustExecSQL(c, se1, "begin") - mustExecSQL(c, se1, "update t set c2 = 222 where c2 = 5") - mustExecSQL(c, se1, "commit") - - _, err = exec(se, "commit") - c.Assert(err, IsNil) - - rs, err = exec(se, "select c1 from t where c2 = 12") - c.Assert(err, IsNil) - r, err = GetRows(rs) - c.Assert(err, IsNil) - c.Assert(r, DeepEquals, expect) - currLastInsertID = se.GetSessionVars().PrevLastInsertID - c.Assert(lastInsertID+3, Equals, currLastInsertID) - - mustExecSQL(c, se, dropDBSQL) - se.Close() - se1.Close() -} - func (s *testSessionSuite) TestHaving(c *C) { defer testleak.AfterTest(c)() dbName := "test_having"