use crate::database::Database; use crate::error::Result; use crate::migration::migration_2::PosAndNetherMigration; use crate::migration::migration_3::TunnelToPortalMigration; use crate::migration::migration_4::OutOfStockVoting; use geoffrey_models::models::db_metadata::DBMetadata; mod migration_2; mod migration_3; mod migration_4; trait Migration { fn up(db: &Database) -> Result<()>; fn down(db: &Database) -> Result<()>; fn version() -> u64; } pub(crate) fn upgrade(db: &Database, current_version: u64, target_version: u64) -> Result<()> { for ver in current_version + 1..=target_version { match ver { 2 => PosAndNetherMigration::up(db)?, 3 => TunnelToPortalMigration::up(db)?, 4 => OutOfStockVoting::up(db)?, _ => (), } } Ok(()) } pub(crate) fn downgrade(db: &Database, current_version: u64, target_version: u64) -> Result<()> { for ver in (target_version..current_version).rev() { match ver { 2 => PosAndNetherMigration::down(db)?, 3 => TunnelToPortalMigration::down(db)?, 4 => OutOfStockVoting::down(db)?, _ => (), } } Ok(()) } pub fn do_migration(db: &Database, target_version: u64) -> Result<()> { let current_version = db.version().unwrap_or(0); #[allow(clippy::comparison_chain)] if target_version > current_version { upgrade(db, current_version, target_version)?; } else if target_version < current_version { downgrade(db, current_version, target_version)?; } let metadata = DBMetadata { version: target_version, }; db.insert(metadata)?; Ok(()) }