migrations_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. //"github.com/grafana/grafana/pkg/log"
  9. )
  10. var indexTypes = []string{"Unknown", "INDEX", "UNIQUE INDEX"}
  11. func TestMigrations(t *testing.T) {
  12. testDBs := []sqlutil.TestDB{
  13. sqlutil.TestDB_Sqlite3,
  14. }
  15. for _, testDB := range testDBs {
  16. sql := `select count(*) as count from migration_log`
  17. r := struct {
  18. Count int64
  19. }{}
  20. Convey("Initial "+testDB.DriverName+" migration", t, func() {
  21. x, err := xorm.NewEngine(testDB.DriverName, testDB.ConnStr)
  22. So(err, ShouldBeNil)
  23. sqlutil.CleanDB(x)
  24. has, err := x.SQL(sql).Get(&r)
  25. So(err, ShouldNotBeNil)
  26. mg := NewMigrator(x)
  27. AddMigrations(mg)
  28. err = mg.Start()
  29. So(err, ShouldBeNil)
  30. has, err = x.SQL(sql).Get(&r)
  31. So(err, ShouldBeNil)
  32. So(has, ShouldBeTrue)
  33. 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
  34. So(r.Count, ShouldEqual, expectedMigrations)
  35. mg = NewMigrator(x)
  36. AddMigrations(mg)
  37. err = mg.Start()
  38. So(err, ShouldBeNil)
  39. has, err = x.SQL(sql).Get(&r)
  40. So(err, ShouldBeNil)
  41. So(has, ShouldBeTrue)
  42. So(r.Count, ShouldEqual, expectedMigrations)
  43. })
  44. }
  45. }