Skip to content

Commit 50894ac

Browse files
committed
tapdb: check and error on dirty database version
Add a check during startup to ensure that the database version is not dirty (i.e., the last migration did not complete successfully). If it is dirty, the application will exit with an error. This is done to resolve a bug that would occur when the **latest** migration failed. In that scenario, the database version would correctly be marked as dirty and exit with an error. However, under `sqlite` database backends, when `tapd` was restarted, the dirty database version would never be checked and `tapd` would start up normally, despite the database being in a dirty state. This is due to the check here: https://github.com/lightninglabs/taproot-assets/blob/0ddb08670f59e41e7edb2925a49df18d2124598e/tapdb/sqlite.go#L206-L213 In that scenario, since no migration is needed the migrate library is never called to perform any potential migration. And since the dirty check is normally done in the migrate library during a migration, it is never performed in that scenario.
1 parent b999baf commit 50894ac

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

tapdb/migrations.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,20 @@ func applyMigrations(fs fs.FS, driver database.Driver, path, dbName string,
166166
return err
167167
}
168168

169-
migrationVersion, _, err := sqlMigrate.Version()
169+
migrationVersion, dirty, err := sqlMigrate.Version()
170170
if err != nil && !errors.Is(err, migrate.ErrNilVersion) {
171171
return fmt.Errorf("unable to determine current migration"+
172172
"version: %w", err)
173173
}
174174

175+
// If the migration version is dirty, we should not proceed with further
176+
// migrations, as this indicates that a previous migration did not
177+
// complete successfully and requires manual intervention.
178+
if dirty {
179+
return fmt.Errorf("database is in a dirty state at version "+
180+
"%v, manual intervention required", migrationVersion)
181+
}
182+
175183
// As the down migrations may end up *dropping* data, we want to
176184
// prevent that without explicit accounting.
177185
latestVersion := opts.latestVersion.UnwrapOr(LatestMigrationVersion)

0 commit comments

Comments
 (0)