summaryrefslogtreecommitdiffstats
path: root/mysql-test/main/trigger_wl3253.test
blob: 3504eeaf889d54d7cb9d2b34459745e9d5945c43 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
--echo #
--echo # WL#3253: multiple triggers per table
--echo #

SET @binlog_format_saved = @@binlog_format;
SET binlog_format=ROW;
SET time_zone='+00:00';

--echo #
--echo # Test 1.
--echo # Check that the sequence of triggers for the same combination 
--echo # of event type/action type can be created for a table
--echo # and is fired consequently in the order of its creation
--echo # during statement execution.
--echo # In this test we check BEFORE triggers.
--echo # 

CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT, b INT AUTO_INCREMENT PRIMARY KEY);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a);
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 100);

INSERT INTO t1 VALUES (1);

SELECT * FROM t2 ORDER BY b;

DROP TABLE t2;
DROP TABLE t1;

--echo #
--echo # Test 2.
--echo # Check that the sequence of triggers for the same combination 
--echo # of event type/action type can be created for a table
--echo # and is fired consequently in the order of its creation
--echo # during statement execution.
--echo # In this test we check AFTER triggers.
--echo # 

CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT, b INT AUTO_INCREMENT PRIMARY KEY);

CREATE TRIGGER tr1_bi AFTER INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a);
CREATE TRIGGER tr2_bi AFTER INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 100);

INSERT INTO t1 VALUES (1);

SELECT * FROM t2 ORDER BY b;

DROP TABLE t2;
DROP TABLE t1;

--echo #
--echo # Test 3.
--echo # Check that the sequences of triggers for the different event types
--echo # can be created for a table and are fired consequently
--echo # in the order of its creation during statement execution.
--echo #

CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT, b INT AUTO_INCREMENT PRIMARY KEY);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a);
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 100);

CREATE TRIGGER tr1_bu BEFORE UPDATE ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a);
CREATE TRIGGER tr2_bu BEFORE UPDATE ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 300);

INSERT INTO t1 VALUES (1);

SELECT * FROM t2 ORDER BY b;

UPDATE t1 SET a = 5;

SELECT * FROM t2 ORDER BY b;

DROP TABLE t2;
DROP TABLE t1;

--echo #
--echo # Test 4.
--echo # Check that every new created trigger has unique action_order value
--echo # started from 1 and NOT NULL value for creation timestamp.
--echo #

CREATE TABLE t1 (a INT);

SET TIMESTAMP=UNIX_TIMESTAMP('2013-01-31 09:00:00');
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
SET TIMESTAMP=UNIX_TIMESTAMP('2013-01-31 09:00:01');
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=2;

SELECT trigger_name, created, action_order FROM information_schema.triggers WHERE trigger_schema='test';

DROP TABLE t1;
SET TIMESTAMP=DEFAULT;

--echo #
--echo # Test 5.
--echo # Check that action_order attribute isn't shown 
--echo # in the output of SHOW TRIGGERS and SHOW CREATE TRIGGER
--echo #

CREATE TABLE t1 (a INT);
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;

--replace_column 6 #
SHOW TRIGGERS;

--replace_column 17 #
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'test';

--replace_column 7 #
SHOW CREATE TRIGGER tr1_bi;

DROP TABLE t1;

--echo #
--echo # Test 6.
--echo # Check that action_order attribute is reused when trigger 
--echo # are recreated.
--echo #

CREATE TABLE t1 (a INT);
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

DROP TRIGGER tr1_bi;

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=2;

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

DROP TABLE t1;

--echo #
--echo # Test 7.
--echo # Check that it is possible to create several triggers with
--echo # the same value for creation timestamp.
--echo #

CREATE TABLE t1 (a INT);

SET TIMESTAMP=UNIX_TIMESTAMP('2013-01-31 09:00:01');
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=2;

SELECT trigger_name, created, action_order FROM information_schema.triggers WHERE trigger_schema='test';

DROP TABLE t1;
SET TIMESTAMP=DEFAULT;

--echo #
--echo # Test 8.
--echo # Check that SHOW CREATE TRIGGER outputs the CREATED attribute
--echo # and it is not NULL
--echo #

CREATE TABLE t1 (a INT);

SET TIMESTAMP=UNIX_TIMESTAMP('2013-01-31 09:00:01');
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
SHOW CREATE TRIGGER tr1_bi;

