-
Notifications
You must be signed in to change notification settings - Fork 1
/
mig_test.go
300 lines (250 loc) · 6.5 KB
/
mig_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
package mig
import (
"database/sql"
stdlog "log"
"os"
"os/exec"
"strings"
"sync"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
)
/*mysql:
requires mysqladmin
create user 'testuser'@'localhost' identified by 'testpassword';
grant all privileges on testdb . * to 'testuser'@'localhost';
*/
/*postgres:
create user testuser;
alter user testuser password 'testpassword'
alter user testuser createdb;
*/
func init() {
SetLogger(stdlog.New(os.Stdout, "migrations: ", 0))
}
func Test(t *testing.T) {
//create the postgres table
_ = exec.Command("dropdb", "-U", "testuser", "testdb").Run()
output, err := exec.Command("createdb", "-U", "testuser", "testdb").CombinedOutput()
defer exec.Command("dropdb", "-U", "testuser", "testdb").Run()
if err != nil {
t.Fatalf("couldn't create postgres db: %v, %s\n", err, output)
}
//create the mysql database
mysqlTestDB := func(command string, input ...string) ([]byte, error) {
cmd := exec.Command("mysqladmin", "-u", "testuser", command, "testdb")
//passing passwords as command-line args in mysqladmin is broken: A space makes it prompt.
cmd.Env = []string{"MYSQL_PWD=testpassword"}
cmd.Stdin = strings.NewReader(strings.Join(input, " "))
return cmd.CombinedOutput()
}
_, _ = mysqlTestDB("drop", "yes")
output, err = mysqlTestDB("create")
if err != nil {
t.Fatalf("couldn't create mysql db: %v, %s\n", err, string(output))
}
defer mysqlTestDB("drop", "yes")
pg, err := sqlx.Connect("postgres", "postgres://testuser:testpassword@localhost/testdb?sslmode=disable")
if err != nil {
t.Fatalf("couldn't connect to postgres test db: %v\n", err)
}
mysql, err := sqlx.Connect("mysql", "testuser:testpassword@/testdb")
if err != nil {
t.Fatalf("couldn't connect to mysql test db: %v\n", err)
}
pg.SetMaxOpenConns(30)
mysql.SetMaxOpenConns(30)
t.Run("database lock", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping lock for faster tests")
}
testDatabaseLock(t, mysql)
})
t.Run("values-preserved", func(t *testing.T) {
testValuesPreserved(t, pg)
testValuesPreserved(t, mysql)
})
t.Run("prereq", func(t *testing.T) {
testPrereq(t, pg)
testPrereq(t, mysql)
})
t.Run("whitespace", func(t *testing.T) {
testWhitespace(t, pg)
testWhitespace(t, mysql)
})
t.Run("function", func(t *testing.T) {
testFunctionMigrations(t, pg)
testFunctionMigrations(t, mysql)
})
}
func testValuesPreserved(t *testing.T, db *sqlx.DB) {
registered = nil
RegisterMigrations(
`create table rerun(id int)`,
)
err := RunMigrations(MakeDB(db.DriverName(), db.DB))
if err != nil {
t.Fatalf("running migrations: %v\n", err)
}
_, err = db.Exec(`insert into rerun(id) values (42)`)
if err != nil {
t.Fatalf("inserting placeholder value: %v\n", err)
}
registered = nil
RegisterMigrations(
`create table rerun(id int)`,
`ALTER TABLE rerun ADD COLUMN name TEXT`,
)
err = RunMigrations(db)
if err != nil {
t.Fatalf("running migrations: %v\n", err)
}
var val struct {
Id int
Name *string
}
err = db.Get(&val, `select id, name from rerun limit 1`)
if err != nil {
t.Fatalf("getting placeholder value: %v\n", err)
}
if val.Id != 42 {
t.Fatalf(`val.Id != 42, val.Id == "%v"`, val)
}
if val.Name != nil {
t.Fatalf(`val.Name != nil, val.Name == "%v"`, val.Name)
}
}
func testPrereq(t *testing.T, db *sqlx.DB) {
registered = nil
RegisterMigrations(
Prereq(` select 1 from test_prereq`),
`alter table test_prereq add column food varchar(20)`,
)
RegisterMigrations(
`create table test_prereq(dummy int)`,
)
err := RunMigrations(db)
if err != nil {
t.Fatalf("couldn't run migrations: %v\n", err)
}
_, err = db.Exec("insert into test_prereq(food) values ('nachos'), ('burritos')")
if err != nil {
t.Fatalf("couldn't run migration: %v\n", err)
}
var result []struct {
Food string
}
err = db.Select(&result, "select food from test_prereq")
if err != nil {
t.Fatalf(": %v\n", err)
}
if len(result) != 2 {
t.Fatalf(`len(result) != 2, len(result) == "%v"`, len(result))
}
// now try the migrations again to make sure it works out
registered = nil
RegisterMigrations(
Prereq(`select 1 from test_prereq`),
`alter table test_prereq add column food varchar(20)`,
)
RegisterMigrations(
`create table test_prereq(dummy int)`,
)
err = RunMigrations(db)
if err != nil {
t.Fatalf("Re-running migrations: %v\n", err)
}
}
func testWhitespace(t *testing.T, db *sqlx.DB) {
registered = nil
RegisterMigrations(
`
--comments shouldn't affect things...
create table test_whitespace(
survive int
)
`,
)
err := RunMigrations(db)
if err != nil {
t.Fatalf(": %v\n", err)
}
//insert a value which is expected to survive the migration below
_, err = db.Exec("insert into test_whitespace values (42)")
if err != nil {
t.Fatalf("couldn't insert: %v\n", err)
}
//this is the same migration, except for whitespace differences
registered = nil
RegisterMigrations(
strings.Join([]string{
"create table test_whitespace(",
"survive int",
")",
}, "\n"),
)
if err = RunMigrations(db); err != nil {
t.Fatalf(": %v\n", err)
}
// check if the value survived as expected
var result struct {
Survive int
}
if err = db.Get(&result, "select * from test_whitespace"); err != nil {
t.Fatalf("couldn't select: %v\n", err)
}
if result.Survive != 42 {
t.Fatalf(`result.Survive != 42, result.Survive == "%v"`, result.Survive)
}
}
func testFunctionMigrations(t *testing.T, db *sqlx.DB) {
RegisterMigrations(
`create table numbers(num int)`,
`insert into numbers values (10), (20), (30)`,
Function("double all rows", func(tx *sql.Tx) error {
_, err := tx.Exec(`
insert into numbers(num)
select num from numbers
`)
return err
}),
)
err := RunMigrations(MakeDB(db.DriverName(), db.DB))
if err != nil {
t.Fatalf("running migrations: %v\n", err)
}
sum := 0
err = db.Get(&sum, `select SUM(num) from numbers`)
if err != nil {
t.Fatalf("bad query: %v\n", err)
}
if sum != 120 {
t.Fatalf(`sum != 60, sum == "%v"`, sum)
}
}
func testDatabaseLock(t *testing.T, db *sqlx.DB) {
const count = 20
counts := map[int]int{}
wait := sync.WaitGroup{}
wait.Add(count)
for i := 0; i < count; i++ {
go func() {
err := WithDatabaseLock(db, 30*time.Second, func() error {
l := len(counts)
counts[l]++
return nil
})
if err != nil {
t.Fatalf("error locking: %v\n", err)
}
wait.Done()
}()
}
wait.Wait()
if len(counts) != count {
t.Fatalf(`len(counts) != %d, len(counts) == "%d"`, count, len(counts))
}
}