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
9
DROP TABLE IF EXISTS t1;
10
DROP TABLE IF EXISTS t2;
11
DROP TABLE IF EXISTS t3;
16
# #12482: Triggers has side effects with auto_increment values
19
create table t1 (a int auto_increment, primary key (a), b int, rand_value double not null);
20
create table t2 (a int auto_increment, primary key (a), b int);
21
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
create trigger t1 before insert on t1 for each row
26
insert into t3 values (NULL, "t1", new.a, new.b, rand());
29
create trigger t2 after insert on t2 for each row
31
insert into t3 values (NULL, "t2", new.a, new.b, rand());
35
insert into t3 values(100,"log",0,0,0);
37
# Ensure we always have same random numbers
38
SET @@RAND_SEED1=658490765, @@RAND_SEED2=635893186;
40
# Emulate that we have rows 2-9 deleted on the slave
41
insert into t1 values(1,1,rand()),(NULL,2,rand());
42
insert into t2 (b) values(last_insert_id());
43
insert into t2 values(3,0),(NULL,0);
44
insert into t2 values(NULL,0),(500,0);
46
select a,b, truncate(rand_value,4) from t1;
48
select a,name, old_a, old_b, truncate(rand_value,4) from t3;
53
select "--- On slave --" as "";
55
select a,b, truncate(rand_value,4) from t1;
57
select a,name, old_a, old_b, truncate(rand_value,4) from t3;
62
# #12480: NOW() is not constant in a trigger
63
# #12481: Using NOW() in a stored function breaks statement based replication
66
# Start by getting a lock on 'bug12480' to be able to use get_lock() as sleep()
67
connect (con2,localhost,root,,);
69
select get_lock("bug12480",2);
72
create table t1 (a datetime,b datetime, c datetime);
74
drop function if exists bug12480;
79
create function bug12480() returns datetime
81
set @a=get_lock("bug12480",2);
85
create trigger t1_first before insert on t1
87
set @a=get_lock("bug12480",2);
89
set new.c= bug12480();
94
insert into t1 set a = now();
95
select a=b && a=c from t1;
96
let $time=`select a from t1`;
98
# Check that definer attribute is replicated properly:
99
# - dump definers on the master;
100
# - wait for the slave to synchronize with the master;
101
# - dump definers on the slave;
103
SELECT routine_name, definer
104
FROM information_schema.routines
105
WHERE routine_name = 'bug12480';
107
SELECT trigger_name, definer
108
FROM information_schema.triggers
109
WHERE trigger_name = 't1_first';
115
select "--- On slave --" as "";
118
# XXX: Definers of stored procedures and functions are not replicated. WL#2897
119
# (Complete definer support in the stored routines) addresses this issue. So,
120
# the result file is expected to be changed after implementation of this WL
123
SELECT routine_name, definer
124
FROM information_schema.routines
125
WHERE routine_name = 'bug12480';
127
SELECT trigger_name, definer
128
FROM information_schema.triggers
129
WHERE trigger_name = 't1_first';
131
select a=b && a=c from t1;
133
eval select a='$time' as 'test' from t1;
140
drop trigger t1_first;
142
insert into t1 values ("2003-03-03","2003-03-03","2003-03-03"),(bug12480(),bug12480(),bug12480()),(now(),now(),now());
143
select a=b && a=c from t1;
145
drop function bug12480;
149
# #14614: Replication of tables with trigger generates error message if databases is changed
150
# Note. The error message is emitted by _myfree() using fprintf() to the stderr
151
# and because of that does not fall into the .result file.
154
create table t1 (i int);
155
create table t2 (i int);
158
create trigger tr1 before insert on t1 for each row
160
insert into t2 values (1);
164
create database other;
166
insert into test.t1 values (1);
179
# Test specific triggers including SELECT into var with replication
181
# slave performs an update to the replicatable table, t1,
182
# and modifies its local data, t3, by mean of its local trigger that uses
183
# another local table t2.
184
# Expected values are commented into queries.
186
# Body of the test executes in a loop since the problem occurred randomly.
192
--echo test case for BUG#13227
195
--echo -------------------
197
--echo -------------------
203
eval drop table if exists t1$rnd;
205
eval drop table if exists t2$rnd,t3$rnd;
209
eval create table t1$rnd (f1 int) /* 2 replicate */;
213
eval insert into t1$rnd values (-$i);
217
sync_slave_with_master;
219
eval select * from t1$rnd;
221
eval create trigger trg1$rnd before update on t1$rnd /* slave local */
225
SELECT f2 INTO r FROM t2$rnd where f1=NEW.f1;
226
INSERT INTO t3$rnd values (r);
229
eval create table t2$rnd (f1 int, f2 int) /* slave local */;
230
eval create table t3$rnd (f3 int) /* slave local */;
234
eval insert into t2$rnd values ($i, $i*100);
242
# trigger works as specified when updates from slave
243
eval select * from t2$rnd;
244
eval UPDATE t1$rnd SET f1=$max_rows where f1=-$max_rows;
245
eval SELECT * from t1$rnd /* must be f1 $max_rows, 1 - $max_rows 2 - $max_rows ... -1 */;
246
eval SELECT * from t3$rnd /* must be f3 $max_rows*100 */;
252
eval UPDATE t1$rnd SET f1=$i where f1=-$i;
256
sync_slave_with_master;
258
eval SELECT * from t1$rnd /* must be f1 $max_rows ... 1 */;
259
eval SELECT * from t3$rnd /* must be f3 $max_rows * 100 ... 100 */;
263
eval drop trigger trg1$rnd;
264
eval drop table t2$rnd,t3$rnd;
267
eval drop table t1$rnd;
274
# BUG#16266: Definer is not fully qualified error during replication.
276
# The idea of this test is to emulate replication of a trigger from the old
277
# master (master w/o "DEFINER in triggers" support) to the new slave and check
279
# 1. the trigger on the slave will be replicated w/o errors;
280
# 2. the trigger on the slave will be non-SUID (will have no DEFINER);
281
# 3. the trigger can be activated later on the slave w/o errors.
283
# In order to emulate this kind of replication, we make the slave playing the binlog,
284
# recorded by 5.0.16 master. This binlog contains the following statements:
285
# CREATE TABLE t1(c INT);
286
# CREATE TABLE t2(s CHAR(200));
287
# CREATE TRIGGER trg1 AFTER INSERT ON t1
289
# INSERT INTO t2 VALUES(CURRENT_USER());
290
# INSERT INTO t1 VALUES(1);
293
# 1. Check that the trigger's replication is succeeded.
300
# Replace master's binlog.
304
exec cp $MYSQL_TEST_DIR/std_data/bug16266.000001 $MYSQLTEST_VARDIR/log/master-bin.000001;
306
# Make the slave to replay the new binlog.
312
SELECT MASTER_POS_WAIT('master-bin.000001', 513) >= 0;
314
# Check that the replication succeeded.
316
SHOW TABLES LIKE 't_';
321
# 2. Check that the trigger is non-SUID on the slave;
322
# 3. Check that the trigger can be activated on the slave.
324
# We disable warnings here since it affects the result file in
325
# different ways depending on the mode being used.
328
INSERT INTO t1 VALUES(2);
334
# That's all, cleanup.
343
# The master should be clean.
346
SHOW TABLES LIKE 't_';
358
# BUG#20438: CREATE statements for views, stored routines and triggers can be
363
--echo ---> Test for BUG#20438
365
# Prepare environment.
368
--echo ---> Preparing environment...
369
--echo ---> connection: master
373
DROP TABLE IF EXISTS t1;
374
DROP TABLE IF EXISTS t2;
378
--echo ---> Synchronizing slave with master...
385
--echo ---> connection: master
391
--echo ---> Creating objects...
393
CREATE TABLE t1(c INT);
394
CREATE TABLE t2(c INT);
396
/*!50003 CREATE TRIGGER t1_bi BEFORE INSERT ON t1
398
INSERT INTO t2 VALUES(NEW.c * 10) */;
401
--echo ---> Inserting value...
403
INSERT INTO t1 VALUES(1);
406
--echo ---> Checking on master...
412
--echo ---> Synchronizing slave with master...
418
--echo ---> connection: master
421
--echo ---> Checking on slave...
429
--echo ---> connection: master
433
--echo ---> Cleaning up...
444
# BUG#23703: DROP TRIGGER needs an IF EXISTS
450
drop table if exists t1;
453
create table t1(a int, b varchar(50));
455
-- error ER_TRG_DOES_NOT_EXIST
456
drop trigger not_a_trigger;
458
drop trigger if exists not_a_trigger;
460
create trigger t1_bi before insert on t1
461
for each row set NEW.b := "In trigger t1_bi";
463
insert into t1 values (1, "a");
464
drop trigger if exists t1_bi;
465
insert into t1 values (2, "b");
466
drop trigger if exists t1_bi;
467
insert into t1 values (3, "c");