DROP TABLE t1;
SET TIMESTAMP=DEFAULT;

--echo #
--echo # Test 9.
--echo # Check that SHOW TRIGGERS outputs the CREATED attribute
--echo # and it is not NULL.
--echo #

CREATE TABLE t1 (a INT);

SET TIMESTAMP=UNIX_TIMESTAMP('2013-01-31 09:00:01');

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;

SHOW TRIGGERS;

SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'test';

DROP TABLE t1;

SET TIMESTAMP=DEFAULT;

--echo #
--echo # Test 10.
--echo # Check that FOLLOWS clause is supported and works correctly.
--echo #

CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT, b INT AUTO_INCREMENT PRIMARY KEY);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 100);
CREATE TRIGGER tr3_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 300);
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW FOLLOWS tr1_bi INSERT INTO t2 (a) VALUES (NEW.a + 200);

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

INSERT INTO t1 VALUES (1);
SELECT * FROM t2 ORDER BY b;

DROP TABLE t2;
DROP TABLE t1;

--echo #
--echo # Test 11.
--echo # Check that PRECEDES clause is supported and works correctly.
--echo #

CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT, b INT AUTO_INCREMENT PRIMARY KEY);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 100);
CREATE TRIGGER tr3_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 300);
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW PRECEDES tr3_bi INSERT INTO t2 (a) VALUES (NEW.a + 200);

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

INSERT INTO t1 VALUES (1);
SELECT * FROM t2 ORDER BY b;

DROP TABLE t2;
DROP TABLE t1;

--echo #
--echo # Test 12.
--echo # Check that the PRECEDES works properly for the 1st trigger in the chain.
--echo #

CREATE TABLE t1 (a INT);
CREATE TABLE t2 (a INT, b INT AUTO_INCREMENT PRIMARY KEY);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW INSERT INTO t2 (a) VALUES (NEW.a + 100);
CREATE TRIGGER tr0_bi BEFORE INSERT ON t1 FOR EACH ROW PRECEDES tr1_bi INSERT INTO t2 (a) VALUES (NEW.a);

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

INSERT INTO t1 VALUES (1);
SELECT * FROM t2 ORDER BY b;

DROP TABLE t2;
DROP TABLE t1;

--echo #
--echo # Test 13.
--echo # Check that error is reported if the FOLLOWS clause references to
--echo # non-existing trigger
--echo #

CREATE TABLE t1 (a INT);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr3_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=3;

--error ER_REFERENCED_TRG_DOES_NOT_EXIST
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW FOLLOWS tr0_bi SET @a:=2;

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

DROP TABLE t1;

--echo #
--echo # Test 14.
--echo # Check that error is reported if the PRECEDES clause references to
--echo # non-existing trigger
--echo #

CREATE TABLE t1 (a INT);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr3_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=3;

--error ER_REFERENCED_TRG_DOES_NOT_EXIST
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW PRECEDES tr0_bi SET @a:=2;

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

DROP TABLE t1;

--echo #
--echo # Test 15.
--echo # Check that action_order value is independent for each type of event
--echo # (INSERT/UPDATE/DELETE)
--echo #

CREATE TABLE t1 (a INT);

CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=2;
CREATE TRIGGER tr1_bu BEFORE UPDATE ON t1 FOR EACH ROW SET @a:=3;

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

CREATE TRIGGER tr3_bi BEFORE INSERT ON t1 FOR EACH ROW FOLLOWS tr2_bi SET @a:=3;
CREATE TRIGGER tr2_bu BEFORE UPDATE ON t1 FOR EACH ROW FOLLOWS tr1_bu SET @a:=3;

SELECT trigger_name, action_order FROM information_schema.triggers WHERE trigger_schema='test';

DROP TABLE t1;

--echo #
--echo # Test 16.
--echo # Check that the trigger in the clause FOLLOWS/PRECEDES can refences
--echo # only to the trigger for the same ACTION/TIMINMG
--echo #

CREATE TABLE t1 (a INT);
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr1_bu BEFORE UPDATE ON t1 FOR EACH ROW SET @a:=3;

--error ER_REFERENCED_TRG_DOES_NOT_EXIST
CREATE TRIGGER tr2_bu BEFORE UPDATE ON t1 FOR EACH ROW FOLLOWS tr1_bi SET @a:=3;

