Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: missing tables in Database::delete_all_database_rows function #220

Merged
merged 2 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ use crate::models::torrent_tag::{TagId, TorrentTag};
use crate::models::tracker_key::TrackerKey;
use crate::models::user::{User, UserAuthentication, UserCompact, UserId, UserProfile};

/// Database tables to be truncated when upgrading from v1.0.0 to v2.0.0.
/// They must be in the correct order to avoid foreign key errors.
pub const TABLES_TO_TRUNCATE: &[&str] = &[
"torrust_torrent_announce_urls",
"torrust_torrent_files",
"torrust_torrent_info",
"torrust_torrent_tag_links",
"torrust_torrent_tracker_stats",
"torrust_torrents",
"torrust_tracker_keys",
"torrust_user_authentication",
"torrust_user_bans",
"torrust_user_invitation_uses",
"torrust_user_invitations",
"torrust_user_profiles",
"torrust_user_public_keys",
"torrust_users",
"torrust_categories",
"torrust_torrent_tags",
];

/// Database drivers.
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub enum Driver {
Expand Down
56 changes: 7 additions & 49 deletions src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chrono::NaiveDateTime;
use sqlx::mysql::{MySqlConnectOptions, MySqlPoolOptions};
use sqlx::{query, query_as, Acquire, ConnectOptions, MySqlPool};

use super::database::TABLES_TO_TRUNCATE;
use crate::databases::database;
use crate::databases::database::{Category, Database, Driver, Sorting, TorrentCompact};
use crate::models::category::CategoryId;
Expand Down Expand Up @@ -852,55 +853,12 @@ impl Database for Mysql {
}

async fn delete_all_database_rows(&self) -> Result<(), database::Error> {
query("DELETE FROM torrust_categories;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_torrents;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_tracker_keys;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_users;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_authentication;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_bans;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_invitations;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_profiles;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_torrents;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_public_keys;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;
for table in TABLES_TO_TRUNCATE {
query(&format!("DELETE FROM {table};"))
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;
}

Ok(())
}
Expand Down
56 changes: 7 additions & 49 deletions src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use chrono::NaiveDateTime;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{query, query_as, Acquire, ConnectOptions, SqlitePool};

use super::database::TABLES_TO_TRUNCATE;
use crate::databases::database;
use crate::databases::database::{Category, Database, Driver, Sorting, TorrentCompact};
use crate::models::category::CategoryId;
Expand Down Expand Up @@ -842,55 +843,12 @@ impl Database for Sqlite {
}

async fn delete_all_database_rows(&self) -> Result<(), database::Error> {
query("DELETE FROM torrust_categories;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_torrents;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_tracker_keys;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_users;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_authentication;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_bans;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_invitations;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_profiles;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_torrents;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;

query("DELETE FROM torrust_user_public_keys;")
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;
for table in TABLES_TO_TRUNCATE {
query(&format!("DELETE FROM {table};"))
.execute(&self.pool)
.await
.map_err(|_| database::Error::Error)?;
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/upgrades/from_v1_0_0_to_v2_0_0/databases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn migrate_target_database(target_database: Arc<SqliteDatabaseV2_0_0>)
target_database.migrate().await;
}

pub async fn reset_target_database(target_database: Arc<SqliteDatabaseV2_0_0>) {
pub async fn truncate_target_database(target_database: Arc<SqliteDatabaseV2_0_0>) {
println!("Truncating all tables in target database ...");
target_database
.delete_all_database_rows()
Expand Down
36 changes: 7 additions & 29 deletions src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sqlx::sqlite::{SqlitePoolOptions, SqliteQueryResult};
use sqlx::{query, query_as, SqlitePool};

use super::sqlite_v1_0_0::{TorrentRecordV1, UserRecordV1};
use crate::databases::database;
use crate::databases::database::{self, TABLES_TO_TRUNCATE};
use crate::models::torrent_file::{TorrentFile, TorrentInfo};

#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
Expand Down Expand Up @@ -261,34 +261,12 @@ impl SqliteDatabaseV2_0_0 {

#[allow(clippy::missing_panics_doc)]
pub async fn delete_all_database_rows(&self) -> Result<(), database::Error> {
query("DELETE FROM torrust_categories").execute(&self.pool).await.unwrap();

query("DELETE FROM torrust_torrents").execute(&self.pool).await.unwrap();

query("DELETE FROM torrust_tracker_keys").execute(&self.pool).await.unwrap();

query("DELETE FROM torrust_users").execute(&self.pool).await.unwrap();

query("DELETE FROM torrust_user_authentication")
.execute(&self.pool)
.await
.unwrap();

query("DELETE FROM torrust_user_bans").execute(&self.pool).await.unwrap();

query("DELETE FROM torrust_user_invitations")
.execute(&self.pool)
.await
.unwrap();

query("DELETE FROM torrust_user_profiles").execute(&self.pool).await.unwrap();

query("DELETE FROM torrust_torrents").execute(&self.pool).await.unwrap();

query("DELETE FROM torrust_user_public_keys")
.execute(&self.pool)
.await
.unwrap();
for table in TABLES_TO_TRUNCATE {
query(&format!("DELETE FROM {table};"))
.execute(&self.pool)
.await
.expect("table {table} should be deleted");
}

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use std::time::SystemTime;
use chrono::prelude::{DateTime, Utc};
use text_colorizer::Colorize;

use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::{current_db, migrate_target_database, new_db, reset_target_database};
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::{current_db, migrate_target_database, new_db, truncate_target_database};
use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::category_transferrer::transfer_categories;
use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::torrent_transferrer::transfer_torrents;
use crate::upgrades::from_v1_0_0_to_v2_0_0::transferrers::tracker_key_transferrer::transfer_tracker_keys;
Expand Down Expand Up @@ -120,7 +120,7 @@ pub async fn upgrade(args: &Arguments, date_imported: &str) {
println!("Upgrading data from version v1.0.0 to v2.0.0 ...");

migrate_target_database(target_database.clone()).await;
reset_target_database(target_database.clone()).await;
truncate_target_database(target_database.clone()).await;

transfer_categories(source_database.clone(), target_database.clone()).await;
transfer_users(source_database.clone(), target_database.clone(), date_imported).await;
Expand Down
47 changes: 0 additions & 47 deletions tests/databases/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions tests/databases/docker-compose.yml

This file was deleted.

36 changes: 0 additions & 36 deletions tests/databases/mod.rs

This file was deleted.

15 changes: 0 additions & 15 deletions tests/databases/mysql.rs

This file was deleted.

8 changes: 0 additions & 8 deletions tests/databases/sqlite.rs

This file was deleted.

Loading