Skip to content

Commit

Permalink
test: [#56] WIP. scaffolding to test upgrader command
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 7f0a7ea commit 44927e5
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod databases;
pub mod upgrades;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS torrust_users (
user_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
username VARCHAR(32) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL UNIQUE,
email_verified BOOLEAN NOT NULL DEFAULT FALSE,
password TEXT NOT NULL
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS torrust_tracker_keys (
key_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
key VARCHAR(32) NOT NULL,
valid_until INT(10) NOT NULL,
FOREIGN KEY(user_id) REFERENCES torrust_users(user_id)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE torrust_categories (
category_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name VARCHAR(64) NOT NULL UNIQUE
);

INSERT INTO torrust_categories (name) VALUES
('movies'), ('tv shows'), ('games'), ('music'), ('software');
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS torrust_torrent_files (
file_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
torrent_id INTEGER NOT NULL,
number INTEGER NOT NULL,
path VARCHAR(255) NOT NULL,
length INTEGER NOT NULL,
FOREIGN KEY(torrent_id) REFERENCES torrust_torrents(torrent_id)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE torrust_users
ADD COLUMN administrator BOOLEAN NOT NULL DEFAULT FALSE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE IF NOT EXISTS torrust_torrents (
torrent_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
uploader VARCHAR(32) NOT NULL,
info_hash VARCHAR(20) UNIQUE NOT NULL,
title VARCHAR(256) UNIQUE NOT NULL,
category_id INTEGER NOT NULL,
description TEXT,
upload_date INT(10) NOT NULL,
file_size BIGINT NOT NULL,
seeders INTEGER NOT NULL,
leechers INTEGER NOT NULL,
FOREIGN KEY(uploader) REFERENCES torrust_users(username) ON DELETE CASCADE,
FOREIGN KEY(category_id) REFERENCES torrust_categories(category_id) ON DELETE CASCADE
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE torrust_categories
ADD COLUMN icon VARCHAR(32);
1 change: 1 addition & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tests;
1 change: 1 addition & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/output/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.db
77 changes: 77 additions & 0 deletions tests/upgrades/from_v1_0_0_to_v2_0_0/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//! You can run this test with:
//!
//! ```text
//! cargo test upgrade_data_from_version_v1_0_0_to_v2_0_0 -- --nocapture
//! ```
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]
async fn upgrade_data_from_version_v1_0_0_to_v2_0_0() {
/* TODO:
* - Insert data: user, tracker key and torrent
* - Assertions
*/
let fixtures_dir = "./tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/".to_string();
let debug_output_dir = "./tests/upgrades/from_v1_0_0_to_v2_0_0/output/".to_string();

let source_database_file = format!("{}source.db", debug_output_dir);
let destiny_database_file = format!("{}destiny.db", debug_output_dir);

// TODO: use a unique temporary dir
fs::remove_file(&source_database_file).expect("Can't remove source DB file.");
fs::remove_file(&destiny_database_file).expect("Can't remove destiny DB file.");

let source_database = source_db_connection(&source_database_file).await;

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

let args = Arguments {
source_database_file,
destiny_database_file,
upload_path: format!("{}uploads/", fixtures_dir),
};

upgrade(&args).await;
}

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);
}
1 change: 1 addition & 0 deletions tests/upgrades/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod from_v1_0_0_to_v2_0_0;

0 comments on commit 44927e5

Please sign in to comment.