Skip to content

Commit

Permalink
feat: the upgrader command takes args
Browse files Browse the repository at this point in the history
Removed hardocoded arguments. Now you can use the "upgrader" with:

```
cargo run --bin upgrade ./data_v2.db ./uploads
```

Where "./data_v2.db" is the newly generated DB and "./uploads" the
folder where torrent files weere stored in version v1.0.0.
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 693994f commit aabc3ef
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/bin/upgrade.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
56 changes: 50 additions & 6 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 @@ -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<String> = 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),
Expand All @@ -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 ...");

Expand All @@ -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<SqliteDatabaseV1_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`
- 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:

Expand Down

0 comments on commit aabc3ef

Please sign in to comment.