Chore: Rewrite sqlstore migration test to use standard library (#29589)

Signed-off-by: Emil Hessman <emil@hessman.se>
This commit is contained in:
Emil Hessman 2020-12-04 09:05:00 +01:00 committed by GitHub
parent 3c1bcc7275
commit db0fb1e2c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,56 +5,45 @@ import (
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator" . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil" "github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
"github.com/stretchr/testify/require"
"xorm.io/xorm" "xorm.io/xorm"
. "github.com/smartystreets/goconvey/convey"
) )
func TestMigrations(t *testing.T) { func TestMigrations(t *testing.T) {
testDBs := []sqlutil.TestDB{ testDB := sqlutil.SQLite3TestDB()
sqlutil.SQLite3TestDB(), const query = `select count(*) as count from migration_log`
} result := struct{ Count int }{}
for _, testDB := range testDBs { x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
sql := `select count(*) as count from migration_log` require.NoError(t, err)
r := struct {
Count int64
}{}
Convey("Initial "+testDB.DriverName+" migration", t, func() { err = NewDialect(x).CleanDB()
x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr) require.NoError(t, err)
So(err, ShouldBeNil)
err = NewDialect(x).CleanDB() _, err = x.SQL(query).Get(&result)
So(err, ShouldBeNil) require.Error(t, err)
_, err = x.SQL(sql).Get(&r) mg := NewMigrator(x)
So(err, ShouldNotBeNil) AddMigrations(mg)
expectedMigrations := mg.MigrationsCount()
mg := NewMigrator(x) err = mg.Start()
AddMigrations(mg) require.NoError(t, err)
err = mg.Start() has, err := x.SQL(query).Get(&result)
So(err, ShouldBeNil) require.NoError(t, err)
require.True(t, has)
has, err := x.SQL(sql).Get(&r) require.Equal(t, expectedMigrations, result.Count)
So(err, ShouldBeNil)
So(has, ShouldBeTrue)
// we currently skip to migrations. We should rewrite skipped migrations to write in the log as well.
// until then we have to keep this
expectedMigrations := mg.MigrationsCount()
So(r.Count, ShouldEqual, expectedMigrations)
mg = NewMigrator(x) mg = NewMigrator(x)
AddMigrations(mg) AddMigrations(mg)
err = mg.Start() err = mg.Start()
So(err, ShouldBeNil) require.NoError(t, err)
has, err = x.SQL(sql).Get(&r) has, err = x.SQL(query).Get(&result)
So(err, ShouldBeNil) require.NoError(t, err)
So(has, ShouldBeTrue) require.True(t, has)
So(r.Count, ShouldEqual, expectedMigrations) require.Equal(t, expectedMigrations, result.Count)
})
}
} }