Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions go/libraries/doltcore/sqle/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 75 additions & 2 deletions go/libraries/doltcore/sqle/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
package sqle

import (
"context"
"testing"

"github.com/dolthub/dolt/go/libraries/doltcore/sqle/dsess"
"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"
)

func testKeyFunc(t *testing.T, keyFunc func(string) (bool, string), testVal string, expectedIsKey bool, expectedDBName string) {
Expand All @@ -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)
})
}
4 changes: 2 additions & 2 deletions go/libraries/doltcore/sqle/procedures_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down