|
| 1 | +// Copyright 2023 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package queries |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/dolthub/go-mysql-server/sql" |
| 19 | + "github.com/dolthub/go-mysql-server/sql/types" |
| 20 | +) |
| 21 | + |
| 22 | +// EventTests tests any EVENT related behavior. Events have at least one timestamp value (AT/STARTS/ENDS), so to test |
| 23 | +// SHOW EVENTS and SHOW CREATE EVENTS statements, some tests have those timestamps defined in 2037. |
| 24 | +var EventTests = []ScriptTest{ |
| 25 | + { |
| 26 | + Name: "EVENTs with ON SCHEDULE EVERY", |
| 27 | + SetUpScript: []string{ |
| 28 | + "USE mydb;", |
| 29 | + "CREATE TABLE totals (num int);", |
| 30 | + }, |
| 31 | + Assertions: []ScriptTestAssertion{ |
| 32 | + { |
| 33 | + Query: "CREATE EVENT event_with_starts_and_ends ON SCHEDULE EVERY '1:2' MINUTE_SECOND STARTS CURRENT_TIMESTAMP + INTERVAL 1 HOUR ENDS CURRENT_TIMESTAMP + INTERVAL 1 DAY DISABLE DO INSERT INTO totals VALUES (1);", |
| 34 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 35 | + }, |
| 36 | + { |
| 37 | + Query: "CREATE EVENT event_with_starts_only ON SCHEDULE EVERY '1:2' MINUTE_SECOND STARTS CURRENT_TIMESTAMP + INTERVAL 1 HOUR DISABLE DO INSERT INTO totals VALUES (1);", |
| 38 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 39 | + }, |
| 40 | + { |
| 41 | + Query: "CREATE EVENT event_with_ends_only ON SCHEDULE EVERY '1:2' MINUTE_SECOND ENDS CURRENT_TIMESTAMP + INTERVAL 1 DAY DISABLE DO INSERT INTO totals VALUES (1);", |
| 42 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 43 | + }, |
| 44 | + { |
| 45 | + Query: "CREATE EVENT event_without_starts_and_ends ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (1);", |
| 46 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 47 | + }, |
| 48 | + { |
| 49 | + Query: "CREATE EVENT event2 ON SCHEDULE EVERY 3 DAY STARTS '2037-10-16 23:59:00' + INTERVAL 2 DAY ENDS '2037-11-16 23:59:00' + INTERVAL 1 MONTH DO INSERT INTO totals VALUES (1000);", |
| 50 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 51 | + }, |
| 52 | + { |
| 53 | + Query: "SHOW EVENTS LIKE 'event2';", |
| 54 | + Expected: []sql.Row{{"mydb", "event2", "`root`@`localhost`", "SYSTEM", "RECURRING", nil, "3", "DAY", "2037-10-18 23:59:00", "2037-12-16 23:59:00", "ENABLED", 0, "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}}, |
| 55 | + }, |
| 56 | + { |
| 57 | + Query: "SHOW CREATE EVENT event2;", |
| 58 | + Expected: []sql.Row{ |
| 59 | + {"event2", "", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `event2` ON SCHEDULE EVERY 3 DAY STARTS '2037-10-18 23:59:00' ENDS '2037-12-16 23:59:00' ON COMPLETION NOT PRESERVE ENABLE DO INSERT INTO totals VALUES (1000)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + Name: "EVENTs with ON SCHEDULE AT", |
| 66 | + SetUpScript: []string{ |
| 67 | + "USE mydb;", |
| 68 | + "CREATE TABLE totals (num int);", |
| 69 | + }, |
| 70 | + Assertions: []ScriptTestAssertion{ |
| 71 | + { |
| 72 | + Query: "CREATE EVENT event2 ON SCHEDULE AT '2038-01-16 23:59:00 +0000 UTC' + INTERVAL 1 DAY ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (100);", |
| 73 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 74 | + }, |
| 75 | + { |
| 76 | + Query: "SHOW EVENTS;", |
| 77 | + Expected: []sql.Row{{"mydb", "event2", "`root`@`localhost`", "SYSTEM", "ONE TIME", "2038-01-17 23:59:00", nil, nil, nil, nil, "DISABLED", 0, "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}}, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + Name: "DROP EVENTs", |
| 83 | + SetUpScript: []string{ |
| 84 | + "USE mydb;", |
| 85 | + "CREATE TABLE totals (num int);", |
| 86 | + "CREATE EVENT event1 ON SCHEDULE AT '2038-01-15 23:59:00 +0000 UTC' + INTERVAL 2 DAY DISABLE DO INSERT INTO totals VALUES (100);", |
| 87 | + }, |
| 88 | + Assertions: []ScriptTestAssertion{ |
| 89 | + { |
| 90 | + Query: "SHOW EVENTS;", |
| 91 | + Expected: []sql.Row{{"mydb", "event1", "`root`@`localhost`", "SYSTEM", "ONE TIME", "2038-01-17 23:59:00", nil, nil, nil, nil, "DISABLED", 0, "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}}, |
| 92 | + }, |
| 93 | + { |
| 94 | + Query: "DROP EVENT event1", |
| 95 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 96 | + }, |
| 97 | + { |
| 98 | + Query: "SHOW EVENTS;", |
| 99 | + Expected: []sql.Row{}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + Name: "invalid events actions", |
| 105 | + SetUpScript: []string{ |
| 106 | + "USE mydb;", |
| 107 | + "CREATE TABLE totals (num int);", |
| 108 | + "CREATE EVENT my_event1 ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (1);", |
| 109 | + }, |
| 110 | + Assertions: []ScriptTestAssertion{ |
| 111 | + { |
| 112 | + Query: "CREATE EVENT my_event1 ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (1);", |
| 113 | + ExpectedErr: sql.ErrEventAlreadyExists, |
| 114 | + }, |
| 115 | + { |
| 116 | + Query: "CREATE EVENT IF NOT EXISTS my_event1 ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (1);", |
| 117 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 118 | + ExpectedWarningsCount: 1, |
| 119 | + }, |
| 120 | + { |
| 121 | + Query: "SHOW WARNINGS;", |
| 122 | + Expected: []sql.Row{{"Note", 1537, "Event 'my_event1' already exists"}}, |
| 123 | + }, |
| 124 | + { |
| 125 | + Query: "CREATE EVENT ends_before_starts ON SCHEDULE EVERY 1 MINUTE ENDS '2006-02-10 23:59:00' DO INSERT INTO totals VALUES (1);", |
| 126 | + ExpectedErrStr: "ENDS is either invalid or before STARTS", |
| 127 | + }, |
| 128 | + { |
| 129 | + Query: "SHOW CREATE EVENT non_existent_event;", |
| 130 | + ExpectedErr: sql.ErrUnknownEvent, |
| 131 | + }, |
| 132 | + { |
| 133 | + Query: "DROP EVENT non_existent_event", |
| 134 | + ExpectedErr: sql.ErrEventDoesNotExist, |
| 135 | + }, |
| 136 | + { |
| 137 | + Query: "DROP EVENT IF EXISTS non_existent_event", |
| 138 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 139 | + ExpectedWarningsCount: 1, |
| 140 | + }, |
| 141 | + { |
| 142 | + Query: "SHOW WARNINGS;", |
| 143 | + Expected: []sql.Row{{"Note", 1305, "Event non_existent_event does not exist"}}, |
| 144 | + }, |
| 145 | + { |
| 146 | + Query: "CREATE EVENT past_event1 ON SCHEDULE AT '2006-02-10 23:59:00' DISABLE DO INSERT INTO totals VALUES (100);", |
| 147 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 148 | + ExpectedWarningsCount: 1, |
| 149 | + }, |
| 150 | + { |
| 151 | + Query: "SHOW WARNINGS;", |
| 152 | + Expected: []sql.Row{{"Note", 1588, "Event execution time is in the past and ON COMPLETION NOT PRESERVE is set. The event was dropped immediately after creation."}}, |
| 153 | + }, |
| 154 | + { |
| 155 | + Query: "SHOW EVENTS LIKE 'past_event1';", |
| 156 | + Expected: []sql.Row{}, |
| 157 | + }, |
| 158 | + { |
| 159 | + Query: "CREATE EVENT past_event2 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DO INSERT INTO totals VALUES (100);", |
| 160 | + Expected: []sql.Row{{types.OkResult{}}}, |
| 161 | + ExpectedWarningsCount: 1, |
| 162 | + }, |
| 163 | + { |
| 164 | + Query: "SHOW WARNINGS;", |
| 165 | + Expected: []sql.Row{{"Note", 1544, "Event execution time is in the past. Event has been disabled"}}, |
| 166 | + }, |
| 167 | + { |
| 168 | + Query: "SHOW EVENTS LIKE 'past_event2';", |
| 169 | + Expected: []sql.Row{{"mydb", "past_event2", "`root`@`localhost`", "SYSTEM", "ONE TIME", "2006-02-10 23:59:00", nil, nil, nil, nil, "DISABLED", 0, "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}}, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | +} |
0 commit comments