diff --git a/documentation/src/developer_documentation/db_migration.md b/documentation/src/developer_documentation/db_migration.md new file mode 100644 index 000000000000..510bbf6ca8b4 --- /dev/null +++ b/documentation/src/developer_documentation/db_migration.md @@ -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. \ No newline at end of file diff --git a/scripts/migration_check.sh b/scripts/migration_check.sh index 5526bf12552f..818cf6f5e7e0 100755 --- a/scripts/migration_check.sh +++ b/scripts/migration_check.sh @@ -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 @@ -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 diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index 81935a05feb6..6064fc3b6d3d 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -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}; @@ -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(); diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs index ca692bc6d495..3719000ab257 100644 --- a/src/db/migration/mod.rs +++ b/src/db/migration/mod.rs @@ -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, } @@ -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, }; @@ -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 { @@ -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")) -}