Skip to content

Commit

Permalink
refactor: extract mod sqlite_v1_0_0 in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 44927e5 commit 6188b10
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
3 changes: 2 additions & 1 deletion tests/upgrades/from_v1_0_0_to_v2_0_0/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod tests;
pub mod sqlite_v1_0_0;
pub mod tests;
53 changes: 53 additions & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/sqlite_v1_0_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::SqlitePool;
use std::fs;

pub struct SqliteDatabaseV1_0_0 {
pub pool: SqlitePool,
}

impl SqliteDatabaseV1_0_0 {
pub async fn db_connection(source_database_file: &str) -> Self {
let source_database_connect_url = format!("sqlite://{}?mode=rwc", source_database_file);
SqliteDatabaseV1_0_0::new(&source_database_connect_url).await
}

pub async fn new(database_url: &str) -> Self {
let db = SqlitePoolOptions::new()
.connect(database_url)
.await
.expect("Unable to create database pool.");
Self { pool: db }
}

/// Execute migrations for database in version v1.0.0
pub async fn migrate(&self, fixtures_dir: &str) {
let migrations_dir = format!("{}database/v1.0.0/migrations/", fixtures_dir);

let migrations = vec![
"20210831113004_torrust_users.sql",
"20210904135524_torrust_tracker_keys.sql",
"20210905160623_torrust_categories.sql",
"20210907083424_torrust_torrent_files.sql",
"20211208143338_torrust_users.sql",
"20220308083424_torrust_torrents.sql",
"20220308170028_torrust_categories.sql",
];

for migration_file_name in &migrations {
let migration_file_path = format!("{}{}", &migrations_dir, &migration_file_name);
self.run_migration_from_file(&migration_file_path).await;
}
}

async fn run_migration_from_file(&self, migration_file_path: &str) {
println!("Executing migration: {:?}", migration_file_path);

let sql = fs::read_to_string(migration_file_path)
.expect("Should have been able to read the file");

let res = sqlx::query(&sql).execute(&self.pool).await;

println!("Migration result {:?}", res);
}
}
41 changes: 3 additions & 38 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! ```text
//! cargo test upgrade_data_from_version_v1_0_0_to_v2_0_0 -- --nocapture
//! ```
use crate::upgrades::from_v1_0_0_to_v2_0_0::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
use std::fs;
use std::sync::Arc;
use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
use torrust_index_backend::upgrades::from_v1_0_0_to_v2_0_0::upgrader::{upgrade, Arguments};

#[tokio::test]
Expand All @@ -26,7 +26,7 @@ async fn upgrade_data_from_version_v1_0_0_to_v2_0_0() {

let source_database = source_db_connection(&source_database_file).await;

migrate(source_database.clone(), &fixtures_dir).await;
source_database.migrate(&fixtures_dir).await;

let args = Arguments {
source_database_file,
Expand All @@ -38,40 +38,5 @@ async fn upgrade_data_from_version_v1_0_0_to_v2_0_0() {
}

async fn source_db_connection(source_database_file: &str) -> Arc<SqliteDatabaseV1_0_0> {
let source_database_connect_url = format!("sqlite://{}?mode=rwc", source_database_file);
Arc::new(SqliteDatabaseV1_0_0::new(&source_database_connect_url).await)
}

/// Execute migrations for database in version v1.0.0
async fn migrate(source_database: Arc<SqliteDatabaseV1_0_0>, fixtures_dir: &str) {
let migrations_dir = format!("{}database/v1.0.0/migrations/", fixtures_dir);

let migrations = vec![
"20210831113004_torrust_users.sql",
"20210904135524_torrust_tracker_keys.sql",
"20210905160623_torrust_categories.sql",
"20210907083424_torrust_torrent_files.sql",
"20211208143338_torrust_users.sql",
"20220308083424_torrust_torrents.sql",
"20220308170028_torrust_categories.sql",
];

for migration_file_name in &migrations {
let migration_file_path = format!("{}{}", &migrations_dir, &migration_file_name);
run_migration_from_file(source_database.clone(), &migration_file_path).await;
}
}

async fn run_migration_from_file(
source_database: Arc<SqliteDatabaseV1_0_0>,
migration_file_path: &str,
) {
println!("Executing migration: {:?}", migration_file_path);

let sql =
fs::read_to_string(migration_file_path).expect("Should have been able to read the file");

let res = sqlx::query(&sql).execute(&source_database.pool).await;

println!("Migration result {:?}", res);
Arc::new(SqliteDatabaseV1_0_0::db_connection(&source_database_file).await)
}

0 comments on commit 6188b10

Please sign in to comment.