Skip to content

Commit 3fea6ea

Browse files
committed
feat: [#56] trasnfer torrents (2/4 tables) from v1.0.0 to v2.0.0
1 parent 03e4bef commit 3fea6ea

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use sqlx::sqlite::{SqlitePoolOptions, SqliteQueryResult};
33
use sqlx::{query, query_as, SqlitePool};
44

55
use crate::databases::database::DatabaseError;
6+
use crate::models::torrent_file::TorrentFile;
7+
8+
use super::sqlite_v1_0_0::Torrent;
69

710
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
811
pub struct Category {
@@ -178,6 +181,43 @@ impl SqliteDatabaseV2_0_0 {
178181
.map(|v| v.last_insert_rowid())
179182
}
180183

184+
pub async fn insert_torrent_file_for_torrent_with_one_file(
185+
&self,
186+
torrent_id: i64,
187+
md5sum: &Option<String>,
188+
length: i64,
189+
) -> Result<i64, sqlx::Error> {
190+
query(
191+
"
192+
INSERT INTO torrust_torrent_files (md5sum, torrent_id, LENGTH)
193+
VALUES (?, ?, ?)",
194+
)
195+
.bind(md5sum)
196+
.bind(torrent_id)
197+
.bind(length)
198+
.execute(&self.pool)
199+
.await
200+
.map(|v| v.last_insert_rowid())
201+
}
202+
203+
pub async fn insert_torrent_file_for_torrent_with_multiple_files(
204+
&self,
205+
torrent: &Torrent,
206+
file: &TorrentFile,
207+
) -> Result<i64, sqlx::Error> {
208+
query(
209+
"INSERT INTO torrust_torrent_files (md5sum, torrent_id, LENGTH, PATH)
210+
VALUES (?, ?, ?, ?)",
211+
)
212+
.bind(file.md5sum.clone())
213+
.bind(torrent.torrent_id)
214+
.bind(file.length)
215+
.bind(file.path.join("/"))
216+
.execute(&self.pool)
217+
.await
218+
.map(|v| v.last_insert_rowid())
219+
}
220+
181221
pub async fn delete_all_database_rows(&self) -> Result<(), DatabaseError> {
182222
query("DELETE FROM torrust_categories;")
183223
.execute(&self.pool)

src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,55 @@ async fn transfer_torrents(
322322

323323
// TODO
324324

325+
println!("[v2][torrust_torrent_files] adding torrent files");
326+
327+
let _is_torrent_with_multiple_files = torrent_from_file.info.files.is_some();
328+
let is_torrent_with_a_single_file = torrent_from_file.info.length.is_some();
329+
330+
if is_torrent_with_a_single_file {
331+
// Only one file is being shared:
332+
// - "path" is NULL
333+
// - "md5sum" can be NULL
334+
335+
println!(
336+
"[v2][torrust_torrent_files][one] adding torrent file {:?} with length {:?} ...",
337+
&torrent_from_file.info.name, &torrent_from_file.info.length,
338+
);
339+
340+
let file_id = dest_database
341+
.insert_torrent_file_for_torrent_with_one_file(
342+
torrent.torrent_id,
343+
// TODO: it seems med5sum can be None. Why? When?
344+
&torrent_from_file.info.md5sum.clone(),
345+
torrent_from_file.info.length.unwrap(),
346+
)
347+
.await;
348+
349+
println!(
350+
"[v2][torrust_torrent_files][one] torrent file insert result: {:?}",
351+
&file_id
352+
);
353+
} else {
354+
// Multiple files are being shared
355+
let files = torrent_from_file.info.files.as_ref().unwrap();
356+
357+
for file in files.iter() {
358+
println!(
359+
"[v2][torrust_torrent_files][multiple] adding torrent file: {:?} ...",
360+
&file
361+
);
362+
363+
let file_id = dest_database
364+
.insert_torrent_file_for_torrent_with_multiple_files(torrent, file)
365+
.await;
366+
367+
println!(
368+
"[v2][torrust_torrent_files][multiple] torrent file insert result: {:?}",
369+
&file_id
370+
);
371+
}
372+
}
373+
325374
// [v2] table torrust_torrent_announce_urls
326375

327376
// TODO

upgrades/from_v1_0_0_to_v2_0_0/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Before replacing the DB in production you can make some tests like:
2626

2727
- 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.
2828
- Try to create a new user.
29-
- Try to upload and download a new torrent containing a single file.
29+
- Try to upload and download a new torrent containing a single file (with and without md5sum).
3030
- Try to upload and download a new torrent containing a folder.
3131

3232
## Notes

0 commit comments

Comments
 (0)