diff --git a/tests/mod.rs b/tests/mod.rs index 22adeb6d..27bea3bd 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1 +1,2 @@ mod databases; +pub mod upgrades; diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210831113004_torrust_users.sql b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210831113004_torrust_users.sql new file mode 100644 index 00000000..c535dfb9 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210831113004_torrust_users.sql @@ -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 +) \ No newline at end of file diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210904135524_torrust_tracker_keys.sql b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210904135524_torrust_tracker_keys.sql new file mode 100644 index 00000000..ef6f6865 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210904135524_torrust_tracker_keys.sql @@ -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) +) diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210905160623_torrust_categories.sql b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210905160623_torrust_categories.sql new file mode 100644 index 00000000..c88abfe2 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210905160623_torrust_categories.sql @@ -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'); diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210907083424_torrust_torrent_files.sql b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210907083424_torrust_torrent_files.sql new file mode 100644 index 00000000..aeb3135a --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20210907083424_torrust_torrent_files.sql @@ -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) +) diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20211208143338_torrust_users.sql b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20211208143338_torrust_users.sql new file mode 100644 index 00000000..0b574c69 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20211208143338_torrust_users.sql @@ -0,0 +1,2 @@ +ALTER TABLE torrust_users +ADD COLUMN administrator BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308083424_torrust_torrents.sql b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308083424_torrust_torrents.sql new file mode 100644 index 00000000..413539a4 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308083424_torrust_torrents.sql @@ -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 +) diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308170028_torrust_categories.sql b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308170028_torrust_categories.sql new file mode 100644 index 00000000..b786dcd2 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/fixtures/database/v1.0.0/migrations/20220308170028_torrust_categories.sql @@ -0,0 +1,2 @@ +ALTER TABLE torrust_categories +ADD COLUMN icon VARCHAR(32); diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/mod.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/mod.rs new file mode 100644 index 00000000..3023529a --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/mod.rs @@ -0,0 +1 @@ +pub mod tests; \ No newline at end of file diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/output/.gitignore b/tests/upgrades/from_v1_0_0_to_v2_0_0/output/.gitignore new file mode 100644 index 00000000..3997bead --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/output/.gitignore @@ -0,0 +1 @@ +*.db \ No newline at end of file diff --git a/tests/upgrades/from_v1_0_0_to_v2_0_0/tests.rs b/tests/upgrades/from_v1_0_0_to_v2_0_0/tests.rs new file mode 100644 index 00000000..3ab90e32 --- /dev/null +++ b/tests/upgrades/from_v1_0_0_to_v2_0_0/tests.rs @@ -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 { + 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, 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, + 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); +} diff --git a/tests/upgrades/mod.rs b/tests/upgrades/mod.rs new file mode 100644 index 00000000..736d54f6 --- /dev/null +++ b/tests/upgrades/mod.rs @@ -0,0 +1 @@ +pub mod from_v1_0_0_to_v2_0_0; \ No newline at end of file