~ubuntu-branches/ubuntu/trusty/mysql-5.6/trusty

« back to all changes in this revision

Viewing changes to mysql-test/suite/rpl/t/rpl_trigger.test

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-02-12 11:54:27 UTC
  • Revision ID: package-import@ubuntu.com-20140212115427-oq6tfsqxl1wuwehi
Tags: upstream-5.6.15
ImportĀ upstreamĀ versionĀ 5.6.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Test of triggers with replication
 
3
# Adding statement include due to Bug 12574
 
4
# TODO: Remove statement include once 12574 is patched
 
5
--source include/have_binlog_format_mixed_or_statement.inc 
 
6
--source include/master-slave.inc
 
7
--source include/not_gtid_enabled.inc
 
8
 
 
9
disable_query_log;
 
10
call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT");
 
11
--connection slave
 
12
call mtr.add_suppression("The master's UUID has changed, although this should not happen unless you have changed it manually.");
 
13
--connection master
 
14
enable_query_log;
 
15
 
 
16
--disable_warnings
 
17
DROP TABLE IF EXISTS t1;
 
18
DROP TABLE IF EXISTS t2;
 
19
DROP TABLE IF EXISTS t3;
 
20
 
 
21
--enable_warnings
 
22
 
 
23
#
 
24
# #12482: Triggers has side effects with auto_increment values
 
25
#
 
26
 
 
27
create table t1 (a int auto_increment, primary key (a), b int, rand_value double not null);
 
28
create table t2 (a int auto_increment, primary key (a), b int);
 
29
create table t3 (a int auto_increment, primary key (a), name varchar(64) not null, old_a int, old_b int, rand_value double not null);
 
30
 
 
31
delimiter |;
 
32
create trigger t1 before insert on t1 for each row
 
33
begin
 
34
 insert into t3 values (NULL, "t1", new.a, new.b, rand());
 
35
end|
 
36
 
 
37
create trigger t2 after insert on t2 for each row
 
38
begin
 
39
 insert into t3 values (NULL, "t2", new.a, new.b, rand());
 
40
end|
 
41
delimiter ;|
 
42
 
 
43
insert into t3 values(100,"log",0,0,0);
 
44
 
 
45
# Ensure we always have same random numbers
 
46
SET @@RAND_SEED1=658490765, @@RAND_SEED2=635893186;
 
47
 
 
48
# Emulate that we have rows 2-9 deleted on the slave
 
49
--disable_warnings
 
50
insert into t1 values(1,1,rand()),(NULL,2,rand());
 
51
insert into t2 (b) values(last_insert_id());
 
52
insert into t2 values(3,0),(NULL,0);
 
53
insert into t2 values(NULL,0),(500,0);
 
54
--enable_warnings
 
55
 
 
56
select a,b, truncate(rand_value,4) from t1;
 
57
select * from t2;
 
58
select a,name, old_a, old_b, truncate(rand_value,4) from t3;
 
59
sync_slave_with_master;
 
60
--disable_query_log
 
61
select "--- On slave --" as "";
 
62
--enable_query_log
 
63
select a,b, truncate(rand_value,4) from t1;
 
64
select * from t2;
 
65
select a,name, old_a, old_b, truncate(rand_value,4) from t3;
 
66
connection master;
 
67
drop table t1,t2,t3;
 
68
 
 
69
#
 
70
# #12480: NOW() is not constant in a trigger
 
71
# #12481: Using NOW() in a stored function breaks statement based replication
 
72
#
 
73
 
 
74
# Start by getting a lock on 'bug12480' to be able to use get_lock() as sleep()
 
75
connect (con2,localhost,root,,);
 
76
connection con2;
 
77
select get_lock("bug12480",2);
 
78
connection default;
 
79
 
 
80
create table t1 (a datetime,b  datetime, c datetime);
 
81
--disable_warnings
 
82
drop function if exists bug12480;
 
83
--enable_warnings
 
84
 
 
85
delimiter |;
 
86
 
 
87
create function bug12480() returns datetime
 
