-
Notifications
You must be signed in to change notification settings - Fork 59
/
db_test.go
151 lines (127 loc) · 3.07 KB
/
db_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
package migrations_test
import (
"fmt"
"testing"
"github.com/go-pg/migrations/v8"
"github.com/go-pg/pg/v10"
)
func connectDB() *pg.DB {
db := pg.Connect(&pg.Options{
User: "postgres",
})
_, err := db.Exec("DROP TABLE IF EXISTS gopg_migrations")
if err != nil {
panic(err)
}
_, _, err = migrations.Run(db, "init")
if err != nil {
panic(err)
}
return db
}
func TestVersion(t *testing.T) {
db := connectDB()
version, err := migrations.Version(db)
if err != nil {
t.Fatalf("Version failed: %s", err)
}
if version != 0 {
t.Fatalf("got version %d, wanted 0", version)
}
if err := migrations.SetVersion(db, 999); err != nil {
t.Fatalf("SetVersion failed: %s", err)
}
version, err = migrations.Version(db)
if err != nil {
t.Fatalf("Version failed: %s", err)
}
if version != 999 {
t.Fatalf("got version %d, wanted 999", version)
}
}
func TestUpDown(t *testing.T) {
db := connectDB()
coll := migrations.NewCollection([]*migrations.Migration{
{Version: 2, Up: doNothing, Down: doNothing},
{Version: 1, Up: doNothing, Down: doNothing},
{Version: 3, Up: doNothing, Down: doNothing},
}...)
oldVersion, newVersion, err := coll.Run(db, "up")
if err != nil {
t.Fatal(err)
}
if oldVersion != 0 {
t.Fatalf("got %d, wanted 0", oldVersion)
}
if newVersion != 3 {
t.Fatalf("got %d, wanted 3", newVersion)
}
version, err := coll.Version(db)
if err != nil {
t.Fatal(err)
}
if version != 3 {
t.Fatalf("got version %d, wanted 3", version)
}
for i := 2; i >= -5; i-- {
wantOldVersion := int64(i + 1)
wantNewVersion := int64(i)
if wantNewVersion < 0 {
wantOldVersion = 0
wantNewVersion = 0
}
oldVersion, newVersion, err = coll.Run(db, "down")
if err != nil {
t.Fatal(err)
}
if oldVersion != wantOldVersion {
t.Fatalf("got %d, wanted %d", oldVersion, wantOldVersion)
}
if newVersion != wantNewVersion {
t.Fatalf("got %d, wanted %d", newVersion, wantNewVersion)
}
version, err = coll.Version(db)
if err != nil {
t.Fatal(err)
}
if version != wantNewVersion {
t.Fatalf("got version %d, wanted %d", version, wantNewVersion)
}
}
}
func TestSetVersion(t *testing.T) {
db := connectDB()
coll := migrations.NewCollection([]*migrations.Migration{
{Version: 3, Up: doPanic, Down: doPanic},
{Version: 1, Up: doPanic, Down: doPanic},
{Version: 2, Up: doPanic, Down: doPanic},
}...)
for i := 0; i < 5; i++ {
wantOldVersion := int64(i)
wantNewVersion := int64(i + 1)
oldVersion, newVersion, err := coll.Run(
db, "set_version", fmt.Sprint(wantNewVersion))
if err != nil {
t.Fatal(err)
}
if oldVersion != wantOldVersion {
t.Fatalf("got %d, wanted %d", oldVersion, wantOldVersion)
}
if newVersion != wantNewVersion {
t.Fatalf("got %d, wanted %d", newVersion, wantNewVersion)
}
version, err := coll.Version(db)
if err != nil {
t.Fatal(err)
}
if version != wantNewVersion {
t.Fatalf("got version %d, wanted %d", version, wantNewVersion)
}
}
}
func doNothing(db migrations.DB) error {
return nil
}
func doPanic(db migrations.DB) error {
panic("this migration should not be run")
}