migrations_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package migrations
  2. import (
  3. "testing"
  4. "github.com/go-xorm/xorm"
  5. . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  6. "github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. func TestMigrations(t *testing.T) {
  10. testDBs := []sqlutil.TestDB{
  11. sqlutil.TestDB_Sqlite3,
  12. }
  13. for _, testDB := range testDBs {
  14. sql := `select count(*) as count from migration_log`
  15. r := struct {
  16. Count int64
  17. }{}
  18. Convey("Initial "+testDB.DriverName+" migration", t, func() {
  19. x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
  20. So(err, ShouldBeNil)
  21. sqlutil.CleanDB(x)
  22. has, err := x.SQL(sql).Get(&r)
  23. So(err, ShouldNotBeNil)
  24. mg := NewMigrator(x)
  25. AddMigrations(mg)
  26. err = mg.Start()
  27. So(err, ShouldBeNil)
  28. has, err = x.SQL(sql).Get(&r)
  29. So(err, ShouldBeNil)
  30. So(has, ShouldBeTrue)
  31. expectedMigrations := mg.MigrationsCount() - 2 //we currently skip to migrations. We should rewrite skipped migrations to write in the log as well. until then we have to keep this
  32. So(r.Count, ShouldEqual, expectedMigrations)
  33. mg = NewMigrator(x)
  34. AddMigrations(mg)
  35. err = mg.Start()
  36. So(err, ShouldBeNil)
  37. has, err = x.SQL(sql).Get(&r)
  38. So(err, ShouldBeNil)
  39. So(has, ShouldBeTrue)
  40. So(r.Count, ShouldEqual, expectedMigrations)
  41. })
  42. }
  43. }