88
begin
 
89
  set @a=get_lock("bug12480",2);
 
90
  return now();
 
91
end|
 
92
 
 
93
create trigger t1_first before insert on t1
 
94
for each row begin
 
95
  set @a=get_lock("bug12480",2);
 
96
  set new.b= now();
 
97
  set new.c= bug12480();
 
98
end
 
99
|
 
100
 
 
101
delimiter ;|
 
102
# The trigger causes a warning for unsafe statement when
 
103
# binlog_format=statement since it uses get_lock.
 
104
--disable_warnings
 
105
insert into t1 set a = now();
 
106
--enable_warnings
 
107
select a=b && a=c from t1;
 
108
let $time=`select a from t1`;
 
109
 
 
110
# Check that definer attribute is replicated properly:
 
111
#   - dump definers on the master;
 
112
#   - wait for the slave to synchronize with the master;
 
113
#   - dump definers on the slave;
 
114
 
 
115
SELECT routine_name, definer
 
116
FROM information_schema.routines
 
117
WHERE routine_name = 'bug12480';
 
118
 
 
119
SELECT trigger_name, definer
 
120
FROM information_schema.triggers
 
121
WHERE trigger_name = 't1_first';
 
122
 
 
123
sync_slave_with_master;
 
124
--disable_query_log
 
125
select "--- On slave --" as "";
 
126
--enable_query_log
 
127
 
 
128
# XXX: Definers of stored procedures and functions are not replicated. WL#2897
 
129
# (Complete definer support in the stored routines) addresses this issue. So,
 
130
# the result file is expected to be changed after implementation of this WL
 
131
# item.
 
132
 
 
133
SELECT routine_name, definer
 
134
FROM information_schema.routines
 
135
WHERE routine_name = 'bug12480';
 
136
 
 
137
SELECT trigger_name, definer
 
138
FROM information_schema.triggers
 
139
WHERE trigger_name = 't1_first';
 
140
 
 
141
select a=b && a=c from t1;
 
142
--disable_query_log
 
143
eval select a='$time' as 'test' from t1;
 
144
--enable_query_log
 
145
 
 
146
connection master;
 
147
disconnect con2;
 
148
 
 
149
truncate table t1;
 
150
drop trigger t1_first;
 
151
 
 
152
# The trigger causes a warning for unsafe statement when
 
153
# binlog_format=statement since it uses get_lock.
 
154
--disable_warnings
 
155
insert into t1 values ("2003-03-03","2003-03-03","2003-03-03"),(bug12480(),bug12480(),bug12480()),(now(),now(),now());
 
156
--enable_warnings
 
157
select a=b && a=c from t1;
 
158
 
 
159
drop function bug12480;
 
160
drop table t1;
 
161
 
 
162
#
 
163
# #14614: Replication of tables with trigger generates error message if databases is changed
 
164
# Note. The error message is emitted by _myfree() using fprintf() to the stderr
 
165
# and because of that does not fall into the .result file.
 
166
#
 
167
 
 
168
create table t1 (i int);
 
169
create table t2 (i int);
 
170
 
 
171
delimiter |;
 
172
create trigger tr1 before insert on t1 for each row
 
173
begin
 
174
 insert into t2 values (1);
 
175
end|
 
176
delimiter ;|
 
177
 
 
178
create database other;
 
179
use other;
 
180
insert into test.t1 values (1);
 
181
 
 
182
sync_slave_with_master;
 
183
 
 
184
connection master;
 
185
use test;
 
186
drop table t1,t2;
 
187
drop database other;
 
188
 
 
189
 
 
190
#
 
191
# Test specific triggers including SELECT into var with replication
 
192
# BUG#13227:
 
193
# slave performs an update to the replicatable table, t1, 
 
194
# and modifies its local data, t3, by mean of its local trigger that uses
 
195
# another local table t2.
 
196
# Expected values are commented into queries.
 
197
#
 
198
# Body of the test executes in a loop since the problem occurred randomly.
 
199
 
200
 
 
201
let $max_rows=5;
 
