-
Notifications
You must be signed in to change notification settings - Fork 3
/
migrator_test.go
550 lines (501 loc) · 17.7 KB
/
migrator_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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package pgmigrate_test
import (
"context"
"database/sql"
"testing"
_ "github.com/jackc/pgx/v5/stdlib" // pgx driver
"github.com/peterldowns/testy/assert"
"github.com/peterldowns/testy/check"
"github.com/peterldowns/pgmigrate"
"github.com/peterldowns/pgmigrate/internal/schema"
"github.com/peterldowns/pgmigrate/internal/withdb"
)
func TestApplyNoMigrationsSucceeds(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations := []pgmigrate.Migration{}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
check.Nil(t, err)
check.Equal(t, nil, verrs)
return nil
})
assert.Nil(t, err)
}
func TestApplyOneMigrationSucceeds(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations := []pgmigrate.Migration{
{
ID: "0001_initial",
SQL: "CREATE TABLE users (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY);",
},
}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(applied), 1)
check.Equal(t, migrations[0].ID, applied[0].ID)
check.Equal(t, migrations[0].MD5(), applied[0].Checksum)
return nil
})
assert.Nil(t, err)
}
func TestApplySameMigrationTwiceSucceeds(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
m := pgmigrate.Migration{
ID: "0001_initial",
SQL: "CREATE TABLE users (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY);",
}
migrations := []pgmigrate.Migration{m}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(applied), 1)
check.Equal(t, applied[0].ID, m.ID)
check.Equal(t, applied[0].Checksum, m.MD5())
// Running apply again with the same migrations should succeed without
// any errors and without attempting to re-apply the migration.
verrs, err = migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err = migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(applied), 1)
check.Equal(t, applied[0].ID, m.ID)
check.Equal(t, applied[0].Checksum, m.MD5())
return nil
})
assert.Nil(t, err)
}
func TestApplyMultipleSucceedsInCorrectOrder(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations := []pgmigrate.Migration{
{ // Depends on 0003_houses
ID: "0004_users",
SQL: "CREATE TABLE users (name text, house_id int references houses (id));",
},
{
ID: "0002_dogs",
SQL: "CREATE TABLE dogs (id int primary key, furry bool);",
},
{
// Dpeends on 0002_dogs
ID: "0003_cats",
SQL: "CREATE TABLE cats (id int primary key, enemy_id int references dogs(id));",
},
{ // Depends on 0003_cats
ID: "0003_houses",
SQL: "CREATE TABLE houses (id int primary key, cat_id int references cats (id));",
},
}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
// The computed plan should sort ascending by ID
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(plan), 4)
assert.Equal(t, "0002_dogs", plan[0].ID)
assert.Equal(t, "0003_cats", plan[1].ID)
assert.Equal(t, "0003_houses", plan[2].ID)
assert.Equal(t, "0004_users", plan[3].ID)
// Applying should happen in the same order as the plan.
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(applied), 4)
return nil
})
assert.Nil(t, err)
}
func TestApplyFailsWithConflictingIDs(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
m1 := pgmigrate.Migration{
ID: "0001_initial",
SQL: "CREATE TABLE users (name text);",
}
// Because this migration re-uses the earlier migration's ID, it will fail to apply
m2 := pgmigrate.Migration{
ID: "0001_initial",
SQL: "CREATE TABLE money (amount bigint);",
}
// Because m2 fails to be applied, this migration (which would succeed) is not applied
m3 := pgmigrate.Migration{
ID: "0002_something_else",
SQL: "CREATE TABLE dogs (furry bool);",
}
migrations := []pgmigrate.Migration{m1, m2, m3}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
check.Error(t, err)
check.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
check.Nil(t, err)
check.Equal(t, len(applied), 1)
check.Equal(t, applied[0].ID, m1.ID)
check.Equal(t, applied[0].Checksum, m1.MD5())
return nil
})
assert.Nil(t, err)
}
func TestApplyFailsWithInvalidSQL(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
m1 := pgmigrate.Migration{
ID: "0001_initial",
SQL: "this is definitely not valid sql!!!",
}
// Because the first migration failed, this will not be applied
m2 := pgmigrate.Migration{
ID: "0002_money",
SQL: "CREATE TABLE money (amount bigint);",
}
migrations := []pgmigrate.Migration{m1, m2}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
check.Error(t, err)
check.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
check.Nil(t, err)
check.Equal(t, 0, len(applied))
return nil
})
assert.Nil(t, err)
}
func TestVerifyMD5Mismatch(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
m1 := pgmigrate.Migration{
ID: "0001_initial",
SQL: "CREATE TABLE users (name text);",
}
migrations := []pgmigrate.Migration{m1}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
check.Nil(t, err)
check.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
check.Nil(t, err)
check.Equal(t, len(applied), 1)
check.Equal(t, applied[0].ID, m1.ID)
check.Equal(t, applied[0].Checksum, m1.MD5())
// With the same ID, but different query content, the MD5 will differ
// and we should get a warning.
m1modified := m1
m1modified.SQL = "CREATE TABLE dogs (furry bool);"
migrator = pgmigrate.NewMigrator([]pgmigrate.Migration{m1modified})
migrator.Logger = logger
verrs, err = migrator.Migrate(ctx, db)
check.Nil(t, err)
check.Equal(t, len(verrs), 1)
verr := verrs[0]
check.Equal(t, verr.Message, "found applied migration with a different checksum")
check.Equal(t, m1modified.MD5(), verr.Fields["calculated_checksum"].(string))
check.Equal(t, m1.MD5(), verr.Fields["migration_checksum_from_db"].(string))
return nil
})
assert.Nil(t, err)
}
func TestVerifyMissingMigration(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
m1 := pgmigrate.Migration{
ID: "0001_initial",
SQL: "CREATE TABLE users (name text);",
}
migrations := []pgmigrate.Migration{m1}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
check.Nil(t, err)
check.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
check.Nil(t, err)
check.Equal(t, len(applied), 1)
check.Equal(t, applied[0].ID, m1.ID)
check.Equal(t, applied[0].Checksum, m1.MD5())
migrator = pgmigrate.NewMigrator(nil)
migrator.Logger = logger
verrs, err = migrator.Migrate(ctx, db)
check.Nil(t, err)
check.Equal(t, len(verrs), 1)
verr := verrs[0]
check.Equal(t, verr.Message, "found applied migration not present on disk")
check.Equal(t, m1.ID, verr.Fields["migration_id"].(string))
check.Equal(t, m1.MD5(), verr.Fields["migration_checksum"].(string))
return nil
})
assert.Nil(t, err)
}
func TestAppliedAndPlanWithoutMigrationsTable(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
// Starting from an empty database, Applied() and Plan() should work without
// issues and act as if no migrations had previously been applied.
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
m1 := pgmigrate.Migration{
ID: "0001_initial",
SQL: "CREATE TABLE users (name text);",
}
migrations := []pgmigrate.Migration{m1}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, applied)
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(plan), 1)
assert.Equal(t, m1, plan[0])
return nil
})
assert.Nil(t, err)
}
// By default, pgmigrate will use the [DefaultTableName] table to
// keep track of migrations. Because this is a fully qualified table name,
// including a schema prefix, pgmigrate will not be affected by migrations
// that change the search_path of the current database connection when they're
// executed.
func TestSettingSearchPathInMigrationsDoesntBreak(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
m1 := pgmigrate.Migration{
ID: "01_devices",
SQL: `--sql
CREATE SCHEMA IF NOT EXISTS another_schema;
SET search_path TO another_schema;
CREATE TABLE IF NOT EXISTS devices (
id uuid NOT NULL PRIMARY KEY,
state jsonb NOT NULL
);
`,
}
m2 := pgmigrate.Migration{
ID: "02_goals",
SQL: `--sql
-- This line relies on migration 01_devices having been executed,
-- since "another_schema" will not exist otherwise.
SET search_path TO another_schema;
CREATE TABLE IF NOT EXISTS goals (
device_id uuid PRIMARY KEY NOT NULL REFERENCES devices (id) ON DELETE CASCADE,
goal jsonb
);
`,
}
m3 := pgmigrate.Migration{
ID: "03_ambiguous",
SQL: `--sql
-- This ambiguous table name will be resolved using the current value of
-- search_path. By default, the search_path is "default" but can be overridden
-- by the connection string that is used to connect to the database, or changed
-- by other SQL commands run on the same connection.
--
-- If this migration is applied as part of the same plan as the migration m2/02_goals,
-- this will create the table "another_schema"."users".
--
-- If this migration is applied in a second session, for instance if the earlier
-- migrations had already been shipped to the users and this was written and shipped
-- as an update, then it will create the table "public"."users".
--
-- If you're setting the search_path inside of your migrations, you should make
-- sure to be aware of this possibility, and consistently set/reset the search
-- path.
CREATE TABLE users (
id uuid NOT NULL PRIMARY KEY,
name text NOT NULL default ''
);
`,
}
t.Run("all_migrations_in_one_session", func(t *testing.T) {
t.Parallel()
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations := []pgmigrate.Migration{m1, m2, m3}
migrator := pgmigrate.NewMigrator(migrations)
assert.Equal(t, pgmigrate.DefaultTableName, migrator.TableName)
migrator.Logger = logger
// Check to confirm that 0 migrations have been applied.
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, applied)
// Check to confirm that the migrations should be applied.
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(plan), 3)
assert.Equal(t, migrations, plan)
// Apply the migrations.
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
// Check to make sure that the migrations have been applied.
applied, err = migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 3, len(applied))
assert.Equal(t, migrations[0].ID, applied[0].ID)
assert.Equal(t, migrations[1].ID, applied[1].ID)
assert.Equal(t, migrations[2].ID, applied[2].ID)
// Check to make sure that the migrations will not be applied again.
plan, err = migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, len(plan), 0)
// The [DefaultTableName] table was created correctly in the public schema.
publicTables, err := schema.LoadTables(schema.Config{
Schema: "public",
}, db)
assert.Nil(t, err)
check.Equal(t, 1, len(publicTables))
check.Equal(t, "pgmigrate_migrations", publicTables[0].Name)
// Because all three migrations were applied over the same
// connection, when m2 modified the search_path, the m3 migration
// was applied in that context, so the users table ended up in
// "another_schema".
otherTables, err := schema.LoadTables(schema.Config{
Schema: "another_schema",
}, db)
assert.Nil(t, err)
check.Equal(t, 3, len(otherTables))
check.Equal(t, "devices", otherTables[0].Name)
check.Equal(t, "goals", otherTables[1].Name)
check.Equal(t, "users", otherTables[2].Name)
return nil
})
assert.Nil(t, err)
})
t.Run("first_two_migrations_then_the_third_separately", func(t *testing.T) {
t.Parallel()
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
// Only m1 and m2, NOT m3, to start.
migrations := []pgmigrate.Migration{m1, m2}
migrator := pgmigrate.NewMigrator(migrations)
assert.Equal(t, pgmigrate.DefaultTableName, migrator.TableName)
migrator.Logger = logger
// Check to confirm that 0 migrations have been applied.
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, applied)
// Check to confirm that the migrations should be applied.
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
check.Equal(t, migrations, plan)
// Apply the migrations.
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
// Check to make sure that the migrations have been applied.
applied, err = migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 2, len(applied))
check.Equal(t, migrations[0].ID, applied[0].ID)
check.Equal(t, migrations[1].ID, applied[1].ID)
// Check to make sure that the migrations will not be applied again.
plan, err = migrator.Plan(ctx, db)
assert.Nil(t, err)
check.Equal(t, len(plan), 0)
// The [DefaultTableName] table was created correctly in the public schema.
publicTables, err := schema.LoadTables(schema.Config{
Schema: "public",
}, db)
assert.Nil(t, err)
assert.Equal(t, 1, len(publicTables))
check.Equal(t, "pgmigrate_migrations", publicTables[0].Name)
// m1 and m2 were applied correctly and created their tables in "another_schema".
otherTables, err := schema.LoadTables(schema.Config{
Schema: "another_schema",
}, db)
assert.Nil(t, err)
assert.Equal(t, 2, len(otherTables))
check.Equal(t, "devices", otherTables[0].Name)
check.Equal(t, "goals", otherTables[1].Name)
// Now, add the third migration.
migrations = []pgmigrate.Migration{m1, m2, m3}
migrator.Migrations = migrations
// Reset the search_state to public, as if we had opened a new connection to the
// database --- for instance, if we were applying this migration a few weeks
// after the first two had been applied.
//
// This is an implementation detail of this test: the *sql.DB will
// re-use the same connection from the earlier steps, which means
// that the search_path will still be "another_schema".
var searchpath string
err = db.QueryRowContext(ctx, `SHOW search_path`).Scan(&searchpath)
assert.Nil(t, err)
assert.Equal(t, "another_schema", searchpath)
_, err = db.ExecContext(ctx, "SET search_path TO DEFAULT")
assert.Nil(t, err)
err = db.QueryRowContext(ctx, `SHOW search_path`).Scan(&searchpath)
assert.Nil(t, err)
assert.Equal(t, `"$user", public`, searchpath)
// Check to confirm that just the third migration should be applied.
plan, err = migrator.Plan(ctx, db)
assert.Nil(t, err)
check.Equal(t, []pgmigrate.Migration{m3}, plan)
// Apply the third migration.
verrs, err = migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
// Check to make sure that all 3 migrations have been applied.
applied, err = migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 3, len(applied))
check.Equal(t, migrations[0].ID, applied[0].ID)
check.Equal(t, migrations[1].ID, applied[1].ID)
check.Equal(t, migrations[2].ID, applied[2].ID)
// Because m3 was applied by itself, not on the same connection that
// had previously executed m1 and m2, it was executed while the search_path
// was still set to the default. This means that it resulted in the table "public"."users",
// NOT "another_schema"."users", as in the previous scenario.
publicTables, err = schema.LoadTables(schema.Config{
Schema: "public",
}, db)
assert.Nil(t, err)
assert.Equal(t, 2, len(publicTables))
check.Equal(t, "pgmigrate_migrations", publicTables[0].Name)
check.Equal(t, "users", publicTables[1].Name)
// Nothing has changed in "another_schema", it still has the tables
// created by m1 and m2.
otherTables, err = schema.LoadTables(schema.Config{
Schema: "another_schema",
}, db)
assert.Nil(t, err)
assert.Equal(t, 2, len(otherTables))
check.Equal(t, "devices", otherTables[0].Name)
check.Equal(t, "goals", otherTables[1].Name)
return nil
})
assert.Nil(t, err)
})
}