1
by brian
clean slate |
1 |
###########################################################
|
2 |
# 2006-02-01: By JBM: Added 1022, ORDER BY
|
|
3 |
###########################################################
|
|
4 |
# See if queries that use both auto_increment and LAST_INSERT_ID()
|
|
5 |
# are replicated well
|
|
6 |
############################################################
|
|
7 |
# REQUIREMENT
|
|
8 |
# Auto increment should work for a table with an auto_increment
|
|
9 |
# column and index but without primary key.
|
|
10 |
##############################################################
|
|
11 |
||
12 |
--echo # |
|
13 |
--echo # Setup |
|
14 |
--echo # |
|
15 |
||
16 |
use test; |
|
17 |
--disable_warnings |
|
18 |
drop table if exists t1, t2, t3; |
|
19 |
--enable_warnings |
|
20 |
||
21 |
--echo # |
|
22 |
--echo # See if queries that use both auto_increment and LAST_INSERT_ID() |
|
23 |
--echo # are replicated well |
|
24 |
--echo # |
|
25 |
--echo # We also check how the foreign_key_check variable is replicated |
|
26 |
--echo # |
|
27 |
||
28 |
-- source include/master-slave.inc |
|
29 |
#should work for both SBR and RBR
|
|
30 |
||
31 |
connection master; |
|
32 |
create table t1(a int auto_increment, key(a)); |
|
33 |
create table t2(b int auto_increment, c int, key(b)); |
|
34 |
insert into t1 values (1),(2),(3); |
|
35 |
insert into t1 values (null); |
|
36 |
insert into t2 values (null,last_insert_id()); |
|
37 |
save_master_pos; |
|
38 |
connection slave; |
|
39 |
sync_with_master; |
|
40 |
select * from t1 ORDER BY a; |
|
41 |
select * from t2 ORDER BY b; |
|
42 |
connection master; |
|
43 |
#check if multi-line inserts,
|
|
44 |
#which set last_insert_id to the first id inserted,
|
|
45 |
#are replicated the same way
|
|
46 |
drop table t1; |
|
47 |
drop table t2; |
|
48 |
--disable_warnings |
|
49 |
eval create table t1(a int auto_increment, key(a)) engine=$engine_type; |
|
50 |
eval create table t2(b int auto_increment, c int, key(b), foreign key(b) references t1(a)) engine=$engine_type; |
|
51 |
--enable_warnings |
|
52 |
SET FOREIGN_KEY_CHECKS=0; |
|
53 |
insert into t1 values (10); |
|
54 |
insert into t1 values (null),(null),(null); |
|
55 |
insert into t2 values (5,0); |
|
56 |
insert into t2 values (null,last_insert_id()); |
|
57 |
SET FOREIGN_KEY_CHECKS=1; |
|
58 |
save_master_pos; |
|
59 |
connection slave; |
|
60 |
sync_with_master; |
|
61 |
select * from t1; |
|
62 |
select * from t2; |
|
63 |
connection master; |
|
64 |
||
65 |
--echo # |
|
66 |
--echo # check if INSERT SELECT in auto_increment is well replicated (bug #490) |
|
67 |
--echo # |
|
68 |
||
69 |
drop table t2; |
|
70 |
drop table t1; |
|
71 |
create table t1(a int auto_increment, key(a)); |
|
72 |
create table t2(b int auto_increment, c int, key(b)); |
|
73 |
insert into t1 values (10); |
|
74 |
insert into t1 values (null),(null),(null); |
|
75 |
insert into t2 values (5,0); |
|
76 |
insert into t2 (c) select * from t1 ORDER BY a; |
|
77 |
select * from t2 ORDER BY b; |
|
78 |
save_master_pos; |
|
79 |
connection slave; |
|
80 |
sync_with_master; |
|
81 |
select * from t1 ORDER BY a; |
|
82 |
select * from t2 ORDER BY b; |
|
83 |
connection master; |
|
84 |
drop table t1; |
|
85 |
drop table t2; |
|
86 |
save_master_pos; |
|
87 |
connection slave; |
|
88 |
sync_with_master; |
|
89 |
||
90 |
--echo # |
|
91 |
--echo # Bug#8412: Error codes reported in binary log for CHARACTER SET, |
|
92 |
--echo # FOREIGN_KEY_CHECKS |
|
93 |
--echo # |
|
94 |
||
95 |
connection master; |
|
96 |
SET TIMESTAMP=1000000000; |
|
97 |
CREATE TABLE t1 ( a INT UNIQUE ); |
|
98 |
SET FOREIGN_KEY_CHECKS=0; |
|
99 |
# Duplicate Key Errors
|
|
100 |
--error 1022, ER_DUP_ENTRY |
|
101 |
INSERT INTO t1 VALUES (1),(1); |
|
102 |
sync_slave_with_master; |
|
103 |
connection master; |
|
104 |
drop table t1; |
|
105 |
sync_slave_with_master; |
|
106 |
||
107 |
--echo # |
|
108 |
--echo # Bug#14553: NULL in WHERE resets LAST_INSERT_ID |
|
109 |
--echo # |
|
110 |
||
111 |
connection master; |
|
112 |
create table t1(a int auto_increment, key(a)); |
|
113 |
create table t2(a int); |
|
114 |
insert into t1 (a) values (null); |
|
115 |
insert into t2 (a) select a from t1 where a is null; |
|
116 |
insert into t2 (a) select a from t1 where a is null; |
|
117 |
select * from t2; |
|
118 |
sync_slave_with_master; |
|
119 |
connection slave; |
|
120 |
select * from t2; |
|
121 |
connection master; |
|
122 |
drop table t1; |
|
123 |
drop table t2; |
|
124 |
||
125 |
--echo # |
|
126 |
--echo # End of 4.1 tests |
|
127 |
--echo # |
|
128 |
||
129 |
--echo # |
|
130 |
--echo # BUG#15728: LAST_INSERT_ID function inside a stored function returns 0 |
|
131 |
--echo # |
|
132 |
--echo # The solution is not to reset last_insert_id on enter to sub-statement. |
|
133 |
--echo # |
|
134 |
||
135 |
connection master; |
|
136 |
--disable_warnings |
|
137 |
drop function if exists bug15728; |
|
138 |
drop function if exists bug15728_insert; |
|
139 |
drop table if exists t1, t2; |
|
140 |
--enable_warnings |
|
141 |
||
142 |
create table t1 ( |
|
143 |
id int not null auto_increment, |
|
144 |
last_id int, |
|
145 |
primary key (id) |
|
146 |
);
|
|
147 |
create function bug15728() returns int(11) |
|
148 |
return last_insert_id(); |
|
149 |
||
150 |
insert into t1 (last_id) values (0); |
|
151 |
insert into t1 (last_id) values (last_insert_id()); |
|
152 |
insert into t1 (last_id) values (bug15728()); |
|
153 |
||
154 |
# Check that nested call replicates too.
|
|
155 |
create table t2 ( |
|
156 |
id int not null auto_increment, |
|
157 |
last_id int, |
|
158 |
primary key (id) |
|
159 |
);
|
|
160 |
delimiter |; |
|
161 |
create function bug15728_insert() returns int(11) modifies sql data |
|
162 |
begin
|
|
163 |
insert into t2 (last_id) values (bug15728()); |
|
164 |
return bug15728(); |
|
165 |
end| |
|
166 |
create trigger t1_bi before insert on t1 for each row |
|
167 |
begin
|
|
168 |
declare res int; |
|
169 |
select bug15728_insert() into res; |
|
170 |
set NEW.last_id = res; |
|
171 |
end| |
|
172 |
delimiter ;| |
|
173 |
||
174 |
insert into t1 (last_id) values (0); |
|
175 |
||
176 |
drop trigger t1_bi; |
|
177 |
||
178 |
# Check that nested call doesn't affect outer context.
|
|
179 |
select last_insert_id(); |
|
180 |
select bug15728_insert(); |
|
181 |
select last_insert_id(); |
|
182 |
insert into t1 (last_id) values (bug15728()); |
|
183 |
# This should be exactly one greater than in the previous call.
|
|
184 |
select last_insert_id(); |
|
185 |
||
186 |
# BUG#20339 - stored procedure using LAST_INSERT_ID() does not
|
|
187 |
# replicate statement-based
|
|
188 |
--disable_warnings |
|
189 |
drop procedure if exists foo; |
|
190 |
--enable_warnings |
|
191 |
delimiter |; |
|
192 |
create procedure foo() |
|
193 |
begin
|
|
194 |
declare res int; |
|
195 |
insert into t2 (last_id) values (bug15728()); |
|
196 |
insert into t1 (last_id) values (bug15728()); |
|
197 |
end| |
|
198 |
delimiter ;| |
|
199 |
call foo(); |
|
200 |
||
201 |
select * from t1; |
|
202 |
select * from t2; |
|
203 |
save_master_pos; |
|
204 |
connection slave; |
|
205 |
sync_with_master; |
|
206 |
select * from t1; |
|
207 |
select * from t2; |
|
208 |
connection master; |
|
209 |
||
210 |
drop function bug15728; |
|
211 |
drop function bug15728_insert; |
|
212 |
drop table t1,t2; |
|
213 |
drop procedure foo; |
|
214 |
||
215 |
# test of BUG#20188 REPLACE or ON DUPLICATE KEY UPDATE in
|
|
216 |
# auto_increment breaks binlog
|
|
217 |
||
218 |
create table t1 (n int primary key auto_increment not null, |
|
219 |
b int, unique(b)); |
|
220 |
||
221 |
# First, test that we do not call restore_auto_increment() too early
|
|
222 |
# in write_record():
|
|
223 |
set sql_log_bin=0; |
|
224 |
insert into t1 values(null,100); |
|
225 |
replace into t1 values(null,50),(null,100),(null,150); |
|
226 |
select * from t1 order by n; |
|
227 |
truncate table t1; |
|
228 |
set sql_log_bin=1; |
|
229 |
||
230 |
insert into t1 values(null,100); |
|
231 |
select * from t1 order by n; |
|
232 |
sync_slave_with_master; |
|
233 |
# make slave's table autoinc counter bigger
|
|
234 |
insert into t1 values(null,200),(null,300); |
|
235 |
delete from t1 where b <> 100; |
|
236 |
# check that slave's table content is identical to master
|
|
237 |
select * from t1 order by n; |
|
238 |
# only the auto_inc counter differs.
|
|
239 |
||
240 |
connection master; |
|
241 |
replace into t1 values(null,100),(null,350); |
|
242 |
select * from t1 order by n; |
|
243 |
sync_slave_with_master; |
|
244 |
select * from t1 order by n; |
|
245 |
||
246 |
# Same test as for REPLACE, but for ON DUPLICATE KEY UPDATE
|
|
247 |
||
248 |
# We first check that if we update a row using a value larger than the
|
|
249 |
# table's counter, the counter for next row is bigger than the
|
|
250 |
# after-value of the updated row.
|
|
251 |
connection master; |
|
252 |
insert into t1 values (NULL,400),(3,500),(NULL,600) on duplicate key UPDATE n=1000; |
|
253 |
select * from t1 order by n; |
|
254 |
sync_slave_with_master; |
|
255 |
select * from t1 order by n; |
|
256 |
||
257 |
# and now test for the bug:
|
|
258 |
connection master; |
|
259 |
drop table t1; |
|
260 |
create table t1 (n int primary key auto_increment not null, |
|
261 |
b int, unique(b)); |
|
262 |
insert into t1 values(null,100); |
|
263 |
select * from t1 order by n; |
|
264 |
sync_slave_with_master; |
|
265 |
insert into t1 values(null,200),(null,300); |
|
266 |
delete from t1 where b <> 100; |
|
267 |
select * from t1 order by n; |
|
268 |
||
269 |
connection master; |
|
270 |
insert into t1 values(null,100),(null,350) on duplicate key update n=2; |
|
271 |
select * from t1 order by n; |
|
272 |
sync_slave_with_master; |
|
273 |
select * from t1 order by n; |
|
274 |
||
275 |
connection master; |
|
276 |
drop table t1; |
|
277 |
sync_slave_with_master; |
|
278 |
||
279 |
#
|
|
280 |
# BUG#24432 "INSERT... ON DUPLICATE KEY UPDATE skips auto_increment values"
|
|
281 |
#
|
|
282 |
||
283 |
connection master; |
|
284 |
# testcase with INSERT VALUES
|
|
285 |
CREATE TABLE t1 (a INT NOT NULL PRIMARY KEY AUTO_INCREMENT, b INT, |
|
286 |
UNIQUE(b)); |
|
287 |
INSERT INTO t1(b) VALUES(1),(1),(2) ON DUPLICATE KEY UPDATE t1.b=10; |
|
288 |
SELECT * FROM t1; |
|
289 |
sync_slave_with_master; |
|
290 |
SELECT * FROM t1; |
|
291 |
connection master; |
|
292 |
drop table t1; |
|
293 |
||
294 |
# tescase with INSERT SELECT
|
|
295 |
CREATE TABLE t1 ( |
|
296 |
id bigint(20) unsigned NOT NULL auto_increment, |
|
297 |
field_1 int(10) unsigned NOT NULL, |
|
298 |
field_2 varchar(255) NOT NULL, |
|
299 |
field_3 varchar(255) NOT NULL, |
|
300 |
PRIMARY KEY (id), |
|
301 |
UNIQUE KEY field_1 (field_1, field_2) |
|
302 |
);
|
|
303 |
CREATE TABLE t2 ( |
|
304 |
field_a int(10) unsigned NOT NULL, |
|
305 |
field_b varchar(255) NOT NULL, |
|
306 |
field_c varchar(255) NOT NULL |
|
307 |
);
|
|
308 |
INSERT INTO t2 (field_a, field_b, field_c) VALUES (1, 'a', '1a'); |
|
309 |
INSERT INTO t2 (field_a, field_b, field_c) VALUES (2, 'b', '2b'); |
|
310 |
INSERT INTO t2 (field_a, field_b, field_c) VALUES (3, 'c', '3c'); |
|
311 |
INSERT INTO t2 (field_a, field_b, field_c) VALUES (4, 'd', '4d'); |
|
312 |
INSERT INTO t2 (field_a, field_b, field_c) VALUES (5, 'e', '5e'); |
|
313 |
# Updating table t1 based on values from table t2
|
|
314 |
INSERT INTO t1 (field_1, field_2, field_3) |
|
315 |
SELECT t2.field_a, t2.field_b, t2.field_c |
|
316 |
FROM t2 |
|
317 |
ON DUPLICATE KEY UPDATE |
|
318 |
t1.field_3 = t2.field_c; |
|
319 |
# Inserting new record into t2
|
|
320 |
INSERT INTO t2 (field_a, field_b, field_c) VALUES (6, 'f', '6f'); |
|
321 |
# Updating t1 again
|
|
322 |
INSERT INTO t1 (field_1, field_2, field_3) |
|
323 |
SELECT t2.field_a, t2.field_b, t2.field_c |
|
324 |
FROM t2 |
|
325 |
ON DUPLICATE KEY UPDATE |
|
326 |
t1.field_3 = t2.field_c; |
|
327 |
SELECT * FROM t1; |
|
328 |
sync_slave_with_master; |
|
329 |
SELECT * FROM t1; |
|
330 |
connection master; |
|
331 |
drop table t1, t2; |
|
332 |
||
333 |
#
|
|
334 |
# BUG#20339: stored procedure using LAST_INSERT_ID() does not
|
|
335 |
# replicate statement-based.
|
|
336 |
#
|
|
337 |
# There is another version of the test for bug#20339 above that is
|
|
338 |
# actually originates in 5.1, and this is the version that is merged
|
|
339 |
# from 5.0.
|
|
340 |
#
|
|
341 |
connection master; |
|
342 |
||
343 |
--disable_warnings |
|
344 |
DROP PROCEDURE IF EXISTS p1; |
|
345 |
DROP TABLE IF EXISTS t1, t2; |
|
346 |
--enable_warnings |
|
347 |
||
348 |
# Reset result of LAST_INSERT_ID().
|
|
349 |
SELECT LAST_INSERT_ID(0); |
|
350 |
||
351 |
CREATE TABLE t1 ( |
|
352 |
id INT NOT NULL DEFAULT 0, |
|
353 |
last_id INT, |
|
354 |
PRIMARY KEY (id) |
|
355 |
);
|
|
356 |
||
357 |
CREATE TABLE t2 ( |
|
358 |
id INT NOT NULL AUTO_INCREMENT, |
|
359 |
last_id INT, |
|
360 |
PRIMARY KEY (id) |
|
361 |
);
|
|
362 |
||
363 |
delimiter |; |
|
364 |
CREATE PROCEDURE p1() |
|
365 |
BEGIN
|
|
366 |
INSERT INTO t2 (last_id) VALUES (LAST_INSERT_ID()); |
|
367 |
INSERT INTO t1 (last_id) VALUES (LAST_INSERT_ID()); |
|
368 |
END| |
|
369 |
delimiter ;| |
|
370 |
||
371 |
CALL p1(); |
|
372 |
SELECT * FROM t1; |
|
373 |
SELECT * FROM t2; |
|
374 |
||
375 |
sync_slave_with_master; |
|
376 |
SELECT * FROM t1; |
|
377 |
SELECT * FROM t2; |
|
378 |
||
379 |
connection master; |
|
380 |
||
381 |
DROP PROCEDURE p1; |
|
382 |
DROP TABLE t1, t2; |
|
383 |
||
384 |
||
385 |
#
|
|
386 |
# BUG#21726: Incorrect result with multiple invocations of
|
|
387 |
# LAST_INSERT_ID
|
|
388 |
#
|
|
389 |
--disable_warnings |
|
390 |
DROP PROCEDURE IF EXISTS p1; |
|
391 |
DROP FUNCTION IF EXISTS f1; |
|
392 |
DROP FUNCTION IF EXISTS f2; |
|
393 |
DROP FUNCTION IF EXISTS f3; |
|
394 |
DROP TABLE IF EXISTS t1, t2; |
|
395 |
--enable_warnings |
|
396 |
||
397 |
CREATE TABLE t1 ( |
|
398 |
i INT NOT NULL AUTO_INCREMENT PRIMARY KEY, |
|
399 |
j INT DEFAULT 0 |
|
400 |
);
|
|
401 |
CREATE TABLE t2 (i INT); |
|
402 |
||
403 |
delimiter |; |
|
404 |
CREATE PROCEDURE p1() |
|
405 |
BEGIN
|
|
406 |
INSERT INTO t1 (i) VALUES (NULL); |
|
407 |
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID()); |
|
408 |
INSERT INTO t1 (i) VALUES (NULL), (NULL); |
|
409 |
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID()); |
|
410 |
END | |
|
411 |
||
412 |
CREATE FUNCTION f1() RETURNS INT MODIFIES SQL DATA |
|
413 |
BEGIN
|
|
414 |
INSERT INTO t1 (i) VALUES (NULL); |
|
415 |
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID()); |
|
416 |
INSERT INTO t1 (i) VALUES (NULL), (NULL); |
|
417 |
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID()); |
|
418 |
RETURN 0; |
|
419 |
END | |
|
420 |
||
421 |
CREATE FUNCTION f2() RETURNS INT NOT DETERMINISTIC |
|
422 |
RETURN LAST_INSERT_ID() | |
|
423 |
||
424 |
CREATE FUNCTION f3() RETURNS INT MODIFIES SQL DATA |
|
425 |
BEGIN
|
|
426 |
INSERT INTO t2 (i) VALUES (LAST_INSERT_ID()); |
|
427 |
RETURN 0; |
|
428 |
END | |
|
429 |
delimiter ;| |
|
430 |
||
431 |
INSERT INTO t1 VALUES (NULL, -1); |
|
432 |
CALL p1(); |
|
433 |
SELECT f1(); |
|
434 |
INSERT INTO t1 VALUES (NULL, f2()), (NULL, LAST_INSERT_ID()), |
|
435 |
(NULL, LAST_INSERT_ID()), (NULL, f2()), (NULL, f2()); |
|
436 |
INSERT INTO t1 VALUES (NULL, f2()); |
|
437 |
INSERT INTO t1 VALUES (NULL, LAST_INSERT_ID()), (NULL, LAST_INSERT_ID(5)), |
|
438 |
(NULL, @@LAST_INSERT_ID); |
|
439 |
# Test replication of substitution "IS NULL" -> "= LAST_INSERT_ID".
|
|
440 |
INSERT INTO t1 VALUES (NULL, 0), (NULL, LAST_INSERT_ID()); |
|
441 |
UPDATE t1 SET j= -1 WHERE i IS NULL; |
|
442 |
||
443 |
# Test statement-based replication of function calls.
|
|
444 |
INSERT INTO t1 (i) VALUES (NULL); |
|
445 |
||
446 |
connection master1; |
|
447 |
INSERT INTO t1 (i) VALUES (NULL); |
|
448 |
||
449 |
connection master; |
|
450 |
SELECT f3(); |
|
451 |
||
452 |
SELECT * FROM t1; |
|
453 |
SELECT * FROM t2; |
|
454 |
||
455 |
sync_slave_with_master; |
|
456 |
SELECT * FROM t1; |
|
457 |
SELECT * FROM t2; |
|
458 |
||
459 |
connection master; |
|
460 |
DROP PROCEDURE p1; |
|
461 |
DROP FUNCTION f1; |
|
462 |
DROP FUNCTION f2; |
|
463 |
DROP FUNCTION f3; |
|
464 |
DROP TABLE t1, t2; |
|
465 |
||
466 |
||
467 |
sync_slave_with_master; |
|
468 |
||
469 |
--echo # |
|
470 |
--echo # End of 5.0 tests |
|
471 |
--echo # |
|
472 |
||
473 |
# Tests in this file are tightly bound together. Recreate t2.
|
|
474 |
connection master; |
|
475 |
create table t2 ( |
|
476 |
id int not null auto_increment, |
|
477 |
last_id int, |
|
478 |
primary key (id) |
|
479 |
);
|
|
480 |
||
481 |
||
482 |
# Test for BUG#20341 "stored function inserting into one
|
|
483 |
# auto_increment puts bad data in slave"
|
|
484 |
||
485 |
connection master; |
|
486 |
truncate table t2; |
|
487 |
create table t1 (id tinyint primary key); # no auto_increment |
|
488 |
||
489 |
delimiter |; |
|
490 |
create function insid() returns int |
|
491 |
begin
|
|
492 |
insert into t2 (last_id) values (0); |
|
493 |
return 0; |
|
494 |
end| |
|
495 |
delimiter ;| |
|
496 |
set sql_log_bin=0; |
|
497 |
insert into t2 (id) values(1),(2),(3); |
|
498 |
delete from t2; |
|
499 |
set sql_log_bin=1; |
|
500 |
#inside SELECT, then inside INSERT
|
|
501 |
select insid(); |
|
502 |
set sql_log_bin=0; |
|
503 |
insert into t2 (id) values(5),(6),(7); |
|
504 |
delete from t2 where id>=5; |
|
505 |
set sql_log_bin=1; |
|
506 |
insert into t1 select insid(); |
|
507 |
select * from t1; |
|
508 |
select * from t2; |
|
509 |
||
510 |
sync_slave_with_master; |
|
511 |
select * from t1; |
|
512 |
select * from t2; |
|
513 |
||
514 |
connection master; |
|
515 |
drop table t1; |
|
516 |
drop function insid; |
|
517 |
||
518 |
truncate table t2; |
|
519 |
create table t1 (n int primary key auto_increment not null, |
|
520 |
b int, unique(b)); |
|
521 |
delimiter |; |
|
522 |
create procedure foo() |
|
523 |
begin
|
|
524 |
insert into t1 values(null,10); |
|
525 |
insert ignore into t1 values(null,10); |
|
526 |
insert ignore into t1 values(null,10); |
|
527 |
insert into t2 values(null,3); |
|
528 |
end| |
|
529 |
delimiter ;| |
|
530 |
call foo(); |
|
531 |
select * from t1; |
|
532 |
select * from t2; |
|
533 |
||
534 |
sync_slave_with_master; |
|
535 |
select * from t1; |
|
536 |
select * from t2; |
|
537 |
||
538 |
connection master; |
|
539 |
drop table t1, t2; |
|
540 |
drop procedure foo; |
|
541 |
sync_slave_with_master; |