-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate_test.go
205 lines (192 loc) · 4.47 KB
/
migrate_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
package migrate
import (
"context"
"errors"
"reflect"
"testing"
"blake.io/pqx/pqxtest"
_ "github.com/lib/pq"
"golang.org/x/sync/errgroup"
)
func TestMain(m *testing.M) {
pqxtest.TestMain(m)
}
func TestMigrateConcurrent(t *testing.T) {
db := pqxtest.CreateDB(t, ``)
migrations := []Migration{
{
Name: "2000-01-01.0.migrate.select1.sql",
SQL: "CREATE TABLE foo (id text NOT NULL PRIMARY KEY);",
},
}
var group errgroup.Group
for i := 0; i < 5; i++ {
group.Go(func() error {
return Run(context.Background(), db, migrations)
})
}
err := group.Wait()
if err != nil {
t.Error(err)
}
// Check that there are no remaining, unapplied migrations.
unapplied, err := FilterApplied(db, migrations)
if err != nil {
t.Fatal(err)
}
if len(unapplied) > 0 {
t.Errorf("len(FilterApplied(migrations)) = %d, want 0", len(unapplied))
}
}
func TestFilterApplied(t *testing.T) {
const migrationTable = `
CREATE SEQUENCE IF NOT EXISTS migration_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
CREATE TABLE IF NOT EXISTS migrations (
filename text NOT NULL,
hash text NOT NULL,
applied_at timestamp with time zone DEFAULT now() NOT NULL,
index int DEFAULT nextval('migration_seq') NOT NULL,
PRIMARY KEY(filename)
);
`
const oneMigration = `
INSERT INTO migrations (filename, hash, applied_at)
VALUES ('x', 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', '2016-02-09T23:21:55 US/Pacific');
`
cases := []struct {
initSQL string
migs []Migration
want []Migration
}{
{
initSQL: `SELECT 1;`,
migs: nil,
want: nil,
},
{
initSQL: migrationTable,
migs: nil,
want: nil,
},
{
initSQL: `SELECT 1;`,
migs: []Migration{{Name: "x", SQL: "a"}},
want: []Migration{{Name: "x", SQL: "a"}},
},
{
initSQL: migrationTable + oneMigration,
migs: []Migration{{Name: "x", SQL: "a"}},
want: []Migration{},
},
{
initSQL: `SELECT 1;`,
migs: []Migration{
{Name: "x", SQL: "a"},
{Name: "y", SQL: "b"},
},
want: []Migration{
{Name: "x", SQL: "a"},
{Name: "y", SQL: "b"},
},
},
{
initSQL: migrationTable + oneMigration,
migs: []Migration{
{Name: "x", SQL: "a"},
{Name: "y", SQL: "b"},
},
want: []Migration{{Name: "y", SQL: "b"}},
},
{
initSQL: migrationTable + oneMigration,
migs: []Migration{{Name: "x1", SQL: "a"}},
want: []Migration{},
},
}
for _, test := range cases {
db := pqxtest.CreateDB(t, ``)
_, err := db.ExecContext(context.TODO(), test.initSQL)
if err != nil {
t.Error(err)
continue
}
got, err := FilterApplied(db, test.migs)
if err != nil {
t.Error(err)
continue
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("FilterApplied() = %#v, want %#v", got, test.want)
}
}
}
func TestFilterAppliedError(t *testing.T) {
const migrationTable = `
CREATE SEQUENCE IF NOT EXISTS migration_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
CREATE TABLE IF NOT EXISTS migrations (
filename text NOT NULL,
hash text NOT NULL,
applied_at timestamp with time zone DEFAULT now() NOT NULL,
index int DEFAULT nextval('migration_seq') NOT NULL,
PRIMARY KEY(filename)
);
`
const oneMigration = `
INSERT INTO migrations (filename, hash, applied_at)
VALUES ('x', 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', '2016-02-09T23:21:55 US/Pacific');
`
cases := []struct {
initSQL string
migs []Migration
want *Error
}{
{
initSQL: migrationTable + oneMigration,
migs: nil,
want: &Error{
Mig: Migration{
Name: "x",
Hash: "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
},
Index: 0,
Err: errors.New("applied but not requested"),
},
},
{
initSQL: migrationTable + oneMigration,
migs: []Migration{{Name: "x", SQL: "a1"}},
want: &Error{
Mig: Migration{
Name: "x",
Hash: "ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb",
},
Index: 0,
Err: errors.New("hash mismatch"),
},
},
}
for _, test := range cases {
db := pqxtest.CreateDB(t, ``)
_, err := db.ExecContext(context.TODO(), test.initSQL)
if err != nil {
t.Error(err)
continue
}
_, got := FilterApplied(db, test.migs)
if !reflect.DeepEqual(got, test.want) {
t.Errorf("FilterApplied(db, %v) err = %v, want %v", test.migs, got, test.want)
t.Logf("got: %#v", got)
t.Logf("want: %#v", test.want)
}
}
}