diff --git a/pkg/services/annotations/annotationsimpl/cleanup_test.go b/pkg/services/annotations/annotationsimpl/cleanup_test.go index 278bb48e365..324699736b1 100644 --- a/pkg/services/annotations/annotationsimpl/cleanup_test.go +++ b/pkg/services/annotations/annotationsimpl/cleanup_test.go @@ -119,8 +119,8 @@ func TestIntegrationAnnotationCleanUp(t *testing.T) { t.Cleanup(func() { err := fakeSQL.WithDbSession(context.Background(), func(session *db.Session) error { - _, deleteAnnotationErr := session.Exec("DELETE FROM annotation") - _, deleteAnnotationTagErr := session.Exec("DELETE FROM annotation_tag") + _, deleteAnnotationErr := session.Exec("DELETE FROM annotation WHERE true") + _, deleteAnnotationTagErr := session.Exec("DELETE FROM annotation_tag WHERE true") return errors.Join(deleteAnnotationErr, deleteAnnotationTagErr) }) assert.NoError(t, err) @@ -157,7 +157,7 @@ func TestIntegrationOldAnnotationsAreDeletedFirst(t *testing.T) { t.Cleanup(func() { err := fakeSQL.WithDbSession(context.Background(), func(session *db.Session) error { - _, err := session.Exec("DELETE FROM annotation") + _, err := session.Exec("DELETE FROM annotation WHERE true") return err }) assert.NoError(t, err) diff --git a/pkg/services/annotations/annotationsimpl/xorm_store.go b/pkg/services/annotations/annotationsimpl/xorm_store.go index 7b957189312..8bfa69e80cd 100644 --- a/pkg/services/annotations/annotationsimpl/xorm_store.go +++ b/pkg/services/annotations/annotationsimpl/xorm_store.go @@ -333,10 +333,11 @@ func (r *xormRepositoryImpl) Get(ctx context.Context, query annotations.ItemQuer } if len(tags) > 0 { + // "at" is a keyword in Spanner and needs to be quoted. tagsSubQuery := fmt.Sprintf(` - SELECT SUM(1) FROM annotation_tag at - INNER JOIN tag on tag.id = at.tag_id - WHERE at.annotation_id = a.id + SELECT SUM(1) FROM annotation_tag `+r.db.Quote("at")+` + INNER JOIN tag on tag.id = `+r.db.Quote("at")+`.tag_id + WHERE `+r.db.Quote("at")+`.annotation_id = a.id AND ( %s ) diff --git a/pkg/services/sqlstore/sqlstore.go b/pkg/services/sqlstore/sqlstore.go index a3029fffe77..10dcf1d0c14 100644 --- a/pkg/services/sqlstore/sqlstore.go +++ b/pkg/services/sqlstore/sqlstore.go @@ -628,6 +628,8 @@ func TestMain(m *testing.M) { if err := testSQLStore.dialect.TruncateDBTables(testSQLStore.GetEngine()); err != nil { return nil, err } + testSQLStore.engine.ResetSequenceGenerator() + if err := testSQLStore.Reset(); err != nil { return nil, err } diff --git a/pkg/services/sqlstore/sqlstore_testinfra.go b/pkg/services/sqlstore/sqlstore_testinfra.go index fa7e1c47b3d..0a5c4b5db4f 100644 --- a/pkg/services/sqlstore/sqlstore_testinfra.go +++ b/pkg/services/sqlstore/sqlstore_testinfra.go @@ -183,6 +183,7 @@ func NewTestStore(tb TestingTB, opts ...TestOption) *SQLStore { tb.Fatalf("failed to truncate DB tables after migrations: %v", err) panic("unreachable") } + testSQLStore.engine.ResetSequenceGenerator() } return store diff --git a/pkg/util/xorm/sequence.go b/pkg/util/xorm/sequence.go index bb74e2064b6..4e11c0fbab1 100644 --- a/pkg/util/xorm/sequence.go +++ b/pkg/util/xorm/sequence.go @@ -19,6 +19,10 @@ func newSequenceGenerator(db *sql.DB) *sequenceGenerator { } } +func (sg *sequenceGenerator) Reset() { + // Nothing to do. This generator always uses state from DB. +} + func (sg *sequenceGenerator) Next(ctx context.Context, table, column string) (int64, error) { // Current implementation fetches new value for each Next call. key := fmt.Sprintf("%s:%s", table, column) diff --git a/pkg/util/xorm/sequence_inmem.go b/pkg/util/xorm/sequence_inmem.go index e412e687664..1d5cfacea76 100644 --- a/pkg/util/xorm/sequence_inmem.go +++ b/pkg/util/xorm/sequence_inmem.go @@ -18,6 +18,13 @@ func newInMemSequenceGenerator() *inMemSequenceGenerator { } } +func (g *inMemSequenceGenerator) Reset() { + g.sequencesMu.Lock() + defer g.sequencesMu.Unlock() + + g.nextValues = make(map[string]int) +} + func (g *inMemSequenceGenerator) Next(_ context.Context, table, column string) (int64, error) { if table == "migration_log" { // Don't use sequential IDs for migration log entries, as we don't clean up migration_log table between tests, diff --git a/pkg/util/xorm/xorm.go b/pkg/util/xorm/xorm.go index 109a0bca16a..d1464af1da4 100644 --- a/pkg/util/xorm/xorm.go +++ b/pkg/util/xorm/xorm.go @@ -127,8 +127,15 @@ func NewEngine(driverName string, dataSourceName string) (*Engine, error) { return engine, nil } +func (engine *Engine) ResetSequenceGenerator() { + if engine.sequenceGenerator != nil { + engine.sequenceGenerator.Reset() + } +} + type SequenceGenerator interface { Next(ctx context.Context, table, column string) (int64, error) + Reset() } type DialectExt interface {