Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iroh-sync): prevent panic in namespace migration #1775

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions iroh-sync/src/store/fs/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use super::{
/// Run all database migrations, if needed.
pub fn run_migrations(db: &Database) -> Result<()> {
run_migration(db, migration_001_populate_latest_table)?;
run_migration(db, migration_002_namespaces_v2)?;
run_migration(db, migration_003_populate_by_key_index)?;
run_migration(db, migration_002_namespaces_populate_v2)?;
run_migration(db, migration_003_namespaces_delete_v1)?;
run_migration(db, migration_004_populate_by_key_index)?;
Ok(())
}

Expand Down Expand Up @@ -74,12 +75,11 @@ fn migration_001_populate_latest_table(tx: &WriteTransaction) -> Result<MigrateO
Ok(MigrateOutcome::Execute(len))
}

/// Migrate the namespaces table from V1 to V2.
fn migration_002_namespaces_v2(tx: &WriteTransaction) -> Result<MigrateOutcome> {
/// Copy the namespaces data from V1 to V2.
fn migration_002_namespaces_populate_v2(tx: &WriteTransaction) -> Result<MigrateOutcome> {
let namespaces_v1_exists = tx
.list_tables()?
.any(|handle| handle.name() == NAMESPACES_TABLE_V1.name());
// migration 002: update namespaces from V1 to V2
if !namespaces_v1_exists {
return Ok(MigrateOutcome::Skip);
}
Expand All @@ -95,12 +95,23 @@ fn migration_002_namespaces_v2(tx: &WriteTransaction) -> Result<MigrateOutcome>
namespaces_v2.insert(&id, (raw_kind, &raw_bytes))?;
entries += 1;
}
tx.delete_table(NAMESPACES_TABLE_V1)?;
Ok(MigrateOutcome::Execute(entries))
}

/// Delete the v1 namespaces table.
fn migration_003_namespaces_delete_v1(tx: &WriteTransaction) -> Result<MigrateOutcome> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment why this is its own migration? this is quite surprising

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 added a commit with a comment

let namespaces_v1_exists = tx
.list_tables()?
.any(|handle| handle.name() == NAMESPACES_TABLE_V1.name());
if !namespaces_v1_exists {
return Ok(MigrateOutcome::Skip);
}
tx.delete_table(NAMESPACES_TABLE_V1)?;
Ok(MigrateOutcome::Execute(1))
}

/// migration 003: populate the by_key index table(which did not exist before)
fn migration_003_populate_by_key_index(tx: &WriteTransaction) -> Result<MigrateOutcome> {
fn migration_004_populate_by_key_index(tx: &WriteTransaction) -> Result<MigrateOutcome> {
let mut by_key_table = tx.open_table(RECORDS_BY_KEY_TABLE)?;
let records_table = tx.open_table(RECORDS_TABLE)?;
if !by_key_table.is_empty()? || records_table.is_empty()? {
Expand Down
Loading