-
Notifications
You must be signed in to change notification settings - Fork 5
/
migrator_test.go
323 lines (281 loc) · 10.5 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
package schema
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"
)
// TestCreateMigrationsTable ensures that each dialect and test database can
// successfully create the schema_migrations table.
func TestCreateMigrationsTable(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
migrator := makeTestMigrator(WithDialect(tdb.Dialect))
err := tdb.Dialect.CreateMigrationsTable(migrator.ctx, db, migrator.QuotedTableName())
if err != nil {
t.Errorf("Error occurred when creating migrations table: %s", err)
}
// Test that we can re-run it again with no error
err = tdb.Dialect.CreateMigrationsTable(migrator.ctx, db, migrator.QuotedTableName())
if err != nil {
t.Errorf("Calling createMigrationsTable a second time failed: %s", err)
}
})
}
// TestLockAndUnlock tests the Lock and Unlock mechanisms of each dialect and
// test database in isolation from any migrations actually being run.
func TestLockAndUnlock(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
migrator := makeTestMigrator(WithDialect(tdb.Dialect))
if _, isLocker := tdb.Dialect.(Locker); isLocker {
err := migrator.lock(db)
if err != nil {
t.Fatal(err)
}
err = migrator.unlock(db)
if err != nil {
t.Fatal(err)
}
}
})
}
// TestApplyInLexicalOrder ensures that each dialect runs migrations in their
// lexical order rather than the order they were provided in the slice. This is
// also the primary test to assert that the data in the tracking table is
// all correct.
func TestApplyInLexicalOrder(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
start := time.Now().Truncate(time.Second) // MySQL has only second accuracy, so we need start/end to span 1 second
tableName := "lexical_order_migrations"
migrator := NewMigrator(WithDialect(tdb.Dialect), WithTableName(tableName))
err := migrator.Apply(db, unorderedMigrations())
if err != nil {
t.Error(err)
}
end := time.Now().Add(time.Second).Truncate(time.Second) // MySQL has only second accuracy, so we need start/end to span 1 second
applied, err := migrator.GetAppliedMigrations(db)
if err != nil {
t.Error(err)
}
if len(applied) != 3 {
t.Errorf("Expected exactly 2 applied migrations. Got %d", len(applied))
}
firstMigration := applied["2021-01-01 001"]
if firstMigration == nil {
t.Fatal("Missing first migration")
}
if firstMigration.Checksum == "" {
t.Error("Expected non-blank Checksum value after successful migration")
}
if firstMigration.ExecutionTimeInMillis < 1 {
t.Errorf("Expected ExecutionTimeInMillis of %s to be tracked. Got %d", firstMigration.ID, firstMigration.ExecutionTimeInMillis)
}
// Put value in consistent timezone to aid error message readability
appliedAt := firstMigration.AppliedAt.Round(time.Second)
if appliedAt.IsZero() || appliedAt.Before(start) || appliedAt.After(end) {
t.Errorf("Expected AppliedAt between %s and %s, got %s", start, end, appliedAt)
}
assertZonesMatch(t, start, appliedAt)
secondMigration := applied["2021-01-01 002"]
if secondMigration == nil {
t.Fatal("Missing second migration")
} else if secondMigration.Checksum == "" {
t.Fatal("Expected checksum to get populated when migration ran")
}
if firstMigration.AppliedAt.After(secondMigration.AppliedAt) {
t.Errorf("Expected migrations to run in lexical order, but first migration ran at %s and second one ran at %s", firstMigration.AppliedAt, secondMigration.AppliedAt)
}
})
}
// TestFailedMigration ensures that a migration with a syntax error triggers
// an expected error when Apply() is run. This test is run on every dialect
// and every test database instance
func TestFailedMigration(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
tableName := time.Now().Format(time.RFC3339Nano)
migrator := NewMigrator(WithTableName(tableName), WithDialect(tdb.Dialect))
migrations := []*Migration{
{
ID: "2019-01-01 Bad Migration",
Script: "CREATE TIBBLE bad_table_name (id INTEGER NOT NULL PRIMARY KEY)",
},
}
err := migrator.Apply(db, migrations)
expectErrorContains(t, err, "TIBBLE")
query := "SELECT * FROM " + migrator.QuotedTableName()
rows, _ := db.Query(query)
// We expect either an error (because the transaction was rolled back
// and the table no longer exists)... or a query with no results
if rows != nil {
if rows.Next() {
t.Error("Record was inserted in tracking table even though the migration failed")
}
_ = rows.Close()
}
})
}
// TestSimultaneousApply creates multiple Migrators and multiple distinct
// connections to each test database and attempts to call .Apply() on them all
// concurrently. The migrations include an INSERT statement, which allows us
// to count to ensure that each unique migration was only run once.
func TestSimultaneousApply(t *testing.T) {
concurrency := 4
dataTable := fmt.Sprintf("data%d", rand.Int()) // #nosec we don't need cryptographic security here
migrationsTable := fmt.Sprintf("Migrations %s", time.Now().Format(time.RFC3339Nano))
sharedMigrations := []*Migration{
{
ID: "2020-05-02 Create Data Table",
Script: fmt.Sprintf(`CREATE TABLE %s (number INTEGER)`, dataTable),
},
{
ID: "2020-05-03 Add Initial Record",
Script: fmt.Sprintf(`INSERT INTO %s (number) VALUES (1)`, dataTable),
},
}
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
var wg sync.WaitGroup
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(i int) {
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
migrator := NewMigrator(WithDialect(tdb.Dialect), WithTableName(migrationsTable))
err := migrator.Apply(db, sharedMigrations)
if err != nil {
t.Error(err)
}
_, err = db.Exec(fmt.Sprintf("INSERT INTO %s (number) VALUES (1)", dataTable))
if err != nil {
t.Error(err)
}
wg.Done()
}(i)
}
wg.Wait()
// We expect concurrency + 1 rows in the data table
// (1 from the migration, and one each for the
// goroutines which ran Apply and then did an
// insert afterwards)
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
count := 0
row := db.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM %s", dataTable))
err := row.Scan(&count)
if err != nil {
t.Error(err)
}
if count != concurrency+1 {
t.Errorf("Expected to get %d rows in %s table. Instead got %d", concurrency+1, dataTable, count)
}
})
}
// TestMultiSchemaSupport ensures that each dialect and test database support
// having multiple tracking tables each tracking separate sets of migrations.
//
// The test scenario here is one set of "music" migrations which deal with
// artists, albums and tracks, and a separate set of "contacts" migrations
// which deal with contacts, phone_numbers, and addresses.
func TestMultiSchemaSupport(t *testing.T) {
withEachTestDB(t, func(t *testing.T, tdb *TestDB) {
music := NewMigrator(WithDialect(tdb.Dialect), WithTableName("music_migrations"))
contacts := NewMigrator(WithDialect(tdb.Dialect), WithTableName("contacts_migrations"))
// Use the same connection for both sets of migrations
db := tdb.Connect(t)
defer func() { _ = db.Close() }()
// Apply the Music migrations
err := music.Apply(db, testMigrations(t, "music"))
if err != nil {
t.Errorf("Failed to apply music migrations: %s", err)
}
// ... then the Contacts Migrations
err = contacts.Apply(db, testMigrations(t, "contacts"))
if err != nil {
t.Errorf("Failed to apply contact migrations: %s", err)
}
// Then run a SELECT COUNT(*) query on each table to ensure that all of the
// expected tables are co-existing in the same database and that they all
// contain the expected number of rows (this approach is admittedly odd,
// but it relies only on ANSI SQL code, so it should run on any SQL database).
expectedRowCounts := map[string]int{
"music_migrations": 3,
"contacts_migrations": 3,
"contacts": 1,
"phone_numbers": 3,
"addresses": 2,
"artists": 0,
"albums": 0,
"tracks": 0,
}
for table, expectedRowCount := range expectedRowCounts {
qtn := tdb.Dialect.QuotedTableName("", table)
actualCount := -1 // Don't initialize to 0 because that's an expected value
query := fmt.Sprintf("SELECT COUNT(*) FROM %s", qtn)
rows, err := db.Query(query)
if err != nil {
t.Error(err)
}
if rows != nil && rows.Next() {
err = rows.Scan(&actualCount)
if err != nil {
t.Error(err)
}
} else {
t.Errorf("Expected rows")
}
if actualCount != expectedRowCount {
t.Errorf("Expected %d rows in table %s. Got %d", expectedRowCount, qtn, actualCount)
}
}
})
}
// TestRunFailure ensures that a low-level connection or query-related failure
// triggers an expected error.
func TestRunFailure(t *testing.T) {
bq := BadQueryer{}
m := makeTestMigrator()
err := m.run(bq, testMigrations(t, "useless-ansi"))
expectErrorContains(t, err, "SELECT id, checksum")
err = m.run(nil, testMigrations(t, "useless-ansi"))
if err != ErrNilDB {
t.Errorf("Expected error '%s'. Got '%v'.", ErrNilDB, err)
}
}
func TestNewMigratorApplyChain(t *testing.T) {
// This is a compilability test... it is here to confirm that
// NewMigrator()'s return value can have Apply() called on it.
_ = NewMigrator().Apply(nil, testMigrations(t, "useless-ansi"))
}
// makeTestMigrator is a utility function which produces a migrator with an
// isolated environment (isolated due to a unique name for the migration
// tracking table).
func makeTestMigrator(options ...Option) *Migrator {
tableName := time.Now().Format(time.RFC3339Nano)
options = append(options, WithTableName(tableName))
return NewMigrator(options...)
}
func testMigrations(t *testing.T, dirName string) []*Migration {
path := fmt.Sprintf("test-migrations/%s", dirName)
migrations, err := MigrationsFromDirectoryPath(path)
if err != nil {
t.Fatalf("Failed to load test migrations from '%s'", path)
}
return migrations
}
// assertZonesMatch accepts two Times and fails the test if their time zones
// don't match.
func assertZonesMatch(t *testing.T, expected, actual time.Time) {
t.Helper()
expectedName, expectedOffset := expected.Zone()
actualName, actualOffset := actual.Zone()
if expectedOffset != actualOffset {
t.Errorf("Expected Zone '%s' with offset %d. Got Zone '%s' with offset %d", expectedName, expectedOffset, actualName, actualOffset)
}
}