Commit db0fb1e2 by Emil Hessman Committed by GitHub

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

Signed-off-by: Emil Hessman <emil@hessman.se>
parent 3c1bcc72
......@@ -5,56 +5,45 @@ import (
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
"github.com/stretchr/testify/require"
"xorm.io/xorm"
. "github.com/smartystreets/goconvey/convey"
)
func TestMigrations(t *testing.T) {
testDBs := []sqlutil.TestDB{
sqlutil.SQLite3TestDB(),
}
for _, testDB := range testDBs {
sql := `select count(*) as count from migration_log`
r := struct {
Count int64
}{}
Convey("Initial "+testDB.DriverName+" migration", t, func() {
x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
So(err, ShouldBeNil)
err = NewDialect(x).CleanDB()
So(err, ShouldBeNil)
_, err = x.SQL(sql).Get(&r)
So(err, ShouldNotBeNil)
mg := NewMigrator(x)
AddMigrations(mg)
err = mg.Start()
So(err, ShouldBeNil)
has, err := x.SQL(sql).Get(&r)
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)
AddMigrations(mg)
err = mg.Start()
So(err, ShouldBeNil)
has, err = x.SQL(sql).Get(&r)
So(err, ShouldBeNil)
So(has, ShouldBeTrue)
So(r.Count, ShouldEqual, expectedMigrations)
})
}
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)
AddMigrations(mg)
expectedMigrations := mg.MigrationsCount()
err = mg.Start()
require.NoError(t, err)
has, err := x.SQL(query).Get(&result)
require.NoError(t, err)
require.True(t, has)
require.Equal(t, expectedMigrations, result.Count)
mg = NewMigrator(x)
AddMigrations(mg)
err = mg.Start()
require.NoError(t, err)
has, err = x.SQL(query).Get(&result)
require.NoError(t, err)
require.True(t, has)
require.Equal(t, expectedMigrations, result.Count)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment