Skip to content

Commit

Permalink
feat: [#56] trasnfer torrents (4/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 8bdf32f commit 21174d4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
13 changes: 13 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 @@ -231,6 +231,19 @@ impl SqliteDatabaseV2_0_0 {
.map(|v| v.last_insert_rowid())
}

pub async fn insert_torrent_announce_url(
&self,
torrent_id: i64,
tracker_url: &str,
) -> Result<i64, sqlx::Error> {
query("INSERT INTO torrust_torrent_announce_urls (torrent_id, tracker_url) VALUES (?, ?)")
.bind(torrent_id)
.bind(tracker_url)
.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
53 changes: 48 additions & 5 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 @@ -268,6 +268,8 @@ async fn transfer_torrents(
// TODO: confirm with @WarmBeer that
// - All torrents were public in version v1.0.0
// - Infohashes were in lowercase en v1.0. and uppercase in version v2.0.0
// - Only one option is used for announce url if we have two the announce and the announce list.
// And announce has priority over announce list.
let private = false;

let uploader = source_database
Expand Down Expand Up @@ -371,10 +373,6 @@ async fn transfer_torrents(
}
}

// [v2] table torrust_torrent_announce_urls

// TODO

// [v2] table torrust_torrent_info

println!(
Expand All @@ -389,8 +387,53 @@ async fn transfer_torrents(
&id
);

println!("Torrents transferred");
// [v2] table torrust_torrent_announce_urls

println!(
"[v2][torrust_torrent_announce_urls] adding the torrent announce url for torrent {:?} ...",
&torrent.torrent_id
);

if torrent_from_file.announce.is_some() {
println!("[v2][torrust_torrent_announce_urls] adding the torrent announce url for torrent {:?} ...", &torrent.torrent_id);

let announce_url_id = dest_database
.insert_torrent_announce_url(
torrent.torrent_id,
&torrent_from_file.announce.unwrap(),
)
.await;

println!(
"[v2][torrust_torrent_announce_urls] torrent announce url insert result {:?} ...",
&announce_url_id
);
} else if torrent_from_file.announce_list.is_some() {
// BEP-0012. Multiple trackers.

println!("[v2][torrust_torrent_announce_urls] adding the torrent announce url for torrent {:?} ...", &torrent.torrent_id);

// flatten the nested vec (this will however remove the)
let announce_urls = torrent_from_file
.announce_list
.clone()
.unwrap()
.into_iter()
.flatten()
.collect::<Vec<String>>();

for tracker_url in announce_urls.iter() {
println!("[v2][torrust_torrent_announce_urls] adding the torrent announce url (from announce list) for torrent {:?} ...", &torrent.torrent_id);

let announce_url_id = dest_database
.insert_torrent_announce_url(torrent.torrent_id, tracker_url)
.await;

println!("[v2][torrust_torrent_announce_urls] torrent announce url insert result {:?} ...", &announce_url_id);
}
}
}
println!("Torrents transferred");
}

fn read_torrent_from_file(path: &str) -> Result<Torrent, Box<dyn error::Error>> {
Expand Down

0 comments on commit 21174d4

Please sign in to comment.