migrations_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package migrations
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/go-xorm/xorm"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func cleanDB(x *xorm.Engine) {
  9. tables, _ := x.DBMetas()
  10. sess := x.NewSession()
  11. defer sess.Close()
  12. for _, table := range tables {
  13. if _, err := sess.Exec("SET FOREIGN_KEY_CHECKS = 0"); err != nil {
  14. panic("Failed to disable foreign key checks")
  15. }
  16. if _, err := sess.Exec("DROP TABLE " + table.Name); err != nil {
  17. panic(fmt.Sprintf("Failed to delete table: %v, err: %v", table.Name, err))
  18. }
  19. if _, err := sess.Exec("SET FOREIGN_KEY_CHECKS = 1"); err != nil {
  20. panic("Failed to disable foreign key checks")
  21. }
  22. }
  23. }
  24. func TestMigrationsSqlite(t *testing.T) {
  25. testDBs := [][]string{
  26. []string{"sqlite3", ":memory:"},
  27. []string{"mysql", "grafana:password@tcp(localhost:3306)/grafana_tests?charset=utf8"},
  28. }
  29. for _, testDB := range testDBs {
  30. Convey("Initial "+testDB[0]+" migration", t, func() {
  31. x, err := xorm.NewEngine(testDB[0], testDB[1])
  32. So(err, ShouldBeNil)
  33. if testDB[0] == "mysql" {
  34. cleanDB(x)
  35. }
  36. StartMigration(x)
  37. tables, err := x.DBMetas()
  38. So(err, ShouldBeNil)
  39. So(len(tables), ShouldEqual, 2)
  40. })
  41. }
  42. }