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

« back to all changes in this revision

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