-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration_test.go
85 lines (74 loc) · 1.47 KB
/
migration_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
package dsmigration
import (
"database/sql"
"log"
"os"
"testing"
_ "github.com/mattn/go-sqlite3"
)
var testMigrations = []Migration{
Migration{
Version: 1,
Up: `CREATE TABLE hello (id INT, text STRING);`,
Down: `DROP TABLE hello;`,
},
Migration{
Version: 3,
Up: `CREATE TABLE hello2 (id INT,text STRING);`,
Down: `DROP TABLE hello2;`,
},
Migration{
Version: 4,
Up: `INSERT INTO hello2 (id, text) VALUES (1, 'cjwjbcfkjew'),(2, 'xqmkqccw');`,
Down: `DELETE FROM hello2 WHERE id = 1 OR id = 2;`,
},
}
func TestUpAll(t *testing.T) {
db, err := sql.Open("sqlite3", "file:locked.sqlite?cache=shared&mode=memory")
if err != nil {
t.Fatal(err)
}
err = UpAll(db, testMigrations)
if err != nil {
t.Fatal(err)
}
ver, err := Version(db)
if ver != 4 {
t.Fatalf("Expected version %d got %d", 4, ver)
}
}
func TestUpDownDance(t *testing.T) {
Logger = log.New(os.Stderr, "", log.LstdFlags)
db, err := sql.Open("sqlite3", "file:updowndance.sqlite?cache=shared&mode=memory")
if err != nil {
t.Fatal(err)
}
err = Up(db, testMigrations)
if err != nil {
t.Fatal(err)
}
err = Down(db, testMigrations)
if err != nil {
t.Fatal(err)
}
for {
err = Up(db, testMigrations)
if err == ErrNoNewerVersion {
break
}
if err != nil {
t.Fatal(err)
}
err = Up(db, testMigrations)
if err == ErrNoNewerVersion {
break
}
if err != nil {
t.Fatal(err)
}
err = Down(db, testMigrations)
if err != nil {
t.Fatal(err)
}
}
}