Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyfhust committed Oct 30, 2023
1 parent 5ab124c commit a9b1fd8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
18 changes: 0 additions & 18 deletions pkg/ddl/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,21 +287,3 @@ func TestAlterEnforcedConstraintStateChange(t *testing.T) {
tk.MustExec("alter table t alter constraint c1 enforced")
tk.MustQuery("select * from t").Check(testkit.Rows("12"))
}

// Related issue TiDB#47567, #47631 and #47632.
func TestCheckConstraintIssue47567(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec("set @@global.tidb_enable_check_constraint = 1")
tk.MustExec("use test")
tk.MustExec("CREATE TABLE `t` (`a` int(11) DEFAULT NULL)")
tk.MustExec("insert t values(1)")
tk.MustGetErrMsg("alter table t ADD CONSTRAINT chk CHECK (a > 1) ENFORCED", "[ddl:3819]Check constraint 'chk' is violated.")
tk.MustGetErrMsg("alter table t ADD CONSTRAINT chk CHECK (a > 1) ENFORCED", "[ddl:3819]Check constraint 'chk' is violated.")
tk.MustExec("alter table t ADD CONSTRAINT chk CHECK (a > 1) NOT ENFORCED")
tk.MustGetErrMsg("ALTER TABLE t ALTER CONSTRAINT chk ENFORCED;", "[ddl:3819]Check constraint 'chk' is violated.")
tk.MustQuery("select constraint_name from information_schema.CHECK_CONSTRAINTS where constraint_schema = 'test'").Check(testkit.Rows("chk"))
tk.MustExec("alter table t drop CONSTRAINT chk")
tk.MustQuery("select constraint_name from information_schema.CHECK_CONSTRAINTS where constraint_schema = 'test'").Check(testkit.Rows())
}
1 change: 0 additions & 1 deletion pkg/ddl/rollingback.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@ func rollingBackAddConstraint(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int6
job.State = model.JobStateCancelled
return ver, dbterror.ErrCancelledDDLJob
}
// Is there a case constrInfoInMeta.State become StatePublic that means the constraint have already been added successfully?
for i, constr := range tblInfo.Constraints {
if constr.Name.L == constrInfoInMeta.Name.L {
tblInfo.Constraints = append(tblInfo.Constraints[0:i], tblInfo.Constraints[i+1:]...)
Expand Down
32 changes: 31 additions & 1 deletion tests/integrationtest/r/ddl/constraint.result
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,6 @@ insert into t values(1), (2), (3);
alter table t add constraint check(a < 2);
Error 3819 (HY000): Check constraint 't_chk_1' is violated.
alter table t add constraint check(a < 2) not enforced;
Error 3819 (HY000): Check constraint 't_chk_1' is violated.
drop table if exists t;
set @@global.tidb_enable_check_constraint = 1;
create table t(a int not null check(a>0), b int, constraint haha check(a < b), check(a<b+1));
Expand Down Expand Up @@ -789,4 +788,35 @@ t CREATE TABLE `t` (
CONSTRAINT `haha` CHECK ((`a` < `b`)) /*!80016 NOT ENFORCED */,
CONSTRAINT `t_chk_1` CHECK ((`a` > 0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table if exists t;
set @@global.tidb_enable_check_constraint = 1;
CREATE TABLE `t` (`a` int(11) DEFAULT NULL);
show create table t;
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
insert t values(1);
select * from t;
a
1
alter table t ADD CONSTRAINT chk CHECK (a > 1) ENFORCED;
Error 3819 (HY000): Check constraint 'chk' is violated.
alter table t ADD CONSTRAINT chk CHECK (a > 1) ENFORCED;
Error 3819 (HY000): Check constraint 'chk' is violated.
alter table t ADD CONSTRAINT chk CHECK (a > 1) NOT ENFORCED;
ALTER TABLE t ALTER CONSTRAINT chk ENFORCED;
Error 3819 (HY000): Check constraint 'chk' is violated.
show create table t;
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL,
CONSTRAINT `chk` CHECK ((`a` > 1)) /*!80016 NOT ENFORCED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
alter table t drop CONSTRAINT chk;
show create table t;
Table Create Table
t CREATE TABLE `t` (
`a` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
set @@global.tidb_enable_check_constraint = 0;
21 changes: 20 additions & 1 deletion tests/integrationtest/t/ddl/constraint.test
Original file line number Diff line number Diff line change
Expand Up @@ -658,4 +658,23 @@ show create table t;
# _, err = tk.Exec("alter table t alter constraint haha enforced")
# require.Errorf(t, err, "[table:3819]Check constraint 'haha' is violated.")

set @@global.tidb_enable_check_constraint = 0;
# Related issue TiDB#47567, #47631 and #47632.
# TestCheckConstraintIssue47567
drop table if exists t;
set @@global.tidb_enable_check_constraint = 1;
CREATE TABLE `t` (`a` int(11) DEFAULT NULL);
show create table t;
insert t values(1);
select * from t;
-- error 3940
alter table t ADD CONSTRAINT chk CHECK (a > 1) ENFORCED;
-- error 3940
alter table t ADD CONSTRAINT chk CHECK (a > 1) ENFORCED;
alter table t ADD CONSTRAINT chk CHECK (a > 1) NOT ENFORCED;
-- error 3940
ALTER TABLE t ALTER CONSTRAINT chk ENFORCED;
show create table t;
alter table t drop CONSTRAINT chk;
show create table t;

set @@global.tidb_enable_check_constraint = 0;

0 comments on commit a9b1fd8

Please sign in to comment.