Debian Codegraph @

mysql-test/suite/innodb/t/foreign-keys.test

raw
1 --source include/have_innodb.inc
2 --source include/have_debug.inc
3 --source include/have_debug_sync.inc
4
5 --enable_connect_log
6
7 --echo #
8 --echo # Bug #19471516 SERVER CRASHES WHEN EXECUTING ALTER TABLE
9 --echo # ADD FOREIGN KEY
10 --echo #
11
12 CREATE TABLE `department` (`department_id` INT, `department_people_fk` INT,
13 PRIMARY KEY (`department_id`)) engine=innodb;
14
15 CREATE TABLE `title` (`title_id` INT, `title_manager_fk` INT,
16 `title_reporter_fk` INT, PRIMARY KEY (`title_id`)) engine=innodb;
17
18 CREATE TABLE `people` (`people_id` INT, PRIMARY KEY (`people_id`)) engine=innodb;
19
20 ALTER TABLE `department` ADD FOREIGN KEY (`department_people_fk`) REFERENCES
21 `people` (`people_id`);
22
23 ALTER TABLE `title` ADD FOREIGN KEY (`title_manager_fk`) REFERENCES `people`
24 (`people_id`);
25
26 ALTER TABLE `title` ADD FOREIGN KEY (`title_reporter_fk`) REFERENCES `people`
27 (`people_id`);
28
29 drop table title, department, people;
30
31 #
32 # FK and prelocking:
33 # child table accesses (reads and writes) wait for locks.
34 #
35 create table t1 (a int primary key, b int) engine=innodb;
36 create table t2 (c int primary key, d int,
37 foreign key (d) references t1 (a) on update cascade) engine=innodb;
38 insert t1 values (1,1),(2,2),(3,3);
39 insert t2 values (4,1),(5,2),(6,3);
40 flush table t2 with read lock; # this takes MDL_SHARED_NO_WRITE
41 connect (con1,localhost,root);
42 --error ER_ROW_IS_REFERENCED_2
43 delete from t1 where a=2;
44 send update t1 set a=10 where a=1;
45 connection default;
46 let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
47 source include/wait_condition.inc;
48 unlock tables;
49 connection con1;
50 reap;
51 connection default;
52 lock table t2 write; # this takes MDL_SHARED_NO_READ_WRITE
53 connection con1;
54 send delete from t1 where a=2;
55 connection default;
56 let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
57 source include/wait_condition.inc;
58 unlock tables;
59 connection con1;
60 --error ER_ROW_IS_REFERENCED_2
61 reap;
62 connection default;
63 unlock tables;
64 disconnect con1;
65
66 # but privileges should not be checked
67 create user foo;
68 grant select,update on test.t1 to foo;
69 connect(foo,localhost,foo);
70 update t1 set a=30 where a=3;
71 disconnect foo;
72 connection default;
73 select * from t2;
74 drop table t2, t1;
75 drop user foo;
76
77 #
78 # MDEV-16465 Invalid (old?) table or database name or hang in ha_innobase::delete_table and log semaphore wait upon concurrent DDL with foreign keys
79 #
80 create table t1 (f1 int primary key) engine=innodb;
81 create table t2 (f2 int primary key) engine=innodb;
82 create table t3 (f3 int primary key, foreign key (f3) references t2(f2)) engine=innodb;
83 insert into t1 values (1),(2),(3),(4),(5);
84 insert into t2 values (1),(2),(3),(4),(5);
85 insert into t3 values (1),(2),(3),(4),(5);
86 connect con1,localhost,root;
87 set debug_sync='alter_table_before_rename_result_table signal g1 wait_for g2';
88 send alter table t2 add constraint foreign key (f2) references t1(f1) on delete cascade on update cascade;
89 connection default;
90 let $conn=`select connection_id()`;
91 set debug_sync='before_execute_sql_command wait_for g1';
92 send update t1 set f1 = f1 + 100000 limit 2;
93 connect con2,localhost,root;
94 let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock' and info like 'update t1 %';
95 source include/wait_condition.inc;
96 --replace_result $conn UPDATE
97 eval kill query $conn;
98 disconnect con2;
99 connection default;
100 error ER_QUERY_INTERRUPTED;
101 reap;
102 set debug_sync='now signal g2';
103 connection con1;
104 reap;
105 show create table t2;
106 disconnect con1;
107 connection default;
108 select * from t2 where f2 not in (select f1 from t1);
109 select * from t3 where f3 not in (select f2 from t2);
110 drop table t3;
111 drop table t2;
112 drop table t1;
113 set debug_sync='reset';
114
115
116 --echo #
117 --echo # MDEV-17595 - Server crashes in copy_data_between_tables or
118 --echo # Assertion `thd->transaction.stmt.is_empty() ||
119 --echo # (thd->state_flags & Open_tables_state::BACKUPS_AVAIL)'
120 --echo # fails in close_tables_for_reopen upon concurrent
121 --echo # ALTER TABLE and FLUSH
122 --echo #
123 CREATE TABLE t1 (a INT, KEY(a)) ENGINE=InnoDB;
124 INSERT INTO t1 VALUES(1),(2);
125 CREATE TABLE t2 (b INT, KEY(b)) ENGINE=InnoDB;
126 INSERT INTO t2 VALUES(2);
127 ALTER TABLE t2 ADD FOREIGN KEY(b) REFERENCES t1(a), LOCK=EXCLUSIVE;
128 DROP TABLE t2, t1;
129
130
131 --echo #
132 --echo # MDEV-16060 - InnoDB: Failing assertion: ut_strcmp(index->name, key->name)
133 --echo #
134 CREATE TABLE t1 (`pk` INT PRIMARY KEY) ENGINE=InnoDB;
135 CREATE TABLE t2 LIKE t1;
136 FLUSH TABLES;
137
138 SET debug_sync='alter_table_intermediate_table_created SIGNAL ready WAIT_FOR go';
139 send ALTER TABLE t1 ADD FOREIGN KEY(pk) REFERENCES t2(pk) ON UPDATE CASCADE;
140
141 connect con1, localhost, root;
142 SET debug_sync='now WAIT_FOR ready';
143 SET lock_wait_timeout=0;
144 --error ER_LOCK_WAIT_TIMEOUT
145 UPDATE t2 SET pk=10 WHERE pk=1;
146 PREPARE stmt FROM 'UPDATE t2 SET pk=10 WHERE pk=1';
147 DEALLOCATE PREPARE stmt;
148 SET debug_sync='now SIGNAL go';
149
150 connection default;
151 reap;
152
153 # Cleanup
154 disconnect con1;
155
156 connection default;
157 SET debug_sync='reset';
158 SHOW OPEN TABLES FROM test;
159 DROP TABLE t1, t2;
160
161 #
162 # FK and prelocking:
163 # child table accesses (reads and writes) wait for locks.
164 #
165 create table t1 (a int primary key, b int) engine=innodb;
166 create table t2 (c int primary key, d int,
167 foreign key (d) references t1 (a) on update cascade) engine=innodb;
168 insert t1 values (1,1),(2,2),(3,3);
169 insert t2 values (4,1),(5,2),(6,3);
170 flush table t2 with read lock; # this takes MDL_SHARED_NO_WRITE
171 connect (con1,localhost,root);
172 --error ER_ROW_IS_REFERENCED_2
173 delete from t1 where a=2;
174 send update t1 set a=10 where a=1;
175 connection default;
176 let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
177 source include/wait_condition.inc;
178 unlock tables;
179 connection con1;
180 reap;
181 connection default;
182 lock table t2 write; # this takes MDL_SHARED_NO_READ_WRITE
183 connection con1;
184 send delete from t1 where a=2;
185 connection default;
186 let $wait_condition= select 1 from information_schema.processlist where state='Waiting for table metadata lock';
187 source include/wait_condition.inc;
188 unlock tables;
189 connection con1;
190 --error ER_ROW_IS_REFERENCED_2
191 reap;
192 connection default;
193 unlock tables;
194 disconnect con1;
195
196 # but privileges should not be checked
197 create user foo;
198 grant select,update on test.t1 to foo;
199 connect(foo,localhost,foo);
200 update t1 set a=30 where a=3;
201 disconnect foo;
202 connection default;
203 select * from t2;
204 drop table t2, t1;
205 drop user foo;
206
207 --echo #
208 --echo # MDEV-17187 table doesn't exist in engine after ALTER other tables
209 --echo # with CONSTRAINTs
210 --echo #
211
212 set foreign_key_checks=on;
213 create table t1 (id int not null primary key) engine=innodb;
214 create table t2 (id int not null primary key, fid int not null,
215 CONSTRAINT fk_fid FOREIGN KEY (fid) REFERENCES t1 (id))engine=innodb;
216
217 insert into t1 values (1), (2), (3);
218 insert into t2 values (1, 1), (2, 1), (3, 2);
219
220 set foreign_key_checks=off;
221 alter table t2 drop index fk_fid;
222 set foreign_key_checks=on;
223
224 --error ER_ROW_IS_REFERENCED_2
225 delete from t1 where id=2;
226 --error ER_NO_REFERENCED_ROW_2
227 insert into t2 values(4, 99);
228
229 select * from t1;
230 select * from t2;
231
232 set foreign_key_checks=off;
233 delete from t1 where id=2;
234 insert into t2 values(4, 99);
235 set foreign_key_checks=on;
236
237 select * from t1;
238 select * from t2;
239
240 show create table t1;
241 show create table t2;
242
243 # Optional: test DROP TABLE without any prior ha_innobase::open().
244 # This was tested manually, but it would cause --embedded to skip the test,
245 # and the restart would significantly increase the running time.
246 # --source include/restart_mysqld.inc
247
248 --error ER_ROW_IS_REFERENCED_2
249 drop table t1,t2;
250 --error ER_BAD_TABLE_ERROR
251 drop table t1,t2;
252
253 --echo #
254 --echo # MDEV-23470 InnoDB: Failing assertion: cmp < 0 in
255 --echo # row_ins_check_foreign_constraint
256 --echo #
257 CREATE TABLE t1(f1 INT NOT NULL PRIMARY KEY, f2 INT NOT NULL)ENGINE=InnoDB;
258 CREATE TABLE t2(f1 VARCHAR(100), f2 INT NOT NULL,
259 INDEX(f2))ENGINE=InnoDB;
260
261 INSERT INTO t1 VALUES(99, 2);
262 ALTER TABLE t2 ADD FOREIGN KEY(f2) REFERENCES t1(f1);
263
264 SET FOREIGN_KEY_CHECKS=0;
265 DROP INDEX f2 ON t2;
266 SET FOREIGN_KEY_CHECKS=1;
267 --error ER_NO_REFERENCED_ROW_2
268 INSERT INTO t2 VALUES('G', 3);
269 DROP TABLE t2, t1;
270 SET FOREIGN_KEY_CHECKS=DEFAULT;
271
272 --error ER_CANT_CREATE_TABLE
273 CREATE TABLE t1(a SERIAL) ENGINE=InnoDB ROW_FORMAT=COMPRESSED PAGE_COMPRESSED=1;
274 SHOW WARNINGS;
275
276 --echo # End of 10.5 tests
277
278 --echo #
279 --echo # MDEV-35598 foreign key error is unnecessary truncated
280 --echo #
281 set names utf8;
282 create table t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш
283 (f1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш int not null primary key,
284 f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш int not null
285 ) engine=innodb;
286 create table t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш
287 (f1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш varchar(100),
288 f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш int not null,
289 index i2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш
290 (f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш)
291 ) engine=innodb;
292
293 insert t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш values(99, 2);
294 alter table t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш add foreign key(f2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш) references t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш(f1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш);
295
296 --error ER_NO_REFERENCED_ROW_2
297 insert t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш values('g', 3);
298 drop table t2яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш,
299 t1яяяяяяяяяяььььььььььззззззззззшшшшшшшшшш;
300
301 --echo # End of 10.6 tests