forked from go-gormigrate/gormigrate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gormigrate_test.go
438 lines (374 loc) · 11 KB
/
gormigrate_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
package gormigrate
import (
"errors"
"fmt"
"os"
"testing"
"github.com/misterEggroll/gorm"
_ "github.com/joho/godotenv/autoload"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var databases []database
type database struct {
name string
connEnv string
}
var migrations = []*Migration{
{
ID: "201608301400",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&Person{}).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("people").Error
},
},
{
ID: "201608301430",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&Pet{}).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("pets").Error
},
},
}
var extendedMigrations = append(migrations, &Migration{
ID: "201807221927",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&Book{}).Error
},
Rollback: func(tx *gorm.DB) error {
return tx.DropTable("books").Error
},
})
var failingMigration = []*Migration{
{
ID: "201904231300",
Migrate: func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Book{}).Error; err != nil {
return err
}
return errors.New("this transaction should be rolled back")
},
Rollback: func(tx *gorm.DB) error {
return nil
},
},
}
type Person struct {
gorm.Model
Name string
}
type Pet struct {
gorm.Model
Name string
PersonID int
}
type Book struct {
gorm.Model
Name string
PersonID int
}
func TestMigration(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, migrations)
err := m.Migrate()
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))
err = m.RollbackLast()
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
err = m.RollbackLast()
assert.NoError(t, err)
assert.False(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.Equal(t, 0, tableCount(t, db, "migrations"))
})
}
func TestMigrateTo(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, extendedMigrations)
err := m.MigrateTo("201608301430")
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.False(t, db.HasTable(&Book{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))
})
}
func TestRollbackTo(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, extendedMigrations)
// First, apply all migrations.
err := m.Migrate()
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.True(t, db.HasTable(&Book{}))
assert.Equal(t, 3, tableCount(t, db, "migrations"))
// Rollback to the first migration: only the last 2 migrations are expected to be rolled back.
err = m.RollbackTo("201608301400")
assert.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.False(t, db.HasTable(&Book{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
})
}
// If initSchema is defined, but no migrations are provided,
// then initSchema is executed.
func TestInitSchemaNoMigrations(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, []*Migration{})
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Person{}).Error; err != nil {
return err
}
if err := tx.AutoMigrate(&Pet{}).Error; err != nil {
return err
}
return nil
})
assert.NoError(t, m.Migrate())
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
})
}
// If initSchema is defined and migrations are provided,
// then initSchema is executed and the migration IDs are stored,
// even though the relevant migrations are not applied.
func TestInitSchemaWithMigrations(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, migrations)
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Person{}).Error; err != nil {
return err
}
return nil
})
assert.NoError(t, m.Migrate())
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.Equal(t, 3, tableCount(t, db, "migrations"))
})
}
// If the schema has already been initialised,
// then initSchema() is not executed, even if defined.
func TestInitSchemaAlreadyInitialised(t *testing.T) {
type Car struct {
gorm.Model
}
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, []*Migration{})
// Migrate with empty initialisation
m.InitSchema(func(tx *gorm.DB) error {
return nil
})
assert.NoError(t, m.Migrate())
// Then migrate again, this time with a non empty initialisation
// This second initialisation should not happen!
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Car{}).Error; err != nil {
return err
}
return nil
})
assert.NoError(t, m.Migrate())
assert.False(t, db.HasTable(&Car{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
})
}
// If the schema has not already been initialised,
// but any other migration has already been applied,
// then initSchema() is not executed, even if defined.
func TestInitSchemaExistingMigrations(t *testing.T) {
type Car struct {
gorm.Model
}
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, migrations)
// Migrate without initialisation
assert.NoError(t, m.Migrate())
// Then migrate again, this time with a non empty initialisation
// This initialisation should not happen!
m.InitSchema(func(tx *gorm.DB) error {
if err := tx.AutoMigrate(&Car{}).Error; err != nil {
return err
}
return nil
})
assert.NoError(t, m.Migrate())
assert.False(t, db.HasTable(&Car{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))
})
}
func TestMigrationIDDoesNotExist(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, DefaultOptions, migrations)
assert.Equal(t, ErrMigrationIDDoesNotExist, m.MigrateTo("1234"))
assert.Equal(t, ErrMigrationIDDoesNotExist, m.RollbackTo("1234"))
assert.Equal(t, ErrMigrationIDDoesNotExist, m.MigrateTo(""))
assert.Equal(t, ErrMigrationIDDoesNotExist, m.RollbackTo(""))
})
}
func TestMissingID(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
migrationsMissingID := []*Migration{
{
Migrate: func(tx *gorm.DB) error {
return nil
},
},
}
m := New(db, DefaultOptions, migrationsMissingID)
assert.Equal(t, ErrMissingID, m.Migrate())
})
}
func TestReservedID(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
migrationsReservedID := []*Migration{
{
ID: "SCHEMA_INIT",
Migrate: func(tx *gorm.DB) error {
return nil
},
},
}
m := New(db, DefaultOptions, migrationsReservedID)
_, isReservedIDError := m.Migrate().(*ReservedIDError)
assert.True(t, isReservedIDError)
})
}
func TestDuplicatedID(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
migrationsDuplicatedID := []*Migration{
{
ID: "201705061500",
Migrate: func(tx *gorm.DB) error {
return nil
},
},
{
ID: "201705061500",
Migrate: func(tx *gorm.DB) error {
return nil
},
},
}
m := New(db, DefaultOptions, migrationsDuplicatedID)
_, isDuplicatedIDError := m.Migrate().(*DuplicatedIDError)
assert.True(t, isDuplicatedIDError)
})
}
func TestEmptyMigrationList(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
t.Run("with empty list", func(t *testing.T) {
m := New(db, DefaultOptions, []*Migration{})
err := m.Migrate()
assert.Equal(t, ErrNoMigrationDefined, err)
})
t.Run("with nil list", func(t *testing.T) {
m := New(db, DefaultOptions, nil)
err := m.Migrate()
assert.Equal(t, ErrNoMigrationDefined, err)
})
})
}
func TestMigration_WithUseTransactions(t *testing.T) {
options := DefaultOptions
options.UseTransaction = true
forEachDatabase(t, func(db *gorm.DB) {
m := New(db, options, migrations)
err := m.Migrate()
require.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.True(t, db.HasTable(&Pet{}))
assert.Equal(t, 2, tableCount(t, db, "migrations"))
err = m.RollbackLast()
require.NoError(t, err)
assert.True(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.Equal(t, 1, tableCount(t, db, "migrations"))
err = m.RollbackLast()
require.NoError(t, err)
assert.False(t, db.HasTable(&Person{}))
assert.False(t, db.HasTable(&Pet{}))
assert.Equal(t, 0, tableCount(t, db, "migrations"))
}, "postgres", "sqlite3", "mssql")
}
func TestMigration_WithUseTransactionsShouldRollback(t *testing.T) {
options := DefaultOptions
options.UseTransaction = true
forEachDatabase(t, func(db *gorm.DB) {
assert.True(t, true)
m := New(db, options, failingMigration)
// Migration should return an error and not leave around a Book table
err := m.Migrate()
assert.Error(t, err)
assert.False(t, db.HasTable(&Book{}))
}, "postgres", "sqlite3", "mssql")
}
func TestUnexpectedMigrationEnabled(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
options := DefaultOptions
options.ValidateUnknownMigrations = true
m := New(db, options, migrations)
// Migrate without initialisation
assert.NoError(t, m.Migrate())
// Try with fewer migrations. Should fail as we see a migration in the db that
// we don't recognise any more
n := New(db, DefaultOptions, migrations[:1])
assert.Equal(t, ErrUnknownPastMigration, n.Migrate())
})
}
func TestUnexpectedMigrationDisabled(t *testing.T) {
forEachDatabase(t, func(db *gorm.DB) {
options := DefaultOptions
options.ValidateUnknownMigrations = false
m := New(db, options, migrations)
// Migrate without initialisation
assert.NoError(t, m.Migrate())
// Try with fewer migrations. Should pass as we see a migration in the db that
// we don't recognise any more, but the validation defaults off
n := New(db, DefaultOptions, migrations[:1])
assert.NoError(t, n.Migrate())
})
}
func tableCount(t *testing.T, db *gorm.DB, tableName string) (count int) {
assert.NoError(t, db.Table(tableName).Count(&count).Error)
return
}
func forEachDatabase(t *testing.T, fn func(database *gorm.DB), dialects ...string) {
if len(databases) == 0 {
panic("No database choosen for testing!")
}
for _, database := range databases {
if len(dialects) > 0 && !contains(dialects, database.name) {
t.Skip(fmt.Sprintf("test is not supported by [%s] dialect", database.name))
}
// Ensure defers are not stacked up for each DB
func() {
db, err := gorm.Open(database.name, os.Getenv(database.connEnv))
require.NoError(t, err, "Could not connect to database %s, %v", database.name, err)
defer db.Close()
// ensure tables do not exists
assert.NoError(t, db.DropTableIfExists("migrations", "people", "pets").Error)
fn(db)
}()
}
}
func contains(haystack []string, needle string) bool {
for _, straw := range haystack {
if straw == needle {
return true
}
}
return false
}