-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_runner_test.go
314 lines (257 loc) · 11 KB
/
migration_runner_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
package pgutil
import (
"context"
"sync"
"testing"
"time"
"github.com/go-nacelle/log/v2"
"github.com/stretchr/testify/require"
)
func TestApply(t *testing.T) {
definitions := []RawDefinition{
{ID: 1, RawUpQuery: "CREATE TABLE users (id SERIAL PRIMARY KEY, email TEXT);"},
{ID: 2, RawUpQuery: "INSERT INTO users (email) VALUES ('[email protected]');"},
{ID: 3, RawUpQuery: "ALTER TABLE users ADD COLUMN name TEXT;"},
{ID: 4, RawUpQuery: "UPDATE users SET name = 'test';"},
{ID: 5, RawUpQuery: "CREATE UNIQUE INDEX users_email_idx ON users (email);"},
}
definitionsWithoutUpdates := []RawDefinition{definitions[0], definitions[1], definitions[2], definitions[4]}
reader := MigrationReaderFunc(func() ([]RawDefinition, error) { return definitions, nil })
readerWithoutUpdates := MigrationReaderFunc(func() ([]RawDefinition, error) { return definitionsWithoutUpdates, nil })
t.Run("all", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply all migrations from scratch
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.ApplyAll(ctx))
// Assert last migration (unique index) was applied
err = db.Exec(ctx, Query(
"INSERT INTO users (name, email) VALUES ({:name}, {:email})",
Args{"name": "duplicate", "email": "[email protected]"},
))
require.ErrorContains(t, err, "duplicate key value violates unique constraint")
})
t.Run("tail", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Head first
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.Apply(ctx, 2))
// Assert no name column yet
_, _, err = ScanString(db.Query(ctx, RawQuery("SELECT name FROM users WHERE email = '[email protected]'")))
require.ErrorContains(t, err, "column \"name\" does not exist")
// Apply the tail
require.NoError(t, runner.Apply(ctx, 5))
// Assert name column added and populated
email, _, err := ScanString(db.Query(ctx, RawQuery("SELECT name FROM users WHERE email = '[email protected]'")))
require.NoError(t, err)
require.Equal(t, "test", email)
})
t.Run("gaps", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply all migrations except #4
runnerWithHoles, err := NewMigrationRunner(db, readerWithoutUpdates, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runnerWithHoles.ApplyAll(ctx))
// Assert name column exists but is not yet populated
namePtr, _, err := ScanNilString(db.Query(ctx, RawQuery("SELECT name FROM users WHERE email = '[email protected]'")))
require.NoError(t, err)
require.Nil(t, namePtr)
// Apply all missing migrations
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.ApplyAll(ctx))
// Assert name colum now populated
name, _, err := ScanString(db.Query(ctx, RawQuery("SELECT name FROM users WHERE email = '[email protected]'")))
require.NoError(t, err)
require.Equal(t, "test", name)
})
}
func TestApplyCreateConcurrentIndex(t *testing.T) {
definitions := []RawDefinition{
{ID: 1, RawUpQuery: "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL);"},
{ID: 2, RawUpQuery: "INSERT INTO users (name, email) VALUES ('test1', '[email protected]');"},
{ID: 3, RawUpQuery: "CREATE UNIQUE INDEX CONCURRENTLY users_email_idx ON users (email);"},
{ID: 4, RawUpQuery: "INSERT INTO users (name, email) VALUES ('test2', '[email protected]');"},
}
reader := MigrationReaderFunc(func() ([]RawDefinition, error) { return definitions, nil })
t.Run("CIC", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply all migrations from scratch
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.ApplyAll(ctx))
// Assert last migration (unique index) was applied
err = db.Exec(ctx, Query(
"INSERT INTO users (name, email) VALUES ({:name}, {:email})",
Args{"name": "duplicate", "email": "[email protected]"},
))
require.ErrorContains(t, err, "duplicate key value violates unique constraint")
})
t.Run("CIC (already created)", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply just the first migration
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.Apply(ctx, 2))
// Create the index outside of the migration infrastructure
require.NoError(t, db.Exec(ctx, RawQuery(definitions[2].RawUpQuery)))
// Apply remaining migrations
require.NoError(t, runner.ApplyAll(ctx))
// Assert last migration (unique index) was applied
err = db.Exec(ctx, Query(
"INSERT INTO users (name, email) VALUES ({:name}, {:email})",
Args{"name": "duplicate", "email": "[email protected]"},
))
require.ErrorContains(t, err, "duplicate key value violates unique constraint")
})
t.Run("CIC (invalid)", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply just the first migration
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.Apply(ctx, 2))
// Create the index outside of the migration infrastructure and force it to be invalid
require.NoError(t, db.Exec(ctx, RawQuery(definitions[2].RawUpQuery)))
require.NoError(t, db.Exec(ctx, RawQuery(`
UPDATE pg_index
SET indisvalid = false
WHERE indexrelid = (
SELECT oid
FROM pg_class
WHERE relname = 'users_email_idx'
);
`)))
// Apply remaining migrations
require.NoError(t, runner.ApplyAll(ctx))
// Assert last migration (unique index) as applied
err = db.Exec(ctx, Query(
"INSERT INTO users (name, email) VALUES ({:name}, {:email})",
Args{"name": "duplicate", "email": "[email protected]"},
))
require.ErrorContains(t, err, "duplicate key value violates unique constraint")
})
t.Run("CIC (in progress)", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply the first two migrations
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.Apply(ctx, 2))
var wg sync.WaitGroup
errCh := make(chan error, 1)
async := func(f func() error) {
wg.Add(1)
go func() {
defer wg.Done()
if err := f(); err != nil {
errCh <- err
}
}()
}
// Start a transaction and insert a row in the users table but don't
// commit so that we hold open a lock for the following async tasks.
tx, err := db.Transact(ctx)
require.NoError(t, err)
require.NoError(t, tx.Exec(ctx, RawQuery("INSERT INTO users (name, email) VALUES ('blocking', '[email protected]')")))
// Begin creating the index concurrently outside the migration runner
// This will block until the transaction above commits or rolls back
async(func() error { return db.Exec(ctx, RawQuery(definitions[2].RawUpQuery)) })
// Jitter time to ensure index creation has started (and is blocked)
<-time.After(1 * time.Second)
// Apply the index creation and remaining migrations
// This will block until the other index creation completes
async(func() error { return runner.ApplyAll(ctx) })
// Jitter time to ensure the ApplyAll has started (and is blocked)
<-time.After(2 * time.Second)
// Commit the transaction and allow the index creation to complete
require.NoError(t, tx.Done(nil))
// Sync on async tasks and check for errors
wg.Wait()
close(errCh)
for err := range errCh {
require.NoError(t, err)
}
// Assert that the migration runner has unblocked and the index exists
err = db.Exec(ctx, Query(
"INSERT INTO users (name, email) VALUES ({:name}, {:email})",
Args{"name": "duplicate", "email": "[email protected]"},
))
require.ErrorContains(t, err, "duplicate key value violates unique constraint")
})
}
func TestUndo(t *testing.T) {
definitions := []RawDefinition{
{
ID: 1,
RawUpQuery: "CREATE TABLE users (id SERIAL PRIMARY KEY, email TEXT);",
RawDownQuery: "DROP TABLE users;",
},
{
ID: 2,
RawUpQuery: "CREATE TABLE comments (id SERIAL PRIMARY KEY, content TEXT NOT NULL, user_id INTEGER NOT NULL);",
RawDownQuery: "DROP TABLE comments;",
},
{
ID: 3,
RawUpQuery: "ALTER TABLE comments ADD COLUMN updated_at TIMESTAMP WITH TIME ZONE;",
RawDownQuery: "ALTER TABLE comments DROP COLUMN updated_at;",
},
{
ID: 4,
RawUpQuery: "ALTER TABLE comments ADD COLUMN created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW();",
RawDownQuery: "ALTER TABLE comments DROP COLUMN created_at;",
},
{ID: 5, RawUpQuery: "INSERT INTO users (email) VALUES ('[email protected]');"},
{ID: 6, RawUpQuery: "INSERT INTO comments (content, user_id) VALUES ('test', 1);"},
{ID: 7, RawUpQuery: "UPDATE comments SET updated_at = NOW();"},
}
definitionsWithoutCreatedAt := []RawDefinition{definitions[0], definitions[1], definitions[2], definitions[4], definitions[5], definitions[6]}
reader := MigrationReaderFunc(func() ([]RawDefinition, error) { return definitions, nil })
readerWithoutCreatedAt := MigrationReaderFunc(func() ([]RawDefinition, error) { return definitionsWithoutCreatedAt, nil })
t.Run("tail", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply all migrations
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.ApplyAll(ctx))
// Assert columns exist and are populated
updatedAt, _, err := ScanNilTimestamp(db.Query(ctx, RawQuery("SELECT created_at FROM comments WHERE user_id = 1")))
require.NoError(t, err)
require.NotNil(t, updatedAt)
// Undo migrations that added created_at/updated_at columns
require.NoError(t, runner.Undo(ctx, 3))
// Assert columns dropped
_, _, err = ScanString(db.Query(ctx, RawQuery("SELECT updated_at FROM comments WHERE user_id = 1")))
require.ErrorContains(t, err, "column \"updated_at\" does not exist")
})
t.Run("gaps", func(t *testing.T) {
db := NewTestDB(t)
ctx := context.Background()
// Apply all migrations
runner, err := NewMigrationRunner(db, reader, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runner.ApplyAll(ctx))
// Undo migrations but skip #4
runnerWithHoles, err := NewMigrationRunner(db, readerWithoutCreatedAt, log.NewNilLogger())
require.NoError(t, err)
require.NoError(t, runnerWithHoles.Undo(ctx, 3))
// Assert created_at exists but updated_at does not
_, _, err = ScanNilTimestamp(db.Query(ctx, RawQuery("SELECT created_at FROM comments WHERE user_id = 1")))
require.NoError(t, err)
_, _, err = ScanString(db.Query(ctx, RawQuery("SELECT updated_at FROM comments WHERE user_id = 1")))
require.ErrorContains(t, err, "column \"updated_at\" does not exist")
// Undo migrations including #4
require.NoError(t, runner.Undo(ctx, 3))
// Assert both columns dropped
_, _, err = ScanString(db.Query(ctx, RawQuery("SELECT created_at FROM comments WHERE user_id = 1")))
require.ErrorContains(t, err, "column \"created_at\" does not exist")
})
}