Skip to content

Commit 7e54682

Browse files
committed
sqldb: fix dirty migration in v0.19.0-rc1
1 parent dae2126 commit 7e54682

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

sqldb/migrations.go

+30
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ type MigrationExecutor interface {
115115

116116
// GetSchemaVersion returns the current schema version of the database.
117117
GetSchemaVersion() (int, bool, error)
118+
119+
// SetSchemaVersion sets the schema version of the database.
120+
//
121+
// NOTE: This alters the internal database schema tracker. USE WITH
122+
// CAUTION!!!
123+
SetSchemaVersion(version int, dirty bool) error
118124
}
119125

120126
var (
@@ -381,6 +387,30 @@ func ApplyMigrations(ctx context.Context, db *BaseDB,
381387
"(dirty=%v) as base version", currentVersion, dirty)
382388
}
383389

390+
// Due to an a migration issue in v0.19.0-rc1 we may be at version 2 and
391+
// have a dirty schema due to failing migration 3. If this is indeed the
392+
// case, we need to reset the dirty flag to be able to apply the fixed
393+
// migration.
394+
// NOTE: this could be removed as soon as we drop v0.19.0-beta.
395+
if version == 2 {
396+
schemaVersion, dirty, err := migrator.GetSchemaVersion()
397+
if err != nil {
398+
return err
399+
}
400+
401+
if schemaVersion == 3 && dirty {
402+
log.Warnf("Schema version %d is dirty. This is "+
403+
"likely a consequence of a failed migration "+
404+
"in v0.19.0-rc1. Attempting to recover by "+
405+
"resetting the dirty flag", schemaVersion)
406+
407+
err = migrator.SetSchemaVersion(4, false)
408+
if err != nil {
409+
return err
410+
}
411+
}
412+
}
413+
384414
for _, migration := range migrations {
385415
if migration.Version <= currentVersion {
386416
log.Infof("Skipping migration '%s' (version %d) as it "+

sqldb/postgres.go

+12
Original file line numberDiff line numberDiff line change
@@ -198,3 +198,15 @@ func (s *PostgresStore) GetSchemaVersion() (int, bool, error) {
198198

199199
return version, dirty, nil
200200
}
201+
202+
// SetSchemaVersion sets the schema version of the Postgres database.
203+
//
204+
// NOTE: This alters the internal database schema tracker. USE WITH CAUTION!!!
205+
func (s *PostgresStore) SetSchemaVersion(version int, dirty bool) error {
206+
driver, err := pgx_migrate.WithInstance(s.DB, &pgx_migrate.Config{})
207+
if err != nil {
208+
return errPostgresMigration(err)
209+
}
210+
211+
return driver.SetVersion(version, dirty)
212+
}

sqldb/sqlite.go

+14
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ func (s *SqliteStore) GetSchemaVersion() (int, bool, error) {
197197
return version, dirty, nil
198198
}
199199

200+
// SetSchemaVersion sets the schema version of the SQLite database.
201+
//
202+
// NOTE: This alters the internal database schema tracker. USE WITH CAUTION!!!
203+
func (s *SqliteStore) SetSchemaVersion(version int, dirty bool) error {
204+
driver, err := sqlite_migrate.WithInstance(
205+
s.DB, &sqlite_migrate.Config{},
206+
)
207+
if err != nil {
208+
return errSqliteMigration(err)
209+
}
210+
211+
return driver.SetVersion(version, dirty)
212+
}
213+
200214
// NewTestSqliteDB is a helper function that creates an SQLite database for
201215
// testing.
202216
func NewTestSqliteDB(t *testing.T) *SqliteStore {

0 commit comments

Comments
 (0)