Alerting: Make Unified Alerting enabled by default for those who do not use legacy alerting (#42200)

* update AlertingEnabled and UnifiedAlertingSettings.Enabled to be pointers
* add a pseudo migration to fix the AlertingEnabled and UnifiedAlertingSettings.Enabled if the latter is not defined
* update the default configuration file to make default value for both 'enabled' flags be undefined

Misc
* update Migrator to expose DB engine. This is needed for a ualert migration to access the database while the list of migrations is created.
* add more verbose failure when migrations do not match

Co-authored-by: gotjosh <josue@grafana.com>
Co-authored-by: Yuriy Tseretyan <yuriy.tseretyan@grafana.com>
Co-authored-by: gillesdemey <gilles.de.mey@gmail.com>
This commit is contained in:
Armand Grillet
2021-11-24 14:56:07 -05:00
committed by GitHub
co-authored by gotjosh Yuriy Tseretyan gillesdemey
parent 1c261aea8e
commit 6523486122
19 changed files with 352 additions and 180 deletions
+22 -10
View File
@@ -5,16 +5,17 @@ import (
"time"
_ "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"
"xorm.io/xorm"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util/errutil"
)
type Migrator struct {
x *xorm.Engine
DBEngine *xorm.Engine
Dialect Dialect
migrations []Migration
Logger log.Logger
@@ -32,10 +33,10 @@ type MigrationLog struct {
func NewMigrator(engine *xorm.Engine, cfg *setting.Cfg) *Migrator {
mg := &Migrator{}
mg.x = engine
mg.DBEngine = engine
mg.Logger = log.New("migrator")
mg.migrations = make([]Migration, 0)
mg.Dialect = NewDialect(mg.x)
mg.Dialect = NewDialect(mg.DBEngine)
mg.Cfg = cfg
return mg
}
@@ -49,11 +50,22 @@ func (mg *Migrator) AddMigration(id string, m Migration) {
mg.migrations = append(mg.migrations, m)
}
func (mg *Migrator) GetMigrationIDs(excludeNotLogged bool) []string {
result := make([]string, 0, len(mg.migrations))
for _, migration := range mg.migrations {
if migration.SkipMigrationLog() && excludeNotLogged {
continue
}
result = append(result, migration.Id())
}
return result
}
func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
logMap := make(map[string]MigrationLog)
logItems := make([]MigrationLog, 0)
exists, err := mg.x.IsTableExist(new(MigrationLog))
exists, err := mg.DBEngine.IsTableExist(new(MigrationLog))
if err != nil {
return nil, errutil.Wrap("failed to check table existence", err)
}
@@ -61,7 +73,7 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
return logMap, nil
}
if err = mg.x.Find(&logItems); err != nil {
if err = mg.DBEngine.Find(&logItems); err != nil {
return nil, err
}
@@ -132,7 +144,7 @@ func (mg *Migrator) Start() error {
mg.Logger.Info("migrations completed", "performed", migrationsPerformed, "skipped", migrationsSkipped, "duration", time.Since(start))
// Make sure migrations are synced
return mg.x.Sync2()
return mg.DBEngine.Sync2()
}
func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
@@ -178,7 +190,7 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
type dbTransactionFunc func(sess *xorm.Session) error
func (mg *Migrator) InTransaction(callback dbTransactionFunc) error {
sess := mg.x.NewSession()
sess := mg.DBEngine.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {