-
Notifications
You must be signed in to change notification settings - Fork 3
/
migratorops_test.go
280 lines (246 loc) · 7.91 KB
/
migratorops_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
package pgmigrate_test
import (
"context"
"database/sql"
"testing"
"github.com/peterldowns/testy/assert"
"github.com/peterldowns/testy/check"
"github.com/peterldowns/pgmigrate"
"github.com/peterldowns/pgmigrate/internal/migrations"
"github.com/peterldowns/pgmigrate/internal/withdb"
)
func TestMarkApplied(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations := []pgmigrate.Migration{
{
ID: "0001_initial",
SQL: "CREATE TABLE users (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY);",
},
}
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
applied, err := migrator.MarkApplied(ctx, db, "0001_initial")
assert.Nil(t, err)
assert.Equal(t, 1, len(applied))
mig := applied[0]
assert.Equal(t, migrations[0], mig.Migration)
assert.Equal(t, migrations[0].MD5(), mig.Checksum)
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, plan)
return nil
})
assert.Nil(t, err)
}
func TestMarkAllApplied(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations, err := pgmigrate.Load(migrations.FS)
assert.Nil(t, err)
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
applied, err := migrator.MarkAllApplied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(applied))
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, plan)
return nil
})
assert.Nil(t, err)
}
func TestMarkUnapplied(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations := []pgmigrate.Migration{
{
ID: "0001_initial",
SQL: "CREATE TABLE users (id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY);",
},
}
// Start with all migrations applied, empty plan.
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 1, len(applied))
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, plan)
// Unapply the migration and check that it becomes present in the plan
unapplied, err := migrator.MarkUnapplied(ctx, db, migrations[0].ID)
assert.Nil(t, err)
assert.Equal(t, 1, len(unapplied))
assert.Equal(t, migrations[0].ID, unapplied[0].ID)
plan, err = migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 1, len(plan))
assert.Equal(t, migrations[0], plan[0])
return nil
})
assert.Nil(t, err)
}
func TestMarkAllUnapplied(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations, err := pgmigrate.Load(migrations.FS)
assert.Nil(t, err)
// Start with all migrations applied, empty plan.
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(applied))
plan, err := migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, plan)
// Unapply the migrations and check that they become present in the plan
unapplied, err := migrator.MarkAllUnapplied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(unapplied))
plan, err = migrator.Plan(ctx, db)
assert.Nil(t, err)
assert.Equal(t, migrations, plan)
return nil
})
assert.Nil(t, err)
}
func TestSetChecksums(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations, err := pgmigrate.Load(migrations.FS)
assert.Nil(t, err)
// Apply the migration
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(applied))
updates := []pgmigrate.ChecksumUpdate{
{
MigrationID: applied[0].ID,
NewChecksum: "veryfakechecksum",
},
{
MigrationID: applied[1].ID,
NewChecksum: "anotherfakechecksum",
},
}
assert.NotEqual(t, applied[0].Checksum, updates[0].NewChecksum)
assert.NotEqual(t, applied[1].Checksum, updates[1].NewChecksum)
updated, err := migrator.SetChecksums(ctx, db, updates...)
assert.Nil(t, err)
assert.Equal(t, 2, len(updated))
assert.Equal(t, updates[0].NewChecksum, updated[0].Checksum)
assert.Equal(t, updates[0].MigrationID, updated[0].ID)
assert.Equal(t, updates[1].NewChecksum, updated[1].Checksum)
assert.Equal(t, updates[1].MigrationID, updated[1].ID)
return nil
})
assert.Nil(t, err)
}
func TestRecalculateChecksums(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations, err := pgmigrate.Load(migrations.FS)
assert.Nil(t, err)
// Apply the migration
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(applied))
recalculated, err := migrator.RecalculateChecksums(ctx, db, migrations[0].ID)
assert.Nil(t, err)
assert.Equal(t, nil, recalculated)
updated, err := migrator.SetChecksums(ctx, db, pgmigrate.ChecksumUpdate{
MigrationID: migrations[0].ID,
NewChecksum: "somethingfake",
})
assert.Nil(t, err)
assert.Equal(t, 1, len(updated))
assert.Equal(t, migrations[0].ID, updated[0].ID)
recalculated, err = migrator.RecalculateChecksums(ctx, db, migrations[0].ID)
assert.Nil(t, err)
assert.Equal(t, 1, len(recalculated))
assert.Equal(t, migrations[0].MD5(), recalculated[0].Checksum)
return nil
})
assert.Nil(t, err)
}
func TestRecalculateAllChecksums(t *testing.T) {
t.Parallel()
ctx := context.Background()
logger := pgmigrate.NewTestLogger(t)
err := withdb.WithDB(ctx, "pgx", func(db *sql.DB) error {
migrations, err := pgmigrate.Load(migrations.FS)
assert.Nil(t, err)
// Apply the migration
migrator := pgmigrate.NewMigrator(migrations)
migrator.Logger = logger
verrs, err := migrator.Migrate(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
applied, err := migrator.Applied(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(applied))
recalculated, err := migrator.RecalculateChecksums(ctx, db, migrations[0].ID)
assert.Nil(t, err)
assert.Equal(t, nil, recalculated)
updates := []pgmigrate.ChecksumUpdate{}
for _, migration := range migrations {
updates = append(updates, pgmigrate.ChecksumUpdate{
MigrationID: migration.ID,
NewChecksum: "fakefakefake!",
})
}
updated, err := migrator.SetChecksums(ctx, db, updates...)
assert.Nil(t, err)
assert.Equal(t, 4, len(updated))
verrs, err = migrator.Verify(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(verrs))
for i, verr := range verrs {
check.Equal(t, migrations[i].ID, verr.Fields["migration_id"].(string))
check.Equal(t, migrations[i].MD5(), verr.Fields["calculated_checksum"].(string))
check.Equal(t, updates[i].NewChecksum, verr.Fields["migration_checksum_from_db"].(string))
}
recalculated, err = migrator.RecalculateAllChecksums(ctx, db)
assert.Nil(t, err)
assert.Equal(t, 4, len(recalculated))
for i, recalc := range recalculated {
check.Equal(t, migrations[i].ID, recalc.ID)
check.Equal(t, migrations[i].MD5(), recalc.Checksum)
}
verrs, err = migrator.Verify(ctx, db)
assert.Nil(t, err)
assert.Equal(t, nil, verrs)
return nil
})
assert.Nil(t, err)
}