From 217fae2a6672dfdc8e1b42b6d36bb5778b6e5479 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Wed, 9 Nov 2022 13:16:19 +0000 Subject: [PATCH] feat: [#56] take source DB in upgrader command from args Instead of reading the current configuration. --- src/bin/upgrade.rs | 6 +-- .../from_v1_0_0_to_v2_0_0/upgrader.rs | 44 +++++++++---------- upgrades/from_v1_0_0_to_v2_0_0/README.md | 2 +- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/bin/upgrade.rs b/src/bin/upgrade.rs index 1c5a27a3..874f0fad 100644 --- a/src/bin/upgrade.rs +++ b/src/bin/upgrade.rs @@ -1,10 +1,10 @@ //! Upgrade command. //! It updates the application from version v1.0.0 to v2.0.0. -//! You can execute it with: `cargo run --bin upgrade ./data_v2.db ./uploads` +//! You can execute it with: `cargo run --bin upgrade ./data.db ./data_v2.db ./uploads` -use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::upgrader::upgrade; +use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::upgrader::run_upgrader; #[actix_web::main] async fn main() { - upgrade().await; + run_upgrader().await; } diff --git a/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs b/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs index d084ede4..6d9d5493 100644 --- a/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs +++ b/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs @@ -24,25 +24,26 @@ use chrono::prelude::{DateTime, Utc}; use std::{env, error, fs}; use std::{sync::Arc, time::SystemTime}; -use crate::config::Configuration; - use text_colorizer::*; +const NUMBER_OF_ARGUMENTS: i64 = 3; + #[derive(Debug)] -struct Arguments { - database_file: String, // The new database - upload_path: String, // The relative dir where torrent files are stored +pub struct Arguments { + source_database_file: String, // The source database in version v1.0.0 we want to migrate + destiny_database_file: String, // The new migrated database in version v2.0.0 + upload_path: String, // The relative dir where torrent files are stored } fn print_usage() { eprintln!( "{} - migrates date from version v1.0.0 to v2.0.0. - cargo run --bin upgrade TARGET_SLQLITE_FILE_PATH TORRENT_UPLOAD_DIR + cargo run --bin upgrade SOURCE_DB_FILE DESTINY_DB_FILE TORRENT_UPLOAD_DIR For example: - cargo run --bin upgrade ./data_v2.db ./uploads + cargo run --bin upgrade ./data.db ./data_v2.db ./uploads ", "Upgrader".green() @@ -52,38 +53,33 @@ fn print_usage() { fn parse_args() -> Arguments { let args: Vec = env::args().skip(1).collect(); - if args.len() != 2 { + if args.len() != 3 { eprintln!( - "{} wrong number of arguments: expected 2, got {}", + "{} wrong number of arguments: expected {}, got {}", "Error".red().bold(), + NUMBER_OF_ARGUMENTS, args.len() ); print_usage(); } Arguments { - database_file: args[0].clone(), - upload_path: args[1].clone(), + source_database_file: args[0].clone(), + destiny_database_file: args[1].clone(), + upload_path: args[2].clone(), } } -pub async fn upgrade() { - let args = parse_args(); - - let cfg = match Configuration::load_from_file().await { - Ok(config) => Arc::new(config), - Err(error) => { - panic!("{}", error) - } - }; - - let settings = cfg.settings.read().await; +pub async fn run_upgrader() { + upgrade(&parse_args()).await +} +pub async fn upgrade(args: &Arguments) { // Get connection to source database (current DB in settings) - let source_database = current_db(&settings.database.connect_url).await; + let source_database = current_db(&args.source_database_file).await; // Get connection to destiny database - let dest_database = new_db(&args.database_file).await; + let dest_database = new_db(&args.destiny_database_file).await; println!("Upgrading data from version v1.0.0 to v2.0.0 ..."); diff --git a/upgrades/from_v1_0_0_to_v2_0_0/README.md b/upgrades/from_v1_0_0_to_v2_0_0/README.md index c5ca1601..cd2c1c11 100644 --- a/upgrades/from_v1_0_0_to_v2_0_0/README.md +++ b/upgrades/from_v1_0_0_to_v2_0_0/README.md @@ -7,7 +7,7 @@ To upgrade from version `v1.0.0` to `v2.0.0` you have to follow these steps: - Back up your current database and the `uploads` folder. You can find which database and upload folder are you using in the `Config.toml` file in the root folder of your installation. - Set up a local environment exactly as you have it in production with your production data (DB and torrents folder). - Run the application locally with: `cargo run`. -- Execute the upgrader command: `cargo run --bin upgrade ./data_v2.db ./uploads` +- Execute the upgrader command: `cargo run --bin upgrade ./data.db ./data_v2.db ./uploads` - A new SQLite file should have been created in the root folder: `data_v2.db` - Stop the running application and change the DB configuration to use the newly generated configuration: