-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
migrate_test.go
304 lines (244 loc) · 9.27 KB
/
migrate_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
package migrate //nolint:testpackage // allow direct tests
import (
"errors"
"fmt"
"testing"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestMigrate(t *testing.T) {
t.Parallel()
postgres := prepareDB()
err := postgres.Start()
if err != nil {
t.Error(err)
}
defer func() {
// If this fails to stop properly, the process will be stuck in the system background. Requires a manual kill.
if err = postgres.Stop(); err != nil {
t.Error(err)
}
}()
err = performMigrateWithMigrations(t, Options{})
assert.NoError(t, err, "Migrate Default")
err = performMigrateWithMigrations(t, Options{RefreshSchema: true})
assert.NoError(t, err, "Refresh Schema")
err = performMigrateWithMigrations(t, Options{SchemasToRefresh: []string{"public", "test"}})
assert.NoError(t, err, "Refresh Schemas 'public' and 'test'")
err = performMigrateWithMigrations(t, Options{VersionNumberToApply: 2})
assert.NoError(t, err, "Migrate Down 2")
err = performMigrateWithMigrations(t, Options{VersionNumberToApply: 1})
assert.NoError(t, err, "Migrate Down 1")
err = performMigrateWithMigrations(t, Options{VersionNumberToApply: 2})
assert.NoError(t, err, "Migrate Forward 2")
err = performMigrateWithMigrations(t, Options{VersionNumberToApply: 3})
assert.NoError(t, err, "Migrate Forward 3")
err = performMigrateWithMigrations(t, Options{VersionNumberToApply: 2, ForceVersionWithoutMigrations: true})
assert.NoError(t, err, "Force Incorrect Version")
err = performMigrateWithMigrations(t, Options{PrintInfoAndExit: true})
assert.NoError(t, err, "Print Info")
err = performMigrateWithMigrations(t, Options{VersionNumberToApply: 3, ForceVersionWithoutMigrations: true})
assert.NoError(t, err, "Force Correct Version")
err = performMigrate(t, Options{})
assert.NoError(t, err, "No Migrations To apply")
err = performMigrateWithMigrations(t, Options{VersionNumberToApply: 0, ForceVersionWithoutMigrations: true})
assert.ErrorIs(t, err, errNoMigrationVersion, "Force Version Is Missing")
}
func performMigrateWithMigrations(t *testing.T, options Options) error {
t.Helper()
migrations = prepareMigrations()
return performMigrate(t, options)
}
func performMigrate(t *testing.T, options Options) error {
t.Helper()
options.DatabaseURI = "postgres://migrate:migrate@localhost:54320/migrate?sslmode=disable"
migrate, err := New(options)
if err != nil {
t.Error(err)
}
err = migrate.Migrate()
if err != nil {
return fmt.Errorf("failed to execute migration: %w", err)
}
return nil
}
//nolint:funlen // allow longer function
func TestMigrateErrors(t *testing.T) {
t.Parallel()
repo := new(mockRepository)
someErr := errors.New("test-err") //nolint:goerr113 // used for tests only
repo.On("EnsureMigrationTable").Return(someErr).Once()
err := performMigrateTaskWithMigrations(t, repo, Options{})
assert.ErrorIs(t, err, someErr, "Error On MigrationTable")
repo.On("DropSchema", "public").Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{RefreshSchema: true})
assert.ErrorIs(t, err, someErr, "Error On DropSchema")
repo.On("DropSchema", "public").Return(nil).Once()
repo.On("EnsureMigrationTable").Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{RefreshSchema: true})
assert.ErrorIs(t, err, someErr, "Error On EnsureMigrationTable After DropSchema")
repo.On("EnsureMigrationTable").Return(nil).Once()
repo.On("RemoveMigrationsAfter", mock.Anything).Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{ForceVersionWithoutMigrations: true, VersionNumberToApply: 3})
assert.ErrorIs(t, err, someErr, "Error On RemoveMigrationsAfter")
repo.On("EnsureMigrationTable").Return(nil).Once()
repo.On("RemoveMigrationsAfter", mock.Anything).Return(nil).Once()
repo.On("InsertMigration", mock.Anything).Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{ForceVersionWithoutMigrations: true, VersionNumberToApply: 3})
assert.ErrorIs(t, err, someErr, "Error On InsertMigration")
repo.On("EnsureMigrationTable").Return(nil).Once()
repo.On("GetLatestMigrationNumber").Return(uint(0), someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{})
assert.ErrorIs(t, err, someErr, "Error On GetLatestMigrationNumber")
repo.On("EnsureMigrationTable").Return(nil).Once()
repo.On("GetLatestMigrationNumber").Return(uint(3), nil).Once()
repo.On("ApplyMigration", mock.Anything, mock.Anything).Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{VersionNumberToApply: 2})
assert.ErrorIs(t, err, someErr, "Error On BackwardMigration")
repo.On("EnsureMigrationTable").Return(nil).Once()
repo.On("GetLatestMigrationNumber").Return(uint(3), nil).Once()
repo.On("ApplyMigration", mock.Anything).Return(nil).Once()
repo.On("RemoveMigrationsAfter", mock.Anything).Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{VersionNumberToApply: 2})
assert.ErrorIs(t, err, someErr, "Error On RemoveMigrationsAfter")
repo.On("EnsureMigrationTable").Return(nil).Once()
repo.On("GetLatestMigrationNumber").Return(uint(0), nil).Once()
repo.On("ApplyMigration", mock.Anything, mock.Anything).Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{})
assert.ErrorIs(t, err, someErr, "Error On ForwardMigration")
repo.On("EnsureMigrationTable").Return(nil).Once()
repo.On("GetLatestMigrationNumber").Return(uint(0), nil).Once()
repo.On("ApplyMigration", mock.Anything).Return(nil).Once()
repo.On("InsertMigration", mock.Anything).Return(someErr).Once()
err = performMigrateTaskWithMigrations(t, repo, Options{})
assert.ErrorIs(t, err, someErr, "Error On InsertMigration")
}
func performMigrateTaskWithMigrations(t *testing.T, repo repository, options Options) error {
t.Helper()
return performMigrateTask(t, repo, options, prepareMigrations())
}
func performMigrateTask(t *testing.T, repo repository, options Options, migrations []*Migration) error {
t.Helper()
options.LogInfo = func(format string, args ...interface{}) {
//nolint:forbidigo // allow in tests
fmt.Printf(format+"\n", args...)
}
task := migrationTask{
migrations: mapMigrations(migrations),
repo: repo,
opt: options,
}
return task.migrate()
}
func TestNew(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
migrations []*Migration
expectedErr error
}{
{
name: "missing migrations",
migrations: []*Migration{{Name: "Test Migration", Number: 1, Up: nil, Down: nil}},
expectedErr: errMigrationIsMissing,
},
{
name: "duplicate migrations",
migrations: []*Migration{
{Name: "Test Migration", Number: 1, Up: func(tx Tx) error { return nil }, Down: nil},
{Name: "Test Migration 2", Number: 1, Up: func(tx Tx) error { return nil }, Down: nil},
},
expectedErr: errDuplicateMigrationVersion,
},
{
name: "empty name",
migrations: []*Migration{
{Name: "", Number: 1, Up: func(tx Tx) error { return nil }, Down: nil},
},
expectedErr: errMigrationNameCannotBeEmpty,
},
{
name: "success",
migrations: []*Migration{
{Name: "Test Migration", Number: 1, Up: func(tx Tx) error { return nil }, Down: nil},
{Name: "Test Migration 2", Number: 2, Up: func(tx Tx) error { return nil }, Down: nil},
},
expectedErr: nil,
},
}
var opt Options
for _, testCase := range testCases {
migrations = testCase.migrations
_, err := New(opt)
assert.ErrorIs(t, err, testCase.expectedErr, testCase.name)
}
}
func prepareDB() *embeddedpostgres.EmbeddedPostgres {
return embeddedpostgres.NewDatabase(embeddedpostgres.DefaultConfig().
Username("migrate").
Password("migrate").
Database("migrate").
Version(embeddedpostgres.V14).
Port(54320))
}
//nolint:funlen // allow longer function
func prepareMigrations() []*Migration {
migrations := []*Migration{
{
Name: "Create Users Table",
Number: 1,
Up: func(tx Tx) error {
_, err := tx.Exec("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)")
if err != nil {
return fmt.Errorf("failed to create users table: %w", err)
}
return nil
},
Down: func(tx Tx) error {
_, err := tx.Exec("DROP TABLE users")
if err != nil {
return fmt.Errorf("failed to drop users table: %w", err)
}
return nil
},
},
{
Name: "Add Email For Users",
Number: 2,
Up: func(tx Tx) error {
_, err := tx.Exec("ALTER TABLE users ADD COLUMN email TEXT")
if err != nil {
return fmt.Errorf("failed to alter users table to add email: %w", err)
}
return nil
},
Down: func(tx Tx) error {
_, err := tx.Exec("ALTER TABLE users DROP COLUMN email")
if err != nil {
return fmt.Errorf("failed to drop email column for users table: %w", err)
}
return nil
},
},
{
Name: "Add Address For Users",
Number: 3,
Up: func(tx Tx) error {
_, err := tx.Exec("ALTER TABLE users ADD COLUMN address TEXT")
if err != nil {
return fmt.Errorf("failed to alter users table to add address: %w", err)
}
return nil
},
Down: func(tx Tx) error {
_, err := tx.Exec("ALTER TABLE users DROP COLUMN address")
if err != nil {
return fmt.Errorf("failed to drop address column for users table: %w", err)
}
return nil
},
},
}
return migrations
}