202
let $rnd=10;
 
203
 
 
204
--echo test case for BUG#13227
 
205
while ($rnd)
 
206
{
 
207
  --echo -------------------
 
208
    echo $rnd;
 
209
  --echo -------------------
 
210
 
 
211
### SETUP
 
212
 
 
213
--disable_warnings
 
214
  connection master;
 
215
  eval drop table if exists t1$rnd;
 
216
  connection slave;
 
217
  eval drop table if exists t2$rnd,t3$rnd;
 
218
--enable_warnings
 
219
 
 
220
  connection master;
 
221
  eval create table t1$rnd (f1 int)  /* 2 replicate */;  
 
222
  let $i=$max_rows;
 
223
  while ($i)
 
224
  {
 
225
    eval insert into t1$rnd values (-$i);
 
226
    dec $i;
 
227
  }
 
228
 
 
229
  sync_slave_with_master;
 
230
#connection slave;
 
231
  eval select * from t1$rnd;
 
232
  delimiter |;
 
233
  eval create trigger trg1$rnd before update on t1$rnd /* slave local */
 
234
  for each row
 
235
  begin
 
236
    DECLARE r integer;
 
237
    SELECT f2 INTO r FROM t2$rnd where f1=NEW.f1;
 
238
    INSERT INTO t3$rnd values (r);
 
239
  end|
 
240
  delimiter ;|
 
241
  eval create table t2$rnd (f1 int, f2 int) /* slave local */;        
 
242
  eval create table t3$rnd (f3 int) /* slave local */;                
 
243
  let $i=$max_rows;
 
244
  while ($i) 
 
245
  {
 
246
    eval insert into t2$rnd values ($i, $i*100);
 
247
    dec $i;
 
248
  }
 
249
 
 
250
### Test
 
251
 
 
252
#connection slave;
 
253
 
 
254
# trigger works as specified when updates from slave
 
255
  eval select * from t2$rnd;
 
256
  eval UPDATE t1$rnd SET f1=$max_rows where f1=-$max_rows;
 
257
  eval SELECT * from t1$rnd /* must be f1 $max_rows, 1 - $max_rows 2 - $max_rows ... -1 */;
 
258
  eval SELECT * from t3$rnd /* must be f3 $max_rows*100 */;
 
259
 
 
260
  connection master;
 
261
  let $i=$max_rows;
 
262
  while ($i)
 
263
  {
 
264
    eval UPDATE t1$rnd SET f1=$i where f1=-$i;
 
265
    dec $i;
 
266
  }
 
267
  
 
268
  sync_slave_with_master;
 
269
#connection slave;
 
270
  eval SELECT * from t1$rnd /* must be f1 $max_rows ... 1 */;
 
271
  eval SELECT * from t3$rnd /* must be f3 $max_rows * 100 ...  100 */;
 
272
  
 
273
### CLEANUP
 
274
#connection slave;
 
275
  eval drop trigger trg1$rnd;
 
276
  eval drop table t2$rnd,t3$rnd;
 
277
  
 
278
  connection master;
 
279
  eval drop table t1$rnd;
 
280
  sync_slave_with_master;
 
281
  connection master;
 
282
  
 
283
  dec $rnd;
 
284
}
 
285
 
 
286
 
 
287
#
 
288
# BUG#16266: Definer is not fully qualified error during replication.
 
289
#
 
290
# The idea of this test is to emulate replication of a trigger from the old
 
291
# master (master w/o "DEFINER in triggers" support) to the new slave and check
 
292
# that:
 
293
#   1. the trigger on the slave will be replicated w/o errors;
 
294
#   2. the trigger on the slave will be non-SUID (will have no DEFINER);
 
295
#   3. the trigger can be activated later on the slave w/o errors.
 
296
#
 
297
# In order to emulate this kind of replication, we make the slave playing the binlog,
 
298
# recorded by 5.0.16 master. This binlog contains the following statements:
 
299
#   CREATE TABLE t1(c INT);
 
