sqldb: fix dirty migration in v0.19.0-rc1
This commit is contained in:
3 files changed
+56
No files matched your search
@@ -115,6 +115,12 @@ type MigrationExecutor interface {
|
|||||||
|
|
||||||
// GetSchemaVersion returns the current schema version of the database.
|
// GetSchemaVersion returns the current schema version of the database.
|
||||||
GetSchemaVersion() (int, bool, error)
|
GetSchemaVersion() (int, bool, error)
|
||||||
|
|
||||||
|
// SetSchemaVersion sets the schema version of the database.
|
||||||
|
//
|
||||||
|
// NOTE: This alters the internal database schema tracker. USE WITH
|
||||||
|
// CAUTION!!!
|
||||||
|
SetSchemaVersion(version int, dirty bool) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -381,6 +387,30 @@ func ApplyMigrations(ctx context.Context, db *BaseDB,
|
|||||||
"(dirty=%v) as base version", currentVersion, dirty)
|
"(dirty=%v) as base version", currentVersion, dirty)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Due to an a migration issue in v0.19.0-rc1 we may be at version 2 and
|
||||||
|
// have a dirty schema due to failing migration 3. If this is indeed the
|
||||||
|
// case, we need to reset the dirty flag to be able to apply the fixed
|
||||||
|
// migration.
|
||||||
|
// NOTE: this could be removed as soon as we drop v0.19.0-beta.
|
||||||
|
if version == 2 {
|
||||||
|
schemaVersion, dirty, err := migrator.GetSchemaVersion()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if schemaVersion == 3 && dirty {
|
||||||
|
log.Warnf("Schema version %d is dirty. This is "+
|
||||||
|
"likely a consequence of a failed migration "+
|
||||||
|
"in v0.19.0-rc1. Attempting to recover by "+
|
||||||
|
"resetting the dirty flag", schemaVersion)
|
||||||
|
|
||||||
|
err = migrator.SetSchemaVersion(4, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, migration := range migrations {
|
for _, migration := range migrations {
|
||||||
if migration.Version <= currentVersion {
|
if migration.Version <= currentVersion {
|
||||||
log.Infof("Skipping migration '%s' (version %d) as it "+
|
log.Infof("Skipping migration '%s' (version %d) as it "+
|
||||||
|
|||||||
@@ -198,3 +198,15 @@ func (s *PostgresStore) GetSchemaVersion() (int, bool, error) {
|
|||||||
|
|
||||||
return version, dirty, nil
|
return version, dirty, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSchemaVersion sets the schema version of the Postgres database.
|
||||||
|
//
|
||||||
|
// NOTE: This alters the internal database schema tracker. USE WITH CAUTION!!!
|
||||||
|
func (s *PostgresStore) SetSchemaVersion(version int, dirty bool) error {
|
||||||
|
driver, err := pgx_migrate.WithInstance(s.DB, &pgx_migrate.Config{})
|
||||||
|
if err != nil {
|
||||||
|
return errPostgresMigration(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return driver.SetVersion(version, dirty)
|
||||||
|
}
|
||||||
@@ -197,6 +197,20 @@ func (s *SqliteStore) GetSchemaVersion() (int, bool, error) {
|
|||||||
return version, dirty, nil
|
return version, dirty, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSchemaVersion sets the schema version of the SQLite database.
|
||||||
|
//
|
||||||
|
// NOTE: This alters the internal database schema tracker. USE WITH CAUTION!!!
|
||||||
|
func (s *SqliteStore) SetSchemaVersion(version int, dirty bool) error {
|
||||||
|
driver, err := sqlite_migrate.WithInstance(
|
||||||
|
s.DB, &sqlite_migrate.Config{},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return errSqliteMigration(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return driver.SetVersion(version, dirty)
|
||||||
|
}
|
||||||
|
|
||||||
// NewTestSqliteDB is a helper function that creates an SQLite database for
|
// NewTestSqliteDB is a helper function that creates an SQLite database for
|
||||||
// testing.
|
// testing.
|
||||||
func NewTestSqliteDB(t *testing.T) *SqliteStore {
|
func NewTestSqliteDB(t *testing.T) *SqliteStore {
|
||||||
|
|||||||
Reference in new issue
Block a user