-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
611 lines (516 loc) · 14.9 KB
/
manager.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package golembic
import (
"context"
"database/sql"
"fmt"
"time"
)
// NOTE: Ensure that
// - `Manager.sinceOrAll` satisfies `migrationsFilter`.
var (
_ migrationsFilter = (*Manager)(nil).sinceOrAll
)
const (
// DefaultMetadataTable is the default name for the table used to store
// metadata about migrations.
DefaultMetadataTable = "golembic_migrations"
)
// NewManager creates a new manager for orchestrating migrations.
func NewManager(opts ...ManagerOption) (*Manager, error) {
m := &Manager{
MetadataTable: DefaultMetadataTable,
Log: &stdoutPrintf{},
}
for _, opt := range opts {
err := opt(m)
if err != nil {
return nil, err
}
}
return m, nil
}
// Manager orchestrates database operations done via `Up` / `UpConn` as well as
// supporting operations such as creating a table for migration metadata and
// writing rows into that metadata table during a migration.
type Manager struct {
// MetadataTable is the name of the table that stores migration metadata.
// The expected default value (`DefaultMetadataTable`) is
// "golembic_migrations".
MetadataTable string
// ConnectionPool is a cache-able pool of connections to the database.
ConnectionPool *sql.DB
// Provider delegates all actions to an abstract SQL database engine, with
// the expectation that the provider also encodes connection information.
Provider EngineProvider
// Sequence is the collection of registered migrations to be applied,
// verified, described, etc. by this manager.
Sequence *Migrations
// DevelopmentMode is a flag indicating that this manager is currently
// being run in development mode, so things like extra validation should
// intentionally be disabled. This is intended for use in testing and
// development, where an entire database is spun up locally (e.g. in Docker)
// and migrations will be applied from scratch (including milestones that
// may not come at the end).
DevelopmentMode bool
// Log is used for printing output
Log PrintfReceiver
}
// NewConnectionPool creates a new database connection pool and validates that
// it can ping the DB.
func (m *Manager) NewConnectionPool(ctx context.Context) (*sql.DB, error) {
pool, err := m.Provider.Open()
if err != nil {
return nil, err
}
err = pool.PingContext(ctx)
if err != nil {
return nil, err
}
return pool, nil
}
// CloseConnectionPool closes the connection pool and removes it, if one is
// set / cached on the current manager.
func (m *Manager) CloseConnectionPool() error {
if m.ConnectionPool == nil {
return nil
}
err := m.ConnectionPool.Close()
if err != nil {
return err
}
m.ConnectionPool = nil
return nil
}
// EnsureConnectionPool returns a cached database connection pool (if already
// set) or invokes `NewConnection()` to create a new one.
func (m *Manager) EnsureConnectionPool(ctx context.Context) (*sql.DB, error) {
if m.ConnectionPool != nil {
return m.ConnectionPool, nil
}
pool, err := m.NewConnectionPool(ctx)
if err != nil {
return nil, err
}
m.ConnectionPool = pool
return m.ConnectionPool, nil
}
// EnsureMigrationsTable checks that the migrations metadata table exists
// and creates it if not.
func (m *Manager) EnsureMigrationsTable(ctx context.Context) error {
return CreateMigrationsTable(ctx, m)
}
// InsertMigration inserts a migration into the migrations metadata table.
func (m *Manager) InsertMigration(ctx context.Context, tx *sql.Tx, migration Migration) error {
if migration.Previous == "" {
statement := fmt.Sprintf(
"INSERT INTO %s (serial_id, revision, previous) VALUES (0, %s, NULL)",
m.Provider.QuoteIdentifier(m.MetadataTable),
m.Provider.QueryParameter(1),
)
_, err := tx.ExecContext(ctx, statement, migration.Revision)
return err
}
statement := fmt.Sprintf(
"INSERT INTO %s (serial_id, revision, previous) VALUES (%s, %s, %s)",
m.Provider.QuoteIdentifier(m.MetadataTable),
m.Provider.QueryParameter(1),
m.Provider.QueryParameter(2),
m.Provider.QueryParameter(3),
)
_, err := tx.ExecContext(
ctx,
statement,
migration.serialID, // Parameter 1
migration.Revision, // Parameter 2
migration.Previous, // Parameter 3
)
return err
}
// NewTx creates a new transaction after ensuring there is an existing
// connection.
func (m *Manager) NewTx(ctx context.Context) (*sql.Tx, error) {
pool, err := m.EnsureConnectionPool(ctx)
if err != nil {
return nil, err
}
return pool.BeginTx(ctx, nil)
}
// ApplyMigration creates a transaction that runs the "Up" migration.
func (m *Manager) ApplyMigration(ctx context.Context, migration Migration) (err error) {
var tx *sql.Tx
defer func() {
err = txFinalize(tx, err)
}()
m.Log.Printf("Applying %s: %s", migration.Revision, migration.ExtendedDescription())
pool, err := m.EnsureConnectionPool(ctx)
if err != nil {
return
}
tx, err = m.NewTx(ctx)
if err != nil {
return
}
err = migration.InvokeUp(ctx, pool, tx)
if err != nil {
return
}
err = m.InsertMigration(ctx, tx, migration)
if err != nil {
return
}
err = tx.Commit()
return
}
// filterMigrations applies a filter function that takes the revision of the
// last applied migration to determine a set of migrations to run.
func (m *Manager) filterMigrations(ctx context.Context, filter migrationsFilter, verifyHistory bool) (int, []Migration, error) {
err := m.EnsureMigrationsTable(ctx)
if err != nil {
return 0, nil, err
}
latest, _, err := m.latestMaybeVerify(ctx, verifyHistory)
if err != nil {
return 0, nil, err
}
pastMigrationCount, migrations, err := filter(latest)
if err != nil {
return 0, nil, err
}
if len(migrations) == 0 {
format := "No migrations to run; latest revision: %s"
// Add `milestoneSuffix`, if we can detect `latest` is a milestone.
migration := m.Sequence.Get(latest)
if migration != nil && migration.Milestone {
format += milestoneSuffix
}
m.Log.Printf(format, latest)
return pastMigrationCount, nil, nil
}
return pastMigrationCount, migrations, nil
}
func (m *Manager) validateMilestones(pastMigrationCount int, migrations []Migration) error {
// Early exit if no migrations have been run yet. This **assumes** that the
// database is being brought up from scratch.
if pastMigrationCount == 0 {
return nil
}
count := len(migrations)
// Ensure all (but the last) are not a milestone.
for i := 0; i < count-1; i++ {
migration := migrations[i]
if !migration.Milestone {
continue
}
err := fmt.Errorf(
"%w; revision %s (%d / %d migrations)",
ErrCannotPassMilestone, migration.Revision, i+1, count,
)
// In development mode, log the error message but don't return an error.
if m.DevelopmentMode {
m.Log.Printf("Ignoring error in development mode")
m.Log.Printf(" %s", err)
continue
}
return err
}
return nil
}
// Up applies all migrations that have not yet been applied.
func (m *Manager) Up(ctx context.Context, opts ...ApplyOption) error {
ac, err := NewApplyConfig(opts...)
if err != nil {
return err
}
pastMigrationCount, migrations, err := m.filterMigrations(ctx, m.sinceOrAll, ac.VerifyHistory)
if err != nil {
return err
}
if migrations == nil {
return nil
}
err = m.validateMilestones(pastMigrationCount, migrations)
if err != nil {
return err
}
for _, migration := range migrations {
err = m.ApplyMigration(ctx, migration)
if err != nil {
return err
}
}
return nil
}
func (m *Manager) sinceOrAll(revision string) (int, []Migration, error) {
if revision == "" {
return 0, m.Sequence.All(), nil
}
return m.Sequence.Since(revision)
}
// UpOne applies the **next** migration that has yet been applied, if any.
func (m *Manager) UpOne(ctx context.Context, opts ...ApplyOption) error {
ac, err := NewApplyConfig(opts...)
if err != nil {
return err
}
_, migrations, err := m.filterMigrations(ctx, m.sinceOrAll, ac.VerifyHistory)
if err != nil {
return err
}
if migrations == nil {
return nil
}
migration := migrations[0]
return m.ApplyMigration(ctx, migration)
}
// UpTo applies all migrations that have yet to be applied up to (and
// including) a revision, if any. This expects the `ApplyConfig` revision to
// be set in `opts`.
func (m *Manager) UpTo(ctx context.Context, opts ...ApplyOption) error {
ac, err := NewApplyConfig(opts...)
if err != nil {
return err
}
var filter migrationsFilter = func(latest string) (int, []Migration, error) {
return m.betweenOrUntil(latest, ac.Revision)
}
pastMigrationCount, migrations, err := m.filterMigrations(ctx, filter, ac.VerifyHistory)
if err != nil {
return err
}
if migrations == nil {
return nil
}
err = m.validateMilestones(pastMigrationCount, migrations)
if err != nil {
return err
}
for _, migration := range migrations {
err = m.ApplyMigration(ctx, migration)
if err != nil {
return err
}
}
return nil
}
func (m *Manager) betweenOrUntil(latest string, revision string) (int, []Migration, error) {
if latest == "" {
return m.Sequence.Until(revision)
}
return m.Sequence.Between(latest, revision)
}
// Latest determines the revision and timestamp of the most recently applied
// migration.
//
// NOTE: This assumes, but does not check, that the migrations metadata table
// exists.
func (m *Manager) Latest(ctx context.Context) (revision string, createdAt time.Time, err error) {
var tx *sql.Tx
defer func() {
err = txFinalize(tx, err)
}()
tx, err = m.NewTx(ctx)
if err != nil {
return
}
query := fmt.Sprintf(
"SELECT revision, previous, created_at FROM %s ORDER BY serial_id DESC LIMIT 1",
m.Provider.QuoteIdentifier(m.MetadataTable),
)
tc := m.Provider.TimestampColumn()
rows, err := readAllMigration(ctx, tx, query, tc)
if err != nil {
return
}
if len(rows) == 0 {
return
}
// NOTE: Here we trust that the query is sufficient to guarantee that
// `len(rows) == 1`.
revision = rows[0].Revision
createdAt = rows[0].createdAt
return
}
// latestMaybeVerify determines the latest applied migration and verifies all of the
// migration history if `verifyHistory` is true.
func (m *Manager) latestMaybeVerify(ctx context.Context, verifyHistory bool) (revision string, createdAt time.Time, err error) {
if !verifyHistory {
revision, createdAt, err = m.Latest(ctx)
return
}
var tx *sql.Tx
defer func() {
err = txFinalize(tx, err)
}()
tx, err = m.NewTx(ctx)
if err != nil {
return
}
history, _, err := m.verifyHistory(ctx, tx)
if err != nil {
return
}
if len(history) == 0 {
return
}
revision = history[len(history)-1].Revision
createdAt = history[len(history)-1].createdAt
err = tx.Commit()
return
}
// GetVersion returns the migration that corresponds to the version that was
// most recently applied.
func (m *Manager) GetVersion(ctx context.Context, opts ...ApplyOption) (*Migration, error) {
ac, err := NewApplyConfig(opts...)
if err != nil {
return nil, err
}
err = m.EnsureMigrationsTable(ctx)
if err != nil {
return nil, err
}
revision, createdAt, err := m.latestMaybeVerify(ctx, ac.VerifyHistory)
if err != nil {
return nil, err
}
if revision == "" {
return nil, nil
}
migration := m.Sequence.Get(revision)
if migration == nil {
err = fmt.Errorf("%w; revision: %q", ErrMigrationNotRegistered, revision)
return nil, err
}
withCreated := &Migration{
Previous: migration.Previous,
Revision: migration.Revision,
Description: migration.Description,
createdAt: createdAt,
}
return withCreated, nil
}
// Verify checks that the rows in the migrations metadata table match the
// sequence.
func (m *Manager) Verify(ctx context.Context) (err error) {
var tx *sql.Tx
defer func() {
err = txFinalize(tx, err)
}()
err = m.EnsureMigrationsTable(ctx)
if err != nil {
return
}
tx, err = m.NewTx(ctx)
if err != nil {
return
}
history, registered, err := m.verifyHistory(ctx, tx)
if err != nil {
return
}
for i, migration := range registered {
description := migration.ExtendedDescription()
if i < len(history) {
applied := history[i]
m.Log.Printf(
"%d | %s | %s (applied %s)",
i, migration.Revision, description, applied.createdAt,
)
} else {
m.Log.Printf(
"%d | %s | %s (not yet applied)",
i, migration.Revision, description,
)
}
}
return
}
// verifyHistory retrieves a full history of migrations and compares it against
// the sequence of registered migrations. If they match (up to the end of the
// history, the registered sequence can be longer), this will return with no
// error and include slices of the history and the registered migrations.
func (m *Manager) verifyHistory(ctx context.Context, tx *sql.Tx) (history, registered []Migration, err error) {
query := fmt.Sprintf(
"SELECT revision, previous, created_at FROM %s ORDER BY serial_id ASC",
m.Provider.QuoteIdentifier(m.MetadataTable),
)
tc := m.Provider.TimestampColumn()
history, err = readAllMigration(ctx, tx, query, tc)
if err != nil {
return
}
registered = m.Sequence.All()
if len(history) > len(registered) {
err = fmt.Errorf(
"%w; sequence has %d migrations but %d are stored in the table",
ErrMigrationMismatch, len(registered), len(history),
)
return
}
for i, row := range history {
expected := registered[i]
if !row.Like(expected) {
err = fmt.Errorf(
"%w; stored migration %d: %q does not match migration %q in sequence",
ErrMigrationMismatch, i, row.Compact(), expected.Compact(),
)
return
}
}
return
}
// Describe displays all of the registered migrations (with descriptions).
func (m *Manager) Describe(_ context.Context) error {
m.Sequence.Describe(m.Log)
return nil
}
// Version displays the revision of the most recent migration to be applied
func (m *Manager) Version(ctx context.Context, opts ...ApplyOption) error {
migration, err := m.GetVersion(ctx, opts...)
if err != nil {
return err
}
if migration == nil {
m.Log.Printf("No migrations have been run")
} else {
m.Log.Printf(
"%s: %s (applied %s)",
migration.Revision, migration.Description, migration.createdAt,
)
}
return nil
}
// IsApplied checks if a migration has already been applied.
//
// NOTE: This assumes, but does not check, that the migrations metadata table
// exists.
func (m *Manager) IsApplied(ctx context.Context, tx *sql.Tx, migration Migration) (bool, error) {
query := fmt.Sprintf(
"SELECT revision, previous, created_at FROM %s WHERE revision = %s",
m.Provider.QuoteIdentifier(m.MetadataTable),
m.Provider.QueryParameter(1),
)
tc := m.Provider.TimestampColumn()
rows, err := readAllMigration(ctx, tx, query, tc, migration.Revision)
if err != nil {
return false, err
}
return verifyMigration(rows, migration)
}
func verifyMigration(rows []Migration, migration Migration) (bool, error) {
if len(rows) == 0 {
return false, nil
}
// NOTE: We don't verify that `len(rows) == 1` since we trust the UNIQUE
// index in the `revision` column.
if rows[0].Previous != migration.Previous {
err := fmt.Errorf(
"%w; revision: %q, registered previous migration %q does not match %q from migrations table",
ErrMigrationMismatch,
migration.Revision,
migration.Previous,
rows[0].Previous,
)
return false, err
}
return true, nil
}