use metaldb::{
migration::{MigrationError, MigrationHelper},
Database,
};
use std::sync::Arc;
mod migration;
use crate::migration::{perform_migration, v1, v2};
fn migrate_wallets(helper: &mut MigrationHelper) -> Result<(), MigrationError> {
helper.iter_loop(|helper, iters| {
let old_schema = v1::Schema::new(helper.old_data());
let mut new_schema = v2::Schema::new(helper.new_data());
const CHUNK_SIZE: usize = 1_000;
let mut count = 0;
for (public_key, wallet) in iters
.create("wallets", &old_schema.wallets)
.take(CHUNK_SIZE)
{
if wallet.username == "Eve" {
helper
.new_data()
.create_tombstone(("histories", &public_key));
} else {
let mut history = new_schema.histories.get(&public_key);
history.extend(&old_schema.histories.get(&public_key));
let new_wallet = v2::Wallet {
username: wallet.username,
balance: wallet.balance,
history_hash: 12,
};
new_schema.wallets.put(&public_key, new_wallet);
}
count += 1;
}
println!("Processed chunk of {} wallets", count);
})
}
fn migration_with_iter_loop(db: Arc<dyn Database>) {
let mut helper = MigrationHelper::new(db.clone(), "test");
{
let old_data = helper.old_data();
let old_schema = v1::Schema::new(old_data);
let new_data = helper.new_data();
let mut new_schema = v2::Schema::new(new_data.clone());
let config = v2::Config {
ticker: old_schema.ticker.get().unwrap(),
divisibility: old_schema.divisibility.get().unwrap_or(0),
};
new_schema.config.set(config);
new_data.create_tombstone("ticker");
new_data.create_tombstone("divisibility");
}
migrate_wallets(&mut helper).expect("Wallet migration failed.");
helper.finish().expect("Migration finish failed.");
}
fn main() {
perform_migration(migration_with_iter_loop);
}