Alerting: Change rule migration to be based on feature flag (#33792)
makes it so the feature flag can be turned on off, and the migration will be cleared and rerun. All existing NG alert rules, configuration settings, etc are removed when disabling the feature flag. for https://github.com/grafana/alerting-squad/issues/142 Co-authored-by: Sofia Papagiannaki <sofia@grafana.com>
This commit is contained in:
co-authored by
Sofia Papagiannaki
parent
ac44a679a7
commit
dc3e17ba5b
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util/errutil"
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -17,6 +18,7 @@ type Migrator struct {
|
||||
Dialect Dialect
|
||||
migrations []Migration
|
||||
Logger log.Logger
|
||||
Cfg *setting.Cfg
|
||||
}
|
||||
|
||||
type MigrationLog struct {
|
||||
@@ -28,12 +30,13 @@ type MigrationLog struct {
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
func NewMigrator(engine *xorm.Engine) *Migrator {
|
||||
func NewMigrator(engine *xorm.Engine, cfg *setting.Cfg) *Migrator {
|
||||
mg := &Migrator{}
|
||||
mg.x = engine
|
||||
mg.Logger = log.New("migrator")
|
||||
mg.migrations = make([]Migration, 0)
|
||||
mg.Dialect = NewDialect(mg.x)
|
||||
mg.Cfg = cfg
|
||||
return mg
|
||||
}
|
||||
|
||||
@@ -168,6 +171,16 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mg *Migrator) ClearMigrationEntry(id string) error {
|
||||
sess := mg.x.NewSession()
|
||||
defer sess.Close()
|
||||
_, err := sess.SQL(`DELETE from migration_log where migration_id = ?`, id).Query()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to clear migration entry %v: %w", id, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type dbTransactionFunc func(sess *xorm.Session) error
|
||||
|
||||
func (mg *Migrator) inTransaction(callback dbTransactionFunc) error {
|
||||
|
||||
Reference in New Issue
Block a user