Skip to content

Commit

Permalink
fix migration check and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sudo-shashank committed Jul 25, 2023
1 parent 85ba181 commit 9b5cc33
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
9 changes: 9 additions & 0 deletions documentation/src/developer_documentation/db_migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Steps to add support for a new database version:

- Add a new enum variant for every new database version
- Add version transition for each DBVersion in `migrate_db` method.
- Add steps required for new migration in `migrate` method. In each migration
step, can either do in place migration or use temp_db/ to migrate data from
existing db but finally it must atomically rename temp_db/ back to existing db
name.
- Update `LATEST_DB_VERSION` to latest database version.
11 changes: 5 additions & 6 deletions scripts/migration_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ function sync_with_tag() {
# Write build and sync logic here
git checkout $tag
make clean
make build
make install

./target/debug/forest --chain calibnet --encrypt-keystore false --auto-download-snapshot --detach
./target/debug/forest-cli --chain calibnet sync wait
forest --chain calibnet --encrypt-keystore false --auto-download-snapshot --detach
forest-cli --chain calibnet sync wait
# Check if the sync succeeded for the tag
if [ $? -eq 0 ]; then
echo "Sync successful for tag: $tag"
pkill -9 forest
sleep 5s
else
echo "Sync failed for tag: $tag"
exit 1
Expand Down Expand Up @@ -53,10 +55,7 @@ done
echo "Testing db migration from "V0.11.1" to latest, at once"
# Get latest tag
LATEST_TAG=$(git describe --tags --abbrev=0)
# Clean DB before testing db migration from "V0.11.1" to latest
/target/debug/forest-cli --chain calibnet db clean --force

# Sync all migrations, from "`V0.11.1`" to latest
# Sync calibnet with Forest `V0.11.1`
sync_with_tag $START_TAG
# Sync calibnet with latest version of Forest
Expand Down
4 changes: 2 additions & 2 deletions src/daemon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::cli_shared::{
};
use crate::db::{
db_engine::{db_root, open_proxy_db},
migration::{check_if_another_db_exist, migrate_db, DBVersion},
migration::{check_if_another_db_exist, migrate_db, LATEST_DB_VERSION},
rolling::DbGarbageCollector,
};
use crate::genesis::{get_network_name_from_genesis, import_chain, read_genesis_header};
Expand Down Expand Up @@ -178,7 +178,7 @@ pub(super) async fn start(
)?);

if let Some(db_path) = check_if_another_db_exist(&config) {
migrate_db(&config, db_path, DBVersion::V11).await?;
migrate_db(&config, db_path, LATEST_DB_VERSION).await?;
}

let mut services = JoinSet::new();
Expand Down
21 changes: 9 additions & 12 deletions src/db/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ use crate::utils::proofs_api::paramfetch::{
ensure_params_downloaded, set_proofs_parameter_cache_dir_env,
};
use std::fs;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::sync::Arc;
use tracing::info;

pub const LATEST_DB_VERSION: DBVersion = DBVersion::V11;

// TODO: Add a new enum variant for every new database version
/// Database version for each forest version which supports db migration
#[derive(Debug, Eq, PartialEq)]
pub enum DBVersion {
V0, // Default DBVersion for any unknow db
V0, // Default DBVersion for any unknown db
V11,
}

Expand Down Expand Up @@ -75,6 +78,7 @@ pub async fn migrate_db(
// Iterate over all DBVersion's until database is migrated to lastest version
while current_version != target_version {
let next_version = match current_version {
// TODO: Add version transition for each DBVersion
DBVersion::V0 => DBVersion::V11,
_ => break,
};
Expand All @@ -86,16 +90,14 @@ pub async fn migrate_db(
// - re-compute 100 tipsets
migration_check(config, &db_path).await?;

// Rename db to latest versioned db, if its not dev db
if !is_dev(&chain_path(config)) {
fs::rename(db_path.as_path(), &chain_path(config))?;
}
// Rename db to latest versioned db
fs::rename(db_path.as_path(), &chain_path(config))?;

info!("Database Successfully Migrated to {:?}", target_version);
Ok(())
}

// TODO: Add Steps required for migration
// TODO: Add Steps required for new migration
/// Migrate to an intermediate db version
fn migrate(_existing_db_path: &PathBuf, next_version: &DBVersion) -> anyhow::Result<()> {
match next_version {
Expand Down Expand Up @@ -134,8 +136,3 @@ fn get_db_version(db_path: &PathBuf) -> DBVersion {
None => DBVersion::V0, // Defaults to V0
}
}

fn is_dev(path: &Path) -> bool {
path.components()
.any(|component| matches!(component, std::path::Component::Normal(name) if name == "dev"))
}

0 comments on commit 9b5cc33

Please sign in to comment.