|
| 1 | +# |
| 2 | +# MDEV-30645: CREATE TRIGGER FOR { STARTUP | SHUTDOWN } |
| 3 | +# |
| 4 | +# Test 1: Check that AFTER STARTUP trigger and BEFORE SHUTDOWN trigger |
| 5 | +# are fired at right time |
| 6 | +CREATE TABLE t1 (a VARCHAR(100)); |
| 7 | +CREATE TRIGGER IF NOT EXISTS trg1 AFTER STARTUP INSERT INTO t1 VALUES('Startup'); |
| 8 | +# BEFORE SHUTDOWN should be in action just after it has been created. |
| 9 | +# So, after restarting the server the record about server shutdown should |
| 10 | +# be inserted into the table t1. |
| 11 | +CREATE TRIGGER IF NOT EXISTS trg2 BEFORE SHUTDOWN INSERT INTO t1 VALUES('Shutdown'); |
| 12 | +# restart |
| 13 | +SELECT * FROM t1; |
| 14 | +a |
| 15 | +Shutdown |
| 16 | +Startup |
| 17 | +# Clean up |
| 18 | +DROP TABLE t1; |
| 19 | +DROP TRIGGER trg1; |
| 20 | +DROP TRIGGER trg2; |
| 21 | +# |
| 22 | +# Test 2: Check that ON STARTUP/ON SHUTDOWN can't be created |
| 23 | +# w/o the SUPER privilege |
| 24 | +# |
| 25 | +# Check that ON STARTUP/ON SHUTDOWN can be created w/o the SUPER privilege |
| 26 | +CREATE USER u1; |
| 27 | +GRANT SELECT ON test.* TO u1; |
| 28 | +connect con1, localhost, u1, , test; |
| 29 | +# The following CREATE TRIGGER AFTER STARTUP statement should fail with |
| 30 | +# the error ER_SPECIFIC_ACCESS_DENIED_ERROR since the user `u1` doesn't |
| 31 | +# have the SUPER privilege |
| 32 | +CREATE TRIGGER IF NOT EXISTS trg1 AFTER STARTUP SET @a=1; |
| 33 | +ERROR 42000: Access denied; you need (at least one of) the SUPER privilege(s) for this operation |
| 34 | +# The following CREATE TRIGGER BEFORE SHUTDOWN statement should fail with |
| 35 | +# the error ER_SPECIFIC_ACCESS_DENIED_ERROR since the user `u1` doesn't |
| 36 | +# have the SUPER privilege |
| 37 | +CREATE TRIGGER IF NOT EXISTS trg1 BEFORE SHUTDOWN SET @b=1; |
| 38 | +ERROR 42000: Access denied; you need (at least one of) the SUPER privilege(s) for this operation |
| 39 | +connection default; |
| 40 | +disconnect con1; |
| 41 | +DROP USER u1; |
| 42 | +# |
| 43 | +# Test 3: Check that a user with the explicitly granted the SUPER privilege |
| 44 | +# can create a system trigger on STARTUP/on SHUTDOWN |
| 45 | +# |
| 46 | +CREATE USER u1; |
| 47 | +GRANT SELECT ON test.* TO u1; |
| 48 | +GRANT SUPER ON *.* TO u1; |
| 49 | +connect con1, localhost, u1, , test; |
| 50 | +# The following CREATE TRIGGER AFTER STARTUP statement and |
| 51 | +# CREATE TRIGGER BEFORE SHUTDOWN statement should succeed |
| 52 | +# since the SUPER privilege is granted to the user `u1` |
| 53 | +CREATE TRIGGER IF NOT EXISTS trg1 AFTER STARTUP INSERT INTO t1 VALUES('Startup'); |
| 54 | +CREATE TRIGGER IF NOT EXISTS trg2 BEFORE SHUTDOWN INSERT INTO t1 VALUES('Shutdown'); |
| 55 | +connection default; |
| 56 | +disconnect con1; |
| 57 | +# Clean up |
| 58 | +DROP TRIGGER trg1; |
| 59 | +DROP TRIGGER trg2; |
| 60 | +DROP USER u1; |
| 61 | +# |
| 62 | +# Test 4: Check that it is not possible to create a System trigger |
| 63 | +# and a DML trigger with the same name |
| 64 | +# |
| 65 | +CREATE TABLE t1 (a INT); |
| 66 | +# First check that DML trigger can 't be created with the same name |
| 67 | +# as the existing system trigger |
| 68 | +CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW SET @a=1; |
| 69 | +CREATE TRIGGER trg1 AFTER STARTUP SET @b=1; |
| 70 | +ERROR HY000: Trigger 'test.trg1' already exists |
| 71 | +DROP TRIGGER trg1; |
| 72 | +# And then check in reverse order: a system trigger can't be created |
| 73 | +# on presence of DML trigger with the same name |
| 74 | +CREATE TRIGGER trg1 AFTER STARTUP SET @b=1; |
| 75 | +CREATE TRIGGER trg1 BEFORE INSERT ON t1 FOR EACH ROW SET @a=1; |
| 76 | +ERROR HY000: Trigger 'test.trg1' already exists |
| 77 | +# Clean up |
| 78 | +DROP TRIGGER trg1; |
| 79 | +DROP TABLE t1; |
| 80 | +# |
| 81 | +# Test 5: Check that multiple triggers can be created on the same |
| 82 | +# system event |
| 83 | +# |
| 84 | +CREATE TABLE t1 (a VARCHAR(100)); |
| 85 | +CREATE TRIGGER IF NOT EXISTS trg1_ast AFTER STARTUP INSERT INTO t1 VALUES('Startup: first action'); |
| 86 | +CREATE TRIGGER IF NOT EXISTS trg2_ast AFTER STARTUP INSERT INTO t1 VALUES('Startup: second action'); |
| 87 | +# BEFORE SHUTDOWN should be in action just after it has been created. |
| 88 | +# So, after restarting the server the record about server shutdown should |
| 89 | +# be inserted into the table t1. |
| 90 | +CREATE TRIGGER IF NOT EXISTS trg3_bshd BEFORE SHUTDOWN INSERT INTO t1 VALUES('Shutdown: first action'); |
| 91 | +CREATE TRIGGER IF NOT EXISTS trg4_bshd BEFORE SHUTDOWN INSERT INTO t1 VALUES('Shutdown: second action'); |
| 92 | +# restart |
| 93 | +SELECT * FROM t1; |
| 94 | +a |
| 95 | +Shutdown: first action |
| 96 | +Shutdown: second action |
| 97 | +Startup: first action |
| 98 | +Startup: second action |
| 99 | +# Clean up |
| 100 | +DROP TABLE t1; |
| 101 | +DROP TRIGGER trg1_ast; |
| 102 | +DROP TRIGGER trg2_ast; |
| 103 | +DROP TRIGGER trg3_bshd; |
| 104 | +DROP TRIGGER trg4_bshd; |
| 105 | +# |
| 106 | +# Test 6: SHOW TRIGGERS on a system trigger |
| 107 | +# |
| 108 | +CREATE TRIGGER trg1 AFTER STARTUP SET @b=1; |
| 109 | +SHOW TRIGGERS; |
| 110 | +Trigger Event Table Statement Timing Created sql_mode Definer character_set_client collation_connection Database Collation |
| 111 | +trg1 STARTUP SET @b=1 AFTER # STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION root@localhost latin1 latin1_swedish_ci utf8mb4_uca1400_ai_ci |
| 112 | +# Clean up |
| 113 | +DROP TRIGGER trg1; |
| 114 | +# |
| 115 | +# Test 7: SHOW CREATE TRIGGER on a system trigger |
| 116 | +# |
| 117 | +CREATE TRIGGER trg1 AFTER STARTUP SET @b=1; |
| 118 | +SHOW CREATE TRIGGER trg1; |
| 119 | +Trigger sql_mode SQL Original Statement character_set_client collation_connection Database Collation Created |
| 120 | +trg1 STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION CREATE DEFINER=`root`@`localhost` TRIGGER trg1 AFTER STARTUP SET @b=1 latin1 latin1_swedish_ci utf8mb4_uca1400_ai_ci # |
| 121 | +# Clean up |
| 122 | +DROP TRIGGER trg1; |
| 123 | +# |
| 124 | +# Test 8: Check that triggers and events have the same namespace |
| 125 | +# |
| 126 | +# Test 8.1 Check that attempt to create a trigger with the same name |
| 127 | +# as existent event result in error |
| 128 | +CREATE EVENT ev1 ON SCHEDULE EVERY 1 YEAR DO SET @aaa=1; |
| 129 | +Warnings: |
| 130 | +Warning 1105 Event scheduler is switched off, use SET GLOBAL event_scheduler=ON to enable it. |
| 131 | +CREATE TRIGGER ev1 AFTER STARTUP SET @bbb=1; |
| 132 | +ERROR HY000: Event with the same name 'ev1' already exists |
| 133 | +# Clean up |
| 134 | +DROP EVENT ev1; |
| 135 | +# Test 8.2 Check that attempt to create an event with the same name as |
| 136 | +# as existen trigger result in error |
| 137 | +CREATE TRIGGER trg1 AFTER STARTUP SET @bbb=1; |
| 138 | +CREATE EVENT trg1 ON SCHEDULE EVERY 1 YEAR DO SET @aaa=1; |
| 139 | +ERROR HY000: Trigger with the same name 'trg1' already exists |
| 140 | +# Clean up |
| 141 | +DROP TRIGGER trg1; |
0 commit comments