diff --git a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs index 04c04216..8ce447b2 100644 --- a/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs +++ b/src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs @@ -3,6 +3,9 @@ use sqlx::sqlite::{SqlitePoolOptions, SqliteQueryResult}; use sqlx::{query, query_as, SqlitePool}; use crate::databases::database::DatabaseError; +use crate::models::torrent_file::TorrentFile; + +use super::sqlite_v1_0_0::Torrent; #[derive(Debug, Serialize, Deserialize, sqlx::FromRow)] pub struct Category { @@ -178,6 +181,43 @@ impl SqliteDatabaseV2_0_0 { .map(|v| v.last_insert_rowid()) } + pub async fn insert_torrent_file_for_torrent_with_one_file( + &self, + torrent_id: i64, + md5sum: &Option, + length: i64, + ) -> Result { + query( + " + INSERT INTO torrust_torrent_files (md5sum, torrent_id, LENGTH) + VALUES (?, ?, ?)", + ) + .bind(md5sum) + .bind(torrent_id) + .bind(length) + .execute(&self.pool) + .await + .map(|v| v.last_insert_rowid()) + } + + pub async fn insert_torrent_file_for_torrent_with_multiple_files( + &self, + torrent: &Torrent, + file: &TorrentFile, + ) -> Result { + query( + "INSERT INTO torrust_torrent_files (md5sum, torrent_id, LENGTH, PATH) + VALUES (?, ?, ?, ?)", + ) + .bind(file.md5sum.clone()) + .bind(torrent.torrent_id) + .bind(file.length) + .bind(file.path.join("/")) + .execute(&self.pool) + .await + .map(|v| v.last_insert_rowid()) + } + pub async fn delete_all_database_rows(&self) -> Result<(), DatabaseError> { query("DELETE FROM torrust_categories;") .execute(&self.pool) diff --git a/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs b/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs index b6f23e76..71fa762b 100644 --- a/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs +++ b/src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs @@ -322,6 +322,55 @@ async fn transfer_torrents( // TODO + println!("[v2][torrust_torrent_files] adding torrent files"); + + let _is_torrent_with_multiple_files = torrent_from_file.info.files.is_some(); + let is_torrent_with_a_single_file = torrent_from_file.info.length.is_some(); + + if is_torrent_with_a_single_file { + // Only one file is being shared: + // - "path" is NULL + // - "md5sum" can be NULL + + println!( + "[v2][torrust_torrent_files][one] adding torrent file {:?} with length {:?} ...", + &torrent_from_file.info.name, &torrent_from_file.info.length, + ); + + let file_id = dest_database + .insert_torrent_file_for_torrent_with_one_file( + torrent.torrent_id, + // TODO: it seems med5sum can be None. Why? When? + &torrent_from_file.info.md5sum.clone(), + torrent_from_file.info.length.unwrap(), + ) + .await; + + println!( + "[v2][torrust_torrent_files][one] torrent file insert result: {:?}", + &file_id + ); + } else { + // Multiple files are being shared + let files = torrent_from_file.info.files.as_ref().unwrap(); + + for file in files.iter() { + println!( + "[v2][torrust_torrent_files][multiple] adding torrent file: {:?} ...", + &file + ); + + let file_id = dest_database + .insert_torrent_file_for_torrent_with_multiple_files(torrent, file) + .await; + + println!( + "[v2][torrust_torrent_files][multiple] torrent file insert result: {:?}", + &file_id + ); + } + } + // [v2] table torrust_torrent_announce_urls // TODO diff --git a/upgrades/from_v1_0_0_to_v2_0_0/README.md b/upgrades/from_v1_0_0_to_v2_0_0/README.md index ab04a8b4..e635f8a1 100644 --- a/upgrades/from_v1_0_0_to_v2_0_0/README.md +++ b/upgrades/from_v1_0_0_to_v2_0_0/README.md @@ -26,7 +26,7 @@ Before replacing the DB in production you can make some tests like: - Try to log in with a preexisting user. If you do not know any you can create a new "test" user in production before starting with the upgrade process. Users had a different hash algorithm for the password in v1. - Try to create a new user. -- Try to upload and download a new torrent containing a single file. +- Try to upload and download a new torrent containing a single file (with and without md5sum). - Try to upload and download a new torrent containing a folder. ## Notes