Files
grafana/pkg/util/xorm/sequence_inmem.go
Peter Štibraný 3d53968ecb spanner-annotationsimpls-fixes (#102622)
* Fix DELETE statements.
* Reset sequence generator when truncating tables.
* Quote "at", since it's a keyword in Spanner.
2025-03-21 16:10:51 +01:00

47 lines
968 B
Go

package xorm
import (
"context"
"fmt"
"math/rand/v2"
"sync"
)
type inMemSequenceGenerator struct {
sequencesMu sync.Mutex
nextValues map[string]int
}
func newInMemSequenceGenerator() *inMemSequenceGenerator {
return &inMemSequenceGenerator{
nextValues: make(map[string]int),
}
}
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,
// so restarting the sequence can lead to conflicting IDs.
return rand.Int64(), nil
}
key := fmt.Sprintf("%s:%s", table, column)
g.sequencesMu.Lock()
defer g.sequencesMu.Unlock()
seq, ok := g.nextValues[key]
if !ok {
seq = 1
}
g.nextValues[key] = seq + 1
return int64(seq), nil
}