migrate_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package migrate
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "testing"
  7. _ "github.com/mattn/go-sqlite3"
  8. "github.com/xormplus/xorm"
  9. "gopkg.in/stretchr/testify.v1/assert"
  10. )
  11. type Person struct {
  12. ID int64
  13. Name string
  14. }
  15. type Pet struct {
  16. ID int64
  17. Name string
  18. PersonID int
  19. }
  20. const (
  21. dbName = "testdb.sqlite3"
  22. )
  23. var (
  24. migrations = []*Migration{
  25. {
  26. ID: "201608301400",
  27. Migrate: func(tx *xorm.Engine) error {
  28. return tx.Sync2(&Person{})
  29. },
  30. Rollback: func(tx *xorm.Engine) error {
  31. return tx.DropTables(&Person{})
  32. },
  33. },
  34. {
  35. ID: "201608301430",
  36. Migrate: func(tx *xorm.Engine) error {
  37. return tx.Sync2(&Pet{})
  38. },
  39. Rollback: func(tx *xorm.Engine) error {
  40. return tx.DropTables(&Pet{})
  41. },
  42. },
  43. }
  44. )
  45. func TestMigration(t *testing.T) {
  46. _ = os.Remove(dbName)
  47. db, err := xorm.NewEngine("sqlite3", dbName)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. defer db.Close()
  52. if err = db.DB().Ping(); err != nil {
  53. log.Fatal(err)
  54. }
  55. m := New(db, DefaultOptions, migrations)
  56. err = m.Migrate()
  57. assert.NoError(t, err)
  58. exists, _ := db.IsTableExist(&Person{})
  59. assert.True(t, exists)
  60. exists, _ = db.IsTableExist(&Pet{})
  61. assert.True(t, exists)
  62. assert.Equal(t, 2, tableCount(db, "migrations"))
  63. err = m.RollbackLast()
  64. assert.NoError(t, err)
  65. exists, _ = db.IsTableExist(&Person{})
  66. assert.True(t, exists)
  67. exists, _ = db.IsTableExist(&Pet{})
  68. assert.False(t, exists)
  69. assert.Equal(t, 1, tableCount(db, "migrations"))
  70. err = m.RollbackLast()
  71. assert.NoError(t, err)
  72. exists, _ = db.IsTableExist(&Person{})
  73. assert.False(t, exists)
  74. exists, _ = db.IsTableExist(&Pet{})
  75. assert.False(t, exists)
  76. assert.Equal(t, 0, tableCount(db, "migrations"))
  77. }
  78. func TestInitSchema(t *testing.T) {
  79. os.Remove(dbName)
  80. db, err := xorm.NewEngine("sqlite3", dbName)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. defer db.Close()
  85. if err = db.DB().Ping(); err != nil {
  86. log.Fatal(err)
  87. }
  88. m := New(db, DefaultOptions, migrations)
  89. m.InitSchema(func(tx *xorm.Engine) error {
  90. if err := tx.Sync2(&Person{}); err != nil {
  91. return err
  92. }
  93. if err := tx.Sync2(&Pet{}); err != nil {
  94. return err
  95. }
  96. return nil
  97. })
  98. err = m.Migrate()
  99. assert.NoError(t, err)
  100. exists, _ := db.IsTableExist(&Person{})
  101. assert.True(t, exists)
  102. exists, _ = db.IsTableExist(&Pet{})
  103. assert.True(t, exists)
  104. assert.Equal(t, 2, tableCount(db, "migrations"))
  105. }
  106. func TestMissingID(t *testing.T) {
  107. os.Remove(dbName)
  108. db, err := xorm.NewEngine("sqlite3", dbName)
  109. assert.NoError(t, err)
  110. if db != nil {
  111. defer db.Close()
  112. }
  113. assert.NoError(t, db.DB().Ping())
  114. migrationsMissingID := []*Migration{
  115. {
  116. Migrate: func(tx *xorm.Engine) error {
  117. return nil
  118. },
  119. },
  120. }
  121. m := New(db, DefaultOptions, migrationsMissingID)
  122. assert.Equal(t, ErrMissingID, m.Migrate())
  123. }
  124. func tableCount(db *xorm.Engine, tableName string) (count int) {
  125. row := db.DB().QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", tableName))
  126. row.Scan(&count)
  127. return
  128. }