Skip to content

Commit

Permalink
feat: [torrust#56] trasnfer torrents (2/4 tables) from v1.0.0 to v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 03e4bef commit 3fea6ea
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
40 changes: 40 additions & 0 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 @@ -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 {
Expand Down Expand Up @@ -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<String>,
length: i64,
) -> Result<i64, sqlx::Error> {
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<i64, sqlx::Error> {
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)
Expand Down
49 changes: 49 additions & 0 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 @@ -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
Expand Down
2 changes: 1 addition & 1 deletion upgrades/from_v1_0_0_to_v2_0_0/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3fea6ea

Please sign in to comment.