1# changes 2008-02-20 hhunger splitted events.test into events_1 and events_2
2#
3# Can't test with embedded server that doesn't support grants
4-- source include/not_embedded.inc
5
6set sql_mode="";
7--source include/default_charset.inc
8
9--disable_warnings
10drop database if exists events_test;
11--enable_warnings
12create database events_test;
13use events_test;
14
15#
16# mysql.event intact checking end
17#
18
19create event e_26 on schedule at '2037-01-01 00:00:00' disable do set @a = 5;
20select db, name, body, definer, convert_tz(execute_at, 'UTC', 'SYSTEM'), on_completion from mysql.event;
21drop event e_26;
22--error ER_WRONG_VALUE
23create event e_26 on schedule at NULL disable do set @a = 5;
24--error ER_WRONG_VALUE
25create event e_26 on schedule at 'definitely not a datetime' disable do set @a = 5;
26
27set names utf8;
28create event задачка on schedule every 123 minute starts now() ends now() + interval 1 month do select 1;
29drop event задачка;
30
31--echo "DISABLE the scheduler. Testing that it does not work when the variable is 0"
32set global event_scheduler=off;
33select definer, name, db from mysql.event;
34select get_lock("test_lock1", 20);
35create event закачка on schedule every 10 hour do select get_lock("test_lock1", 20);
36--echo "Should return 1 row"
37select definer, name, db from mysql.event;
38
39--echo "Should be only 0 process"
40select /*1*/ user, host, db, command, state, info
41  from information_schema.processlist
42  where (user='event_scheduler')
43  order by info;
44select release_lock("test_lock1");
45drop event закачка;
46--echo "Should have 0 events"
47select count(*) from mysql.event;
48
49#
50#
51#
52--echo "ENABLE the scheduler and get a lock"
53set global event_scheduler=on;
54select get_lock("test_lock2", 20);
55--echo "Create an event which tries to acquire a mutex. The event locks on the mutex"
56create event закачка on schedule every 10 hour do select get_lock("test_lock2", 20);
57
58--echo "Should have only 2 processes: the scheduler and the locked event"
59let $wait_condition= select count(*) = 2 from information_schema.processlist
60  where ( (state like 'User lock%' AND info like 'select get_lock%')
61       OR (command='Daemon' AND user='event_scheduler' AND
62           state = 'Waiting for next activation'));
63--source include/wait_condition.inc
64
65select /*2*/ user, host, db, command, state, info
66  from information_schema.processlist
67  where (info like "select get_lock%" OR user='event_scheduler')
68  order by info;
69--echo "Release the mutex, the event worker should finish."
70select release_lock("test_lock2");
71drop event закачка;
72
73# Wait for get_lock("test_lock2") to complete,
74# to avoid polluting the next test information_schema.processlist
75let $wait_condition= select count(*) = 0 from information_schema.processlist
76  where info='select get_lock("test_lock2", 20)';
77--source include/wait_condition.inc
78
79
80##
81## 1. get a lock
82## 2. create an event
83## 3. sleep so it has time to start
84## 4. should appear in processlist
85## 5. kill the scheduler, it will wait for the child to stop
86## 6. both processes should be there on show processlist
87## 7. release the lock and sleep, both scheduler and child should end
88set global event_scheduler=1;
89select get_lock("test_lock2_1", 20);
90create event закачка21 on schedule every 10 hour do select get_lock("test_lock2_1", 20);
91
92--echo "Should have only 2 processes: the scheduler and the locked event"
93let $wait_condition= select count(*) = 2 from information_schema.processlist
94  where ( (state like 'User lock%' AND info like 'select get_lock%')
95          OR (command='Daemon' AND user='event_scheduler' AND
96              state = 'Waiting for next activation'));
97--source include/wait_condition.inc
98
99select /*3*/ user, host, db, command, state, info
100  from information_schema.processlist
101  where (info like "select get_lock%" OR user='event_scheduler')
102  order by info;
103
104set global event_scheduler=off;
105
106let $wait_condition= select count(*) =1 from information_schema.processlist
107  where (info like "select get_lock%" OR user='event_scheduler');
108--source include/wait_condition.inc
109
110--echo "Should have only our process now:"
111select /*4*/ user, host, db, command, state, info
112  from information_schema.processlist
113  where (info like "select get_lock%" OR user='event_scheduler')
114  order by info;
115select release_lock("test_lock2_1");
116drop event закачка21;
117let $wait_condition=
118  select count(*) = 0 from information_schema.processlist
119  where db='events_test' and command = 'Connect' and user=current_user();
120--source include/wait_condition.inc
121
122####
123# Bug #16410  Events: CREATE EVENT is legal in a CREATE TRIGGER statement
124#
125create table t_16 (s1 int);
126--error ER_EVENT_RECURSION_FORBIDDEN
127create trigger t_16_bi before insert on t_16 for each row create event  e_16 on schedule every 1 second do set @a=5;
128drop table t_16;
129#
130# end of test case
131####
132
133#
134# START: BUG #17453: Creating Event crash the server
135#
136create event white_space
137on schedule every 10 hour
138disable
139do
140select 1;
141select event_schema, event_name, definer, event_definition from information_schema.events where event_name='white_space';
142drop event white_space;
143create event white_space on schedule every 10 hour disable do
144
145select 2;
146select event_schema, event_name, definer, event_definition from information_schema.events where event_name='white_space';
147drop event white_space;
148create event white_space on schedule every 10 hour disable do	select 3;
149select event_schema, event_name, definer, event_definition from information_schema.events where event_name='white_space';
150drop event white_space;
151#
152# END:  BUG #17453: Creating Event crash the server
153#
154
155#
156# Bug#17403 "Events: packets out of order with show create event"
157#
158create event e1 on schedule every 1 year do set @a = 5;
159create table t1 (s1 int);
160--error ER_SP_NO_RETSET
161create trigger t1_ai after insert on t1 for each row show create event e1;
162drop table t1;
163drop event e1;
164
165##set global event_scheduler=1;
166##select get_lock("test_lock3", 20);
167##create event закачка on schedule every 10 hour do select get_lock("test_lock3", 20);
168##select sleep(2);
169##select /*5*/ user, host, db, command, state, info from information_schema.processlist where info is null or info not like '%processlist%' order by info;
170##drop event закачка;
171##select release_lock("test_lock3");
172
173#
174# test with very often occuring event
175# (disabled for now, locks)
176##select get_lock("test_lock4", 20);
177##create event закачка4 on schedule every 1 second do select get_lock("test_lock4", 20);
178##select sleep(3);
179##select /*6*/ user, host, db, command, state, info from information_schema.processlist where info is null or info not like '%processlist%' order by info;
180##drop event закачка4;
181##select release_lock("test_lock4");
182
183##set global event_scheduler=off;
184##select sleep(2);
185##--replace_column 1 # 6 #
186##show processlist;
187##select count(*) from mysql.event;
188
189#
190# Test wrong syntax
191#
192
193--error ER_WRONG_DB_NAME
194SHOW EVENTS FROM aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
195--error ER_WRONG_DB_NAME
196SHOW EVENTS FROM ``;
197
198SHOW EVENTS FROM `events\\test`;
199#
200# A check for events SQL under LOCK TABLES and in pre-locked mode.
201#
202--echo
203--echo LOCK TABLES mode.
204--echo
205#
206# SHOW CREATE EVENT and INFORMATION_SCHEMA.events are available and
207# cause an implicit lock/unlock of mysql.event table, regardless of the
208# currently locked tables.
209#
210create table t1 (a int);
211create event e1 on schedule every 10 hour do select 1;
212#
213lock table t1 read;
214#
215--replace_regex /STARTS '[^']+'/STARTS '#'/
216show create event e1;
217select event_name from information_schema.events;
218--error ER_LOCK_OR_ACTIVE_TRANSACTION
219create event e2 on schedule every 10 hour do select 1;
220--error ER_LOCK_OR_ACTIVE_TRANSACTION
221alter event e2 disable;
222--error ER_LOCK_OR_ACTIVE_TRANSACTION
223alter event e2 rename to e3;
224--error ER_LOCK_OR_ACTIVE_TRANSACTION
225drop event e2;
226--error ER_LOCK_OR_ACTIVE_TRANSACTION
227drop event e1;
228unlock tables;
229#
230lock table t1 write;
231#
232--replace_regex /STARTS '[^']+'/STARTS '#'/
233show create event e1;
234select event_name from information_schema.events;
235--error ER_LOCK_OR_ACTIVE_TRANSACTION
236create event e2 on schedule every 10 hour do select 1;
237--error ER_LOCK_OR_ACTIVE_TRANSACTION
238alter event e2 disable;
239--error ER_LOCK_OR_ACTIVE_TRANSACTION
240alter event e2 rename to e3;
241--error ER_LOCK_OR_ACTIVE_TRANSACTION
242drop event e2;
243--error ER_LOCK_OR_ACTIVE_TRANSACTION
244drop event e1;
245unlock tables;
246#
247lock table t1 read, mysql.event read;
248#
249--replace_regex /STARTS '[^']+'/STARTS '#'/
250show create event e1;
251select event_name from information_schema.events;
252--error ER_LOCK_OR_ACTIVE_TRANSACTION
253create event e2 on schedule every 10 hour do select 1;
254--error ER_LOCK_OR_ACTIVE_TRANSACTION
255alter event e2 disable;
256--error ER_LOCK_OR_ACTIVE_TRANSACTION
257alter event e2 rename to e3;
258--error ER_LOCK_OR_ACTIVE_TRANSACTION
259drop event e2;
260--error ER_LOCK_OR_ACTIVE_TRANSACTION
261drop event e1;
262unlock tables;
263#
264lock table t1 write, mysql.event read;
265#
266--replace_regex /STARTS '[^']+'/STARTS '#'/
267show create event e1;
268select event_name from information_schema.events;
269--error ER_LOCK_OR_ACTIVE_TRANSACTION
270create event e2 on schedule every 10 hour do select 1;
271--error ER_LOCK_OR_ACTIVE_TRANSACTION
272alter event e2 disable;
273--error ER_LOCK_OR_ACTIVE_TRANSACTION
274alter event e2 rename to e3;
275--error ER_LOCK_OR_ACTIVE_TRANSACTION
276drop event e2;
277--error ER_LOCK_OR_ACTIVE_TRANSACTION
278drop event e1;
279unlock tables;
280#
281--error ER_WRONG_LOCK_OF_SYSTEM_TABLE
282lock table t1 read, mysql.event write;
283#
284--error ER_WRONG_LOCK_OF_SYSTEM_TABLE
285lock table t1 write, mysql.event write;
286#
287lock table mysql.event write;
288--replace_regex /STARTS '[^']+'/STARTS '#'/
289show create event e1;
290select event_name from information_schema.events;
291--error ER_LOCK_OR_ACTIVE_TRANSACTION
292create event e2 on schedule every 10 hour do select 1;
293--error ER_LOCK_OR_ACTIVE_TRANSACTION
294alter event e2 disable;
295--error ER_LOCK_OR_ACTIVE_TRANSACTION
296alter event e2 rename to e3;
297--error ER_LOCK_OR_ACTIVE_TRANSACTION
298drop event e3;
299--error ER_LOCK_OR_ACTIVE_TRANSACTION
300drop event e1;
301unlock tables;
302drop event e1;
303--echo Make sure we have left no events
304select event_name from information_schema.events;
305--echo
306--echo Events in sub-statements, events and prelocking
307--echo
308--echo
309create event e1 on schedule every 10 hour do select 1;
310delimiter |;
311--error ER_SP_NO_RETSET
312create function f1() returns int
313begin
314  show create event e1;
315  return 1;
316end|
317--error ER_SP_NO_RETSET
318create trigger trg before insert on t1 for each row
319begin
320  show create event e1;
321end|
322--error ER_SP_NO_RETSET
323create function f1() returns int
324begin
325  select event_name from information_schema.events;
326  return 1;
327end|
328--error ER_SP_NO_RETSET
329create trigger trg before insert on t1 for each row
330begin
331  select event_name from information_schema.events;
332end|
333--error ER_EVENT_RECURSION_FORBIDDEN
334create function f1() returns int
335begin
336  create event e2 on schedule every 10 hour do select 1;
337  return 1;
338end|
339--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
340create function f1() returns int
341begin
342  alter event e1 rename to e2;
343  return 1;
344end|
345--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
346create function f1() returns int
347begin
348  drop event e2;
349  return 1;
350end|
351--echo ----------------------------------------------------------------------
352create trigger trg before insert on t1 for each row
353begin
354  set new.a= f1();
355end|
356create function f1() returns int
357begin
358  call p1();
359  return 0;
360end|
361create procedure p1()
362begin
363  select event_name from information_schema.events;
364end|
365--error ER_SP_NO_RETSET
366insert into t1 (a) values (1)|
367drop procedure p1|
368create procedure p1()
369begin
370  show create event e1;
371end|
372--error ER_SP_NO_RETSET
373insert into t1 (a) values (1)|
374drop procedure p1|
375create procedure p1()
376begin
377  create temporary table tmp select event_name from information_schema.events;
378end|
379--echo expected to work, since we redirect the output into a tmp table
380insert into t1 (a) values (1)|
381select * from tmp|
382drop temporary table tmp|
383drop procedure p1|
384create procedure p1()
385begin
386  alter event e1 rename to e2;
387end|
388--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
389insert into t1 (a) values (1)|
390drop procedure p1|
391create procedure p1()
392begin
393  drop event e1;
394end|
395--error ER_COMMIT_NOT_ALLOWED_IN_SF_OR_TRG
396insert into t1 (a) values (1)|
397drop table t1|
398drop event e1|
399delimiter ;|
400
401#
402# Bug#21432 Database/Table name limited to 64 bytes, not chars, problems with multi-byte
403#
404set names utf8;
405create event имя_события_в_кодировке_утф8_длиной_больше_чем_48 on schedule every 2 year do select 1;
406select EVENT_NAME from information_schema.events
407where event_schema='test';
408drop event имя_события_в_кодировке_утф8_длиной_больше_чем_48;
409--error 1059
410create event
411очень_очень_очень_очень_очень_очень_очень_очень_длинная_строка_66
412on schedule every 2 year do select 1;
413
414#
415# Bug#35981: ALTER EVENT causes the server to change the PRESERVE option.
416#
417
418create event event_35981 on schedule every 6 month on completion preserve
419disable
420do
421  select 1;
422
423echo The following SELECTs should all give 1;
424
425# show current ON_COMPLETION
426select  count(*) from information_schema.events
427where   event_schema = database() and event_name = 'event_35981' and
428        on_completion = 'PRESERVE';
429
430# show ON_COMPLETION remains "PRESERVE" when not given in ALTER EVENT
431alter   event event_35981 enable;
432select  count(*) from information_schema.events
433where   event_schema = database() and event_name = 'event_35981' and
434        on_completion = 'PRESERVE';
435
436# show we can change ON_COMPLETION
437alter   event event_35981 on completion not preserve;
438select  count(*) from information_schema.events
439where   event_schema = database() and event_name = 'event_35981' and
440        on_completion = 'NOT PRESERVE';
441
442# show ON_COMPLETION remains "NOT PRESERVE" when not given in ALTER EVENT
443alter   event event_35981 disable;
444select  count(*) from information_schema.events
445where   event_schema = database() and event_name = 'event_35981' and
446        on_completion = 'NOT PRESERVE';
447
448# show we can change ON_COMPLETION
449alter   event event_35981 on completion preserve;
450select  count(*) from information_schema.events
451where   event_schema = database() and event_name = 'event_35981' and
452        on_completion = 'PRESERVE';
453
454
455drop event event_35981;
456
457create event event_35981 on schedule every 6 month disable
458do
459  select 1;
460
461# show that the defaults for CREATE EVENT are still correct (NOT PRESERVE)
462select  count(*) from information_schema.events
463where   event_schema = database() and event_name = 'event_35981' and
464        on_completion = 'NOT PRESERVE';
465
466drop event event_35981;
467
468
469# show that backdating doesn't break
470
471create event event_35981 on schedule every 1 hour starts current_timestamp
472  on completion not preserve
473do
474  select 1;
475
476# should fail thanks to above's NOT PRESERVE
477--error ER_EVENT_CANNOT_ALTER_IN_THE_PAST
478alter event event_35981 on schedule every 1 hour starts '1999-01-01 00:00:00'
479  ends '1999-01-02 00:00:00';
480
481drop event event_35981;
482
483create event event_35981 on schedule every 1 hour starts current_timestamp
484  on completion not preserve
485do
486  select 1;
487
488# succeed with warning
489alter event event_35981 on schedule every 1 hour starts '1999-01-01 00:00:00'
490  ends '1999-01-02 00:00:00' on completion preserve;
491
492drop event event_35981;
493
494
495
496create event event_35981 on schedule every 1 hour starts current_timestamp
497  on completion preserve
498do
499  select 1;
500
501# this should succeed thanks to above PRESERVE! give a warning though.
502alter event event_35981 on schedule every 1 hour starts '1999-01-01 00:00:00'
503  ends '1999-01-02 00:00:00';
504
505# this should fail, as the event would have passed already
506--error ER_EVENT_CANNOT_ALTER_IN_THE_PAST
507alter event event_35981 on schedule every 1 hour starts '1999-01-01 00:00:00'
508  ends '1999-01-02 00:00:00' on completion not preserve;
509
510# should succeed giving a warning
511alter event event_35981 on schedule every 1 hour starts '1999-01-01 00:00:00'
512  ends '1999-01-02 00:00:00' on completion preserve;
513
514drop event event_35981;
515
516#
517# End of tests
518#
519
520let $wait_condition=
521  select count(*) = 0 from information_schema.processlist
522  where db='events_test' and command = 'Connect' and user=current_user();
523--source include/wait_condition.inc
524
525drop database events_test;
526