Skip to content

Commit

Permalink
feat: [#56] take source DB in upgrader command from args
Browse files Browse the repository at this point in the history
Instead of reading the current configuration.
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent aabc3ef commit 217fae2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/bin/upgrade.rs
Original file line number Diff line number Diff line change
@@ -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;
}
44 changes: 20 additions & 24 deletions src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -52,38 +53,33 @@ fn print_usage() {
fn parse_args() -> Arguments {
let args: Vec<String> = 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 ...");

Expand Down
2 changes: 1 addition & 1 deletion upgrades/from_v1_0_0_to_v2_0_0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down

0 comments on commit 217fae2

Please sign in to comment.