Skip to content

Commit

Permalink
Merge pull request #8219 from dolthub/fulghum/event_disabled_bug
Browse files Browse the repository at this point in the history
Bug fix: events should remain enabled after sql-server restarts
  • Loading branch information
fulghum committed Aug 6, 2024
2 parents 594fd2d + 9cb746d commit b2f0662
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
8 changes: 5 additions & 3 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -1829,10 +1829,12 @@ func (db Database) getCatalog(ctx *sql.Context) *analyzer.Catalog {

// SaveEvent implements sql.EventDatabase.
func (db Database) SaveEvent(ctx *sql.Context, event sql.EventDefinition) (bool, error) {
// If the database is not the default branch database, then the event is disabled.
// If the database is NOT on the DefaultInitBranch, then we disable the event, since
// events only run from a single branch. We check this by looking at the database's
// revision and ensuring it either matches DefaultInitBranch or is empty.
// TODO: need better way to determine the default branch; currently it checks only 'main'
if db.revision != env.DefaultInitBranch && event.Status == sql.EventStatus_Enable.String() {
// using revision database name
if (db.revision != env.DefaultInitBranch && db.revision != "") && event.Status == sql.EventStatus_Enable.String() {
ctx.GetLogger().Debugf("disabling event %s (db.revision == %s)", event.Name, db.revision)
event.Status = sql.EventStatus_Disable.String()
ctx.Session.Warn(&sql.Warning{
Level: "Warning",
Expand Down
29 changes: 24 additions & 5 deletions integration-tests/bats/events.bats
Original file line number Diff line number Diff line change
Expand Up @@ -257,23 +257,42 @@ SQL
}

@test "events: restarting a sql-server correctly schedules existing events" {
# Create the recurring event and make sure it runs at least once
dolt sql -q "CREATE EVENT eventTest1 ON SCHEDULE EVERY 1 SECOND STARTS CURRENT_TIMESTAMP DO INSERT INTO totals (int_col) VALUES (111);"
sleep 1
# Create the recurring event and verify that it's enabled
dolt sql -q "CREATE EVENT eventTest1 ON SCHEDULE EVERY 1 SECOND STARTS '2020-02-20 00:00:00' DO INSERT INTO totals (int_col) VALUES (111);"
run dolt sql -q "SHOW EVENTS"
[ $status -eq 0 ]
[[ $output =~ '| repo1 | eventTest1 | `__dolt_local_user__`@`localhost` | SYSTEM | RECURRING | NULL | 1 | SECOND | 2020-02-20 00:00:00 | NULL | ENABLED | 0 | utf8mb4 | utf8mb4_0900_bin | utf8mb4_0900_bin |' ]] || false

# Sleep for a few seconds to give the scheduler timer to run this event and verify that it executed
sleep 2
run dolt sql -q "SELECT (SELECT COUNT(*) FROM totals) > 0;"
[ $status -eq 0 ]
[[ $output =~ "| 1 " ]] || false

# Verify that the event is still enabled
run dolt sql -q "SHOW EVENTS"
[ $status -eq 0 ]
[[ $output =~ '| repo1 | eventTest1 | `__dolt_local_user__`@`localhost` | SYSTEM | RECURRING | NULL | 1 | SECOND | 2020-02-20 00:00:00 | NULL | ENABLED | 0 | utf8mb4 | utf8mb4_0900_bin | utf8mb4_0900_bin |' ]] || false

# Stop the sql-server, truncate the totals table, and assert it's empty
stop_sql_server 1
dolt sql -q "truncate totals;"
run dolt sql -q "SELECT (SELECT COUNT(*) FROM totals) > 0;"
[ $status -eq 0 ]
[[ $output =~ "| false " ]] || false

# Restart the server and assert that the event gets scheduled and executed again
# Restart the server and assert that the event is still enabled
start_sql_server
sleep 1
run dolt sql -q "SHOW EVENTS;"
[[ $output =~ '| repo1 | eventTest1 | `__dolt_local_user__`@`localhost` | SYSTEM | RECURRING | NULL | 1 | SECOND | 2020-02-20 00:00:00 | NULL | ENABLED | 0 | utf8mb4 | utf8mb4_0900_bin | utf8mb4_0900_bin |' ]] || false

# Sleep for a few seconds to give the scheduler timer to run this event and verify that it is still enabled
sleep 2
run dolt sql -q "SHOW EVENTS"
[ $status -eq 0 ]
[[ $output =~ '| repo1 | eventTest1 | `__dolt_local_user__`@`localhost` | SYSTEM | RECURRING | NULL | 1 | SECOND | 2020-02-20 00:00:00 | NULL | ENABLED | 0 | utf8mb4 | utf8mb4_0900_bin | utf8mb4_0900_bin |' ]] || false

# Verify that the event executed and inserted a row in the totals table
run dolt sql -q "SELECT (SELECT COUNT(*) FROM totals) > 0;"
[ $status -eq 0 ]
[[ $output =~ "| 1 " ]] || false
Expand Down

0 comments on commit b2f0662

Please sign in to comment.