Skip to content

Commit 3b6547a

Browse files
authored
migrations: Better present migration_logs over schema_migrations (sourcegraph#30309)
1 parent 8728024 commit 3b6547a

File tree

1 file changed

+27
-27
lines changed
  • internal/database/migration/store

1 file changed

+27
-27
lines changed

internal/database/migration/store/store.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919

2020
type Store struct {
2121
*basestore.Store
22-
migrationsTable string
23-
operations *Operations
22+
schemaName string
23+
operations *Operations
2424
}
2525

2626
// IndexStatus describes the state of an index. Is{Valid,Ready,Live} is taken
@@ -64,17 +64,17 @@ var CreateIndexConcurrentlyPhases = []string{
6464

6565
func NewWithDB(db dbutil.DB, migrationsTable string, operations *Operations) *Store {
6666
return &Store{
67-
Store: basestore.NewWithDB(db, sql.TxOptions{}),
68-
migrationsTable: migrationsTable,
69-
operations: operations,
67+
Store: basestore.NewWithDB(db, sql.TxOptions{}),
68+
schemaName: migrationsTable,
69+
operations: operations,
7070
}
7171
}
7272

7373
func (s *Store) With(other basestore.ShareableStore) *Store {
7474
return &Store{
75-
Store: s.Store.With(other),
76-
migrationsTable: s.migrationsTable,
77-
operations: s.operations,
75+
Store: s.Store.With(other),
76+
schemaName: s.schemaName,
77+
operations: s.operations,
7878
}
7979
}
8080

@@ -85,9 +85,9 @@ func (s *Store) Transact(ctx context.Context) (*Store, error) {
8585
}
8686

8787
return &Store{
88-
Store: txBase,
89-
migrationsTable: s.migrationsTable,
90-
operations: s.operations,
88+
Store: txBase,
89+
schemaName: s.schemaName,
90+
operations: s.operations,
9191
}, nil
9292
}
9393

@@ -98,8 +98,8 @@ func (s *Store) EnsureSchemaTable(ctx context.Context) (err error) {
9898
defer endObservation(1, observation.Args{})
9999

100100
queries := []*sqlf.Query{
101-
sqlf.Sprintf(`CREATE TABLE IF NOT EXISTS %s(version bigint NOT NULL PRIMARY KEY)`, quote(s.migrationsTable)),
102-
sqlf.Sprintf(`ALTER TABLE %s ADD COLUMN IF NOT EXISTS dirty boolean NOT NULL`, quote(s.migrationsTable)),
101+
sqlf.Sprintf(`CREATE TABLE IF NOT EXISTS %s(version bigint NOT NULL PRIMARY KEY)`, quote(s.schemaName)),
102+
sqlf.Sprintf(`ALTER TABLE %s ADD COLUMN IF NOT EXISTS dirty boolean NOT NULL`, quote(s.schemaName)),
103103

104104
sqlf.Sprintf(`CREATE TABLE IF NOT EXISTS migration_logs(id SERIAL PRIMARY KEY)`),
105105
sqlf.Sprintf(`ALTER TABLE migration_logs ADD COLUMN IF NOT EXISTS migration_logs_schema_version integer NOT NULL`),
@@ -131,7 +131,7 @@ func (s *Store) Version(ctx context.Context) (version int, dirty bool, ok bool,
131131
ctx, endObservation := s.operations.version.With(ctx, &err, observation.Args{})
132132
defer endObservation(1, observation.Args{})
133133

134-
rows, err := s.Query(ctx, sqlf.Sprintf(`SELECT version, dirty FROM %s`, quote(s.migrationsTable)))
134+
rows, err := s.Query(ctx, sqlf.Sprintf(`SELECT version, dirty FROM %s`, quote(s.schemaName)))
135135
if err != nil {
136136
return 0, false, false, err
137137
}
@@ -212,7 +212,7 @@ func (s *Store) TryLock(ctx context.Context) (_ bool, _ func(err error) error, e
212212
}
213213

214214
func (s *Store) lockKey() int32 {
215-
return locker.StringKey(fmt.Sprintf("%s:migrations", s.migrationsTable))
215+
return locker.StringKey(fmt.Sprintf("%s:migrations", s.schemaName))
216216
}
217217

218218
// Up runs the given definition's up query.
@@ -276,16 +276,21 @@ func (s *Store) WithMigrationLog(ctx context.Context, definition definition.Defi
276276
expectedCurrentVersion = definitionVersion
277277
}
278278

279-
logID, err := s.setVersion(ctx, up, expectedCurrentVersion, targetVersion, definitionVersion)
279+
logID, err := s.createMigrationLog(ctx, up, expectedCurrentVersion, targetVersion, definitionVersion)
280280
if err != nil {
281281
return err
282282
}
283283

284+
defer func() {
285+
if err == nil {
286+
err = s.Exec(ctx, sqlf.Sprintf(`UPDATE %s SET dirty = false`, quote(s.schemaName)))
287+
}
288+
}()
284289
defer func() {
285290
if execErr := s.Exec(ctx, sqlf.Sprintf(
286291
`UPDATE migration_logs SET finished_at = NOW(), success = %s, error_message = %s WHERE id = %d`,
287292
err == nil,
288-
strPtr(err),
293+
errMsgPtr(err),
289294
logID,
290295
)); execErr != nil {
291296
err = multierror.Append(err, execErr)
@@ -296,14 +301,10 @@ func (s *Store) WithMigrationLog(ctx context.Context, definition definition.Defi
296301
return err
297302
}
298303

299-
if err := s.Exec(ctx, sqlf.Sprintf(`UPDATE %s SET dirty=false`, quote(s.migrationsTable))); err != nil {
300-
return err
301-
}
302-
303304
return nil
304305
}
305306

306-
func (s *Store) setVersion(ctx context.Context, up bool, expectedCurrentVersion, targetVersion, sourceVersion int) (_ int, err error) {
307+
func (s *Store) createMigrationLog(ctx context.Context, up bool, expectedCurrentVersion, targetVersion, sourceVersion int) (_ int, err error) {
307308
tx, err := s.Transact(ctx)
308309
if err != nil {
309310
return 0, err
@@ -314,7 +315,6 @@ func (s *Store) setVersion(ctx context.Context, up bool, expectedCurrentVersion,
314315
cta := "This condition should not be reachable by normal use of the migration store via the runner and indicates a bug. Please report this issue."
315316
return errors.Errorf(description+"\n\n"+cta+"\n", args...)
316317
}
317-
318318
if currentVersion, dirty, ok, err := tx.Version(ctx); err != nil {
319319
return 0, err
320320
} else if dirty {
@@ -324,12 +324,12 @@ func (s *Store) setVersion(ctx context.Context, up bool, expectedCurrentVersion,
324324
return 0, assertionFailure("expected schema to have version %d, but has version %d\n", expectedCurrentVersion, currentVersion)
325325
}
326326

327-
if err := tx.Exec(ctx, sqlf.Sprintf(`DELETE FROM %s`, quote(s.migrationsTable))); err != nil {
327+
if err := tx.Exec(ctx, sqlf.Sprintf(`DELETE FROM %s`, quote(s.schemaName))); err != nil {
328328
return 0, err
329329
}
330330
}
331331

332-
if err := tx.Exec(ctx, sqlf.Sprintf(`INSERT INTO %s (version, dirty) VALUES (%s, true)`, quote(s.migrationsTable), targetVersion)); err != nil {
332+
if err := tx.Exec(ctx, sqlf.Sprintf(`INSERT INTO %s (version, dirty) VALUES (%s, true)`, quote(s.schemaName), targetVersion)); err != nil {
333333
return 0, err
334334
}
335335

@@ -345,7 +345,7 @@ func (s *Store) setVersion(ctx context.Context, up bool, expectedCurrentVersion,
345345
RETURNING id
346346
`,
347347
currentMigrationLogSchemaVersion,
348-
s.migrationsTable,
348+
s.schemaName,
349349
sourceVersion,
350350
up,
351351
)))
@@ -358,7 +358,7 @@ func (s *Store) setVersion(ctx context.Context, up bool, expectedCurrentVersion,
358358

359359
var quote = sqlf.Sprintf
360360

361-
func strPtr(err error) *string {
361+
func errMsgPtr(err error) *string {
362362
if err == nil {
363363
return nil
364364
}

0 commit comments

Comments
 (0)