--error ER_REFERENCED_TRG_DOES_NOT_EXIST
CREATE TRIGGER tr2_au AFTER UPDATE ON t1 FOR EACH ROW FOLLOWS tr1_bi SET @a:=3;

--error ER_REFERENCED_TRG_DOES_NOT_EXIST
CREATE TRIGGER tr1_au AFTER UPDATE ON t1 FOR EACH ROW FOLLOWS tr1_bu SET @a:=3;

--error ER_REFERENCED_TRG_DOES_NOT_EXIST
CREATE TRIGGER tr1_ai AFTER INSERT ON t1 FOR EACH ROW FOLLOWS tr1_bi SET @a:=3;

--replace_column 6 #
SHOW TRIGGERS;

--replace_column 17 #
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'test';

DROP TABLE t1;

# Binlog is required
--source include/have_log_bin.inc

--echo #
--echo # Test 17. Check that table's triggers are dumped correctly.
--echo #
CREATE TABLE t1 (a INT);
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=2;
CREATE TRIGGER tr1_bu BEFORE UPDATE ON t1 FOR EACH ROW SET @a:=3;

# dump tables and triggers
--exec $MYSQL_DUMP --compact test

DROP TABLE t1;

--echo #
--echo # Test 18. Check that table's triggers are dumped in right order
--echo #          taking into account the PRECEDES/FOLLOWS clauses.
--echo #

CREATE TABLE t1 (a INT);
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=2;
CREATE TRIGGER tr0_bi BEFORE INSERT ON t1 FOR EACH ROW PRECEDES tr1_bi SET @a:=0;
CREATE TRIGGER tr1_1_bi BEFORE INSERT ON t1 FOR EACH ROW FOLLOWS tr1_bi SET @a:=0;

--echo # Expected order of triggers in the dump is: tr0_bi, tr1_bi, tr1_1_bi, tr2_i.
# dump tables and triggers
--exec $MYSQL_DUMP --compact test

DROP TABLE t1;

--echo #
--echo # Test 19. Check that table's triggers are dumped correctly in xml.
--echo #

CREATE TABLE t1 (a INT);
SET TIMESTAMP=UNIX_TIMESTAMP('2013-01-31 09:00:00');
CREATE TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1;
CREATE TRIGGER tr2_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=2;
CREATE TRIGGER tr1_bu BEFORE UPDATE ON t1 FOR EACH ROW SET @a:=3;
SET TIMESTAMP=DEFAULT;

# dump tables and triggers
--exec $MYSQL_DUMP --compact --no-create-info --xml test

DROP TABLE t1;

--echo #
--echo # Test 20. Check that the statement CHECK TABLE FOR UPGRADE outputs
--echo #          the warnings for triggers created by a server without support for wl3253.
--echo #

CREATE TABLE t1 (a INT);

let $MYSQLD_DATADIR=`SELECT @@datadir`;
--write_file $MYSQLD_DATADIR/test/t1.TRG
TYPE=TRIGGERS
triggers='CREATE DEFINER=`root`@`localhost` TRIGGER tr1_bi BEFORE INSERT ON t1 FOR EACH ROW SET @a:=1' 'CREATE DEFINER=`root`@`localhost` TRIGGER tr1_ai AFTER INSERT ON t1 FOR EACH ROW SET @a:=2'
sql_modes=1073741824 1073741824
definers='root@localhost' 'root@localhost'
client_cs_names='latin1' 'latin1'
connection_cl_names='latin1_swedish_ci' 'latin1_swedish_ci'
db_cl_names='latin1_swedish_ci' 'latin1_swedish_ci'
EOF

--write_file $MYSQLD_DATADIR/test/tr1_bi.TRN
TYPE=TRIGGERNAME
trigger_table=t1
EOF

--write_file $MYSQLD_DATADIR/test/tr1_ai.TRN
TYPE=TRIGGERNAME
trigger_table=t1
EOF

FLUSH TABLE t1;

CHECK TABLE t1 FOR UPGRADE;

SHOW TRIGGERS;

SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE trigger_schema = 'test';

SHOW CREATE TRIGGER tr1_bi;
SHOW CREATE TRIGGER tr1_ai;

DROP TABLE t1;

SET binlog_format=@binlog_format_saved;

--echo # End of tests.
--echo #