300
#   CREATE TABLE t2(s CHAR(200));
 
301
#   CREATE TRIGGER trg1 AFTER INSERT ON t1
 
302
#     FOR EACH ROW
 
303
#       INSERT INTO t2 VALUES(CURRENT_USER());
 
304
#   INSERT INTO t1 VALUES(1);
 
305
#
 
306
 
 
307
# 1. Check that the trigger's replication is succeeded.
 
308
 
 
309
# Stop the slave.
 
310
 
 
311
connection slave;
 
312
STOP SLAVE;
 
313
 
 
314
# Replace master's binlog.
 
315
 
 
316
connection master;
 
317
let $MYSQLD_DATADIR= `select @@datadir`;
 
318
FLUSH LOGS;
 
319
 
 
320
# Stop master server
 
321
--let $rpl_server_number= 1
 
322
--source include/rpl_stop_server.inc
 
323
 
 
324
# Replace binlog
 
325
remove_file $MYSQLD_DATADIR/master-bin.000001;
 
326
copy_file $MYSQL_TEST_DIR/std_data/bug16266.000001 $MYSQLD_DATADIR/master-bin.000001;
 
327
 
 
328
--let $rpl_server_number= 1
 
329
--source include/rpl_start_server.inc
 
330
 
 
331
let $binlog_version= query_get_value(SHOW BINLOG EVENTS, Info, 1);
 
332
 
 
333
 
 
334
# Make the slave to replay the new binlog.
 
335
--echo --> Master binlog: $binlog_version
 
336
 
 
337
# Make the slave to replay the new binlog.
 
338
 
 
339
connection slave;
 
340
RESET SLAVE;
 
341
START SLAVE;
 
342
 
 
343
SELECT MASTER_POS_WAIT('master-bin.000001', 513) >= 0;
 
344
 
 
345
# Check that the replication succeeded.
 
346
 
 
347
SHOW TABLES LIKE 't_';
 
348
SHOW TRIGGERS;
 
349
SELECT * FROM t1;
 
350
SELECT * FROM t2;
 
351
 
 
352
# 2. Check that the trigger is non-SUID on the slave;
 
353
# 3. Check that the trigger can be activated on the slave.
 
354
#
 
355
# We disable warnings here since it affects the result file in
 
356
# different ways depending on the mode being used.
 
357
 
 
358
disable_warnings;
 
359
INSERT INTO t1 VALUES(2);
 
360
enable_warnings;
 
361
 
 
362
SELECT * FROM t1;
 
363
SELECT * FROM t2;
 
364
 
 
365
# That's all, cleanup.
 
366
 
 
367
DROP TRIGGER trg1;
 
368
DROP TABLE t1;
 
369
DROP TABLE t2;
 
370
 
 
371
STOP SLAVE;
 
372
RESET SLAVE;
 
373
 
 
374
# The master should be clean.
 
375
 
 
376
connection master;
 
377
SHOW TABLES LIKE 't_';
 
378
SHOW TRIGGERS;
 
379
 
 
380
RESET MASTER;
 
381
 
 
382
# Restart slave.
 
383
 
 
384
connection slave;
 
385
START SLAVE;
 
386
 
 
387
 
 
388
#
 
389
# BUG#20438: CREATE statements for views, stored routines and triggers can be
 
390
# not replicable.
 
391
#
 
392
 
 
393
--source include/rpl_reset.inc
 
394
--echo
 
395
--echo ---> Test for BUG#20438
 
396
 
 
397
# Prepare environment.
 
398
 
 
399
--echo
 
400
--echo ---> Preparing environment...
 
401
--echo ---> connection: master
 
402
--connection master
 
403
 
 
404
--disable_warnings
 
405
DROP TABLE IF EXISTS t1;
 
406
DROP TABLE IF EXISTS t2;
 
407
--enable_warnings
 
408
 
 
409
--echo
 
410
--echo ---> Synchronizing slave with master...
 
411
 
 
412
--sync_slave_with_master
 
413
 
 
414
--echo
 
415
--echo ---> connection: master
 
