migrator_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package migrations
  2. import (
  3. "testing"
  4. "github.com/go-xorm/xorm"
  5. . "github.com/smartystreets/goconvey/convey"
  6. )
  7. // func cleanDB(x *xorm.Engine) {
  8. // tables, _ := x.DBMetas()
  9. // sess := x.NewSession()
  10. // defer sess.Close()
  11. //
  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. //
  25. // var indexTypes = []string{"Unknown", "", "UNIQUE"}
  26. //
  27. func TestMigrator(t *testing.T) {
  28. Convey("Migrator", t, func() {
  29. x, err := xorm.NewEngine(SQLITE, ":memory:")
  30. So(err, ShouldBeNil)
  31. mg := NewMigrator(x)
  32. Convey("Given one migration", func() {
  33. mg.AddMigration("test migration", new(RawSqlMigration).
  34. Sqlite(`
  35. CREATE TABLE account (
  36. id INTEGER PRIMARY KEY AUTOINCREMENT
  37. )`).
  38. Mysql(`
  39. CREATE TABLE account (
  40. id BIGINT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id)
  41. )`))
  42. err := mg.Start()
  43. So(err, ShouldBeNil)
  44. log, err := mg.GetMigrationLog()
  45. So(err, ShouldBeNil)
  46. So(len(log), ShouldEqual, 1)
  47. })
  48. // So(err, ShouldBeNil)
  49. //
  50. // So(len(tables), ShouldEqual, 2)
  51. // fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables))
  52. //
  53. // for _, table := range tables {
  54. // fmt.Printf("\nTable: %v \n", table.Name)
  55. // for _, column := range table.Columns() {
  56. // fmt.Printf("\t %v \n", column.String(x.Dialect()))
  57. // }
  58. //
  59. // if len(table.Indexes) > 0 {
  60. // fmt.Printf("\n\tIndexes:\n")
  61. // for _, index := range table.Indexes {
  62. // fmt.Printf("\t %v (%v) %v \n", index.Name, strings.Join(index.Cols, ","), indexTypes[index.Type])
  63. // }
  64. // }
  65. // }
  66. })
  67. }