-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: [#56] WIP. scaffolding to test upgrader command
- Loading branch information
1 parent
7f0a7ea
commit 44927e5
Showing
12 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
mod databases; | ||
pub mod upgrades; |
7 changes: 7 additions & 0 deletions
7
...rom_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210831113004_torrust_users.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
7 changes: 7 additions & 0 deletions
7
...0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210904135524_torrust_tracker_keys.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
7 changes: 7 additions & 0 deletions
7
...1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210905160623_torrust_categories.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
8 changes: 8 additions & 0 deletions
8
..._0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210907083424_torrust_torrent_files.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
2 changes: 2 additions & 0 deletions
2
...rom_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20211208143338_torrust_users.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
14 changes: 14 additions & 0 deletions
14
..._v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308083424_torrust_torrents.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
2 changes: 2 additions & 0 deletions
2
...1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308170028_torrust_categories.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ALTER TABLE torrust_categories | ||
ADD COLUMN icon VARCHAR(32); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod from_v1_0_0_to_v2_0_0; |