416
--connection master
 
417
 
 
418
# Test.
 
419
 
 
420
--echo
 
421
--echo ---> Creating objects...
 
422
 
 
423
CREATE TABLE t1(c INT);
 
424
CREATE TABLE t2(c INT);
 
425
 
 
426
/*!50003 CREATE TRIGGER t1_bi BEFORE INSERT ON t1
 
427
  FOR EACH ROW
 
428
    INSERT INTO t2 VALUES(NEW.c * 10) */;
 
429
 
 
430
--echo
 
431
--echo ---> Inserting value...
 
432
 
 
433
INSERT INTO t1 VALUES(1);
 
434
 
 
435
--echo
 
436
--echo ---> Checking on master...
 
437
 
 
438
SELECT * FROM t1;
 
439
SELECT * FROM t2;
 
440
 
 
441
--echo
 
442
--echo ---> Synchronizing slave with master...
 
443
 
 
444
--sync_slave_with_master
 
445
 
 
446
--echo ---> connection: master
 
447
 
 
448
--echo
 
449
--echo ---> Checking on slave...
 
450
 
 
451
SELECT * FROM t1;
 
452
SELECT * FROM t2;
 
453
 
 
454
# Cleanup.
 
455
 
 
456
--echo
 
457
--echo ---> connection: master
 
458
--connection master
 
459
 
 
460
--echo
 
461
--echo ---> Cleaning up...
 
462
 
 
463
DROP TABLE t1;
 
464
DROP TABLE t2;
 
465
 
 
466
--sync_slave_with_master
 
467
--connection master
 
468
 
 
469
#
 
470
# BUG#23703: DROP TRIGGER needs an IF EXISTS
 
471
#
 
472
 
 
473
connection master;
 
474
 
 
475
--disable_warnings
 
476
drop table if exists t1;
 
477
--enable_warnings
 
478
 
 
479
create table t1(a int, b varchar(50));
 
480
 
 
481
-- error ER_TRG_DOES_NOT_EXIST
 
482
drop trigger not_a_trigger;
 
483
 
 
484
drop trigger if exists not_a_trigger;
 
485
 
 
486
create trigger t1_bi before insert on t1
 
487
for each row set NEW.b := "In trigger t1_bi";
 
488
 
 
489
insert into t1 values (1, "a");
 
490
drop trigger if exists t1_bi;
 
491
insert into t1 values (2, "b");
 
492
drop trigger if exists t1_bi;
 
493
insert into t1 values (3, "c");
 
494
 
 
495
select * from t1;
 
496
sync_slave_with_master;
 
497
select * from t1;
 
498
 
 
499
connection master;
 
500
 
 
501
drop table t1;
 
502
sync_slave_with_master;
 
503
 
 
504
#
 
505
# Bug#40116: Uncommited changes are replicated and stay on slave after
 
506
# rollback on master
 
507
#
 
508
 
 
509
connection master;
 
510
--source include/rpl_reset.inc
 
511
 
 
512
source include/have_innodb.inc;
 
513
connection slave;
 
514
source include/have_innodb.inc;
 
515
 
 
516
connection master;
 
517
create table t1 ( f int ) engine = innodb;
 
518
create table log ( r int ) engine = myisam; 
 
519
create trigger tr
 
520
  after insert on t1
 
521
  for each row insert into log values ( new.f );
 
522
 
 
523
set autocommit = 0;
 
524
--disable_warnings
 
525
insert into t1 values ( 1 );
 
526
--enable_warnings
 
527
rollback;
 
528
sync_slave_with_master;
 
529
 
 
530
let $diff_tables= master:t1, slave:t1;
 
531
--source include/diff_tables.inc
 
532
 
 
533
let $diff_tables= master:log, slave:log;
 
534
--source include/diff_tables.inc
 
535
 
 
536
connection master;
 
537
drop table t1, log;
 
538
sync_slave_with_master;
 
539
 
 
540
#
 
541
# End of tests
 
542
#
 
543
--source include/rpl_end.inc