From ee0270d0bbf407522edc3c4949235fd606dfe4f4 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Tue, 16 Jan 2024 17:56:35 -0800 Subject: [PATCH 1/5] Fixed spurious error message for event scheduling when the dolt_schemas table doesn't exist. --- go/libraries/doltcore/sqle/database.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/go/libraries/doltcore/sqle/database.go b/go/libraries/doltcore/sqle/database.go index ec77c754cf..81100075f0 100644 --- a/go/libraries/doltcore/sqle/database.go +++ b/go/libraries/doltcore/sqle/database.go @@ -1353,6 +1353,12 @@ func (db Database) GetEvents(ctx *sql.Context) (events []sql.EventDefinition, to // NeedsToReloadEvents implements sql.EventDatabase. func (db Database) NeedsToReloadEvents(ctx *sql.Context, token interface{}) (bool, error) { + // A nil token means no events in this db. If the dolt_schemas table doesn't exist, it will have a zero hash below + // as well, meaning we don't reload events in that case. + if token == nil { + token = hash.Hash{} + } + hash, ok := token.(hash.Hash) if !ok { return false, fmt.Errorf("expected token to be hash.Hash, but received %T", token) @@ -1362,7 +1368,7 @@ func (db Database) NeedsToReloadEvents(ctx *sql.Context, token interface{}) (boo if err != nil { return false, err } - + // If the current hash doesn't match what we last loaded, then we // need to reload event definitions return !tableHash.Equal(hash), nil From 4786c570dd63dbeb0a81c7f4c7d13b55cf686d81 Mon Sep 17 00:00:00 2001 From: zachmu Date: Wed, 17 Jan 2024 02:03:54 +0000 Subject: [PATCH 2/5] [ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/update.sh --- go/libraries/doltcore/sqle/database.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/go/libraries/doltcore/sqle/database.go b/go/libraries/doltcore/sqle/database.go index 81100075f0..b0f79a1440 100644 --- a/go/libraries/doltcore/sqle/database.go +++ b/go/libraries/doltcore/sqle/database.go @@ -1353,12 +1353,12 @@ func (db Database) GetEvents(ctx *sql.Context) (events []sql.EventDefinition, to // NeedsToReloadEvents implements sql.EventDatabase. func (db Database) NeedsToReloadEvents(ctx *sql.Context, token interface{}) (bool, error) { - // A nil token means no events in this db. If the dolt_schemas table doesn't exist, it will have a zero hash below - // as well, meaning we don't reload events in that case. + // A nil token means no events in this db. If the dolt_schemas table doesn't exist, it will have a zero hash below + // as well, meaning we don't reload events in that case. if token == nil { token = hash.Hash{} } - + hash, ok := token.(hash.Hash) if !ok { return false, fmt.Errorf("expected token to be hash.Hash, but received %T", token) @@ -1368,7 +1368,7 @@ func (db Database) NeedsToReloadEvents(ctx *sql.Context, token interface{}) (boo if err != nil { return false, err } - + // If the current hash doesn't match what we last loaded, then we // need to reload event definitions return !tableHash.Equal(hash), nil From 0fbc1e202c5c39c17569ee4e70d5b215adf2def1 Mon Sep 17 00:00:00 2001 From: coffeegoddd Date: Wed, 17 Jan 2024 02:23:33 +0000 Subject: [PATCH 3/5] [skip actions] [ga-update-correctness] SQL Correctness updated to 99.996126 --- .github/scripts/sql-correctness/current_correctness.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/scripts/sql-correctness/current_correctness.txt diff --git a/.github/scripts/sql-correctness/current_correctness.txt b/.github/scripts/sql-correctness/current_correctness.txt new file mode 100644 index 0000000000..64fdf9067c --- /dev/null +++ b/.github/scripts/sql-correctness/current_correctness.txt @@ -0,0 +1 @@ +99.996126 From 632eea5bf0146d2690e33d99554fba93f67be703 Mon Sep 17 00:00:00 2001 From: Zach Musgrave Date: Wed, 17 Jan 2024 11:13:59 -0800 Subject: [PATCH 4/5] Added tests --- go/libraries/doltcore/sqle/database_test.go | 73 +++++++++++++++++++ .../doltcore/sqle/procedures_table_test.go | 4 +- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/go/libraries/doltcore/sqle/database_test.go b/go/libraries/doltcore/sqle/database_test.go index f0f52e3522..8e40a45b4d 100644 --- a/go/libraries/doltcore/sqle/database_test.go +++ b/go/libraries/doltcore/sqle/database_test.go @@ -15,9 +15,14 @@ package sqle import ( + "context" "testing" + "time" + "github.com/dolthub/dolt/go/libraries/doltcore/dtestutils" "github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess" + "github.com/dolthub/dolt/go/libraries/doltcore/table/editor" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/assert" ) @@ -36,3 +41,71 @@ func TestIsKeyFuncs(t *testing.T) { testKeyFunc(t, dsess.IsHeadKey, "dolt_working", false, "") testKeyFunc(t, dsess.IsWorkingKey, "dolt_working", true, "dolt") } + +func TestNeedsToReloadEvents(t *testing.T) { + dEnv := dtestutils.CreateTestEnv() + tmpDir, err := dEnv.TempTableFilesDir() + require.NoError(t, err) + opts := editor.Options{Deaf: dEnv.DbEaFactory(), Tempdir: tmpDir} + + timestamp := time.Now().Truncate(time.Minute).UTC() + + db, err := NewDatabase(context.Background(), "dolt", dEnv.DbData(), opts) + require.NoError(t, err) + + _, ctx, err := NewTestEngine(dEnv, context.Background(), db) + require.NoError(t, err) + + var token any + + t.Run("empty schema table doesn't need to be reloaded", func(t *testing.T) { + needsReload, err := db.NeedsToReloadEvents(ctx, token) + require.NoError(t, err) + assert.False(t, needsReload) + }) + + eventDefn := `CREATE EVENT testEvent +ON SCHEDULE + EVERY 1 DAY + STARTS now() +DO +BEGIN + CALL archive_order_history(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)); +END` + + err = db.addFragToSchemasTable(ctx, "event", "testEvent", eventDefn, timestamp, nil) + require.NoError(t, err) + + t.Run("events need to be reloaded after addition", func(t *testing.T) { + needsReload, err := db.NeedsToReloadEvents(ctx, token) + require.NoError(t, err) + assert.True(t, needsReload) + }) + + _, token, err = db.GetEvents(ctx) + require.NoError(t, err) + + t.Run("events do not need to be reloaded after no change", func(t *testing.T) { + needsReload, err := db.NeedsToReloadEvents(ctx, token) + require.NoError(t, err) + assert.False(t, needsReload) + }) + + err = db.dropFragFromSchemasTable(ctx, "event", "testEvent", nil) + require.NoError(t, err) + + t.Run("events need to be reloaded after dropping one", func(t *testing.T) { + needsReload, err := db.NeedsToReloadEvents(ctx, token) + require.NoError(t, err) + assert.True(t, needsReload) + }) + + _, token, err = db.GetEvents(ctx) + require.NoError(t, err) + + t.Run("events do not need to be reloaded after no change", func(t *testing.T) { + needsReload, err := db.NeedsToReloadEvents(ctx, token) + require.NoError(t, err) + assert.False(t, needsReload) + }) +} \ No newline at end of file diff --git a/go/libraries/doltcore/sqle/procedures_table_test.go b/go/libraries/doltcore/sqle/procedures_table_test.go index 8b048ef47b..1b669caea0 100644 --- a/go/libraries/doltcore/sqle/procedures_table_test.go +++ b/go/libraries/doltcore/sqle/procedures_table_test.go @@ -41,7 +41,7 @@ func TestProceduresMigration(t *testing.T) { timestamp := time.Now().Truncate(time.Minute).UTC() - ctx, db := newDatabase(t, dEnv, opts, timestamp) + ctx, db := newDatabaseWithProcedures(t, dEnv, opts, timestamp) t.Run("test migration logic", func(t *testing.T) { // Call the logic to migrate it to the latest schema @@ -102,7 +102,7 @@ func TestProceduresMigration(t *testing.T) { } -func newDatabase(t *testing.T, dEnv *env.DoltEnv, opts editor.Options, timestamp time.Time) (*sql.Context, *Database) { +func newDatabaseWithProcedures(t *testing.T, dEnv *env.DoltEnv, opts editor.Options, timestamp time.Time) (*sql.Context, *Database) { db, err := NewDatabase(context.Background(), "dolt", dEnv.DbData(), opts) require.NoError(t, err) From 5f5202fcf54648972839701ba2d385c8415b0ca3 Mon Sep 17 00:00:00 2001 From: zachmu Date: Wed, 17 Jan 2024 19:22:49 +0000 Subject: [PATCH 5/5] [ga-format-pr] Run go/utils/repofmt/format_repo.sh and go/Godeps/update.sh --- go/go.mod | 2 +- go/go.sum | 1 - go/libraries/doltcore/sqle/database_test.go | 18 +++++++++--------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/go/go.mod b/go/go.mod index 1fcadf5efb..b61847d432 100644 --- a/go/go.mod +++ b/go/go.mod @@ -66,6 +66,7 @@ require ( github.com/jmoiron/sqlx v1.3.4 github.com/kch42/buzhash v0.0.0-20160816060738-9bdec3dec7c6 github.com/kylelemons/godebug v1.1.0 + github.com/lib/pq v1.10.9 github.com/oracle/oci-go-sdk/v65 v65.55.0 github.com/prometheus/client_golang v1.13.0 github.com/rs/zerolog v1.28.0 @@ -128,7 +129,6 @@ require ( github.com/klauspost/compress v1.10.5 // indirect github.com/klauspost/cpuid/v2 v2.0.12 // indirect github.com/lestrrat-go/strftime v1.0.4 // indirect - github.com/lib/pq v1.10.9 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect diff --git a/go/go.sum b/go/go.sum index 81bd3497ee..c8af08ba7d 100644 --- a/go/go.sum +++ b/go/go.sum @@ -463,7 +463,6 @@ github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopu github.com/lestrrat-go/strftime v1.0.4 h1:T1Rb9EPkAhgxKqbcMIPguPq8glqXTA1koF8n9BHElA8= github.com/lestrrat-go/strftime v1.0.4/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= -github.com/lib/pq v1.10.0 h1:Zx5DJFEYQXio93kgXnQ09fXNiUKsqv4OUEu2UtGcB1E= github.com/lib/pq v1.10.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= diff --git a/go/libraries/doltcore/sqle/database_test.go b/go/libraries/doltcore/sqle/database_test.go index 8e40a45b4d..d5ea762ad5 100644 --- a/go/libraries/doltcore/sqle/database_test.go +++ b/go/libraries/doltcore/sqle/database_test.go @@ -19,12 +19,12 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/dolthub/dolt/go/libraries/doltcore/dtestutils" "github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess" "github.com/dolthub/dolt/go/libraries/doltcore/table/editor" - "github.com/stretchr/testify/require" - - "github.com/stretchr/testify/assert" ) func testKeyFunc(t *testing.T, keyFunc func(string) (bool, string), testVal string, expectedIsKey bool, expectedDBName string) { @@ -57,13 +57,13 @@ func TestNeedsToReloadEvents(t *testing.T) { require.NoError(t, err) var token any - + t.Run("empty schema table doesn't need to be reloaded", func(t *testing.T) { needsReload, err := db.NeedsToReloadEvents(ctx, token) require.NoError(t, err) assert.False(t, needsReload) }) - + eventDefn := `CREATE EVENT testEvent ON SCHEDULE EVERY 1 DAY @@ -72,10 +72,10 @@ DO BEGIN CALL archive_order_history(DATE_SUB(CURDATE(), INTERVAL 1 YEAR)); END` - + err = db.addFragToSchemasTable(ctx, "event", "testEvent", eventDefn, timestamp, nil) require.NoError(t, err) - + t.Run("events need to be reloaded after addition", func(t *testing.T) { needsReload, err := db.NeedsToReloadEvents(ctx, token) require.NoError(t, err) @@ -90,7 +90,7 @@ END` require.NoError(t, err) assert.False(t, needsReload) }) - + err = db.dropFragFromSchemasTable(ctx, "event", "testEvent", nil) require.NoError(t, err) @@ -108,4 +108,4 @@ END` require.NoError(t, err) assert.False(t, needsReload) }) -} \ No newline at end of file +}