Skip to content

Commit

Permalink
fix: [torrust#194] add missing tables to truncate when reseting the DB
Browse files Browse the repository at this point in the history
When you upgrade from v1.0.0 to v2.0.0 you can run an upgrader which
will truncate all the tables in the target database. There were some
missing tables in the list. Probably, there wre tables added after
creting the upgrader command.
  • Loading branch information
josecelano committed Jun 26, 2023
1 parent 7a06fe8 commit 986d2f8
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 130 deletions.
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

0 comments on commit 986d2f8

Please sign in to comment.