Skip to content

Commit e1790f6

Browse files
committed
refactor: [#56] extract mods in upgrader
1 parent e23d948 commit e1790f6

File tree

9 files changed

+408
-382
lines changed

9 files changed

+408
-382
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
1+
use self::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
2+
use self::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
3+
use std::sync::Arc;
4+
15
pub mod sqlite_v1_0_0;
26
pub mod sqlite_v2_0_0;
7+
8+
pub async fn current_db(db_filename: &str) -> Arc<SqliteDatabaseV1_0_0> {
9+
let source_database_connect_url = format!("sqlite://{}?mode=ro", db_filename);
10+
Arc::new(SqliteDatabaseV1_0_0::new(&source_database_connect_url).await)
11+
}
12+
13+
pub async fn new_db(db_filename: &str) -> Arc<SqliteDatabaseV2_0_0> {
14+
let dest_database_connect_url = format!("sqlite://{}?mode=rwc", db_filename);
15+
Arc::new(SqliteDatabaseV2_0_0::new(&dest_database_connect_url).await)
16+
}
17+
18+
pub async fn migrate_destiny_database(dest_database: Arc<SqliteDatabaseV2_0_0>) {
19+
println!("Running migrations in destiny database...");
20+
dest_database.migrate().await;
21+
}
22+
23+
pub async fn reset_destiny_database(dest_database: Arc<SqliteDatabaseV2_0_0>) {
24+
println!("Truncating all tables in destiny database ...");
25+
dest_database
26+
.delete_all_database_rows()
27+
.await
28+
.expect("Can't reset destiny database.");
29+
}

src/upgrades/from_v1_0_0_to_v2_0_0/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod databases;
2+
pub mod transferrers;
13
pub mod upgrader;
2-
pub mod databases;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
2+
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
3+
use std::sync::Arc;
4+
5+
pub async fn transfer_categories(
6+
source_database: Arc<SqliteDatabaseV1_0_0>,
7+
dest_database: Arc<SqliteDatabaseV2_0_0>,
8+
) {
9+
println!("Transferring categories ...");
10+
11+
let source_categories = source_database.get_categories_order_by_id().await.unwrap();
12+
println!("[v1] categories: {:?}", &source_categories);
13+
14+
let result = dest_database.reset_categories_sequence().await.unwrap();
15+
println!("[v2] reset categories sequence result {:?}", result);
16+
17+
for cat in &source_categories {
18+
println!(
19+
"[v2] adding category {:?} with id {:?} ...",
20+
&cat.name, &cat.category_id
21+
);
22+
let id = dest_database
23+
.insert_category_and_get_id(&cat.name)
24+
.await
25+
.unwrap();
26+
27+
if id != cat.category_id {
28+
panic!(
29+
"Error copying category {:?} from source DB to destiny DB",
30+
&cat.category_id
31+
);
32+
}
33+
34+
println!("[v2] category: {:?} {:?} added.", id, &cat.name);
35+
}
36+
37+
let dest_categories = dest_database.get_categories().await.unwrap();
38+
println!("[v2] categories: {:?}", &dest_categories);
39+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pub mod category_transferrer;
2+
pub mod torrent_transferrer;
3+
pub mod tracker_key_transferrer;
4+
pub mod user_transferrer;
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
use crate::models::torrent_file::Torrent;
2+
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
3+
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
4+
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::TorrentRecordV2;
5+
use crate::utils::parse_torrent::decode_torrent;
6+
use std::sync::Arc;
7+
use std::{error, fs};
8+
9+
pub async fn transfer_torrents(
10+
source_database: Arc<SqliteDatabaseV1_0_0>,
11+
dest_database: Arc<SqliteDatabaseV2_0_0>,
12+
upload_path: &str,
13+
) {
14+
println!("Transferring torrents ...");
15+
16+
// Transfer table `torrust_torrents_files`
17+
18+
// Although the The table `torrust_torrents_files` existed in version v1.0.0
19+
// it was was not used.
20+
21+
// Transfer table `torrust_torrents`
22+
23+
let torrents = source_database.get_torrents().await.unwrap();
24+
25+
for torrent in &torrents {
26+
// [v2] table torrust_torrents
27+
28+
println!(
29+
"[v2][torrust_torrents] adding the torrent: {:?} ...",
30+
&torrent.torrent_id
31+
);
32+
33+
let uploader = source_database
34+
.get_user_by_username(&torrent.uploader)
35+
.await
36+
.unwrap();
37+
38+
if uploader.username != torrent.uploader {
39+
panic!(
40+
"Error copying torrent with id {:?}.
41+
Username (`uploader`) in `torrust_torrents` table does not match `username` in `torrust_users` table",
42+
&torrent.torrent_id
43+
);
44+
}
45+
46+
let filepath = format!("{}/{}.torrent", upload_path, &torrent.torrent_id);
47+
48+
let torrent_from_file_result = read_torrent_from_file(&filepath);
49+
50+
if torrent_from_file_result.is_err() {
51+
panic!("Error torrent file not found: {:?}", &filepath);
52+
}
53+
54+
let torrent_from_file = torrent_from_file_result.unwrap();
55+
56+
let id = dest_database
57+
.insert_torrent(&TorrentRecordV2::from_v1_data(
58+
torrent,
59+
&torrent_from_file.info,
60+
&uploader,
61+
))
62+
.await
63+
.unwrap();
64+
65+
if id != torrent.torrent_id {
66+
panic!(
67+
"Error copying torrent {:?} from source DB to destiny DB",
68+
&torrent.torrent_id
69+
);
70+
}
71+
72+
println!(
73+
"[v2][torrust_torrents] torrent with id {:?} added.",
74+
&torrent.torrent_id
75+
);
76+
77+
// [v2] table torrust_torrent_files
78+
79+
println!("[v2][torrust_torrent_files] adding torrent files");
80+
81+
if torrent_from_file.is_a_single_file_torrent() {
82+
// The torrent contains only one file then:
83+
// - "path" is NULL
84+
// - "md5sum" can be NULL
85+
86+
println!(
87+
"[v2][torrust_torrent_files][single-file-torrent] adding torrent file {:?} with length {:?} ...",
88+
&torrent_from_file.info.name, &torrent_from_file.info.length,
89+
);
90+
91+
let file_id = dest_database
92+
.insert_torrent_file_for_torrent_with_one_file(
93+
torrent.torrent_id,
94+
// TODO: it seems med5sum can be None. Why? When?
95+
&torrent_from_file.info.md5sum.clone(),
96+
torrent_from_file.info.length.unwrap(),
97+
)
98+
.await;
99+
100+
println!(
101+
"[v2][torrust_torrent_files][single-file-torrent] torrent file insert result: {:?}",
102+
&file_id
103+
);
104+
} else {
105+
// Multiple files are being shared
106+
let files = torrent_from_file.info.files.as_ref().unwrap();
107+
108+
for file in files.iter() {
109+
println!(
110+
"[v2][torrust_torrent_files][multiple-file-torrent] adding torrent file: {:?} ...",
111+
&file
112+
);
113+
114+
let file_id = dest_database
115+
.insert_torrent_file_for_torrent_with_multiple_files(torrent, file)
116+
.await;
117+
118+
println!(
119+
"[v2][torrust_torrent_files][multiple-file-torrent] torrent file insert result: {:?}",
120+
&file_id
121+
);
122+
}
123+
}
124+
125+
// [v2] table torrust_torrent_info
126+
127+
println!(
128+
"[v2][torrust_torrent_info] adding the torrent info for torrent id {:?} ...",
129+
&torrent.torrent_id
130+
);
131+
132+
let id = dest_database.insert_torrent_info(torrent).await;
133+
134+
println!(
135+
"[v2][torrust_torrents] torrent info insert result: {:?}.",
136+
&id
137+
);
138+
139+
// [v2] table torrust_torrent_announce_urls
140+
141+
println!(
142+
"[v2][torrust_torrent_announce_urls] adding the torrent announce url for torrent id {:?} ...",
143+
&torrent.torrent_id
144+
);
145+
146+
if torrent_from_file.announce_list.is_some() {
147+
// BEP-0012. Multiple trackers.
148+
149+
println!("[v2][torrust_torrent_announce_urls][announce-list] adding the torrent announce url for torrent id {:?} ...", &torrent.torrent_id);
150+
151+
// flatten the nested vec (this will however remove the)
152+
let announce_urls = torrent_from_file
153+
.announce_list
154+
.clone()
155+
.unwrap()
156+
.into_iter()
157+
.flatten()
158+
.collect::<Vec<String>>();
159+
160+
for tracker_url in announce_urls.iter() {
161+
println!("[v2][torrust_torrent_announce_urls][announce-list] adding the torrent announce url for torrent id {:?} ...", &torrent.torrent_id);
162+
163+
let announce_url_id = dest_database
164+
.insert_torrent_announce_url(torrent.torrent_id, tracker_url)
165+
.await;
166+
167+
println!("[v2][torrust_torrent_announce_urls][announce-list] torrent announce url insert result {:?} ...", &announce_url_id);
168+
}
169+
} else if torrent_from_file.announce.is_some() {
170+
println!("[v2][torrust_torrent_announce_urls][announce] adding the torrent announce url for torrent id {:?} ...", &torrent.torrent_id);
171+
172+
let announce_url_id = dest_database
173+
.insert_torrent_announce_url(
174+
torrent.torrent_id,
175+
&torrent_from_file.announce.unwrap(),
176+
)
177+
.await;
178+
179+
println!(
180+
"[v2][torrust_torrent_announce_urls][announce] torrent announce url insert result {:?} ...",
181+
&announce_url_id
182+
);
183+
}
184+
}
185+
println!("Torrents transferred");
186+
}
187+
188+
pub fn read_torrent_from_file(path: &str) -> Result<Torrent, Box<dyn error::Error>> {
189+
let contents = match fs::read(path) {
190+
Ok(contents) => contents,
191+
Err(e) => return Err(e.into()),
192+
};
193+
194+
match decode_torrent(&contents) {
195+
Ok(torrent) => Ok(torrent),
196+
Err(e) => Err(e),
197+
}
198+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
2+
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
3+
use std::sync::Arc;
4+
5+
pub async fn transfer_tracker_keys(
6+
source_database: Arc<SqliteDatabaseV1_0_0>,
7+
dest_database: Arc<SqliteDatabaseV2_0_0>,
8+
) {
9+
println!("Transferring tracker keys ...");
10+
11+
// Transfer table `torrust_tracker_keys`
12+
13+
let tracker_keys = source_database.get_tracker_keys().await.unwrap();
14+
15+
for tracker_key in &tracker_keys {
16+
// [v2] table torrust_tracker_keys
17+
18+
println!(
19+
"[v2][torrust_users] adding the tracker key with id {:?} ...",
20+
&tracker_key.key_id
21+
);
22+
23+
let id = dest_database
24+
.insert_tracker_key(
25+
tracker_key.key_id,
26+
tracker_key.user_id,
27+
&tracker_key.key,
28+
tracker_key.valid_until,
29+
)
30+
.await
31+
.unwrap();
32+
33+
if id != tracker_key.key_id {
34+
panic!(
35+
"Error copying tracker key {:?} from source DB to destiny DB",
36+
&tracker_key.key_id
37+
);
38+
}
39+
40+
println!(
41+
"[v2][torrust_tracker_keys] tracker key with id {:?} added.",
42+
&tracker_key.key_id
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)