Skip to content

Commit 217fae2

Browse files
committed
feat: [#56] take source DB in upgrader command from args
Instead of reading the current configuration.
1 parent aabc3ef commit 217fae2

File tree

3 files changed

+24
-28
lines changed

3 files changed

+24
-28
lines changed

src/bin/upgrade.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! Upgrade command.
22
//! It updates the application from version v1.0.0 to v2.0.0.
3-
//! You can execute it with: `cargo run --bin upgrade ./data_v2.db ./uploads`
3+
//! You can execute it with: `cargo run --bin upgrade ./data.db ./data_v2.db ./uploads`
44
5-
use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::upgrader::upgrade;
5+
use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::upgrader::run_upgrader;
66

77
#[actix_web::main]
88
async fn main() {
9-
upgrade().await;
9+
run_upgrader().await;
1010
}

src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,26 @@ use chrono::prelude::{DateTime, Utc};
2424
use std::{env, error, fs};
2525
use std::{sync::Arc, time::SystemTime};
2626

27-
use crate::config::Configuration;
28-
2927
use text_colorizer::*;
3028

29+
const NUMBER_OF_ARGUMENTS: i64 = 3;
30+
3131
#[derive(Debug)]
32-
struct Arguments {
33-
database_file: String, // The new database
34-
upload_path: String, // The relative dir where torrent files are stored
32+
pub struct Arguments {
33+
source_database_file: String, // The source database in version v1.0.0 we want to migrate
34+
destiny_database_file: String, // The new migrated database in version v2.0.0
35+
upload_path: String, // The relative dir where torrent files are stored
3536
}
3637

3738
fn print_usage() {
3839
eprintln!(
3940
"{} - migrates date from version v1.0.0 to v2.0.0.
4041
41-
cargo run --bin upgrade TARGET_SLQLITE_FILE_PATH TORRENT_UPLOAD_DIR
42+
cargo run --bin upgrade SOURCE_DB_FILE DESTINY_DB_FILE TORRENT_UPLOAD_DIR
4243
4344
For example:
4445
45-
cargo run --bin upgrade ./data_v2.db ./uploads
46+
cargo run --bin upgrade ./data.db ./data_v2.db ./uploads
4647
4748
",
4849
"Upgrader".green()
@@ -52,38 +53,33 @@ fn print_usage() {
5253
fn parse_args() -> Arguments {
5354
let args: Vec<String> = env::args().skip(1).collect();
5455

55-
if args.len() != 2 {
56+
if args.len() != 3 {
5657
eprintln!(
57-
"{} wrong number of arguments: expected 2, got {}",
58+
"{} wrong number of arguments: expected {}, got {}",
5859
"Error".red().bold(),
60+
NUMBER_OF_ARGUMENTS,
5961
args.len()
6062
);
6163
print_usage();
6264
}
6365

6466
Arguments {
65-
database_file: args[0].clone(),
66-
upload_path: args[1].clone(),
67+
source_database_file: args[0].clone(),
68+
destiny_database_file: args[1].clone(),
69+
upload_path: args[2].clone(),
6770
}
6871
}
6972

70-
pub async fn upgrade() {
71-
let args = parse_args();
72-
73-
let cfg = match Configuration::load_from_file().await {
74-
Ok(config) => Arc::new(config),
75-
Err(error) => {
76-
panic!("{}", error)
77-
}
78-
};
79-
80-
let settings = cfg.settings.read().await;
73+
pub async fn run_upgrader() {
74+
upgrade(&parse_args()).await
75+
}
8176

77+
pub async fn upgrade(args: &Arguments) {
8278
// Get connection to source database (current DB in settings)
83-
let source_database = current_db(&settings.database.connect_url).await;
79+
let source_database = current_db(&args.source_database_file).await;
8480

8581
// Get connection to destiny database
86-
let dest_database = new_db(&args.database_file).await;
82+
let dest_database = new_db(&args.destiny_database_file).await;
8783

8884
println!("Upgrading data from version v1.0.0 to v2.0.0 ...");
8985

upgrades/from_v1_0_0_to_v2_0_0/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ To upgrade from version `v1.0.0` to `v2.0.0` you have to follow these steps:
77
- 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.
88
- Set up a local environment exactly as you have it in production with your production data (DB and torrents folder).
99
- Run the application locally with: `cargo run`.
10-
- Execute the upgrader command: `cargo run --bin upgrade ./data_v2.db ./uploads`
10+
- Execute the upgrader command: `cargo run --bin upgrade ./data.db ./data_v2.db ./uploads`
1111
- A new SQLite file should have been created in the root folder: `data_v2.db`
1212
- Stop the running application and change the DB configuration to use the newly generated configuration:
1313

0 commit comments

Comments
 (0)