summaryrefslogtreecommitdiff
path: root/mysql-test/t/rpl_trigger.test
blob: f3db1cb58419da7e7bd9394d7b5999a08b678691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#
# Test of triggers with replication
#

source include/master-slave.inc;

#
# #12482: Triggers has side effects with auto_increment values
#

create table t1 (a int auto_increment, primary key (a), b int, rand_value double not null);
create table t2 (a int auto_increment, primary key (a), b int);
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);

delimiter |;
create trigger t1 before insert on t1 for each row
begin
 insert into t3 values (NULL, "t1", new.a, new.b, rand());
end|

create trigger t2 after insert on t2 for each row
begin
 insert into t3 values (NULL, "t2", new.a, new.b, rand());
end|
delimiter ;|

insert into t3 values(100,"log",0,0,0);

# Ensure we always have same random numbers
SET @@RAND_SEED1=658490765, @@RAND_SEED2=635893186;

# Emulate that we have rows 2-9 deleted on the slave
insert into t1 values(1,1,rand()),(NULL,2,rand());
insert into t2 (b) values(last_insert_id());
insert into t2 values(3,0),(NULL,0);
insert into t2 values(NULL,0),(500,0);

select a,b, truncate(rand_value,4) from t1;
select * from t2;
select a,name, old_a, old_b, truncate(rand_value,4) from t3;
save_master_pos;
connection slave;
sync_with_master;
--disable_query_log
select "--- On slave --" as "";
--enable_query_log
select a,b, truncate(rand_value,4) from t1;
select * from t2;
select a,name, old_a, old_b, truncate(rand_value,4) from t3;
connection master;
drop table t1,t2,t3;

#
# #12480: NOW() is not constant in a trigger
# #12481: Using NOW() in a stored function breaks statement based replication
#

# Start by getting a lock on 'bug12480' to be able to use get_lock() as sleep()
connect (con2,localhost,root,,);
connection con2;
select get_lock("bug12480",2);
connection default;

create table t1 (a datetime,b  datetime, c datetime);
--ignore_warnings
drop function if exists bug12480;
--enable_warnings

delimiter |;

create function bug12480() returns datetime
begin
  set @a=get_lock("bug12480",2);
  return now();
end|

create trigger t1_first before insert on t1
for each row begin
  set @a=get_lock("bug12480",2);
  set new.b= now();
  set new.c= bug12480();
end
|

delimiter ;|
insert into t1 set a = now();
select a=b && a=c from t1;
let $time=`select a from t1`;

# Check that definer attribute is replicated properly:
#   - dump definers on the master;
#   - wait for the slave to synchronize with the master;
#   - dump definers on the slave;

SELECT routine_name, definer
FROM information_schema.routines;

SELECT trigger_name, definer
FROM information_schema.triggers;

save_master_pos;
connection slave;
sync_with_master;
--disable_query_log
select "--- On slave --" as "";
--enable_query_log

# XXX: Definers of stored procedures and functions are not replicated. WL#2897
# (Complete definer support in the stored routines) addresses this issue. So,
# the result file is expected to be changed after implementation of this WL
# item.

SELECT routine_name, definer
FROM information_schema.routines;

SELECT trigger_name, definer
FROM information_schema.triggers;

select a=b && a=c from t1;
--disable_query_log
eval select a='$time' as 'test' from t1;
--enable_query_log

connection master;
disconnect con2;

truncate table t1;
drop trigger t1_first;

insert into t1 values ("2003-03-03","2003-03-03","2003-03-03"),(bug12480(),bug12480(),bug12480()),(now(),now(),now());
select a=b && a=c from t1;

drop function bug12480;
drop table t1;

#
# #14614: Replication of tables with trigger generates error message if databases is changed
# Note. The error message is emitted by _myfree() using fprintf() to the stderr
# and because of that does not fall into the .result file.
#

create table t1 (i int);
create table t2 (i int);

delimiter |;
create trigger tr1 before insert on t1 for each row
begin
 insert into t2 values (1);
end|
delimiter ;|

create database other;
use other;
insert into test.t1 values (1);

save_master_pos;
connection slave;
sync_with_master;

connection master;
use test;
drop table t1,t2;
drop database other;

#
# Test specific triggers including SELECT into var with replication
# BUG#13227:
# slave performs an update to the replicatable table, t1, 
# and modifies its local data, t3, by mean of its local trigger that uses
# another local table t2.
# Expected values are commented into queries.
#
# Body of the test executes in a loop since the problem occurred randomly.
# 

let $max_rows=5;
let $rnd=10;

--echo test case for BUG#13227
while ($rnd)
{
  --echo -------------------
    echo $rnd;
  --echo -------------------

### SETUP

--disable_warnings
  connection master;
  eval drop table if exists t1$rnd;
  connection slave;
  eval drop table if exists t2$rnd,t3$rnd;
--enable_warnings

  connection master;
  eval create table t1$rnd (f1 int)  /* 2 replicate */;  
  let $i=$max_rows;
  while ($i)
  {
    eval insert into t1$rnd values (-$i);
    dec $i;
  }

  sync_slave_with_master;
#connection slave;
  eval select * from t1$rnd;
  delimiter |;
  eval create trigger trg1$rnd before update on t1$rnd /* slave local */
  for each row
  begin
    DECLARE r integer;
    SELECT f2 INTO r FROM t2$rnd where f1=NEW.f1;
    INSERT INTO t3$rnd values (r);
  end|
  delimiter ;|
  eval create table t2$rnd (f1 int, f2 int) /* slave local */;        
  eval create table t3$rnd (f3 int) /* slave local */;                
  let $i=$max_rows;
  while ($i) 
  {
    eval insert into t2$rnd values ($i, $i*100);
    dec $i;
  }

### Test

#connection slave;

# trigger works as specified when updates from slave
  eval select * from t2$rnd;
  eval UPDATE t1$rnd SET f1=$max_rows where f1=-$max_rows;
  eval SELECT * from t1$rnd /* must be f1 $max_rows, 1 - $max_rows 2 - $max_rows ... -1 */;
  eval SELECT * from t3$rnd /* must be f3 $max_rows*100 */;

  connection master;
  let $i=$max_rows;
  while ($i)
  {
    eval UPDATE t1$rnd SET f1=$i where f1=-$i;
    dec $i;
  }
  
  sync_slave_with_master;
#connection slave;
  eval SELECT * from t1$rnd /* must be f1 $max_rows ... 1 */;
  eval SELECT * from t3$rnd /* must be f3 $max_rows * 100 ...  100 */;
  
### CLEANUP
#connection slave;
  eval drop trigger trg1$rnd;
  eval drop table t2$rnd,t3$rnd;
  
  connection master;
  eval drop table t1$rnd;
  
  dec $rnd;
}



#
# End of tests
#
save_master_pos;
connection slave;
sync_with_master;