SecretsManager: Conditionally lock DB before migrations using config setting (#106003)

Secrets: Conditionally lock DB before migrations using config setting (#105949)

Co-authored-by: Matheus Macabu <macabu@users.noreply.github.com>
This commit is contained in:
Dana Axinte 2025-05-26 18:28:53 +01:00 committed by GitHub
parent 74b291d03b
commit 5401175562
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 4 deletions

View File

@ -1,6 +1,8 @@
package contracts
import "context"
// SecretDBMigrator is an interface for running database migrations related to secrets management.
type SecretDBMigrator interface {
RunMigrations() error
RunMigrations(ctx context.Context, lockDatabase bool) error
}

View File

@ -69,7 +69,10 @@ func RegisterAPIService(
return nil, nil
}
if err := secretDBMigrator.RunMigrations(); err != nil {
// Some DBs that claim to be MySQL/Postgres-compatible might not support table locking.
lockDatabase := cfg.Raw.Section("database").Key("migration_locking").MustBool(true)
if err := secretDBMigrator.RunMigrations(context.Background(), lockDatabase); err != nil {
return nil, fmt.Errorf("running secret database migrations: %w", err)
}

View File

@ -1,6 +1,7 @@
package migrator
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/infra/db"
@ -26,12 +27,12 @@ func NewWithEngine(db db.DB) contracts.SecretDBMigrator {
return &SecretDB{engine: db.GetEngine()}
}
func (db *SecretDB) RunMigrations() error {
func (db *SecretDB) RunMigrations(ctx context.Context, lockDatabase bool) error {
mg := migrator.NewScopedMigrator(db.engine, nil, "secret")
db.AddMigration(mg)
return mg.Start(true, 0)
return mg.RunMigrations(ctx, lockDatabase, 0)
}
func (*SecretDB) AddMigration(mg *migrator.Migrator) {