From e7413c1582496563475324ef5afb468753f5e3f7 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Jun 2022 20:07:49 +0800 Subject: [PATCH 1/2] *: close RecordSet Signed-off-by: Weizhen Wang --- ddl/column_type_change_test.go | 21 +++------ ddl/db_integration_test.go | 83 +++++++++++++--------------------- ddl/db_table_test.go | 50 ++++++++++---------- ddl/ddl_error_test.go | 52 ++++++++++----------- ddl/ddl_worker_test.go | 2 +- ddl/sequence_test.go | 37 ++++++++------- 6 files changed, 108 insertions(+), 137 deletions(-) diff --git a/ddl/column_type_change_test.go b/ddl/column_type_change_test.go index e6672616ace29..71fa1da9ee927 100644 --- a/ddl/column_type_change_test.go +++ b/ddl/column_type_change_test.go @@ -102,8 +102,7 @@ func TestColumnTypeChangeBetweenInteger(t *testing.T) { tk.MustGetErrCode("alter table t modify column a mediumint", errno.ErrDataOutOfRange) tk.MustGetErrCode("alter table t modify column a smallint", errno.ErrDataOutOfRange) tk.MustGetErrCode("alter table t modify column a tinyint", errno.ErrDataOutOfRange) - _, err = tk.Exec("admin check table t") - require.NoError(t, err) + tk.MustExec("admin check table t") } func TestColumnTypeChangeStateBetweenInteger(t *testing.T) { @@ -1722,7 +1721,7 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastSucc(t *testing.T) { // For writable column: // Insert / Update should set the column with the casted-related column value. sql := fmt.Sprintf("insert into t values(%d, %d, '2021-06-06 12:13:14')", i+3, i+3) - _, err := tk1.Exec(sql) + err := tk1.ExecToErr(sql) if err != nil { checkErr = err return @@ -1730,7 +1729,7 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastSucc(t *testing.T) { if job.SchemaState == model.StateWriteOnly { // The casted value will be inserted into changing column too. // for point get - _, err := tk1.Exec("update t set b = -1 where a = 1") + err := tk1.ExecToErr("update t set b = -1 where a = 1") if err != nil { checkErr = err return @@ -1738,7 +1737,7 @@ func TestChangingColOriginDefaultValueAfterAddColAndCastSucc(t *testing.T) { } else { // The casted value will be inserted into changing column too. // for point get - _, err := tk1.Exec("update t set b = -2 where a = 2") + err := tk1.ExecToErr("update t set b = -2 where a = 2") if err != nil { checkErr = err return @@ -2332,19 +2331,13 @@ func TestChangeNullValueFromOtherTypeToTimestamp(t *testing.T) { prepare2() // only from other type NULL to timestamp type NOT NULL, it should be successful. (timestamp to timestamp excluded) - _, err = tk.Exec("alter table t modify column a timestamp NOT NULL") - require.Error(t, err) - require.Equal(t, "[ddl:1265]Data truncated for column 'a' at row 1", err.Error()) + tk.MustGetErrMsg("alter table t modify column a timestamp NOT NULL", "[ddl:1265]Data truncated for column 'a' at row 1") // Some dml cases. tk.MustExec("drop table if exists t") tk.MustExec("create table t(a timestamp NOT NULL)") - _, err = tk.Exec("insert into t values()") - require.Error(t, err) - require.Equal(t, "[table:1364]Field 'a' doesn't have a default value", err.Error()) - - _, err = tk.Exec("insert into t values(null)") - require.Equal(t, "[table:1048]Column 'a' cannot be null", err.Error()) + tk.MustGetErrMsg("insert into t values()", "[table:1364]Field 'a' doesn't have a default value") + tk.MustGetErrMsg("insert into t values(null)", "[table:1048]Column 'a' cannot be null") } func TestColumnTypeChangeBetweenFloatAndDouble(t *testing.T) { diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 489944a305545..3148283059998 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -276,16 +276,16 @@ func TestIssue19229(t *testing.T) { _, err := tk.Exec("insert into enumt values('xxx');") terr := errors.Cause(err).(*terror.Error) require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code()) - _, err = tk.Exec("insert into enumt values(-1);") + err = tk.ExecToErr("insert into enumt values(-1);") terr = errors.Cause(err).(*terror.Error) require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code()) tk.MustExec("drop table enumt") tk.MustExec("CREATE TABLE sett (type set('a', 'b') );") - _, err = tk.Exec("insert into sett values('xxx');") + err = tk.ExecToErr("insert into sett values('xxx');") terr = errors.Cause(err).(*terror.Error) require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code()) - _, err = tk.Exec("insert into sett values(-1);") + err = tk.ExecToErr("insert into sett values(-1);") terr = errors.Cause(err).(*terror.Error) require.Equal(t, errors.ErrCode(errno.WarnDataTruncated), terr.Code()) tk.MustExec("drop table sett") @@ -935,14 +935,10 @@ func TestModifyColumnOption(t *testing.T) { assertErrCode("alter table t2 modify column c int references t1(a)", errMsg) _, err := tk.Exec("alter table t1 change a a varchar(16)") require.NoError(t, err) - _, err = tk.Exec("alter table t1 change a a varchar(10)") - require.NoError(t, err) - _, err = tk.Exec("alter table t1 change a a datetime") - require.NoError(t, err) - _, err = tk.Exec("alter table t1 change a a int(11) unsigned") - require.NoError(t, err) - _, err = tk.Exec("alter table t2 change b b int(11) unsigned") - require.NoError(t, err) + tk.MustExec("alter table t1 change a a varchar(10)") + tk.MustExec("alter table t1 change a a datetime") + tk.MustExec("alter table t1 change a a int(11) unsigned") + tk.MustExec("alter table t2 change b b int(11) unsigned") } func TestIndexOnMultipleGeneratedColumn(t *testing.T) { @@ -1635,9 +1631,9 @@ func TestAlterColumn(t *testing.T) { // is forbidden as expected. tk.MustExec("drop table if exists mc") tk.MustExec("create table mc(a int key nonclustered, b int, c int)") - _, err = tk.Exec("alter table mc modify column a int key") // Adds a new primary key + err = tk.ExecToErr("alter table mc modify column a int key") // Adds a new primary key require.Error(t, err) - _, err = tk.Exec("alter table mc modify column c int unique") // Adds a new unique key + err = tk.ExecToErr("alter table mc modify column c int unique") // Adds a new unique key require.Error(t, err) result := tk.MustQuery("show create table mc") createSQL := result.Rows()[0][1] @@ -1663,7 +1659,7 @@ func TestAlterColumn(t *testing.T) { createSQL = result.Rows()[0][1] expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL AUTO_INCREMENT,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" require.Equal(t, expected, createSQL) - _, err = tk.Exec("alter table mc modify column a bigint") // Droppping auto_increment is not allow when @@tidb_allow_remove_auto_inc == 'off' + err = tk.ExecToErr("alter table mc modify column a bigint") // Droppping auto_increment is not allow when @@tidb_allow_remove_auto_inc == 'off' require.Error(t, err) tk.MustExec("set @@tidb_allow_remove_auto_inc = on") tk.MustExec("alter table mc modify column a bigint") // Dropping auto_increment is ok when @@tidb_allow_remove_auto_inc == 'on' @@ -1672,7 +1668,7 @@ func TestAlterColumn(t *testing.T) { expected = "CREATE TABLE `mc` (\n `a` bigint(20) NOT NULL,\n `b` int(11) DEFAULT NULL,\n PRIMARY KEY (`a`) /*T![clustered_index] NONCLUSTERED */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin" require.Equal(t, expected, createSQL) - _, err = tk.Exec("alter table mc modify column a bigint auto_increment") // Adds auto_increment should throw error + err = tk.ExecToErr("alter table mc modify column a bigint auto_increment") // Adds auto_increment should throw error require.Error(t, err) tk.MustExec("drop table if exists t") @@ -2660,14 +2656,10 @@ func TestCreateTableWithAutoIdCache(t *testing.T) { // Test auto_id_cache overflows int64. tk.MustExec("drop table if exists t;") - _, err = tk.Exec("create table t(a int) auto_id_cache = 9223372036854775808") - require.Error(t, err) - require.Equal(t, "table option auto_id_cache overflows int64", err.Error()) + tk.MustGetErrMsg("create table t(a int) auto_id_cache = 9223372036854775808", "table option auto_id_cache overflows int64") tk.MustExec("create table t(a int) auto_id_cache = 9223372036854775807") - _, err = tk.Exec("alter table t auto_id_cache = 9223372036854775808") - require.Error(t, err) - require.Equal(t, "table option auto_id_cache overflows int64", err.Error()) + tk.MustGetErrMsg("alter table t auto_id_cache = 9223372036854775808", "table option auto_id_cache overflows int64") } func TestAlterIndexVisibility(t *testing.T) { @@ -2804,10 +2796,7 @@ func TestDropLastVisibleColumnOrColumns(t *testing.T) { require.Equal(t, "[ddl:1113]A table must have at least 1 column", err.Error()) // for visible columns tk.MustExec("create table t_drop_last_columns(x int, y int, key((1+1)))") - _, err = tk.Exec("alter table t_drop_last_columns drop column x, drop column y") - require.Error(t, err) - require.Equal(t, "[ddl:1113]A table must have at least 1 column", err.Error()) - + tk.MustGetErrMsg("alter table t_drop_last_columns drop column x, drop column y", "[ddl:1113]A table must have at least 1 column") tk.MustExec("drop table if exists t_drop_last_column, t_drop_last_columns") } @@ -3092,8 +3081,7 @@ func TestIssue22028(t *testing.T) { tk.MustExec("drop table if exists t;") tk.MustExec("create table t(a double);") - _, err = tk.Exec("ALTER TABLE t MODIFY COLUMN a DOUBLE(0,0);") - require.Equal(t, "[types:1439]Display width out of range for column 'a' (max = 255)", err.Error()) + tk.MustGetErrMsg("ALTER TABLE t MODIFY COLUMN a DOUBLE(0,0);", "[types:1439]Display width out of range for column 'a' (max = 255)") } func TestIssue21835(t *testing.T) { @@ -3102,8 +3090,7 @@ func TestIssue21835(t *testing.T) { tk := testkit.NewTestKit(t, store) tk.MustExec("use test") tk.MustExec("drop table if exists t;") - _, err := tk.Exec("create table t( col decimal(1,2) not null default 0);") - require.Equal(t, "[types:1427]For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column 'col').", err.Error()) + tk.MustGetErrMsg("create table t( col decimal(1,2) not null default 0);", "[types:1427]For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column 'col').") } func TestCreateTemporaryTable(t *testing.T) { @@ -3147,7 +3134,7 @@ func TestCreateTemporaryTable(t *testing.T) { tk.MustExec("create temporary table tmp_db.t2 (id int)") tk.MustQuery("select * from t1") // No error tk.MustExec("drop database tmp_db") - _, err := tk.Exec("select * from t1") + err := tk.ExecToErr("select * from t1") require.Error(t, err) // In MySQL, drop DB does not really drop the table, it's back! tk.MustExec("create database tmp_db") @@ -3157,7 +3144,7 @@ func TestCreateTemporaryTable(t *testing.T) { // When local temporary table overlap the normal table, it takes a higher priority. tk.MustExec("create table overlap (id int)") tk.MustExec("create temporary table overlap (a int, b int)") - _, err = tk.Exec("insert into overlap values (1)") // column not match + err = tk.ExecToErr("insert into overlap values (1)") // column not match require.Error(t, err) tk.MustExec("insert into overlap values (1, 1)") @@ -3174,7 +3161,7 @@ func TestCreateTemporaryTable(t *testing.T) { // Check create temporary table for if not exists tk.MustExec("create temporary table b_local_temp_table (id int)") - _, err = tk.Exec("create temporary table b_local_temp_table (id int)") + err = tk.ExecToErr("create temporary table b_local_temp_table (id int)") require.True(t, infoschema.ErrTableExists.Equal(err)) tk.MustExec("create temporary table if not exists b_local_temp_table (id int)") @@ -3356,41 +3343,33 @@ func TestAvoidCreateViewOnLocalTemporaryTable(t *testing.T) { tk.MustExec("create temporary table tt2 (c int, d int)") checkCreateView := func() { - _, err := tk.Exec("create view v1 as select * from tt1") + err := tk.ExecToErr("create view v1 as select * from tt1") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v1") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v1' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v1", "[schema:1146]Table 'test.v1' doesn't exist") - _, err = tk.Exec("create view v1 as select * from (select * from tt1) as tt") + err = tk.ExecToErr("create view v1 as select * from (select * from tt1) as tt") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v1") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v1' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v1", "[schema:1146]Table 'test.v1' doesn't exist") - _, err = tk.Exec("create view v2 as select * from tt0 union select * from tt1") + err = tk.ExecToErr("create view v2 as select * from tt0 union select * from tt1") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v2") + err = tk.ExecToErr("select * from v2") require.Error(t, err) require.Equal(t, "[schema:1146]Table 'test.v2' doesn't exist", err.Error()) - _, err = tk.Exec("create view v3 as select * from tt0, tt1 where tt0.a = tt1.a") + err = tk.ExecToErr("create view v3 as select * from tt0, tt1 where tt0.a = tt1.a") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v3") + err = tk.ExecToErr("select * from v3") require.Error(t, err) require.Equal(t, "[schema:1146]Table 'test.v3' doesn't exist", err.Error()) - _, err = tk.Exec("create view v4 as select a, (select count(1) from tt1 where tt1.a = tt0.a) as tt1a from tt0") + err = tk.ExecToErr("create view v4 as select a, (select count(1) from tt1 where tt1.a = tt0.a) as tt1a from tt0") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v4") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v4' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v4", "[schema:1146]Table 'test.v4' doesn't exist") - _, err = tk.Exec("create view v5 as select a, (select count(1) from tt1 where tt1.a = 1) as tt1a from tt0") + err = tk.ExecToErr("create view v5 as select a, (select count(1) from tt1 where tt1.a = 1) as tt1a from tt0") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v5") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v5' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v5", "[schema:1146]Table 'test.v5' doesn't exist") _, err = tk.Exec("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) diff --git a/ddl/db_table_test.go b/ddl/db_table_test.go index bba27104a0d6f..2d6bff92cee44 100644 --- a/ddl/db_table_test.go +++ b/ddl/db_table_test.go @@ -530,7 +530,7 @@ func TestTableLocksLostCommit(t *testing.T) { tk.MustExec("LOCK TABLES t1 WRITE") tk.MustExec("INSERT INTO t1 VALUES(10)") - _, err := tk2.Exec("SELECT * FROM t1") + err := tk2.ExecToErr("SELECT * FROM t1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) tk.Session().Close() @@ -578,32 +578,32 @@ func TestWriteLocal(t *testing.T) { // Test: forbid write tk.MustExec("lock tables t1 write local") - _, err := tk2.Exec("insert into t1 values(NULL)") + err := tk2.ExecToErr("insert into t1 values(NULL)") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) tk.MustExec("unlock tables") tk2.MustExec("unlock tables") // Test mutex: lock write local first tk.MustExec("lock tables t1 write local") - _, err = tk2.Exec("lock tables t1 write local") + err = tk2.ExecToErr("lock tables t1 write local") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("lock tables t1 write") + err = tk2.ExecToErr("lock tables t1 write") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("lock tables t1 read") + err = tk2.ExecToErr("lock tables t1 read") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) tk.MustExec("unlock tables") tk2.MustExec("unlock tables") // Test mutex: lock write first tk.MustExec("lock tables t1 write") - _, err = tk2.Exec("lock tables t1 write local") + err = tk2.ExecToErr("lock tables t1 write local") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) tk.MustExec("unlock tables") tk2.MustExec("unlock tables") // Test mutex: lock read first tk.MustExec("lock tables t1 read") - _, err = tk2.Exec("lock tables t1 write local") + err = tk2.ExecToErr("lock tables t1 write local") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) tk.MustExec("unlock tables") tk2.MustExec("unlock tables") @@ -655,14 +655,14 @@ func TestLockTables(t *testing.T) { _, err = tk.Exec("delete from t1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) - _, err = tk2.Exec("insert into t1 set a=1") + err = tk2.ExecToErr("insert into t1 set a=1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("update t1 set a=1") + err = tk2.ExecToErr("update t1 set a=1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("delete from t1") + err = tk2.ExecToErr("delete from t1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) tk2.MustExec("lock tables t1 read") - _, err = tk2.Exec("insert into t1 set a=1") + err = tk2.ExecToErr("insert into t1 set a=1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) // Test write lock. @@ -674,11 +674,11 @@ func TestLockTables(t *testing.T) { tk.MustExec("delete from t1") tk.MustExec("insert into t1 set a=1") - _, err = tk2.Exec("select * from t1") + err = tk2.ExecToErr("select * from t1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("insert into t1 set a=1") + err = tk2.ExecToErr("insert into t1 set a=1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("lock tables t1 write") + err = tk2.ExecToErr("lock tables t1 write") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) // Test write local lock. @@ -688,13 +688,13 @@ func TestLockTables(t *testing.T) { tk.MustExec("insert into t1 set a=1") tk2.MustQuery("select * from t1") - _, err = tk2.Exec("delete from t1") + err = tk2.ExecToErr("delete from t1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("insert into t1 set a=1") + err = tk2.ExecToErr("insert into t1 set a=1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("lock tables t1 write") + err = tk2.ExecToErr("lock tables t1 write") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("lock tables t1 read") + err = tk2.ExecToErr("lock tables t1 read") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) // Test none unique table. @@ -748,15 +748,15 @@ func TestLockTables(t *testing.T) { tk.MustExec("lock tables t1 write, t2 read") tk.MustExec("truncate table t1") tk.MustExec("insert into t1 set a=1") - _, err = tk2.Exec("insert into t1 set a=1") + err = tk2.ExecToErr("insert into t1 set a=1") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) // Test for lock unsupported schema tables. - _, err = tk2.Exec("lock tables performance_schema.global_status write") + err = tk2.ExecToErr("lock tables performance_schema.global_status write") require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) - _, err = tk2.Exec("lock tables information_schema.tables write") + err = tk2.ExecToErr("lock tables information_schema.tables write") require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) - _, err = tk2.Exec("lock tables mysql.db write") + err = tk2.ExecToErr("lock tables mysql.db write") require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) // Test create table/view when session is holding the table locks. @@ -791,15 +791,15 @@ func TestLockTables(t *testing.T) { require.True(t, terror.ErrorEqual(err, table.ErrLockOrActiveTransaction)) // Test alter/drop database when other session is holding the table locks of the database. tk2.MustExec("create database test_lock2") - _, err = tk2.Exec("drop database test") + err = tk2.ExecToErr("drop database test") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - _, err = tk2.Exec("alter database test charset='utf8mb4'") + err = tk2.ExecToErr("alter database test charset='utf8mb4'") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) // Test for admin cleanup table locks. tk.MustExec("unlock tables") tk.MustExec("lock table t1 write, t2 write") - _, err = tk2.Exec("lock tables t1 write, t2 read") + err = tk2.ExecToErr("lock tables t1 write, t2 read") require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) tk2.MustExec("admin cleanup table lock t1,t2") checkTableLock(t, tk, "test", "t1", model.TableLockNone) diff --git a/ddl/ddl_error_test.go b/ddl/ddl_error_test.go index 59c982a24254f..b869ae42d1aac 100644 --- a/ddl/ddl_error_test.go +++ b/ddl/ddl_error_test.go @@ -38,25 +38,25 @@ func TestTableError(t *testing.T) { tk.MustExec("create table testDrop(a int)") // Schema ID is wrong, so dropping table is failed. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`)) - _, err := tk.Exec("drop table testDrop") + err := tk.ExecToErr("drop table testDrop") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId")) // Table ID is wrong, so dropping table is failed. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobTableId", `return(-1)`)) - _, err = tk.Exec("drop table testDrop") + err = tk.ExecToErr("drop table testDrop") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobTableId")) // Args is wrong, so creating table is failed. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`)) - _, err = tk.Exec("create table test.t1(a int)") + err = tk.ExecToErr("create table test.t1(a int)") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg")) // Table exists, so creating table is failed. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`)) - _, err = tk.Exec("create table test.t1(a int)") + err = tk.ExecToErr("create table test.t1(a int)") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId")) // Table exists, so creating table is failed. @@ -74,7 +74,7 @@ func TestViewError(t *testing.T) { // Args is wrong, so creating view is failed. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`)) - _, err := tk.Exec("create view v as select * from t") + err := tk.ExecToErr("create view v as select * from t") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg")) } @@ -88,9 +88,9 @@ func TestForeignKeyError(t *testing.T) { tk.MustExec("create table t1 (a int, FOREIGN KEY fk(a) REFERENCES t(a))") require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`)) - _, err := tk.Exec("alter table t1 add foreign key idx(a) REFERENCES t(a)") + err := tk.ExecToErr("alter table t1 add foreign key idx(a) REFERENCES t(a)") require.Error(t, err) - _, err = tk.Exec("alter table t1 drop index fk") + err = tk.ExecToErr("alter table t1 drop index fk") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId")) } @@ -106,17 +106,17 @@ func TestIndexError(t *testing.T) { // Schema ID is wrong. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`)) - _, err := tk.Exec("alter table t add index idx(a)") + err := tk.ExecToErr("alter table t add index idx(a)") require.Error(t, err) - _, err = tk.Exec("alter table t1 drop a") + err = tk.ExecToErr("alter table t1 drop a") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId")) // for adding index require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`)) - _, err = tk.Exec("alter table t add index idx(a)") + err = tk.ExecToErr("alter table t add index idx(a)") require.Error(t, err) - _, err = tk.Exec("alter table t drop index a") + err = tk.ExecToErr("alter table t drop index a") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg")) } @@ -132,43 +132,43 @@ func TestColumnError(t *testing.T) { // Invalid schema ID. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`)) - _, err := tk.Exec("alter table t add column ta int") + err := tk.ExecToErr("alter table t add column ta int") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa") + err = tk.ExecToErr("alter table t drop column aa") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa") + err = tk.ExecToErr("alter table t drop column aa") require.Error(t, err) - _, err = tk.Exec("alter table t add column ta int, add column tb int") + err = tk.ExecToErr("alter table t add column ta int, add column tb int") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa, drop column ab") + err = tk.ExecToErr("alter table t drop column aa, drop column ab") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId")) // Invalid table ID. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobTableId", `return(-1)`)) - _, err = tk.Exec("alter table t add column ta int") + err = tk.ExecToErr("alter table t add column ta int") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa") + err = tk.ExecToErr("alter table t drop column aa") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa") + err = tk.ExecToErr("alter table t drop column aa") require.Error(t, err) - _, err = tk.Exec("alter table t add column ta int, add column tb int") + err = tk.ExecToErr("alter table t add column ta int, add column tb int") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa, drop column ab") + err = tk.ExecToErr("alter table t drop column aa, drop column ab") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobTableId")) // Invalid argument. require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`)) - _, err = tk.Exec("alter table t add column ta int") + err = tk.ExecToErr("alter table t add column ta int") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa") + err = tk.ExecToErr("alter table t drop column aa") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa") + err = tk.ExecToErr("alter table t drop column aa") require.Error(t, err) - _, err = tk.Exec("alter table t add column ta int, add column tb int") + err = tk.ExecToErr("alter table t add column ta int, add column tb int") require.Error(t, err) - _, err = tk.Exec("alter table t drop column aa, drop column ab") + err = tk.ExecToErr("alter table t drop column aa, drop column ab") require.Error(t, err) require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg")) diff --git a/ddl/ddl_worker_test.go b/ddl/ddl_worker_test.go index 5de058fc35e8e..f39aa90f1a881 100644 --- a/ddl/ddl_worker_test.go +++ b/ddl/ddl_worker_test.go @@ -231,7 +231,7 @@ func TestParallelDDL(t *testing.T) { time.Sleep(5 * time.Millisecond) wg.Run(func() { tk := testkit.NewTestKit(t, store) - _, err := tk.Exec("alter table test_parallel_ddl_2.t3 add index db3_idx1(c2)") + err := tk.ExecToErr("alter table test_parallel_ddl_2.t3 add index db3_idx1(c2)") require.Error(t, err) rs := tk.MustQuery("select json_extract(@@tidb_last_ddl_info, '$.seq_num')") seqIDs[10], _ = strconv.Atoi(rs.Rows()[0][0].(string)) diff --git a/ddl/sequence_test.go b/ddl/sequence_test.go index 2ec8a77bf5270..743544cc25704 100644 --- a/ddl/sequence_test.go +++ b/ddl/sequence_test.go @@ -131,31 +131,30 @@ func TestDropSequence(t *testing.T) { // Test the specified object is not sequence. tk.MustExec("create table seq3 (a int)") - _, err = tk.Exec("drop sequence seq3") + err = tk.ExecToErr("drop sequence seq3") require.Error(t, err) require.True(t, terror.ErrorEqual(err, dbterror.ErrWrongObject)) // Test schema is not exist. - _, err = tk.Exec("drop sequence unknown.seq") + err = tk.ExecToErr("drop sequence unknown.seq") require.Error(t, err) require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'unknown.seq'") // Test drop sequence successfully. tk.MustExec("create sequence seq") - _, err = tk.Exec("drop sequence seq") - require.NoError(t, err) - _, err = tk.Exec("drop sequence seq") + tk.MustExec("drop sequence seq") + err = tk.ExecToErr("drop sequence seq") require.Error(t, err) require.EqualError(t, err, "[schema:4139]Unknown SEQUENCE: 'test.seq'") // Test drop table when the object is a sequence. tk.MustExec("create sequence seq") - _, err = tk.Exec("drop table seq") + err = tk.ExecToErr("drop table seq") require.Error(t, err) require.EqualError(t, err, "[schema:1051]Unknown table 'test.seq'") // Test drop view when the object is a sequence. - _, err = tk.Exec("drop view seq") + err = tk.ExecToErr("drop view seq") require.Error(t, err) require.True(t, terror.ErrorEqual(err, dbterror.ErrWrongObject)) tk.MustExec("drop sequence seq") @@ -661,24 +660,24 @@ func TestSequenceFunction(t *testing.T) { tk.MustExec("drop table if exists seq1") tk.MustExec("drop view if exists seq1") tk.MustExec("create table seq(a int)") - _, err = tk.Exec("select nextval(seq)") + err = tk.ExecToErr("select nextval(seq)") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.seq' is not SEQUENCE") - _, err = tk.Exec("select lastval(seq)") + err = tk.ExecToErr("select lastval(seq)") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.seq' is not SEQUENCE") - _, err = tk.Exec("select setval(seq, 10)") + err = tk.ExecToErr("select setval(seq, 10)") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.seq' is not SEQUENCE") tk.MustExec("create view seq1 as select * from seq") - _, err = tk.Exec("select nextval(seq1)") + err = tk.ExecToErr("select nextval(seq1)") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.seq1' is not SEQUENCE") - _, err = tk.Exec("select lastval(seq1)") + err = tk.ExecToErr("select lastval(seq1)") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.seq1' is not SEQUENCE") - _, err = tk.Exec("select setval(seq1, 10)") + err = tk.ExecToErr("select setval(seq1, 10)") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.seq1' is not SEQUENCE") tk.MustExec("drop sequence if exists seq") @@ -728,10 +727,10 @@ func TestSequenceFunction(t *testing.T) { tk.MustExec("create table t(a int)") tk.MustExec("insert into t values(1),(2)") tk.MustQuery("select nextval(seq), t.a from t").Check(testkit.Rows("1 1", "2 2")) - _, err = tk.Exec("select nextval(t), t.a from t") + err = tk.ExecToErr("select nextval(t), t.a from t") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.t' is not SEQUENCE") - _, err = tk.Exec("select nextval(seq), nextval(t), t.a from t") + err = tk.ExecToErr("select nextval(seq), nextval(t), t.a from t") require.Error(t, err) require.EqualError(t, err, "[schema:1347]'test.t' is not SEQUENCE") tk.MustQuery("select nextval(seq)").Check(testkit.Rows("3")) @@ -1019,11 +1018,11 @@ func TestSequenceCacheShouldNotBeNegative(t *testing.T) { tk.MustExec("use test") tk.MustExec("drop sequence if exists seq") - _, err := tk.Exec("create sequence seq cache -1") + err := tk.ExecToErr("create sequence seq cache -1") require.Error(t, err) require.EqualError(t, err, "[ddl:4136]Sequence 'test.seq' values are conflicting") - _, err = tk.Exec("create sequence seq cache 0") + err = tk.ExecToErr("create sequence seq cache 0") require.Error(t, err) require.EqualError(t, err, "[ddl:4136]Sequence 'test.seq' values are conflicting") @@ -1033,7 +1032,7 @@ func TestSequenceCacheShouldNotBeNegative(t *testing.T) { // 3: increment = -9223372036854775807 by user // `seqInfo.CacheValue < (math.MaxInt64-absIncrement)/absIncrement` will // ensure there is enough value for one cache allocation at least. - _, err = tk.Exec("create sequence seq INCREMENT -9223372036854775807 cache 1") + err = tk.ExecToErr("create sequence seq INCREMENT -9223372036854775807 cache 1") require.Error(t, err) require.EqualError(t, err, "[ddl:4136]Sequence 'test.seq' values are conflicting") @@ -1124,7 +1123,7 @@ func TestAlterSequencePrivilege(t *testing.T) { tk.MustExec("grant select on test.* to 'myuser'@'localhost'") tk1.MustExec("use test") - _, err = tk1.Exec("alter sequence my_seq increment = 2") + err = tk1.ExecToErr("alter sequence my_seq increment = 2") require.Error(t, err) require.EqualError(t, err, "[planner:1142]ALTER command denied to user 'myuser'@'localhost' for table 'my_seq'") tk.MustExec("drop sequence if exists my_seq") From 1ce9c3b701140d537048e1c92886eb1650a44bf5 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 13 Jun 2022 23:56:55 +0800 Subject: [PATCH 2/2] clean code Signed-off-by: Weizhen Wang --- ddl/db_cache_test.go | 4 +- ddl/db_integration_test.go | 63 ++++++------------ ddl/db_partition_test.go | 90 +++++++------------------ ddl/db_table_test.go | 110 ++++++++++--------------------- ddl/failtest/fail_db_test.go | 30 ++++----- ddl/placement_policy_test.go | 8 +-- ddl/placement_sql_test.go | 4 +- privilege/privileges/BUILD.bazel | 1 - testkit/testkit.go | 6 ++ 9 files changed, 103 insertions(+), 213 deletions(-) diff --git a/ddl/db_cache_test.go b/ddl/db_cache_test.go index 3ffcbcb18b576..8343cad58f37b 100644 --- a/ddl/db_cache_test.go +++ b/ddl/db_cache_test.go @@ -22,7 +22,6 @@ import ( "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/parser/auth" "github.com/pingcap/tidb/parser/model" - "github.com/pingcap/tidb/parser/terror" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/store/mockstore" "github.com/pingcap/tidb/testkit" @@ -170,8 +169,7 @@ func TestAlterTableCache(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t1 set a=1;") tk2.MustExec("alter table t1 cache;") - _, err = tk.Exec("commit") - require.True(t, terror.ErrorEqual(domain.ErrInfoSchemaChanged, err)) + tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) /* Test can skip schema checker */ tk.MustExec("begin") tk.MustExec("alter table t1 nocache") diff --git a/ddl/db_integration_test.go b/ddl/db_integration_test.go index 3148283059998..fff7e16f8a33c 100644 --- a/ddl/db_integration_test.go +++ b/ddl/db_integration_test.go @@ -119,13 +119,8 @@ func TestInvalidDefault(t *testing.T) { tk.MustExec("USE test;") - _, err := tk.Exec("create table t(c1 decimal default 1.7976931348623157E308)") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err) - - _, err = tk.Exec("create table t( c1 varchar(2) default 'TiDB');") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, types.ErrInvalidDefault), "err %v", err) + tk.MustGetDBError("create table t(c1 decimal default 1.7976931348623157E308)", types.ErrInvalidDefault) + tk.MustGetDBError("create table t( c1 varchar(2) default 'TiDB');", types.ErrInvalidDefault) } // TestKeyWithoutLength for issue #13452 @@ -133,12 +128,9 @@ func TestKeyWithoutLengthCreateTable(t *testing.T) { store, clean := testkit.CreateMockStore(t) defer clean() tk := testkit.NewTestKit(t, store) - tk.MustExec("USE test") - - _, err := tk.Exec("create table t_without_length (a text primary key)") - require.Error(t, err) - require.Regexp(t, ".*BLOB/TEXT column 'a' used in key specification without a key length", err.Error()) + tk.MustMatchErrMsg("create table t_without_length (a text primary key)", + ".*BLOB/TEXT column 'a' used in key specification without a key length") } // TestInvalidNameWhenCreateTable for issue #3848 @@ -3371,19 +3363,15 @@ func TestAvoidCreateViewOnLocalTemporaryTable(t *testing.T) { require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) tk.MustGetErrMsg("select * from v5", "[schema:1146]Table 'test.v5' doesn't exist") - _, err = tk.Exec("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)") + err = tk.ExecToErr("create view v6 as select * from tt0 where tt0.a=(select max(tt1.b) from tt1)") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v6") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v6' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v6", "[schema:1146]Table 'test.v6' doesn't exist") - _, err = tk.Exec("create view v7 as select * from tt0 where tt0.b=(select max(tt1.b) from tt1 where tt0.a=tt1.a)") + err = tk.ExecToErr("create view v7 as select * from tt0 where tt0.b=(select max(tt1.b) from tt1 where tt0.a=tt1.a)") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) - _, err = tk.Exec("select * from v7") - require.Error(t, err) - require.Equal(t, "[schema:1146]Table 'test.v7' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from v7", "[schema:1146]Table 'test.v7' doesn't exist") - _, err = tk.Exec("create or replace view v0 as select * from tt1") + err = tk.ExecToErr("create or replace view v0 as select * from tt1") require.True(t, core.ErrViewSelectTemporaryTable.Equal(err)) } @@ -3413,8 +3401,7 @@ func TestDropTemporaryTable(t *testing.T) { tk.MustExec("create temporary table if not exists b_local_temp_table (id int)") tk.MustQuery("select * from b_local_temp_table").Check(testkit.Rows()) tk.MustExec("drop table b_local_temp_table") - _, err := tk.Exec("select * from b_local_temp_table") - require.Equal(t, "[schema:1146]Table 'test.b_local_temp_table' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from b_local_temp_table", "[schema:1146]Table 'test.b_local_temp_table' doesn't exist") // TODO: test drop real data // Check if we have a normal and local temporary table in the same db with the same name, @@ -3427,8 +3414,7 @@ func TestDropTemporaryTable(t *testing.T) { sequenceTable := external.GetTableByName(t, tk, "test", "b_table_local_and_normal") require.Equal(t, model.TempTableNone, sequenceTable.Meta().TempTableType) tk.MustExec("drop table if exists b_table_local_and_normal") - _, err = tk.Exec("select * from b_table_local_and_normal") - require.Equal(t, "[schema:1146]Table 'test.b_table_local_and_normal' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from b_table_local_and_normal", "[schema:1146]Table 'test.b_table_local_and_normal' doesn't exist") // Check dropping local temporary tables should not commit current transaction implicitly. tk.MustExec("drop table if exists check_data_normal_table") @@ -3473,21 +3459,19 @@ func TestDropTemporaryTable(t *testing.T) { tk.MustExec("drop table if exists a_normal_table_2") tk.MustExec("create table a_normal_table_2 (id int)") defer tk.MustExec("drop table if exists a_normal_table_2") - _, err = tk.Exec("drop table a_local_temp_table_3, a_local_temp_table_4, a_local_temp_table_5, a_normal_table_2, a_local_temp_table_6") - require.Equal(t, "[schema:1051]Unknown table 'test.a_local_temp_table_6'", err.Error()) + tk.MustGetErrMsg("drop table a_local_temp_table_3, a_local_temp_table_4, a_local_temp_table_5, a_normal_table_2, a_local_temp_table_6", "[schema:1051]Unknown table 'test.a_local_temp_table_6'") tk.MustExec("drop table if exists check_data_normal_table_3") tk.MustExec("create table check_data_normal_table_3 (id int)") defer tk.MustExec("drop table if exists check_data_normal_table_3") tk.MustExec("create temporary table a_local_temp_table_6 (id int)") - _, err = tk.Exec("drop table check_data_normal_table_3, check_data_normal_table_7, a_local_temp_table_6") - require.Equal(t, "[schema:1051]Unknown table 'test.check_data_normal_table_7'", err.Error()) + tk.MustGetErrMsg("drop table check_data_normal_table_3, check_data_normal_table_7, a_local_temp_table_6", "[schema:1051]Unknown table 'test.check_data_normal_table_7'") // Check filter out data from removed local temp tables tk.MustExec("create temporary table a_local_temp_table_7 (id int)") ctx := tk.Session() require.Nil(t, sessiontxn.NewTxn(context.Background(), ctx)) - _, err = ctx.Txn(true) + _, err := ctx.Txn(true) require.NoError(t, err) sessionVars := tk.Session().GetSessionVars() sessVarsTempTable := sessionVars.LocalTemporaryTables @@ -3505,8 +3489,7 @@ func TestDropTemporaryTable(t *testing.T) { tk.MustExec("drop table if exists a_local_temp_table_7") tk.MustExec("commit") - _, err = tk.Exec("select * from a_local_temp_table_7") - require.Equal(t, "[schema:1146]Table 'test.a_local_temp_table_7' doesn't exist", err.Error()) + tk.MustGetErrMsg("select * from a_local_temp_table_7", "[schema:1146]Table 'test.a_local_temp_table_7' doesn't exist") memData := sessionVars.TemporaryTableData iter, err := memData.Iter(tablePrefix, endTablePrefix) require.NoError(t, err) @@ -3524,8 +3507,7 @@ func TestDropTemporaryTable(t *testing.T) { // Check drop not exists table in transaction. tk.MustExec("begin") tk.MustExec("create temporary table a_local_temp_table_8 (id int)") - _, err = tk.Exec("drop table a_local_temp_table_8, a_local_temp_table_9_not_exist") - require.Equal(t, "[schema:1051]Unknown table 'test.a_local_temp_table_9_not_exist'", err.Error()) + tk.MustGetErrMsg("drop table a_local_temp_table_8, a_local_temp_table_9_not_exist", "[schema:1051]Unknown table 'test.a_local_temp_table_9_not_exist'") tk.MustQuery("select * from a_local_temp_table_8").Check(testkit.Rows()) } @@ -3973,18 +3955,11 @@ func TestInvalidPartitionNameWhenCreateTable(t *testing.T) { defer tk.MustExec("drop database invalidPartitionNames") tk.MustExec("USE invalidPartitionNames") - _, err := tk.Exec("create table t(a int) partition by range (a) (partition p0 values less than (0), partition `p1 ` values less than (3))") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err) - - _, err = tk.Exec("create table t(a int) partition by range (a) (partition `` values less than (0), partition `p1` values less than (3))") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err) + tk.MustGetDBError("create table t(a int) partition by range (a) (partition p0 values less than (0), partition `p1 ` values less than (3))", dbterror.ErrWrongPartitionName) + tk.MustGetDBError("create table t(a int) partition by range (a) (partition `` values less than (0), partition `p1` values less than (3))", dbterror.ErrWrongPartitionName) tk.MustExec("create table t(a int) partition by range (a) (partition `p0` values less than (0), partition `p1` values less than (3))") - _, err = tk.Exec("alter table t add partition (partition `p2 ` values less than (5))") - require.Error(t, err) - require.Truef(t, terror.ErrorEqual(err, dbterror.ErrWrongPartitionName), "err %v", err) + tk.MustGetDBError("alter table t add partition (partition `p2 ` values less than (5))", dbterror.ErrWrongPartitionName) } func TestDDLLastInfo(t *testing.T) { diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index bfd2dca76a6bf..9621ab002efe2 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -157,7 +157,7 @@ func TestCreateTableWithPartition(t *testing.T) { );` tk.MustGetErrCode(sql4, tmysql.ErrPartitionMaxvalue) - _, err = tk.Exec(`CREATE TABLE rc ( + tk.MustExec(`CREATE TABLE rc ( a INT NOT NULL, b INT NOT NULL, c INT NOT NULL @@ -168,7 +168,6 @@ func TestCreateTableWithPartition(t *testing.T) { partition p3 values less than (65,30,13), partition p4 values less than (maxvalue,30,40) );`) - require.NoError(t, err) sql6 := `create table employees ( id int not null, @@ -211,7 +210,7 @@ func TestCreateTableWithPartition(t *testing.T) { );` tk.MustGetErrCode(sql9, tmysql.ErrPartitionFunctionIsNotAllowed) - _, err = tk.Exec(`CREATE TABLE t9 ( + tk.MustGetDBError(`CREATE TABLE t9 ( a INT NOT NULL, b INT NOT NULL, c INT NOT NULL @@ -220,8 +219,7 @@ func TestCreateTableWithPartition(t *testing.T) { partition p0 values less than (10), partition p2 values less than (20), partition p3 values less than (20) - );`) - require.True(t, dbterror.ErrRangeNotIncreasing.Equal(err)) + );`, dbterror.ErrRangeNotIncreasing) tk.MustGetErrCode(`create TABLE t10 (c1 int,c2 int) partition by range(c1 / c2 ) (partition p0 values less than (2));`, tmysql.ErrPartitionFunctionIsNotAllowed) @@ -3088,11 +3086,8 @@ func TestPartitionErrorCode(t *testing.T) { ) partition by hash(store_id) partitions 4;`) - _, err := tk.Exec("alter table employees add partition partitions 8;") - require.True(t, dbterror.ErrUnsupportedAddPartition.Equal(err)) - - _, err = tk.Exec("alter table employees add partition (partition p5 values less than (42));") - require.True(t, dbterror.ErrUnsupportedAddPartition.Equal(err)) + tk.MustGetDBError("alter table employees add partition partitions 8;", dbterror.ErrUnsupportedAddPartition) + tk.MustGetDBError("alter table employees add partition (partition p5 values less than (42));", dbterror.ErrUnsupportedAddPartition) // coalesce partition tk.MustExec(`create table clients ( @@ -3103,16 +3098,14 @@ func TestPartitionErrorCode(t *testing.T) { ) partition by hash( month(signed) ) partitions 12;`) - _, err = tk.Exec("alter table clients coalesce partition 4;") - require.True(t, dbterror.ErrUnsupportedCoalescePartition.Equal(err)) + tk.MustGetDBError("alter table clients coalesce partition 4;", dbterror.ErrUnsupportedCoalescePartition) tk.MustExec(`create table t_part (a int key) partition by range(a) ( partition p0 values less than (10), partition p1 values less than (20) );`) - _, err = tk.Exec("alter table t_part coalesce partition 4;") - require.True(t, dbterror.ErrCoalesceOnlyOnHashPartition.Equal(err)) + tk.MustGetDBError("alter table t_part coalesce partition 4;", dbterror.ErrCoalesceOnlyOnHashPartition) tk.MustGetErrCode(`alter table t_part reorganize partition p0, p1 into ( partition p0 values less than (1980));`, tmysql.ErrUnsupportedDDLOperation) @@ -3135,9 +3128,7 @@ func TestPartitionErrorCode(t *testing.T) { tk2 := testkit.NewTestKit(t, store) tk2.MustExec("use test") tk2.MustExec("alter table t truncate partition p0;") - - _, err = tk1.Exec("commit") - require.NoError(t, err) + tk1.MustExec("commit") } func TestConstAndTimezoneDepent(t *testing.T) { @@ -3337,8 +3328,7 @@ func TestCommitWhenSchemaChange(t *testing.T) { tk.MustExec("insert into nt values (1), (3), (5);") tk2.MustExec("alter table pt exchange partition p1 with table nt;") tk.MustExec("insert into nt values (7), (9);") - _, err = tk.Session().Execute(context.Background(), "commit") - require.True(t, domain.ErrInfoSchemaChanged.Equal(err)) + tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) tk.MustExec("admin check table pt") tk.MustQuery("select * from pt").Check(testkit.Rows()) @@ -3349,8 +3339,7 @@ func TestCommitWhenSchemaChange(t *testing.T) { tk.MustExec("insert into pt values (1), (3), (5);") tk2.MustExec("alter table pt exchange partition p1 with table nt;") tk.MustExec("insert into pt values (7), (9);") - _, err = tk.Session().Execute(context.Background(), "commit") - require.True(t, domain.ErrInfoSchemaChanged.Equal(err)) + tk.MustGetDBError("commit", domain.ErrInfoSchemaChanged) tk.MustExec("admin check table pt") tk.MustQuery("select * from pt").Check(testkit.Rows()) @@ -3365,34 +3354,28 @@ func TestCreatePartitionTableWithWrongType(t *testing.T) { tk.MustExec("use test") tk.MustExec("drop table if exists t") var err error - _, err = tk.Exec(`create table t( + tk.MustGetDBError(`create table t( b int(10) ) partition by range columns (b) ( partition p0 values less than (0x10), partition p3 values less than (0x20) - )`) - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + )`, dbterror.ErrWrongTypeColumnValue) - _, err = tk.Exec(`create table t( + tk.MustGetDBError(`create table t( b int(10) ) partition by range columns (b) ( partition p0 values less than ('g'), partition p3 values less than ('k') - )`) - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + )`, dbterror.ErrWrongTypeColumnValue) - _, err = tk.Exec(`create table t( + tk.MustGetDBError(`create table t( b char(10) ) partition by range columns (b) ( partition p0 values less than (30), partition p3 values less than (60) - )`) - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + )`, dbterror.ErrWrongTypeColumnValue) - _, err = tk.Exec(`create table t( + err = tk.ExecToErr(`create table t( b datetime ) partition by range columns (b) ( partition p0 values less than ('g'), @@ -3422,35 +3405,13 @@ func TestAddPartitionForTableWithWrongType(t *testing.T) { partition p0 values less than ('2020-09-01') )`) - var err error - - _, err = tk.Exec("alter table t_int add partition (partition p1 values less than ('g'))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_int add partition (partition p1 values less than (0x20))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_char add partition (partition p1 values less than (0x20))") - require.Error(t, err) - require.True(t, dbterror.ErrRangeNotIncreasing.Equal(err)) - - _, err = tk.Exec("alter table t_char add partition (partition p1 values less than (10))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_date add partition (partition p1 values less than ('m'))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_date add partition (partition p1 values less than (0x20))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) - - _, err = tk.Exec("alter table t_date add partition (partition p1 values less than (20))") - require.Error(t, err) - require.True(t, dbterror.ErrWrongTypeColumnValue.Equal(err)) + tk.MustGetDBError("alter table t_int add partition (partition p1 values less than ('g'))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_int add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (0x20))", dbterror.ErrRangeNotIncreasing) + tk.MustGetDBError("alter table t_char add partition (partition p1 values less than (10))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_date add partition (partition p1 values less than ('m'))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (0x20))", dbterror.ErrWrongTypeColumnValue) + tk.MustGetDBError("alter table t_date add partition (partition p1 values less than (20))", dbterror.ErrWrongTypeColumnValue) } func TestPartitionListWithTimeType(t *testing.T) { @@ -3517,8 +3478,7 @@ func TestAddTableWithPartition(t *testing.T) { tk.MustGetErrCode("create temporary table local_partition_table (a int, b int) partition by hash(a) partitions 3;", errno.ErrPartitionNoTemporary) tk.MustExec("drop table if exists local_partition_table;") tk.MustExec("drop table if exists partition_table;") - _, err = tk.Exec("create table partition_table (a int, b int) partition by hash(a) partitions 3;") - require.NoError(t, err) + tk.MustExec("create table partition_table (a int, b int) partition by hash(a) partitions 3;") tk.MustExec("drop table if exists partition_table;") tk.MustExec("drop table if exists local_partition_range_table;") tk.MustGetErrCode(`create temporary table local_partition_range_table (c1 smallint(6) not null, c2 char(5) default null) partition by range ( c1 ) ( diff --git a/ddl/db_table_test.go b/ddl/db_table_test.go index 2d6bff92cee44..a385acd7e3427 100644 --- a/ddl/db_table_test.go +++ b/ddl/db_table_test.go @@ -648,38 +648,27 @@ func TestLockTables(t *testing.T) { tk.MustExec("lock tables t1 read") tk.MustQuery("select * from t1") tk2.MustQuery("select * from t1") - _, err := tk.Exec("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) - _, err = tk.Exec("update t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) - _, err = tk.Exec("delete from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) + tk.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableNotLockedForWrite) + tk.MustGetDBError("update t1 set a=1", infoschema.ErrTableNotLockedForWrite) + tk.MustGetDBError("delete from t1", infoschema.ErrTableNotLockedForWrite) - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("update t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("delete from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("update t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("delete from t1", infoschema.ErrTableLocked) tk2.MustExec("lock tables t1 read") - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLockedForWrite)) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableNotLockedForWrite) // Test write lock. - _, err = tk.Exec("lock tables t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk.MustGetDBError("lock tables t1 write", infoschema.ErrTableLocked) tk2.MustExec("unlock tables") tk.MustExec("lock tables t1 write") tk.MustQuery("select * from t1") tk.MustExec("delete from t1") tk.MustExec("insert into t1 set a=1") - err = tk2.ExecToErr("select * from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("lock tables t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("select * from t1", infoschema.ErrTableLocked) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("lock tables t1 write", infoschema.ErrTableLocked) // Test write local lock. tk.MustExec("lock tables t1 write local") @@ -688,18 +677,13 @@ func TestLockTables(t *testing.T) { tk.MustExec("insert into t1 set a=1") tk2.MustQuery("select * from t1") - err = tk2.ExecToErr("delete from t1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("lock tables t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("lock tables t1 read") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("delete from t1", infoschema.ErrTableLocked) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) + tk2.MustGetDBError("lock tables t1 write", infoschema.ErrTableLocked) + tk2.MustGetDBError("lock tables t1 read", infoschema.ErrTableLocked) // Test none unique table. - _, err = tk.Exec("lock tables t1 read, t1 write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrNonuniqTable)) + tk.MustGetDBError("lock tables t1 read, t1 write", infoschema.ErrNonuniqTable) // Test lock table by other session in transaction and commit without retry. tk.MustExec("unlock tables") @@ -708,9 +692,8 @@ func TestLockTables(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t1 set a=1") tk2.MustExec("lock tables t1 write") - _, err = tk.Exec("commit") - require.Error(t, err) - require.Equal(t, "previous statement: insert into t1 set a=1: [domain:8028]Information schema is changed during the execution of the statement(for example, table definition may be updated by other DDL ran in parallel). If you see this error often, try increasing `tidb_max_delta_schema_count`. [try again later]", err.Error()) + tk.MustGetErrMsg("commit", + "previous statement: insert into t1 set a=1: [domain:8028]Information schema is changed during the execution of the statement(for example, table definition may be updated by other DDL ran in parallel). If you see this error often, try increasing `tidb_max_delta_schema_count`. [try again later]") // Test lock table by other session in transaction and commit with retry. tk.MustExec("unlock tables") @@ -719,8 +702,7 @@ func TestLockTables(t *testing.T) { tk.MustExec("begin") tk.MustExec("insert into t1 set a=1") tk2.MustExec("lock tables t1 write") - _, err = tk.Exec("commit") - require.Truef(t, terror.ErrorEqual(err, infoschema.ErrTableLocked), "err: %v\n", err) + tk.MustGetDBError("commit", infoschema.ErrTableLocked) // Test for lock the same table multiple times. tk2.MustExec("lock tables t1 write") @@ -748,59 +730,45 @@ func TestLockTables(t *testing.T) { tk.MustExec("lock tables t1 write, t2 read") tk.MustExec("truncate table t1") tk.MustExec("insert into t1 set a=1") - err = tk2.ExecToErr("insert into t1 set a=1") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("insert into t1 set a=1", infoschema.ErrTableLocked) // Test for lock unsupported schema tables. - err = tk2.ExecToErr("lock tables performance_schema.global_status write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) - err = tk2.ExecToErr("lock tables information_schema.tables write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) - err = tk2.ExecToErr("lock tables mysql.db write") - require.True(t, terror.ErrorEqual(err, infoschema.ErrAccessDenied)) + tk2.MustGetDBError("lock tables performance_schema.global_status write", infoschema.ErrAccessDenied) + tk2.MustGetDBError("lock tables information_schema.tables write", infoschema.ErrAccessDenied) + tk2.MustGetDBError("lock tables mysql.db write", infoschema.ErrAccessDenied) // Test create table/view when session is holding the table locks. tk.MustExec("unlock tables") tk.MustExec("lock tables t1 write, t2 read") - _, err = tk.Exec("create table t3 (a int)") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLocked)) - _, err = tk.Exec("create view v1 as select * from t1;") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableNotLocked)) + tk.MustGetDBError("create table t3 (a int)", infoschema.ErrTableNotLocked) + tk.MustGetDBError("create view v1 as select * from t1;", infoschema.ErrTableNotLocked) // Test for locking view was not supported. tk.MustExec("unlock tables") tk.MustExec("create view v1 as select * from t1;") - _, err = tk.Exec("lock tables v1 read") - require.True(t, terror.ErrorEqual(err, table.ErrUnsupportedOp)) + tk.MustGetDBError("lock tables v1 read", table.ErrUnsupportedOp) // Test for locking sequence was not supported. tk.MustExec("unlock tables") tk.MustExec("create sequence seq") - _, err = tk.Exec("lock tables seq read") - require.True(t, terror.ErrorEqual(err, table.ErrUnsupportedOp)) + tk.MustGetDBError("lock tables seq read", table.ErrUnsupportedOp) tk.MustExec("drop sequence seq") // Test for create/drop/alter database when session is holding the table locks. tk.MustExec("unlock tables") tk.MustExec("lock table t1 write") - _, err = tk.Exec("drop database test") - require.True(t, terror.ErrorEqual(err, table.ErrLockOrActiveTransaction)) - _, err = tk.Exec("create database test_lock") - require.True(t, terror.ErrorEqual(err, table.ErrLockOrActiveTransaction)) - _, err = tk.Exec("alter database test charset='utf8mb4'") - require.True(t, terror.ErrorEqual(err, table.ErrLockOrActiveTransaction)) + tk.MustGetDBError("drop database test", table.ErrLockOrActiveTransaction) + tk.MustGetDBError("create database test_lock", table.ErrLockOrActiveTransaction) + tk.MustGetDBError("alter database test charset='utf8mb4'", table.ErrLockOrActiveTransaction) // Test alter/drop database when other session is holding the table locks of the database. tk2.MustExec("create database test_lock2") - err = tk2.ExecToErr("drop database test") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) - err = tk2.ExecToErr("alter database test charset='utf8mb4'") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("drop database test", infoschema.ErrTableLocked) + tk2.MustGetDBError("alter database test charset='utf8mb4'", infoschema.ErrTableLocked) // Test for admin cleanup table locks. tk.MustExec("unlock tables") tk.MustExec("lock table t1 write, t2 write") - err = tk2.ExecToErr("lock tables t1 write, t2 read") - require.True(t, terror.ErrorEqual(err, infoschema.ErrTableLocked)) + tk2.MustGetDBError("lock tables t1 write, t2 read", infoschema.ErrTableLocked) tk2.MustExec("admin cleanup table lock t1,t2") checkTableLock(t, tk, "test", "t1", model.TableLockNone) checkTableLock(t, tk, "test", "t2", model.TableLockNone) @@ -868,18 +836,12 @@ func TestDDLWithInvalidTableInfo(t *testing.T) { tk.MustExec("create table t (a bigint, b int, c int generated always as (b+1)) partition by hash(a) partitions 4;") // Test drop partition column. - _, err = tk.Exec("alter table t drop column a;") - require.Error(t, err) // TODO: refine the error message to compatible with MySQL - require.Equal(t, "[planner:1054]Unknown column 'a' in 'expression'", err.Error()) + tk.MustGetErrMsg("alter table t drop column a;", "[planner:1054]Unknown column 'a' in 'expression'") // Test modify column with invalid expression. - _, err = tk.Exec("alter table t modify column c int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));") - require.Error(t, err) - require.Equal(t, "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 97 near \"then (b / a) end));\" ", err.Error()) + tk.MustGetErrMsg("alter table t modify column c int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));", "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 97 near \"then (b / a) end));\" ") // Test add column with invalid expression. - _, err = tk.Exec("alter table t add column d int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));") - require.Error(t, err) - require.Equal(t, "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 94 near \"then (b / a) end));\" ", err.Error()) + tk.MustGetErrMsg("alter table t add column d int GENERATED ALWAYS AS ((case when (a = 0) then 0when (a > 0) then (b / a) end));", "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 94 near \"then (b / a) end));\" ") } func TestAddColumn2(t *testing.T) { diff --git a/ddl/failtest/fail_db_test.go b/ddl/failtest/fail_db_test.go index 874acd12b5bbc..b1755f484254e 100644 --- a/ddl/failtest/fail_db_test.go +++ b/ddl/failtest/fail_db_test.go @@ -102,15 +102,15 @@ func TestHalfwayCancelOperations(t *testing.T) { }() tk.MustExec("create table tx(a int)") tk.MustExec("insert into tx values(1)") - _, err = tk.Exec("rename table tx to ty") + err = tk.ExecToErr("rename table tx to ty") require.Error(t, err) tk.MustExec("create table ty(a int)") tk.MustExec("insert into ty values(2)") - _, err = tk.Exec("rename table ty to tz, tx to ty") + err = tk.ExecToErr("rename table ty to tz, tx to ty") require.Error(t, err) - _, err = tk.Exec("select * from tz") + err = tk.ExecToErr("select * from tz") require.Error(t, err) - _, err = tk.Exec("rename table tx to ty, ty to tz") + err = tk.ExecToErr("rename table tx to ty, ty to tz") require.Error(t, err) tk.MustQuery("select * from ty").Check(testkit.Rows("2")) // Make sure that the table's data has not been deleted. @@ -134,7 +134,7 @@ func TestHalfwayCancelOperations(t *testing.T) { tk.MustExec("insert into nt values(7)") tk.MustExec("set @@tidb_enable_exchange_partition=1") defer tk.MustExec("set @@tidb_enable_exchange_partition=0") - _, err = tk.Exec("alter table pt exchange partition p1 with table nt") + err = tk.ExecToErr("alter table pt exchange partition p1 with table nt") require.Error(t, err) tk.MustQuery("select * from pt").Check(testkit.Rows("1", "3", "5")) @@ -263,7 +263,7 @@ func TestFailSchemaSyncer(t *testing.T) { time.Sleep(100 * time.Millisecond) } require.True(t, s.dom.SchemaValidator.IsStarted()) - _, err = tk.Exec("insert into t values(1)") + err = tk.ExecToErr("insert into t values(1)") require.NoError(t, err) } @@ -483,8 +483,7 @@ func TestModifyColumn(t *testing.T) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")) tk.MustExec("admin check table t") tk.MustExec("insert into t values(111, 222, 333)") - _, err = tk.Exec("alter table t change column a aa tinyint after c") - require.EqualError(t, err, "[types:1690]constant 222 overflows tinyint") + tk.MustGetErrMsg("alter table t change column a aa tinyint after c", "[types:1690]constant 222 overflows tinyint") tk.MustExec("alter table t change column a aa mediumint after c") tk.MustQuery("show create table t").Check(testkit.Rows("t CREATE TABLE `t` (\n" + " `bb` mediumint(9) DEFAULT NULL,\n" + @@ -500,17 +499,12 @@ func TestModifyColumn(t *testing.T) { // Test unsupported statements. tk.MustExec("create table t1(a int) partition by hash (a) partitions 2") - _, err = tk.Exec("alter table t1 modify column a mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: table is partition table") + tk.MustGetErrMsg("alter table t1 modify column a mediumint", "[ddl:8200]Unsupported modify column: table is partition table") tk.MustExec("create table t2(id int, a int, b int generated always as (abs(a)) virtual, c int generated always as (a+1) stored)") - _, err = tk.Exec("alter table t2 modify column b mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") - _, err = tk.Exec("alter table t2 modify column c mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") - _, err = tk.Exec("alter table t2 modify column a mediumint generated always as(id+1) stored") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: newCol IsGenerated true, oldCol IsGenerated false") - _, err = tk.Exec("alter table t2 modify column a mediumint") - require.EqualError(t, err, "[ddl:8200]Unsupported modify column: oldCol is a dependent column 'a' for generated column") + tk.MustGetErrMsg("alter table t2 modify column b mediumint", "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") + tk.MustGetErrMsg("alter table t2 modify column c mediumint", "[ddl:8200]Unsupported modify column: newCol IsGenerated false, oldCol IsGenerated true") + tk.MustGetErrMsg("alter table t2 modify column a mediumint generated always as(id+1) stored", "[ddl:8200]Unsupported modify column: newCol IsGenerated true, oldCol IsGenerated false") + tk.MustGetErrMsg("alter table t2 modify column a mediumint", "[ddl:8200]Unsupported modify column: oldCol is a dependent column 'a' for generated column") // Test multiple rows of data. tk.MustExec("create table t3(a int not null default 1, b int default 2, c int not null default 0, primary key(c), index idx(b), index idx1(a), index idx2(b, c))") diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 9df630774dc8b..b4b71e76cd422 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -940,9 +940,7 @@ func TestPolicyCacheAndPolicyDependency(t *testing.T) { require.Equal(t, true, in()) // Test drop policy can't succeed cause there are still some table depend on them. - _, err := tk.Exec("drop placement policy x") - require.Error(t, err) - require.Equal(t, "[ddl:8241]Placement policy 'x' is still in use", err.Error()) + tk.MustGetErrMsg("drop placement policy x", "[ddl:8241]Placement policy 'x' is still in use") // Drop depended table t firstly. tk.MustExec("drop table if exists t") @@ -951,9 +949,7 @@ func TestPolicyCacheAndPolicyDependency(t *testing.T) { require.Equal(t, 1, len(dependencies)) require.Equal(t, tbl2.Meta().ID, dependencies[0]) - _, err = tk.Exec("drop placement policy x") - require.Error(t, err) - require.Equal(t, "[ddl:8241]Placement policy 'x' is still in use", err.Error()) + tk.MustGetErrMsg("drop placement policy x", "[ddl:8241]Placement policy 'x' is still in use") // Drop depended table t2 secondly. tk.MustExec("drop table if exists t2") diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 42ef1e29986e8..4d560831d4044 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -136,9 +136,9 @@ PARTITION BY RANGE (c) ( tk.MustExec("set @@autocommit = 0") tk.MustExec("begin") tk.MustExec(testcase.sql) - _, err = tk.Exec("commit") + err = tk.ExecToErr("commit") } else { - _, err = tk.Exec(testcase.sql) + err = tk.ExecToErr(testcase.sql) } if testcase.err == nil { require.NoError(t, err) diff --git a/privilege/privileges/BUILD.bazel b/privilege/privileges/BUILD.bazel index 356a4a022d04d..fc3084c3ff602 100644 --- a/privilege/privileges/BUILD.bazel +++ b/privilege/privileges/BUILD.bazel @@ -12,7 +12,6 @@ go_library( deps = [ "//errno", "//infoschema", - "//infoschema/perfschema", "//parser/ast", "//parser/auth", "//parser/mysql", diff --git a/testkit/testkit.go b/testkit/testkit.go index c8c0cc0d861c5..83f0476e53499 100644 --- a/testkit/testkit.go +++ b/testkit/testkit.go @@ -290,6 +290,12 @@ func (tk *TestKit) MustGetErrMsg(sql string, errStr string) { tk.require.EqualError(err, errStr) } +// MustGetDBError executes a sql statement and assert its terror. +func (tk *TestKit) MustGetDBError(sql string, dberr *terror.Error) { + err := tk.ExecToErr(sql) + tk.require.Truef(terror.ErrorEqual(err, dberr), "err %v", err) +} + // MustContainErrMsg executes a sql statement and assert its error message containing errStr. func (tk *TestKit) MustContainErrMsg(sql string, errStr interface{}) { err := tk.ExecToErr(sql)