Skip to content

Commit d1059f5

Browse files
committed
feat: [#56] trasnfer user data from v1.0.0 to v2.0.0
TODO: transfer password
1 parent d590972 commit d1059f5

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v1_0_0.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ pub struct Category {
99
pub category_id: i64,
1010
pub name: String,
1111
}
12+
13+
#[derive(Debug, Serialize, Deserialize, sqlx::FromRow)]
14+
pub struct User {
15+
pub user_id: i64,
16+
pub username: String,
17+
pub email: String,
18+
pub email_verified: bool,
19+
pub password: String,
20+
pub administrator: bool,
21+
}
22+
1223
pub struct SqliteDatabaseV1_0_0 {
1324
pub pool: SqlitePool,
1425
}
@@ -30,4 +41,12 @@ impl SqliteDatabaseV1_0_0 {
3041
.await
3142
.map_err(|_| DatabaseError::Error)
3243
}
44+
45+
pub async fn get_users(&self) -> Result<Vec<User>, sqlx::Error> {
46+
query_as::<_, User>(
47+
"SELECT * FROM torrust_users ORDER BY user_id ASC",
48+
)
49+
.fetch_all(&self.pool)
50+
.await
51+
}
3352
}

src/upgrades/from_v1_0_0_to_v2_0_0/databases/sqlite_v2_0_0.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,44 @@ impl SqliteDatabaseV2_0_0 {
5757
})
5858
}
5959

60+
pub async fn insert_user(
61+
&self,
62+
user_id: i64,
63+
date_registered: &str,
64+
administrator: bool,
65+
) -> Result<i64, sqlx::Error> {
66+
query(
67+
"INSERT INTO torrust_users (user_id, date_registered, administrator) VALUES (?, ?, ?)",
68+
)
69+
.bind(user_id)
70+
.bind(date_registered)
71+
.bind(administrator)
72+
.execute(&self.pool)
73+
.await
74+
.map(|v| v.last_insert_rowid())
75+
}
76+
77+
pub async fn insert_user_profile(
78+
&self,
79+
user_id: i64,
80+
username: &str,
81+
email: &str,
82+
email_verified: bool,
83+
bio: &str,
84+
avatar: &str,
85+
) -> Result<i64, sqlx::Error> {
86+
query("INSERT INTO torrust_user_profiles (user_id, username, email, email_verified, bio, avatar) VALUES (?, ?, ?, ?, ?, ?)")
87+
.bind(user_id)
88+
.bind(username)
89+
.bind(email)
90+
.bind(email_verified)
91+
.bind(bio)
92+
.bind(avatar)
93+
.execute(&self.pool)
94+
.await
95+
.map(|v| v.last_insert_rowid())
96+
}
97+
6098
pub async fn delete_all_database_rows(&self) -> Result<(), DatabaseError> {
6199
query("DELETE FROM torrust_categories;")
62100
.execute(&self.pool)

src/upgrades/from_v1_0_0_to_v2_0_0/upgrader.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
//! It updates the application from version v1.0.0 to v2.0.0.
2+
//!
3+
//! NOTES for `torrust_users` table transfer:
4+
//!
5+
//! - In v2, the table `torrust_user` contains a field `date_registered` non existing in v1.
6+
//! It's used the day when the upgrade command is executed.
7+
//! - In v2, the table `torrust_user_profiles` contains two new fields: `bio` and `avatar`.
8+
//! Empty string is used as default value.
9+
110
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::SqliteDatabaseV1_0_0;
211
use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v2_0_0::SqliteDatabaseV2_0_0;
3-
use std::sync::Arc;
12+
use chrono::prelude::{DateTime, Utc};
13+
use std::{sync::Arc, time::SystemTime};
414

515
use crate::config::Configuration;
616

17+
fn today_iso8601() -> String {
18+
let dt: DateTime<Utc> = SystemTime::now().into();
19+
format!("{}", dt.format("%Y-%m-%d"))
20+
}
21+
722
async fn current_db() -> Arc<SqliteDatabaseV1_0_0> {
823
// Connect to the old v1.0.0 DB
924
let cfg = match Configuration::load_from_file().await {
@@ -75,5 +90,64 @@ pub async fn upgrade() {
7590
reset_destiny_database(dest_database.clone()).await;
7691
transfer_categories(source_database.clone(), dest_database.clone()).await;
7792

93+
// Transfer `torrust_users`
94+
95+
let users = source_database.get_users().await.unwrap();
96+
97+
for user in &users {
98+
// [v2] table torrust_users
99+
100+
println!(
101+
"[v2][torrust_users] adding user: {:?} {:?} ...",
102+
&user.user_id, &user.username
103+
);
104+
105+
let default_data_registered = today_iso8601();
106+
107+
let id = dest_database
108+
.insert_user(user.user_id, &default_data_registered, user.administrator)
109+
.await
110+
.unwrap();
111+
112+
if id != user.user_id {
113+
panic!(
114+
"Error copying user {:?} from source DB to destiny DB",
115+
&user.user_id
116+
);
117+
}
118+
119+
println!(
120+
"[v2][torrust_users] user: {:?} {:?} added.",
121+
&user.user_id, &user.username
122+
);
123+
124+
// [v2] table torrust_user_profiles
125+
126+
println!(
127+
"[v2][torrust_user_profiles] adding user: {:?} {:?} ...",
128+
&user.user_id, &user.username
129+
);
130+
131+
let default_user_bio = "".to_string();
132+
let default_user_avatar = "".to_string();
133+
134+
dest_database
135+
.insert_user_profile(
136+
user.user_id,
137+
&user.username,
138+
&user.email,
139+
user.email_verified,
140+
&default_user_bio,
141+
&default_user_avatar,
142+
)
143+
.await
144+
.unwrap();
145+
146+
println!(
147+
"[v2][torrust_user_profiles] user: {:?} {:?} added.",
148+
&user.user_id, &user.username
149+
);
150+
}
151+
78152
// TODO: WIP. We have to transfer data from the 5 tables in V1 and the torrent files in folder `uploads`.
79153
}

0 commit comments

Comments
 (0)