summaryrefslogtreecommitdiffstats
path: root/mysql-test/suite/innodb/t/truncate_foreign.test
blob: abbe1b3df870b53b699dffabb8ab1b920d240478 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
--source include/have_innodb.inc
--source include/have_debug.inc
--source include/have_debug_sync.inc

CREATE TABLE parent (a INT PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO parent SET a=1;

CREATE TABLE child (a INT PRIMARY KEY, FOREIGN KEY (a) REFERENCES parent(a)
ON UPDATE CASCADE)
ENGINE=InnoDB;
INSERT INTO child SET a=1;

--error ER_TRUNCATE_ILLEGAL_FK
TRUNCATE TABLE parent;
TRUNCATE TABLE child;

INSERT INTO child SET a=1;
UPDATE parent SET a=2;
SELECT * FROM child;

connect (dml,localhost,root);
SET DEBUG_SYNC='foreign_constraint_update_cascade SIGNAL fk WAIT_FOR go';
send UPDATE parent SET a=3;

connection default;
SET DEBUG_SYNC='now WAIT_FOR fk';
SET lock_wait_timeout=1;
--error ER_LOCK_WAIT_TIMEOUT
TRUNCATE TABLE child;
SET DEBUG_SYNC='now SIGNAL go';

connection dml;
reap;
SELECT * FROM child;
SET DEBUG_SYNC='foreign_constraint_check_for_update SIGNAL fk WAIT_FOR go';
send DELETE FROM parent;

connection default;
SET DEBUG_SYNC='now WAIT_FOR fk';
SET lock_wait_timeout=1;
--error ER_LOCK_WAIT_TIMEOUT
TRUNCATE TABLE child;
SET DEBUG_SYNC='now SIGNAL go';

connection dml;
--error ER_ROW_IS_REFERENCED_2
reap;
SELECT * FROM child;
INSERT INTO parent SET a=5;
SET DEBUG_SYNC='foreign_constraint_check_for_ins SIGNAL fk WAIT_FOR go';
send INSERT INTO child SET a=5;

connection default;
SET DEBUG_SYNC='now WAIT_FOR fk';
SET foreign_key_checks=0, innodb_lock_wait_timeout=0;
--error ER_LOCK_WAIT_TIMEOUT
TRUNCATE TABLE parent;
SET DEBUG_SYNC='now SIGNAL go';

connection dml;
reap;
SELECT * FROM parent;
SELECT * FROM child;
disconnect dml;

connection default;
SET DEBUG_SYNC = RESET;

DROP TABLE child, parent;

--echo #
--echo # MDEV-24532 Table corruption ER_NO_SUCH_TABLE_IN_ENGINE or
--echo # ER_CRASHED_ON_USAGE after ALTER on table with foreign key
--echo #

CREATE TABLE t1 (a INT, b INT, PRIMARY KEY (a)) ENGINE=InnoDB;
ALTER TABLE t1 ADD FOREIGN KEY (b) REFERENCES t1 (a) ON UPDATE CASCADE;
LOCK TABLE t1 WRITE;
TRUNCATE TABLE t1;
ALTER TABLE t1 ADD c INT;
UNLOCK TABLES;
DROP TABLE t1;

--echo #
--echo # MDEV-26052 Assertion prebuilt->trx_id < table->def_trx_id failed
--echo #

call mtr.add_suppression("InnoDB: In ALTER TABLE `test`\\.`t1` has or is");

CREATE TABLE t1 (pk INT, a INT, PRIMARY KEY (pk), KEY (a)) ENGINE=InnoDB;
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE t1 ADD FOREIGN KEY (a) REFERENCES t1 (a), ALGORITHM=COPY;
INSERT INTO t1 VALUES (1,1);
LOCK TABLES t1 WRITE;
SET FOREIGN_KEY_CHECKS=1;
--error ER_CANNOT_ADD_FOREIGN
TRUNCATE t1;
# Whether TRUNCATE succeeds or fails, it will reload FOREIGN KEY constraints.
# As a result, ha_innobase::referenced_by_foreign_key() will retun TRUE
# (for the self-referential key), and the statement will fail.
--error ER_TABLE_NOT_LOCKED
INSERT INTO t1 VALUES (2,2);
SELECT * FROM t1;
UNLOCK TABLES;
--error ER_NO_REFERENCED_ROW_2
INSERT INTO t1 VALUES (2,2);
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO t1 VALUES (2,2);
SELECT * FROM t1;
DROP TABLE t1;

--echo # End of 10.6 tests