github.com/dolthub/go-mysql-server@v0.18.0/enginetest/queries/event_queries.go (about)

     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", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "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 '38-01-16 12:2:3.' + 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 12:02:03", 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' + 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 mY_EVENt1 ON SCHEDULE EVERY '1:2' HOUR_MINUTE DISABLE DO INSERT INTO totals VALUES (2);",
   117  				ExpectedErr: sql.ErrEventAlreadyExists,
   118  			},
   119  			{
   120  				Query:                           "CREATE EVENT IF NOT EXISTS my_event1 ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (1);",
   121  				Expected:                        []sql.Row{{types.OkResult{}}},
   122  				ExpectedWarning:                 1537,
   123  				ExpectedWarningsCount:           1,
   124  				ExpectedWarningMessageSubstring: "Event 'my_event1' already exists",
   125  			},
   126  			{
   127  				Query:          "CREATE EVENT ends_before_starts ON SCHEDULE EVERY 1 MINUTE ENDS '2006-02-10 23:59:00' DO INSERT INTO totals VALUES (1);",
   128  				ExpectedErrStr: "ENDS is either invalid or before STARTS",
   129  			},
   130  			{
   131  				Query:       "SHOW CREATE EVENT non_existent_event;",
   132  				ExpectedErr: sql.ErrUnknownEvent,
   133  			},
   134  			{
   135  				Query:       "DROP EVENT non_existent_event",
   136  				ExpectedErr: sql.ErrEventDoesNotExist,
   137  			},
   138  			{
   139  				Query:                           "DROP EVENT IF EXISTS non_existent_event",
   140  				Expected:                        []sql.Row{{types.OkResult{}}},
   141  				ExpectedWarning:                 1305,
   142  				ExpectedWarningsCount:           1,
   143  				ExpectedWarningMessageSubstring: "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  				ExpectedWarning:                 1588,
   149  				ExpectedWarningMessageSubstring: "Event execution time is in the past and ON COMPLETION NOT PRESERVE is set. The event was dropped immediately after creation.",
   150  				ExpectedWarningsCount:           1,
   151  			},
   152  			{
   153  				Query:    "SHOW EVENTS LIKE 'past_event1';",
   154  				Expected: []sql.Row{},
   155  			},
   156  			{
   157  				Query:                           "CREATE EVENT past_event2 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DO INSERT INTO totals VALUES (100);",
   158  				Expected:                        []sql.Row{{types.OkResult{}}},
   159  				ExpectedWarning:                 1544,
   160  				ExpectedWarningMessageSubstring: "Event execution time is in the past. Event has been disabled",
   161  				ExpectedWarningsCount:           1,
   162  			},
   163  			{
   164  				Query:    "SHOW EVENTS LIKE 'past_event2';",
   165  				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"}},
   166  			},
   167  			{
   168  				Query:                           "CREATE EVENT myevent ON SCHEDULE AT CURRENT_TIMESTAMP ON COMPLETION PRESERVE DISABLE ON SLAVE DO INSERT INTO totals VALUES (100);",
   169  				Expected:                        []sql.Row{{types.OkResult{}}},
   170  				ExpectedWarning:                 1235,
   171  				ExpectedWarningMessageSubstring: "DISABLE ON SLAVE status is not supported yet, used DISABLE status instead",
   172  				ExpectedWarningsCount:           1,
   173  			},
   174  		},
   175  	},
   176  	{
   177  		Name: "invalid ALTER EVENT actions",
   178  		SetUpScript: []string{
   179  			"USE mydb;",
   180  			"CREATE TABLE totals (num int);",
   181  			"CREATE EVENT my_event1 ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (1);",
   182  		},
   183  		Assertions: []ScriptTestAssertion{
   184  			{
   185  				Query:          "ALTER EVENT my_event1 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION NOT PRESERVE;",
   186  				ExpectedErrStr: "Event execution time is in the past and ON COMPLETION NOT PRESERVE is set. The event was not changed. Specify a time in the future.",
   187  			},
   188  			{
   189  				Query:                           "ALTER EVENT my_event1 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE;",
   190  				Expected:                        []sql.Row{{types.OkResult{}}},
   191  				ExpectedWarning:                 1544,
   192  				ExpectedWarningsCount:           1,
   193  				ExpectedWarningMessageSubstring: "Event execution time is in the past. Event has been disabled",
   194  			},
   195  			{
   196  				Query:    "CREATE EVENT my_event2 ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (2);",
   197  				Expected: []sql.Row{{types.OkResult{}}},
   198  			},
   199  			{
   200  				Query:       "ALTER EVENT my_event2 RENAME TO my_event1;",
   201  				ExpectedErr: sql.ErrEventAlreadyExists,
   202  			},
   203  		},
   204  	},
   205  	{
   206  		Name: "enabling expired event stays disabled",
   207  		SetUpScript: []string{
   208  			"USE mydb;",
   209  			"CREATE TABLE totals (num int);",
   210  			"CREATE EVENT my_event1 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (1);",
   211  		},
   212  		Assertions: []ScriptTestAssertion{
   213  			{
   214  				Query:    "ALTER EVENT my_event1 ENABLE;",
   215  				Expected: []sql.Row{{types.OkResult{}}},
   216  			},
   217  			{
   218  				Query:    "SHOW CREATE EVENT my_event1;",
   219  				Expected: []sql.Row{{"my_event1", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `my_event1` ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (1)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   220  			},
   221  		},
   222  	},
   223  	{
   224  		Name: "enabling expired event with ON COMPLETION NOT PRESERVE drops the event",
   225  		SetUpScript: []string{
   226  			"USE mydb;",
   227  			"CREATE TABLE totals (num int);",
   228  			"CREATE EVENT my_event1 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (1);",
   229  		},
   230  		Assertions: []ScriptTestAssertion{
   231  			{
   232  				Query:    "ALTER EVENT my_event1 ON COMPLETION NOT PRESERVE;",
   233  				Expected: []sql.Row{{types.OkResult{}}},
   234  			},
   235  			{
   236  				Query:    "SHOW CREATE EVENT my_event1;",
   237  				Expected: []sql.Row{{"my_event1", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `my_event1` ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION NOT PRESERVE DISABLE DO INSERT INTO totals VALUES (1)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   238  			},
   239  			{
   240  				Query:    "SHOW EVENTS;",
   241  				Expected: []sql.Row{{"mydb", "my_event1", "`root`@`localhost`", "SYSTEM", "ONE TIME", "2006-02-10 23:59:00", nil, nil, nil, nil, "DISABLED", 0, "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   242  			},
   243  			{
   244  				Query:    "ALTER EVENT my_event1 ENABLE;",
   245  				Expected: []sql.Row{{types.OkResult{}}},
   246  			},
   247  			{
   248  				Query:    "SHOW EVENTS;",
   249  				Expected: []sql.Row{},
   250  			},
   251  		},
   252  	},
   253  	{
   254  		Name: "altering event schedule between AT and EVERY",
   255  		SetUpScript: []string{
   256  			"USE mydb;",
   257  			"CREATE TABLE totals (num int);",
   258  			"CREATE EVENT my_event1 ON SCHEDULE EVERY '1:2' MINUTE_SECOND DISABLE DO INSERT INTO totals VALUES (1);",
   259  		},
   260  		Assertions: []ScriptTestAssertion{
   261  			{
   262  				Query:    "ALTER EVENT my_event1 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE;",
   263  				Expected: []sql.Row{{types.OkResult{}}},
   264  			},
   265  			{
   266  				Query:    "SHOW CREATE EVENT my_event1;",
   267  				Expected: []sql.Row{{"my_event1", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `my_event1` ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (1)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   268  			},
   269  			{
   270  				Query:    "ALTER EVENT my_event1 ON SCHEDULE EVERY 5 HOUR STARTS '2037-10-16 23:59:00' COMMENT 'updating the event schedule from AT';",
   271  				Expected: []sql.Row{{types.OkResult{}}},
   272  			},
   273  			{
   274  				Query:    "SHOW CREATE EVENT my_event1;",
   275  				Expected: []sql.Row{{"my_event1", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `my_event1` ON SCHEDULE EVERY 5 HOUR STARTS '2037-10-16 23:59:00' ON COMPLETION PRESERVE DISABLE COMMENT 'updating the event schedule from AT' DO INSERT INTO totals VALUES (1)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   276  			},
   277  		},
   278  	},
   279  	{
   280  		Name: "altering event fields",
   281  		SetUpScript: []string{
   282  			"USE mydb;",
   283  			"CREATE TABLE totals (num int);",
   284  			"CREATE EVENT my_event1 ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (1);",
   285  		},
   286  		Assertions: []ScriptTestAssertion{
   287  			{
   288  				Query:    "SHOW CREATE EVENT my_event1;",
   289  				Expected: []sql.Row{{"my_event1", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `my_event1` ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (1)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   290  			},
   291  			{
   292  				Query:    "ALTER EVENT my_event1 RENAME TO newEventName;",
   293  				Expected: []sql.Row{{types.OkResult{}}},
   294  			},
   295  			{
   296  				Query:    "SHOW CREATE EVENT newEventName;",
   297  				Expected: []sql.Row{{"newEventName", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `newEventName` ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE DO INSERT INTO totals VALUES (1)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   298  			},
   299  			{
   300  				Query:    "ALTER EVENT newEventName COMMENT 'insert 2 instead of 1' DO INSERT INTO totals VALUES (2);",
   301  				Expected: []sql.Row{{types.OkResult{}}},
   302  			},
   303  			{
   304  				Query:    "SHOW CREATE EVENT newEventName;",
   305  				Expected: []sql.Row{{"newEventName", "NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES", "SYSTEM", "CREATE DEFINER = `root`@`localhost` EVENT `newEventName` ON SCHEDULE AT '2006-02-10 23:59:00' ON COMPLETION PRESERVE DISABLE COMMENT 'insert 2 instead of 1' DO INSERT INTO totals VALUES (2)", "utf8mb4", "utf8mb4_0900_bin", "utf8mb4_0900_bin"}},
   306  			},
   307  		},
   308  	},
   309  }