1# Tests which involve triggers and transactions
2# (or just InnoDB storage engine)
3--source include/have_innodb.inc
4
5# Save the initial number of concurrent sessions
6--source include/count_sessions.inc
7
8--disable_warnings
9drop table if exists t1;
10--enable_warnings
11
12# Test for bug #18153 "OPTIMIZE/ALTER on transactional tables corrupt
13#                      triggers/triggers are lost".
14
15create table t1 (a varchar(16), b int) engine=innodb;
16delimiter |;
17create trigger t1_bi before insert on t1 for each row
18begin
19 set new.a := upper(new.a);
20 set new.b := new.b + 3;
21end|
22delimiter ;|
23select trigger_schema, trigger_name, event_object_schema,
24       event_object_table, action_statement from information_schema.triggers
25       where event_object_schema = 'test' and event_object_table = 't1';
26insert into t1 values ('The Lion', 10);
27select * from t1;
28optimize table t1;
29select trigger_schema, trigger_name, event_object_schema,
30       event_object_table, action_statement from information_schema.triggers
31       where event_object_schema = 'test' and event_object_table = 't1';
32insert into t1 values ('The Unicorn', 20);
33select * from t1;
34alter table t1 add column c int default 0;
35select trigger_schema, trigger_name, event_object_schema,
36       event_object_table, action_statement from information_schema.triggers
37       where event_object_schema = 'test' and event_object_table = 't1';
38insert into t1 values ('Alice', 30, 1);
39select * from t1;
40# Special tricky cases allowed by ALTER TABLE ... RENAME
41alter table t1 rename to t1;
42select trigger_schema, trigger_name, event_object_schema,
43       event_object_table, action_statement from information_schema.triggers
44       where event_object_schema = 'test' and event_object_table = 't1';
45insert into t1 values ('The Crown', 40, 1);
46select * from t1;
47alter table t1 rename to t1, add column d int default 0;
48select trigger_schema, trigger_name, event_object_schema,
49       event_object_table, action_statement from information_schema.triggers
50       where event_object_schema = 'test' and event_object_table = 't1';
51insert into t1 values ('The Pie', 50, 1, 1);
52select * from t1;
53drop table t1;
54
55--echo
56--echo Bug#26141 mixing table types in trigger causes full
57--echo table lock on innodb table
58--echo
59--echo Ensure we do not open and lock tables for the triggers we do not
60--echo fire.
61--echo
62--disable_warnings
63drop table if exists t1, t2, t3;
64drop trigger if exists trg_bug26141_au;
65drop trigger if exists trg_bug26141_ai;
66--enable_warnings
67# Note, for InnoDB to allow concurrent UPDATE and INSERT the
68# table must have a unique key.
69create table t1 (c int primary key) engine=innodb;
70create table t2 (c int) engine=myisam;
71create table t3 (c int) engine=myisam;
72insert into t1 (c) values (1);
73delimiter |;
74
75create trigger trg_bug26141_ai after insert on t1
76for each row
77begin
78  insert into t2 (c) values (1);
79# We need the 'sync' lock to synchronously wait in connection 2 till
80# the moment when the trigger acquired all the locks.
81  select release_lock("lock_bug26141_sync") into @a;
82# 1000 is time in seconds of lock wait timeout -- this is a way
83# to cause a manageable sleep up to 1000 seconds
84  select get_lock("lock_bug26141_wait", 1000) into @a;
85end|
86
87create trigger trg_bug26141_au after update on t1
88for each row
89begin
90  insert into t3 (c) values (1);
91end|
92delimiter ;|
93
94# Establish an alternative connection.
95--connect (connection_aux,localhost,root,,test,,)
96--connect (connection_update,localhost,root,,test,,)
97
98connection connection_aux;
99# Lock the wait lock, it must not be locked, so specify zero timeout.
100select get_lock("lock_bug26141_wait", 0);
101
102#
103connection default;
104#
105# Run the trigger synchronously
106#
107select get_lock("lock_bug26141_sync", /* must not be priorly locked */ 0);
108# Will acquire the table level locks, perform the insert into t2,
109# release the sync lock and block on the wait lock.
110send insert into t1 (c) values (2);
111
112connection connection_update;
113# Wait for the trigger to acquire its locks and unlock the sync lock.
114select get_lock("lock_bug26141_sync", 1000);
115#
116# This must continue: after the fix for the bug, we do not
117# open tables for t2, and with c=4 innobase allows the update
118# to run concurrently with insert.
119update t1 set c=3 where c=1;
120select release_lock("lock_bug26141_sync");
121connection connection_aux;
122select release_lock("lock_bug26141_wait");
123connection default;
124reap;
125select * from t1;
126select * from t2;
127select * from t3;
128
129# Drops the trigger as well.
130drop table t1, t2, t3;
131disconnect connection_update;
132disconnect connection_aux;
133
134#
135# Bug#34643: TRUNCATE crash if trigger and foreign key.
136#
137
138--disable_warnings
139DROP TABLE IF EXISTS t1;
140DROP TABLE IF EXISTS t2;
141--enable_warnings
142
143CREATE TABLE t1(a INT PRIMARY KEY) ENGINE=innodb;
144CREATE TABLE t2(b INT, FOREIGN KEY(b) REFERENCES t1(a)) ENGINE=innodb;
145
146INSERT INTO t1 VALUES (1);
147
148CREATE TRIGGER t1_bd BEFORE DELETE ON t1 FOR EACH ROW SET @a = 1;
149CREATE TRIGGER t1_ad AFTER DELETE ON t1 FOR EACH ROW SET @b = 1;
150
151SET @a = 0;
152SET @b = 0;
153
154--error ER_TRUNCATE_ILLEGAL_FK
155TRUNCATE t1;
156
157SELECT @a, @b;
158
159DELETE FROM t1;
160
161SELECT @a, @b;
162
163INSERT INTO t1 VALUES (1);
164
165DELETE FROM t1;
166
167SELECT @a, @b;
168
169DROP TABLE t2, t1;
170
171
172--echo End of 5.0 tests
173
174--echo BUG#31612
175--echo Trigger fired multiple times leads to gaps in auto_increment sequence
176create table t1 (a int, val char(1)) engine=InnoDB;
177create table t2 (b int auto_increment primary key,
178 val char(1)) engine=InnoDB;
179create trigger t1_after_insert after
180 insert on t1 for each row insert into t2 set val=NEW.val;
181insert into t1 values ( 123, 'a'), ( 123, 'b'), ( 123, 'c'),
182 (123, 'd'), (123, 'e'), (123, 'f'), (123, 'g');
183insert into t1 values ( 654, 'a'), ( 654, 'b'), ( 654, 'c'),
184 (654, 'd'), (654, 'e'), (654, 'f'), (654, 'g');
185select * from t2 order by b;
186drop trigger t1_after_insert;
187drop table t1,t2;
188
189--echo #
190--echo #Bug#19683834 SOME INNODB ERRORS CAUSES STORED FUNCTION
191--echo #             AND TRIGGER HANDLERS TO BE IGNORED
192
193--echo #Code fixed in Bug#16041903
194
195CREATE TABLE t1 (id int unsigned PRIMARY KEY, val int DEFAULT 0)
196ENGINE=InnoDB;
197INSERT INTO t1 (id) VALUES (1), (2);
198
199CREATE TABLE t2 (id int PRIMARY KEY);
200CREATE TABLE t3 LIKE t2;
201
202# Trigger with continue handler for ER_DUP_ENTRY(1062)
203DELIMITER //;
204CREATE TRIGGER bef_insert BEFORE INSERT ON t2 FOR EACH ROW
205BEGIN
206   DECLARE CONTINUE HANDLER FOR 1062 BEGIN END;
207   INSERT INTO t3 (id) VALUES (NEW.id);
208   INSERT INTO t3 (id) VALUES (NEW.id);
209END//
210DELIMITER ;//
211
212# Transaction 1: Grab locks on t1
213START TRANSACTION;
214UPDATE t1 SET val = val + 1;
215
216# Transaction 2:
217--connect (con2,localhost,root,,test,,)
218SET SESSION innodb_lock_wait_timeout = 2;
219# Trigger lock timeout (1205)
220--error ER_LOCK_WAIT_TIMEOUT
221UPDATE t1 SET val = val + 1;
222
223# This insert should go through, as the continue handler should
224# handle ER_DUP_ENTRY, even after ER_LOCK_WAIT_TIMEOUT (Bug#16041903)
225INSERT INTO t2 (id) VALUES (1);
226
227# Cleanup
228disconnect con2;
229--source include/wait_until_disconnected.inc
230connection default;
231
232DROP TABLE t3, t2, t1;
233
234# Wait till we reached the initial number of concurrent sessions
235--source include/wait_until_count_sessions.inc
236
237--echo #
238--echo # MDEV-25738 Assertion `ticket->m_duration == MDL_EXPLICIT' failed in
239--echo # void MDL_context::release_lock(MDL_ticket*)
240--echo #
241
242CREATE TABLE t1 (id int(11)) ENGINE=InnoDB;
243LOCK TABLES t1 WRITE;
244SET max_statement_time= 0.001;
245--error 0,1969,2013
246--disable_warnings
247CREATE TRIGGER tr16 AFTER UPDATE ON t1 FOR EACH ROW INSERT INTO t1 VALUES (1);
248--enable_warnings
249SET max_statement_time= default;
250--disable_warnings
251DROP TRIGGER IF EXISTS trg16;
252--enable_warnings
253DROP TABLE t1;
254
255--echo #
256--echo # End of 10.5 tests
257--echo #
258