-
Notifications
You must be signed in to change notification settings - Fork 27
/
gomigrate_test.go
175 lines (152 loc) · 3.61 KB
/
gomigrate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package gomigrate
import (
"database/sql"
"fmt"
"log"
"os"
"testing"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
)
var (
db *sql.DB
adapter Migratable
dbType string
)
func GetMigrator(test string) *Migrator {
path := fmt.Sprintf("test_migrations/%s_%s", test, dbType)
m, err := NewMigrator(db, adapter, path)
if err != nil {
panic(err)
}
return m
}
func TestNewMigrator(t *testing.T) {
m := GetMigrator("test1")
switch {
case dbType == "pg" && len(m.migrations) != 4:
t.Errorf("Invalid number of migrations detected")
case dbType == "mysql" && len(m.migrations) != 1:
t.Errorf("Invalid number of migrations detected")
case dbType == "sqlite3" && len(m.migrations) != 1:
t.Errorf("Invalid number of migrations detected")
}
migration := m.migrations[1]
if migration.Name != "test" {
t.Errorf("Invalid migration name detected: %s", migration.Name)
}
if migration.Id != 1 {
t.Errorf("Invalid migration num detected: %d", migration.Id)
}
if migration.Status != Inactive {
t.Errorf("Invalid migration num detected: %d", migration.Status)
}
cleanup()
}
func TestCreatingMigratorWhenTableExists(t *testing.T) {
// Create the table and populate it with a row.
_, err := db.Exec(adapter.CreateMigrationTableSql())
if err != nil {
t.Error(err)
}
_, err = db.Exec(adapter.MigrationLogInsertSql(), 123)
if err != nil {
t.Error(err)
}
GetMigrator("test1")
// Check that our row is still present.
row := db.QueryRow("select migration_id from gomigrate")
var id uint64
err = row.Scan(&id)
if err != nil {
t.Error(err)
}
if id != 123 {
t.Error("Invalid id found in database")
}
cleanup()
}
func TestMigrationAndRollback(t *testing.T) {
m := GetMigrator("test1")
if err := m.Migrate(); err != nil {
t.Error(err)
}
// Ensure that the migration ran.
row := db.QueryRow(
adapter.SelectMigrationTableSql(),
"test",
)
var tableName string
if err := row.Scan(&tableName); err != nil {
t.Error(err)
}
if tableName != "test" {
t.Errorf("Migration table not created")
}
// Ensure that the migrate status is correct.
row = db.QueryRow(
adapter.GetMigrationSql(),
1,
)
var status int
if err := row.Scan(&status); err != nil {
t.Error(err)
}
if status != Active || m.migrations[1].Status != Active {
t.Error("Invalid status for migration")
}
if err := m.RollbackN(len(m.migrations)); err != nil {
t.Error(err)
}
// Ensure that the down migration ran.
row = db.QueryRow(
adapter.SelectMigrationTableSql(),
"test",
)
err := row.Scan(&tableName)
if err != nil && err != sql.ErrNoRows {
t.Errorf("Migration table should be deleted: %v", err)
}
// Ensure that the migration log is missing.
row = db.QueryRow(
adapter.GetMigrationSql(),
1,
)
if err := row.Scan(&status); err != nil && err != sql.ErrNoRows {
t.Error(err)
}
if m.migrations[1].Status != Inactive {
t.Errorf("Invalid status for migration, expected: %d, got: %v", Inactive, m.migrations[1].Status)
}
cleanup()
}
func cleanup() {
_, err := db.Exec("drop table gomigrate")
if err != nil {
panic(err)
}
}
func init() {
var err error
switch os.Getenv("DB") {
case "mysql":
dbType = "mysql"
log.Print("Using mysql")
adapter = Mariadb{}
db, err = sql.Open("mysql", "gomigrate:password@/gomigrate")
case "sqlite3":
dbType = "sqlite3"
log.Print("Using sqlite3")
adapter = Sqlite3{}
db, err = sql.Open("sqlite3", "file::memory:?cache=shared")
default:
dbType = "pg"
log.Print("Using postgres")
adapter = Postgres{}
db, err = sql.Open("postgres", "host=localhost dbname=gomigrate sslmode=disable")
}
if err != nil {
panic(err)
}
}