Files
grafana/pkg/services/sqlstore/migrations/migrations_test.go
Armand Grillet 6523486122 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>
2021-11-24 14:56:07 -05:00

94 lines
2.1 KiB
Go

package migrations
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
"xorm.io/xorm"
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
"github.com/grafana/grafana/pkg/setting"
)
func TestMigrations(t *testing.T) {
testDB := sqlutil.SQLite3TestDB()
const query = `select count(*) as count from migration_log`
result := struct{ Count int }{}
x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
require.NoError(t, err)
err = NewDialect(x).CleanDB()
require.NoError(t, err)
_, err = x.SQL(query).Get(&result)
require.Error(t, err)
mg := NewMigrator(x, &setting.Cfg{})
migrations := &OSSMigrations{}
migrations.AddMigration(mg)
expectedMigrations := mg.GetMigrationIDs(true)
err = mg.Start()
require.NoError(t, err)
has, err := x.SQL(query).Get(&result)
require.NoError(t, err)
require.True(t, has)
checkStepsAndDatabaseMatch(t, mg, expectedMigrations)
mg = NewMigrator(x, &setting.Cfg{})
migrations.AddMigration(mg)
err = mg.Start()
require.NoError(t, err)
has, err = x.SQL(query).Get(&result)
require.NoError(t, err)
require.True(t, has)
checkStepsAndDatabaseMatch(t, mg, expectedMigrations)
}
func checkStepsAndDatabaseMatch(t *testing.T, mg *Migrator, expected []string) {
t.Helper()
log, err := mg.GetMigrationLog()
require.NoError(t, err)
missing := make([]string, 0)
for _, id := range expected {
_, ok := log[id]
if !ok {
missing = append(missing, id)
}
}
notIntended := make([]string, 0)
for logId := range log {
found := false
for _, s := range expected {
found = s == logId
if found {
break
}
}
if !found {
notIntended = append(notIntended, logId)
}
}
if len(missing) == 0 && len(notIntended) == 0 {
return
}
var msg string
if len(missing) > 0 {
msg = fmt.Sprintf("was not executed [%v], ", strings.Join(missing, ", "))
}
if len(notIntended) > 0 {
msg += fmt.Sprintf("executed but should not [%v]", strings.Join(notIntended, ", "))
}
require.Failf(t, "the number of migrations does not match log in database", msg)
}