-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigration_runner.go
559 lines (473 loc) · 13.1 KB
/
migration_runner.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package pgutil
import (
"context"
"errors"
"slices"
"time"
"github.com/go-nacelle/log/v2"
"github.com/go-nacelle/nacelle/v2"
"github.com/jackc/pgconn"
)
type Runner struct {
db DB
logger nacelle.Logger
definitions []Definition
locker *TransactionalLocker
}
func NewMigrationRunner(db DB, reader MigrationReader, logger nacelle.Logger) (*Runner, error) {
definitions, err := ReadMigrations(reader)
if err != nil {
return nil, err
}
locker, err := NewTransactionalLocker(db, StringKey("nacelle/pgutil.migration-runner"))
if err != nil {
return nil, err
}
return &Runner{
db: db,
logger: logger,
definitions: definitions,
locker: locker,
}, nil
}
func (r *Runner) Definitions() []Definition {
return r.definitions
}
func (r *Runner) ApplyAll(ctx context.Context) error {
return r.apply(ctx, r.definitions)
}
func (r *Runner) Apply(ctx context.Context, id int) error {
for i, definition := range r.definitions {
if definition.ID == id {
return r.apply(ctx, r.definitions[:i+1])
}
}
return errors.New("migration not found")
}
func (r *Runner) apply(ctx context.Context, definitions []Definition) error {
if err := r.ensureMigrationLogsTable(ctx); err != nil {
return err
}
for {
upToDate, cicDefinition, err := r.applyDefinitions(ctx, definitions, false)
if err != nil || upToDate {
return err
}
if cicDefinition != nil {
if err := r.applyConcurrentIndexCreation(ctx, *cicDefinition); err != nil {
return err
}
}
}
}
func (r *Runner) Undo(ctx context.Context, id int) error {
if err := r.ensureMigrationLogsTable(ctx); err != nil {
return err
}
for i, definition := range r.definitions {
if definition.ID == id {
definitions := slices.Clone(r.definitions[i:])
slices.Reverse(definitions)
// NOTE: CIC are illegal in down migrations; perform this in one shot
_, _, err := r.applyDefinitions(ctx, definitions, true)
return err
}
}
return errors.New("migration not found")
}
func (r *Runner) ensureMigrationLogsTable(ctx context.Context) error {
for _, query := range []string{
"CREATE TABLE IF NOT EXISTS migration_logs(id SERIAL PRIMARY KEY)",
"ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS migration_id integer NOT NULL",
"ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS reverse bool NOT NULL",
"ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS started_at timestamptz NOT NULL DEFAULT current_timestamp",
"ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS last_heartbeat_at timestamptz",
"ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS finished_at timestamptz",
"ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS success boolean",
"ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS error_message text",
} {
if err := r.db.Exec(ctx, RawQuery(query)); err != nil {
return err
}
}
return nil
}
func (r *Runner) applyDefinitions(ctx context.Context, definitions []Definition, reverse bool) (upToDate bool, cicDefinition *Definition, _ error) {
err := r.locker.WithLock(ctx, StringKey("ddl"), func(_ DB) (err error) {
migrationLogs, err := r.MigrationLogs(ctx)
if err != nil {
return err
}
applied := map[int]struct{}{}
for _, log := range migrationLogs {
if log.Success != nil && *log.Success && !log.Reverse {
applied[log.MigrationID] = struct{}{}
}
}
var migrationsToApply []Definition
for _, definition := range definitions {
if _, ok := applied[definition.ID]; ok == reverse {
migrationsToApply = append(migrationsToApply, definition)
}
}
if len(migrationsToApply) == 0 {
r.logger.Info("Migrations are in expected state")
upToDate = true
return nil
}
for _, definition := range migrationsToApply {
if definition.IndexMetadata != nil && !reverse {
// We can't perform CIC while holding a lock or else we'll deadlock.
// Capture this definition to be applied outside of the lock we're holding.
// We can skip this check for reverse application as CIC are illegal in down migrations.
cicDefinition = &definition
return nil
}
if err := r.withMigrationLog(ctx, definition, reverse, func(_ int) error {
query, direction := definition.UpQuery, "up"
if reverse {
query, direction = definition.DownQuery, "down"
}
logger := r.logger.WithFields(log.LogFields{
"id": definition.ID,
"name": definition.Name,
"direction": direction,
})
logger.Info("Applying migration")
if err := r.db.WithTransaction(ctx, func(tx DB) error { return tx.Exec(ctx, query) }); err != nil {
logger.ErrorWithFields(log.LogFields{"error": err}, "Failed to apply migration")
return err
}
return nil
}); err != nil {
return err
}
}
return nil
})
return upToDate, cicDefinition, err
}
func (r *Runner) applyConcurrentIndexCreation(ctx context.Context, definition Definition) error {
tableName := definition.IndexMetadata.TableName
indexName := definition.IndexMetadata.IndexName
logger := r.logger.WithFields(log.LogFields{
"id": definition.ID,
"name": definition.Name,
"direction": "up",
"tableName": tableName,
"indexName": indexName,
})
logger.Info("Handling concurrent index creation")
indexPollLoop:
for i := 0; ; i++ {
if i != 0 {
if err := wait(ctx, time.Second*5); err != nil {
return err
}
}
indexStatus, exists, err := r.getIndexStatus(ctx, tableName, indexName)
if err != nil {
return err
}
if exists {
logger.InfoWithFields(log.LogFields{
"phase": deref(indexStatus.Phase),
"lockersTotal": deref(indexStatus.LockersTotal),
"lockersDone": deref(indexStatus.LockersDone),
"blocksTotal": deref(indexStatus.BlocksTotal),
"blocksDone": deref(indexStatus.BlocksDone),
"tuplesTotal": deref(indexStatus.TuplesTotal),
"tuplesDone": deref(indexStatus.TuplesDone),
}, "Index exists")
if indexStatus.IsValid {
logger.Info("Index is valid")
if recheck, err := r.handleValidIndex(ctx, definition); err != nil {
return err
} else if recheck {
continue indexPollLoop
} else {
return nil
}
}
if indexStatus.Phase != nil {
continue indexPollLoop
}
logger.Info("Dropping invalid index")
// NOTE: Must interpolate identifier here as placeholders aren't valid in this position.
if err := r.db.Exec(ctx, queryf(`DROP INDEX IF EXISTS %s`, indexName)); err != nil {
return err
}
}
logger.Info("Creating index")
if raceDetected, err := r.createIndexConcurrently(ctx, definition); err != nil {
return err
} else if raceDetected {
continue indexPollLoop
}
return nil
}
}
func (r *Runner) handleValidIndex(ctx context.Context, definition Definition) (recheck bool, _ error) {
err := r.locker.WithLock(ctx, StringKey("log"), func(tx DB) error {
log, ok, err := r.getLogForConcurrentIndex(ctx, tx, definition.ID)
if err != nil {
return err
}
if !ok {
if err := tx.Exec(ctx, Query(`
INSERT INTO migration_logs (migration_id, reverse, finished_at, success)
VALUES ({:id}, false, current_timestamp, true)
`,
Args{"id": definition.ID},
)); err != nil {
return err
}
return nil
}
if log.Success != nil {
if *log.Success {
return nil
}
return errors.New(*log.ErrorMessage)
}
if time.Since(log.LastHeartbeatAt) >= time.Second*15 {
recheck = true
return nil
}
if err := tx.Exec(ctx, Query(`
UPDATE migration_logs
SET success = true, finished_at = current_timestamp
WHERE id = {:id}
`,
Args{"id": log.ID},
)); err != nil {
return err
}
return nil
})
return recheck, err
}
func (r *Runner) createIndexConcurrently(ctx context.Context, definition Definition) (raceDetected bool, _ error) {
err := r.withMigrationLog(ctx, definition, false, func(id int) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
for {
if err := r.db.Exec(ctx, Query(`
UPDATE migration_logs
SET last_heartbeat_at = current_timestamp
WHERE id = {:id}
`,
Args{"id": id},
)); err != nil && ctx.Err() != context.Canceled {
r.logger.ErrorWithFields(log.LogFields{
"error": err,
}, "Failed to update heartbeat")
}
if err := wait(ctx, time.Second*5); err != nil {
break
}
}
}()
if err := r.db.Exec(ctx, definition.UpQuery); err != nil {
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) || pgErr.Code == "42P07" {
return err
}
if err := r.db.Exec(ctx, Query(
`DELETE FROM migration_logs WHERE id = {:id}`,
Args{"id": id},
)); err != nil {
return err
}
raceDetected = true
}
return nil
})
return raceDetected, err
}
//
//
type MigrationLog struct {
MigrationID int
Reverse bool
Success *bool
ErrorMessage *string
}
var scanMigrationLogs = NewSliceScanner(func(s Scanner) (ms MigrationLog, _ error) {
err := s.Scan(&ms.MigrationID, &ms.Reverse, &ms.Success, &ms.ErrorMessage)
return ms, err
})
func (r *Runner) MigrationLogs(ctx context.Context) (map[int]MigrationLog, error) {
if err := r.ensureMigrationLogsTable(ctx); err != nil {
return nil, err
}
migrationLogs, err := scanMigrationLogs(r.db.Query(ctx, RawQuery(`
WITH ranked_migration_logs AS (
SELECT
l.*,
ROW_NUMBER() OVER (PARTITION BY migration_id ORDER BY started_at DESC) AS rank
FROM migration_logs l
)
SELECT
migration_id,
reverse,
success,
error_message
FROM ranked_migration_logs
WHERE rank = 1
ORDER BY migration_id
`)))
if err != nil {
return nil, err
}
logMap := map[int]MigrationLog{}
for _, state := range migrationLogs {
logMap[state.MigrationID] = state
}
return logMap, nil
}
func (r *Runner) WriteMigrationLog(ctx context.Context, id int) error {
if err := r.ensureMigrationLogsTable(ctx); err != nil {
return err
}
var definition *Definition
for _, def := range r.definitions {
if def.ID == id {
definition = &def
break
}
}
if definition == nil {
return errors.New("migration not found")
}
return r.withMigrationLog(ctx, *definition, false, func(_ int) error {
r.logger.InfoWithFields(log.LogFields{"id": id}, "Forcing writing migration log")
return nil
})
}
func (r *Runner) withMigrationLog(ctx context.Context, definition Definition, reverse bool, f func(id int) error) (err error) {
id, _, err := ScanInt(r.db.Query(ctx, Query(`
INSERT INTO migration_logs (migration_id, reverse)
VALUES ({:id}, {:reverse})
RETURNING id
`, Args{
"id": definition.ID,
"reverse": reverse,
})))
if err != nil {
return err
}
defer func() {
err = errors.Join(err, r.db.Exec(ctx, Query(`
UPDATE migration_logs
SET
finished_at = current_timestamp,
success = {:success},
error_message = {:error_message}
WHERE id = {:id}
`, Args{
"success": err == nil,
"error_message": extractErrorMessage(err),
"id": id,
})))
}()
return f(id)
}
//
//
type indexStatus struct {
IsValid bool
Phase *string
LockersTotal *int
LockersDone *int
BlocksTotal *int
BlocksDone *int
TuplesTotal *int
TuplesDone *int
}
var scanIndexStatus = NewFirstScanner(func(s Scanner) (is indexStatus, _ error) {
err := s.Scan(
&is.IsValid,
&is.Phase,
&is.LockersTotal,
&is.LockersDone,
&is.BlocksTotal,
&is.BlocksDone,
&is.TuplesTotal,
&is.TuplesDone,
)
return is, err
})
func (r *Runner) getIndexStatus(ctx context.Context, tableName, indexName string) (indexStatus, bool, error) {
return scanIndexStatus(r.db.Query(ctx, Query(`
SELECT
index.indisvalid,
progress.phase,
progress.lockers_total,
progress.lockers_done,
progress.blocks_total,
progress.blocks_done,
progress.tuples_total,
progress.tuples_done
FROM pg_catalog.pg_class table_class
JOIN pg_catalog.pg_index index ON index.indrelid = table_class.oid
JOIN pg_catalog.pg_class index_class ON index_class.oid = index.indexrelid
LEFT JOIN pg_catalog.pg_stat_progress_create_index progress ON progress.relid = table_class.oid AND progress.index_relid = index_class.oid
WHERE
table_class.relname = {:tableName} AND
index_class.relname = {:indexName}
`, Args{
"tableName": tableName,
"indexName": indexName,
})))
}
//
//
type concurrentIndexLog struct {
ID int
Success *bool
ErrorMessage *string
LastHeartbeatAt time.Time
}
var scanConcurrentIndexLog = NewFirstScanner(func(s Scanner) (l concurrentIndexLog, _ error) {
err := s.Scan(&l.ID, &l.Success, &l.ErrorMessage, &l.LastHeartbeatAt)
return l, err
})
func (r *Runner) getLogForConcurrentIndex(ctx context.Context, db DB, id int) (concurrentIndexLog, bool, error) {
return scanConcurrentIndexLog(db.Query(ctx, Query(`
WITH ranked_migration_logs AS (
SELECT
l.*,
ROW_NUMBER() OVER (ORDER BY started_at DESC) AS rank
FROM migration_logs l
WHERE migration_id = {:id}
)
SELECT
id,
success,
error_message,
COALESCE(last_heartbeat_at, started_at)
FROM ranked_migration_logs
WHERE rank = 1 AND NOT reverse
`,
Args{"id": id},
)))
}
//
//
func wait(ctx context.Context, duration time.Duration) error {
select {
case <-time.After(duration):
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func extractErrorMessage(err error) *string {
if err == nil {
return nil
}
msg := err.Error()
return &msg
}