forked from mattermost/morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorph_test.go
502 lines (386 loc) · 13.9 KB
/
morph_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
//go:build !drivers
// +build !drivers
package morph
import (
"context"
"errors"
"testing"
"github.com/mattermost/morph/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
)
func TestSortMigrations(t *testing.T) {
testCases := []struct {
Name string
Migrations []string
ExpectedOrder []string
}{
{
Name: "sequence based migration names",
Migrations: []string{"000002_migration", "000003_migration", "000001_migration"},
ExpectedOrder: []string{"000001_migration", "000002_migration", "000003_migration"},
},
{
Name: "timestamp based migration names",
Migrations: []string{"202103221430_migration_3", "202103221400_migration_2", "202103221321_migration_1"},
ExpectedOrder: []string{"202103221321_migration_1", "202103221400_migration_2", "202103221430_migration_3"},
},
}
migrationsFromNames := func(names []string) []*models.Migration {
migrations := []*models.Migration{}
for _, name := range names {
migrations = append(migrations, &models.Migration{RawName: name})
}
return migrations
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
sortedMigrations := sortMigrations(migrationsFromNames(tc.Migrations))
for i, migration := range sortedMigrations {
assert.Equalf(t, tc.ExpectedOrder[i], migration.RawName, "Expected migration %q to be in position %d, but found %q instead", tc.ExpectedOrder[i], i, migration.RawName)
}
})
}
}
func TestApplyAll(t *testing.T) {
h := newTestHelper(t)
defer h.Teardown(t)
h.AddMigration(t, "test_migration")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
err := engine.ApplyAll()
require.NoError(t, err)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Len(t, migrations, 1)
})
}
func TestApply(t *testing.T) {
h := newTestHelper(t)
defer h.Teardown(t)
h.AddMigration(t, "test_migration_2")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
_, err := engine.Apply(1)
require.NoError(t, err)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Len(t, migrations, 1)
})
}
func TestInterceptors(t *testing.T) {
h := newTestHelper(t)
defer h.Teardown(t)
h.AddMigration(t, "test_migration_1")
h.AddMigration(t, "test_migration_2")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
var i int
engine.AddInterceptor(1, models.Up, func() error {
i++
return nil
})
_, err := engine.Apply(1)
require.NoError(t, err)
require.Equal(t, 1, i)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Len(t, migrations, 1)
engine.AddInterceptor(1, models.Down, func() error {
i--
return nil
})
_, err = engine.ApplyDown(1)
require.NoError(t, err)
require.Zero(t, i)
migrations, err = engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Empty(t, migrations)
}, "run interceptor function before the actual migration")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
var i int
engine.AddInterceptor(1, models.Up, func() error {
i++
return errors.New("fail")
})
_, err := engine.Apply(1)
require.Error(t, err)
require.Equal(t, 1, i)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Empty(t, migrations)
}, "abort migration if interceptor function returns error")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
var i int
engine.AddInterceptor(2, models.Up, func() error {
i++
return errors.New("fail")
})
_, err := engine.Apply(1)
require.NoError(t, err)
require.Zero(t, i)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Len(t, migrations, 1)
_, err = engine.ApplyDown(1)
require.NoError(t, err)
}, "do not call interceptor if the desired version is not reached")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// first get schema to a version
_, err := engine.Apply(1)
require.NoError(t, err)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Len(t, migrations, 1)
var i int
engine.AddInterceptor(1, models.Up, func() error {
i++
return nil
})
_, err = engine.Apply(1)
require.NoError(t, err)
require.Zero(t, i)
migrations, err = engine.driver.AppliedMigrations()
require.NoError(t, err)
require.Len(t, migrations, 2)
}, "do not call interceptor if the desired version is passed already")
}
func TestDiff(t *testing.T) {
h := newTestHelper(t)
defer h.Teardown(t)
h.AddMigration(t, "test_migration_3")
h.AddMigration(t, "test_migration_4")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// we have 2 pending migrations in the source
// the diff should return 2 migrations upwards
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 2)
}, "should have 2 migrations to apply")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// There should be no migrations downwards
// since we didn't apply any migrations yet
migrations, err := engine.Diff(models.Down)
require.NoError(t, err)
require.Empty(t, migrations)
}, "should return an empty list for down migrations")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// We apply the first migration, so we should have 1 migration pending
_, err := engine.Apply(1)
require.NoError(t, err)
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 1)
}, "there should only one migration to apply")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// Apply all remaining migrations, so we should have no migrations pending
err := engine.ApplyAll()
require.NoError(t, err)
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
require.Empty(t, migrations)
}, "there should be no migrations to apply")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// We can now have 2 migrations to rollback
migrations, err := engine.Diff(models.Down)
require.NoError(t, err)
require.Len(t, migrations, 2)
}, "should have 2 migrations to downgrade")
}
func TestOppositeMigrations(t *testing.T) {
h := newTestHelper(t).CreateBasicMigrations(t)
defer h.Teardown(t)
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// Check for applied migrations first, so we can test the opposite
// Should return empty since there are no applied migrations
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
migrations, err = engine.GetOppositeMigrations(migrations)
require.NoError(t, err)
require.Empty(t, migrations)
}, "no migrations applied empty list should be returned")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
// Apply one pending migration, should have one migration to rollback
_, err := engine.Apply(1)
require.NoError(t, err)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
rollbackMigrations, err := engine.GetOppositeMigrations(migrations)
require.NoError(t, err)
require.Len(t, migrations, 1)
require.Equal(t, models.Down, rollbackMigrations[0].Direction)
require.Equal(t, migrations[0].Name, rollbackMigrations[0].Name)
}, "one migration applied, reverse migration should be returned")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
migrations := []*models.Migration{
{Name: "202103221321_migration_1", Direction: models.Up},
{Name: "202103221400_migration_2", Direction: models.Down},
}
rollbackMigrations, err := engine.GetOppositeMigrations(migrations)
require.EqualError(t, err, "migrations have different directions")
require.Empty(t, rollbackMigrations)
}, "error when migrations have different directions")
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
err := engine.ApplyAll()
require.NoError(t, err)
migrations, err := engine.driver.AppliedMigrations()
require.NoError(t, err)
rollbackMigrations, err := engine.GetOppositeMigrations(migrations)
require.NoError(t, err)
require.Len(t, migrations, 3)
for i := range rollbackMigrations {
require.Equal(t, models.Down, rollbackMigrations[i].Direction)
require.Equal(t, migrations[i].Name, rollbackMigrations[i].Name)
}
})
}
func TestGeneratePlan(t *testing.T) {
h := newTestHelper(t).CreateBasicMigrations(t)
defer h.Teardown(t)
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
plan, err := engine.GeneratePlan(migrations, true)
require.NoError(t, err)
require.ElementsMatch(t, plan.Migrations, migrations)
require.Len(t, plan.RevertMigrations, len(migrations))
})
}
func TestApplyPlan(t *testing.T) {
h := newTestHelper(t).CreateBasicMigrations(t)
defer h.Teardown(t)
h.RunForAllDrivers(t, func(t *testing.T, engine *Morph) {
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
plan, err := engine.GeneratePlan(migrations, true)
require.NoError(t, err)
err = engine.ApplyPlan(plan)
require.NoError(t, err)
})
td := &testDriver{failAt: 3, mode: models.Up}
ts := &basicSource{
migrations: []*models.Migration{
{Name: "000001_migration_a", Direction: models.Up, Version: 1, RawName: "000001_migration_a.up.sql"},
{Name: "000002_migration_b", Direction: models.Up, Version: 2, RawName: "000002_migration_b.up.sql"},
{Name: "000003_migration_c", Direction: models.Up, Version: 3, RawName: "000003_migration_c.up.sql"},
{Name: "000004_migration_d", Direction: models.Up, Version: 4, RawName: "000004_migration_d.up.sql"},
{Name: "000001_migration_a", Direction: models.Down, Version: 1, RawName: "000001_migration_a.down.sql"},
{Name: "000002_migration_b", Direction: models.Down, Version: 2, RawName: "000002_migration_b.down.sql"},
{Name: "000003_migration_c", Direction: models.Down, Version: 3, RawName: "000003_migration_c.down.sql"},
{Name: "000004_migration_d", Direction: models.Down, Version: 4, RawName: "000004_migration_d.down.sql"},
},
}
t.Run("should rollback when error occurs/mockDriver", func(t *testing.T) {
engine, err := New(context.Background(), td, ts)
require.NoError(t, err)
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 4)
plan, err := engine.GeneratePlan(migrations, true)
require.NoError(t, err)
err = engine.ApplyPlan(plan)
require.EqualError(t, err, "could not apply migration: failed to apply migration")
migrations, err = engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 4)
})
t.Run("should not rollback all if no error/mockDriver", func(t *testing.T) {
td.failAt = 0 // don't fail
engine, err := New(context.Background(), td, ts)
require.NoError(t, err)
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 4)
plan, err := engine.GeneratePlan(migrations, true)
require.NoError(t, err)
err = engine.ApplyPlan(plan)
require.NoError(t, err)
migrations, err = engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 0)
migrations, err = engine.Diff(models.Down)
require.NoError(t, err)
require.Len(t, migrations, 4)
})
t.Run("there are already applied migrations, should only revert applied/mockDriver", func(t *testing.T) {
td.failAt = 4 // fail at version 3
td.applied = []*models.Migration{ts.migrations[0], ts.migrations[1]}
engine, err := New(context.Background(), td, ts)
require.NoError(t, err)
migrations, err := engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 2)
plan, err := engine.GeneratePlan(migrations, true)
require.NoError(t, err)
err = engine.ApplyPlan(plan)
require.EqualError(t, err, "could not apply migration: failed to apply migration")
migrations, err = engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 2)
migrations, err = engine.Diff(models.Down)
require.NoError(t, err)
require.Len(t, migrations, 2)
})
t.Run("we aim to rollback and rollback fails/mockDriver", func(t *testing.T) {
td.failAt = 2 // fail at version 3
td.mode = models.Down
td.applied = []*models.Migration{ts.migrations[0], ts.migrations[1], ts.migrations[2]}
engine, err := New(context.Background(), td, ts)
require.NoError(t, err)
migrations, err := engine.Diff(models.Down)
require.NoError(t, err)
require.Len(t, migrations, 3)
plan, err := engine.GeneratePlan(migrations, true)
require.NoError(t, err)
err = engine.ApplyPlan(plan)
require.EqualError(t, err, "could not apply migration: failed to apply migration")
migrations, err = engine.Diff(models.Down)
require.NoError(t, err)
require.Len(t, migrations, 3)
migrations, err = engine.Diff(models.Up)
require.NoError(t, err)
require.Len(t, migrations, 1)
})
}
type basicSource struct {
migrations []*models.Migration
}
func (s *basicSource) Migrations() []*models.Migration {
return s.migrations
}
type testDriver struct {
failAt int
applied []*models.Migration
mode models.Direction
}
func (d *testDriver) Ping() error {
return nil
}
func (d *testDriver) Close() error {
return nil
}
// Apply applies a migration and imitates what a real driver would do. The trick is
// it's going to throw an error if the migration version is equal to the failAt value.
func (d *testDriver) Apply(migration *models.Migration, saveVersion bool) error {
if int(migration.Version) == d.failAt && d.mode == migration.Direction {
return errors.New("failed to apply migration")
}
if !saveVersion {
return nil
}
if migration.Direction == models.Down {
for i := range d.applied {
if d.applied[i].Name == migration.Name {
d.applied = append(d.applied[:i], d.applied[i+1:]...)
break
}
}
} else {
d.applied = append(d.applied, migration)
}
return nil
}
func (d *testDriver) AppliedMigrations() ([]*models.Migration, error) {
return d.applied, nil
}
func (d *testDriver) SetConfig(key string, value interface{}) error {
return nil
}