~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysql-test/extra/rpl_tests/rpl_insert_id.test

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

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