|
| 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 | +
|
1 | 10 | use crate::upgrades::from_v1_0_0_to_v2_0_0::databases::sqlite_v1_0_0::SqliteDatabaseV1_0_0; |
2 | 11 | 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}; |
4 | 14 |
|
5 | 15 | use crate::config::Configuration; |
6 | 16 |
|
| 17 | +fn today_iso8601() -> String { |
| 18 | + let dt: DateTime<Utc> = SystemTime::now().into(); |
| 19 | + format!("{}", dt.format("%Y-%m-%d")) |
| 20 | +} |
| 21 | + |
7 | 22 | async fn current_db() -> Arc<SqliteDatabaseV1_0_0> { |
8 | 23 | // Connect to the old v1.0.0 DB |
9 | 24 | let cfg = match Configuration::load_from_file().await { |
@@ -75,5 +90,64 @@ pub async fn upgrade() { |
75 | 90 | reset_destiny_database(dest_database.clone()).await; |
76 | 91 | transfer_categories(source_database.clone(), dest_database.clone()).await; |
77 | 92 |
|
| 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 | + |
78 | 152 | // TODO: WIP. We have to transfer data from the 5 tables in V1 and the torrent files in folder `uploads`. |
79 | 153 | } |
0 commit comments