Skip to content

Commit

Permalink
db migration
Browse files Browse the repository at this point in the history
  • Loading branch information
pugachAG committed May 8, 2023
1 parent 08ecc51 commit 3d29347
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/store/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub type DbVersion = u32;

/// Current version of the database.
pub const DB_VERSION: DbVersion = 36;
pub const DB_VERSION: DbVersion = 37;

/// Database version at which point DbKind was introduced.
const DB_VERSION_WITH_KIND: DbVersion = 34;
Expand Down
28 changes: 28 additions & 0 deletions core/store/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,31 @@ pub fn migrate_34_to_35(store: &Store) -> anyhow::Result<()> {
update.commit()?;
Ok(())
}

/// Migrates the database from version 36 to 37.
///
/// This involves rewriting all FlatStateChanges entries in the new format.
/// The size of that column should not exceed several dozens of entries.
pub fn migrate_36_to_37(store: &Store) -> anyhow::Result<()> {
#[derive(borsh::BorshDeserialize)]
struct LegacyFlatStateChanges(HashMap<Vec<u8>, Option<near_primitives::state::ValueRef>>);

let mut update = store.store_update();
update.delete_all(DBCol::FlatStateChanges);
for result in store.iter(DBCol::FlatStateChanges) {
let (key, old_value) = result?;
let new_value = crate::flat::FlatStateChanges(
LegacyFlatStateChanges::try_from_slice(&old_value)?
.0
.into_iter()
.map(|(key, value_ref)| {
(key, value_ref.map(|v| crate::flat::FlatStateValue::Ref(v)))
})
.collect(),
)
.try_to_vec()?;
update.set(DBCol::FlatStateChanges, &key, &new_value);
}
update.commit()?;
Ok(())
}
1 change: 1 addition & 0 deletions nearcore/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl<'a> near_store::StoreMigrator for Migrator<'a> {
tracing::info!(target: "migrations", "It will happen in parallel with regular block processing. ETA is 15h for RPC node and 2d for archival node.");
Ok(())
}
36 => near_store::migrations::migrate_36_to_37(store),
DB_VERSION.. => unreachable!(),
}
}
Expand Down

0 comments on commit 3d29347

Please sign in to comment.