-
Notifications
You must be signed in to change notification settings - Fork 0
/
imigrate_test.go
323 lines (290 loc) · 7.16 KB
/
imigrate_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package imigrate
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"path"
"strings"
"testing"
"time"
"github.com/bvinc/go-sqlite-lite/sqlite3"
)
type DB struct {
*sqlite3.Conn
}
func check(err error) {
if err != nil {
log.Panic(err)
}
}
func NewDB(uri string) *DB {
conn, err := sqlite3.Open(":memory:")
check(err)
return &DB{Conn: conn}
}
type DBResult struct {
lastInsertID int64
rowsAffected int64
err error
}
func (o DBResult) LastInsertId() (int64, error) {
return o.lastInsertID, o.err
}
func (o DBResult) RowsAffected() (int64, error) {
return o.rowsAffected, o.err
}
func (o DB) Exec(query string, args ...interface{}) (sql.Result, error) {
err := o.Conn.Exec(query, args...)
res := DBResult{
lastInsertID: o.LastInsertRowID(),
rowsAffected: int64(o.Changes()),
err: err,
}
return res, err
}
func (o DB) Get(dst []interface{}, query string, args ...interface{}) (err error) {
stmt, err := o.Conn.Prepare(query, args...)
if err != nil {
return
}
defer stmt.Close()
var hasRow bool
for {
hasRow, err = stmt.Step()
if !hasRow {
break
}
if err != nil {
return
}
err = stmt.Scan(dst...)
if err != nil {
return
}
}
return
}
func (o DB) GetVersions(query string, args ...interface{}) (versions []int64, err error) {
stmt, err := o.Conn.Prepare(query, args...)
if err != nil {
return
}
defer stmt.Close()
var hasRow bool
for {
hasRow, err = stmt.Step()
if !hasRow {
break
}
if err != nil {
return
}
var version int64
err = stmt.Scan(&version)
if err != nil {
return
}
versions = append(versions, version)
}
return
}
type FakeFSFileInfo struct {
name string
size int64
modtime time.Time
}
func (o FakeFSFileInfo) Name() string {
return o.name
}
func (o FakeFSFileInfo) Size() int64 {
return o.size
}
func (o FakeFSFileInfo) Mode() os.FileMode {
return os.ModePerm
}
func (o FakeFSFileInfo) ModTime() time.Time {
return o.modtime
}
func (o FakeFSFileInfo) IsDir() bool {
return false
}
func (o FakeFSFileInfo) Sys() interface{} {
return nil
}
// func (o FakeFSFile) Read(p []byte) (n int, err error) {
// }
// func (o FakeFSFile) Seek(offset int64, whence int) (int64, error) {
// return 0, nil
// }
type FakeFSFile struct {
*strings.Reader
Files []*FakeFSFile
FileInfo os.FileInfo
}
func NewFakeFSFile(name, content string) *FakeFSFile {
return &FakeFSFile{
Reader: strings.NewReader(content),
FileInfo: FakeFSFileInfo{name: name},
}
}
func (o FakeFSFile) Close() error {
return nil
}
func (o FakeFSFile) Readdir(count int) ([]os.FileInfo, error) {
var finfos []os.FileInfo
for _, f := range o.Files {
finfos = append(finfos, f.FileInfo)
}
return finfos, nil
}
func (o FakeFSFile) Stat() (os.FileInfo, error) {
return o.FileInfo, nil
}
type FakeFS struct {
migrationDirectory string
root *FakeFSFile
}
func NewFakeFS(migrationDirectory string, rootFiles []*FakeFSFile) *FakeFS {
root := NewFakeFSFile(migrationDirectory, "root")
root.Files = rootFiles
return &FakeFS{
migrationDirectory: migrationDirectory,
root: root,
}
}
func (o FakeFS) Open(name string) (http.File, error) {
if name == o.migrationDirectory {
return o.root, nil
}
for _, f := range o.root.Files {
if path.Join(o.migrationDirectory, f.FileInfo.Name()) == name {
f.Seek(0, 0)
return f, nil
}
}
return nil, fmt.Errorf(fmt.Sprintf("file %s not found", name))
}
var migrations = map[string]*FakeFSFile{
"mig1": NewFakeFSFile("1111110001-mig1", `
-- ==== UP ====
create table foo (id integer primary key);
-- ==== DOWN ====
drop table foo;
`),
"mig2": NewFakeFSFile("1111110002-mig2", `
-- ==== UP ====
create table bar (id integer primary key);
-- ==== DOWN ====
drop table bar;
`),
"mig3": NewFakeFSFile("1111110003-mig3", `
-- ==== UP ====
drop table bar;
create table baz (id integer primary key);
-- ==== DOWN ====
create table bar (id integer primary key);
drop table baz;
`),
"mig4": NewFakeFSFile("1111110004-mig4", `
-- ==== UP ====
create table bux (id integer primary key);
-- ==== DOWN ====
drop table bux;
`),
}
func TestIMigrateUpDown(t *testing.T) {
db := NewDB(":memory:")
defer db.Close()
fs := NewFakeFS("migrations", []*FakeFSFile{migrations["mig1"]})
mig := NewIMigrator(db, fs)
// UP
mig.Up(-1, 0)
var tableName string
check(db.Get([]interface{}{&tableName}, "select name from sqlite_master where name='foo'"))
if tableName != "foo" {
t.Fatalf("expected %s to equal foo", tableName)
}
tableName = ""
check(db.Get([]interface{}{&tableName}, "select name from sqlite_master where name=?", mig.TableName))
if tableName != mig.TableName {
t.Fatalf("expected %s to equal %s", tableName, mig.TableName)
}
// DOWN
tableName = ""
mig.Down(-1, 0)
check(db.Get([]interface{}{&tableName}, "select name from sqlite_master where name='foo'"))
if tableName != "" {
t.Fatalf("expected %s to equal \"\"", tableName)
}
}
func TestIMigrateUp(t *testing.T) {
db := NewDB(":memory:")
defer db.Close()
fs := NewFakeFS("migrations", []*FakeFSFile{migrations["mig1"], migrations["mig2"], migrations["mig3"], migrations["mig4"]})
mig := NewIMigrator(db, fs)
mig.Up(-1, 0)
tableNames := []string{
"foo", "baz", "bux", mig.TableName,
}
for _, t := range tableNames {
var sqlTbl string
check(db.Get([]interface{}{&sqlTbl}, "select name from sqlite_master where name=?", t))
if sqlTbl != t {
log.Fatalf("expected %s to exist, got %s", t, sqlTbl)
}
}
versions := []int{1111110001, 1111110002, 1111110003, 1111110004}
for _, v := range versions {
var versionMigrated int
sql := fmt.Sprintf("select %s from %s where %s=?", mig.VersionColumn, mig.TableName, mig.VersionColumn)
check(db.Get([]interface{}{&versionMigrated}, sql, v))
if v != versionMigrated {
t.Fatalf("Expected version %d to exist, but got %d", v, versionMigrated)
}
}
mig.Down(-1, 0)
var count int
check(db.Get([]interface{}{&count}, "select count(*) from sqlite_master where type='table'"))
if count != 1 {
log.Fatalf("expected one table to exist, not %d", count)
}
count = 1111
check(db.Get([]interface{}{&count}, fmt.Sprintf("select count(*) from %s", mig.TableName)))
if count != 0 {
log.Fatalf("Expected 0 migrations to exist after Down, got %d", count)
}
var migrationTable string
check(db.Get([]interface{}{&migrationTable}, "select name from sqlite_master where type='table'"))
if migrationTable != mig.TableName {
t.Fatalf("Expected table %s to exist, got %s", mig.TableName, migrationTable)
}
mig.Up(-1, 1111110004)
check(db.Get([]interface{}{&migrationTable}, "select name from sqlite_master where name=?", "bux"))
if migrationTable != "bux" {
log.Fatalf("expected table bux to exist on Up with version, but got %s", migrationTable)
}
mig.Down(-1, 0)
mig.Up(2, 0)
tableNames = []string{
"foo", "bar", mig.TableName,
}
for _, t := range tableNames {
var sqlTbl string
check(db.Get([]interface{}{&sqlTbl}, "select name from sqlite_master where name=?", t))
if sqlTbl != t {
log.Fatalf("expected %s to exist, got %s", t, sqlTbl)
}
}
}
func TestIMigrateDown(t *testing.T) {
}
func TestIMigrateRedo(t *testing.T) {
}
func TestIMigrateRollback(t *testing.T) {
}
func TestIMigrateCreate(t *testing.T) {
}
func TestIMigrateStatus(t *testing.T) {
}