Skip to content

Commit d268f2b

Browse files
authored
fix: migration can now correctly resume after stopping (#7210)
Description --- Fixes a potential migration issue where the migration can break <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved migration process to ensure outdated PayRef index entries are removed before rebuilding, preventing stale or incorrect data. - Updated PayRef entry handling to allow existing entries to be safely overwritten during migrations, reducing potential errors. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 65af015 commit d268f2b

File tree

1 file changed

+23
-14
lines changed
  • base_layer/core/src/chain_storage/lmdb_db

1 file changed

+23
-14
lines changed

base_layer/core/src/chain_storage/lmdb_db/lmdb_db.rs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3130,6 +3130,7 @@ fn run_migrations(db: &mut LMDBDatabase) -> Result<(), ChainStorageError> {
31303130
if migrate_from_version == 2 {
31313131
// Verify database consistency before starting migration
31323132
info!(target: LOG_TARGET, "Starting PayRef migration");
3133+
31333134
let read_txn = db.read_transaction()?;
31343135
let chain_height = match fetch_chain_height(&read_txn, &db.metadata_db) {
31353136
Ok(v) => v,
@@ -3140,23 +3141,31 @@ fn run_migrations(db: &mut LMDBDatabase) -> Result<(), ChainStorageError> {
31403141
},
31413142
};
31423143
drop(read_txn);
3144+
// we need to clear the payref index first as they might now bw wrong
3145+
{
3146+
let txn = db.write_transaction()?;
3147+
lmdb_clear(&txn, &db.payref_to_output_index)?;
3148+
txn.commit()?;
3149+
info!(target: LOG_TARGET, "Cleared PayRef index");
3150+
}
31433151
for height in 0..=chain_height {
31443152
process_payref_for_height(db, height)?;
31453153
}
31463154
info!(target: LOG_TARGET, "PayRef index rebuild completed");
31473155
}
3148-
}
3149-
if last_migrated_version != MIGRATION_VERSION {
3150-
let txn = db.write_transaction()?;
3151-
info!(target: LOG_TARGET, "Migrated database to version {}", MIGRATION_VERSION);
3152-
lmdb_replace(
3153-
&txn,
3154-
&db.metadata_db,
3155-
&k.as_u32(),
3156-
&MetadataValue::MigrationVersion(MIGRATION_VERSION),
3157-
None,
3158-
)?;
3159-
txn.commit()?;
3156+
// lets update the migration version
3157+
{
3158+
let txn = db.write_transaction()?;
3159+
info!(target: LOG_TARGET, "Migrated database to version {}", MIGRATION_VERSION);
3160+
lmdb_replace(
3161+
&txn,
3162+
&db.metadata_db,
3163+
&k.as_u32(),
3164+
&MetadataValue::MigrationVersion(migrate_from_version + 1),
3165+
None,
3166+
)?;
3167+
txn.commit()?;
3168+
}
31603169
}
31613170

31623171
Ok(())
@@ -3282,12 +3291,12 @@ fn process_payref_for_height(db: &LMDBDatabase, height: u64) -> Result<(), Chain
32823291
drop(read_txn);
32833292
let write_txn = db.write_transaction()?;
32843293
for (payref, output_hash) in payrefs {
3285-
lmdb_insert(
3294+
lmdb_replace(
32863295
&write_txn,
32873296
&db.payref_to_output_index,
32883297
payref.as_slice(),
32893298
&output_hash,
3290-
"payref_to_output_index",
3299+
None,
32913300
)?;
32923301
}
32933302
write_txn.commit()?;

0 commit comments

Comments
 (0)