Files
grafana/pkg/services/sqlstore/migrations/ualert/migration_service_migration.go
Matthew Jacobson 6a8649d544 Alerting: Remove vendored models in migration service (#74503)
This PR replaces the vendored models in the migration with their equivalent ngalert models. It also replaces the raw SQL selects and inserts with service calls.

It also fills in some gaps in the testing suite around:

    - Migration of alert rules: verifying that the actual data model (queries, conditions) are correct 9a7cfa9
    - Secure settings migration: verifying that secure fields remain encrypted for all available notifiers and certain fields migrate from plain text to encrypted secure settings correctly e7d3993

Replacing the checks for custom dashboard ACLs will be replaced in a separate targeted PR as it will be complex enough alone.
2023-10-11 17:22:09 +01:00

70 lines
1.8 KiB
Go

package ualert
import (
"fmt"
"os"
"strconv"
"time"
"xorm.io/xorm"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
)
// KVNamespace is a vendored migration.KVNamespace.
var KVNamespace = "ngalert.migration"
// migratedKey is a vendored migration.migratedKey.
var migratedKey = "migrated"
// MigrationServiceMigration moves the legacy alert migration status from the migration log to kvstore.
func MigrationServiceMigration(mg *migrator.Migrator) {
mg.AddMigration("set legacy alert migration status in kvstore", &migrationLogToKVStore{})
}
type migrationLogToKVStore struct {
migrator.MigrationBase
}
func (c migrationLogToKVStore) SQL(migrator.Dialect) string {
return codeMigration
}
func (c migrationLogToKVStore) Exec(sess *xorm.Session, mg *migrator.Migrator) error {
logs, err := mg.GetMigrationLog()
if err != nil {
mg.Logger.Error("alert migration failure: could not get migration log", "error", err)
os.Exit(1)
}
_, migrationRun := logs[migTitle]
var anyOrg int64 = 0
now := time.Now()
entry := kvStoreV1Entry{
OrgID: &anyOrg,
Namespace: &KVNamespace,
Key: &migratedKey,
Value: strconv.FormatBool(migrationRun),
Created: now,
Updated: now,
}
if _, errCreate := sess.Table("kv_store").Insert(&entry); errCreate != nil {
mg.Logger.Error("failed to insert migration status to kvstore", "err", errCreate)
return fmt.Errorf("failed to insert migration status to kvstore: %w", errCreate)
}
return nil
}
// kvStoreV1Entry is a vendored kvstore.Item.
type kvStoreV1Entry struct {
ID int64 `xorm:"pk autoincr 'id'"`
OrgID *int64 `xorm:"org_id"`
Namespace *string `xorm:"namespace"`
Key *string `xorm:"key"`
Value string `xorm:"value"`
Created time.Time `xorm:"created"`
Updated time.Time `xorm:"updated"`
}