use crate::{errors::AtomicResult, Db};
pub fn migrate_maybe(store: &Db) -> AtomicResult<()> {
for tree in store.db.tree_names() {
match String::from_utf8_lossy(&tree).as_ref() {
"resources" => v0_to_v1(store)?,
"reference_index" => ref_v0_to_v1(store)?,
_other => {}
}
}
Ok(())
}
fn v0_to_v1(store: &Db) -> AtomicResult<()> {
tracing::warn!("Migrating resources schema from v0 to v1...");
let new = store.db.open_tree("resources_v1")?;
let old_key = "resources";
let old = store.db.open_tree(old_key)?;
let mut count = 0;
for item in old.into_iter() {
let (subject, resource_bin) = item.expect("Unable to convert into iterable");
let subject: String =
bincode::deserialize(&subject).expect("Unable to deserialize subject");
new.insert(subject.as_bytes(), resource_bin)?;
count += 1;
}
assert_eq!(
new.len(),
store.resources.len(),
"Not all resources were migrated."
);
assert!(
store.db.drop_tree(old_key)?,
"Old resources tree not properly removed."
);
tracing::warn!("Finished migration of {} resources", count);
Ok(())
}
fn ref_v0_to_v1(store: &Db) -> AtomicResult<()> {
tracing::warn!("Rebuilding indexes...");
store.db.drop_tree("reference_index")?;
store.build_index(true)?;
tracing::warn!("Rebuilding index finished!");
Ok(())
}