diff --git a/src/bin/upgrade.rs b/src/bin/upgrade.rs index 15350d1d..1c5a27a3 100644 --- a/src/bin/upgrade.rs +++ b/src/bin/upgrade.rs @@ -1,6 +1,6 @@ //! Upgrade command. //! It updates the application from version v1.0.0 to v2.0.0. -//! You can execute it with: `cargo run --bin upgrade` +//! You can execute it with: `cargo run --bin upgrade ./data_v2.db ./uploads` use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::upgrader::upgrade; 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 e9553392..d084ede4 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 @@ -21,15 +21,54 @@ use crate::{ }; use chrono::prelude::{DateTime, Utc}; -use std::{error, fs}; +use std::{env, error, fs}; use std::{sync::Arc, time::SystemTime}; use crate::config::Configuration; +use text_colorizer::*; + +#[derive(Debug)] +struct Arguments { + database_file: String, // The new database + 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 + + For example: + + cargo run --bin upgrade ./data_v2.db ./uploads + + ", + "Upgrader".green() + ); +} + +fn parse_args() -> Arguments { + let args: Vec = env::args().skip(1).collect(); + + if args.len() != 2 { + eprintln!( + "{} wrong number of arguments: expected 2, got {}", + "Error".red().bold(), + args.len() + ); + print_usage(); + } + + Arguments { + database_file: args[0].clone(), + upload_path: args[1].clone(), + } +} + pub async fn upgrade() { - // TODO: get from command arguments - let database_file = "data_v2.db".to_string(); // The new database - let upload_path = "./uploads".to_string(); // The relative dir where torrent files are stored + let args = parse_args(); let cfg = match Configuration::load_from_file().await { Ok(config) => Arc::new(config), @@ -44,7 +83,7 @@ pub async fn upgrade() { let source_database = current_db(&settings.database.connect_url).await; // Get connection to destiny database - let dest_database = new_db(&database_file).await; + let dest_database = new_db(&args.database_file).await; println!("Upgrading data from version v1.0.0 to v2.0.0 ..."); @@ -53,7 +92,12 @@ pub async fn upgrade() { transfer_categories(source_database.clone(), dest_database.clone()).await; transfer_user_data(source_database.clone(), dest_database.clone()).await; transfer_tracker_keys(source_database.clone(), dest_database.clone()).await; - transfer_torrents(source_database.clone(), dest_database.clone(), &upload_path).await; + transfer_torrents( + source_database.clone(), + dest_database.clone(), + &args.upload_path, + ) + .await; } async fn current_db(connect_url: &str) -> Arc { 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 e635f8a1..c5ca1601 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` +- Execute the upgrader command: `cargo run --bin upgrade ./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: