Skip to content

Commit

Permalink
feat: imported users have importation date instead of registrataion date
Browse files Browse the repository at this point in the history
- Now registration date is optional (we allow NULL) for imported users.
- And imported users have an importation date, which is NULL for
  registered users throught the sdantadrd registration process.
  • Loading branch information
josecelano committed Nov 30, 2022
1 parent 21174d4 commit 99edf52
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_users CHANGE date_registered date_registered DATETIME NOT NULL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_users ADD COLUMN date_imported DATETIME DEFAULT NULL
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS torrust_users_new (
user_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
date_registered TEXT DEFAULT NULL,
administrator BOOL NOT NULL DEFAULT FALSE
);

INSERT INTO torrust_users_new SELECT * FROM torrust_users;

DROP TABLE torrust_users;

ALTER TABLE torrust_users_new RENAME TO torrust_users
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE torrust_users ADD COLUMN date_imported TEXT DEFAULT NULL
6 changes: 4 additions & 2 deletions src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)]
pub struct User {
pub user_id: i64,
pub date_registered: String,
pub date_registered: Option<String>,
pub date_imported: Option<String>,
pub administrator: bool,
}

Expand Down Expand Up @@ -33,7 +34,8 @@ pub struct UserCompact {
#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)]
pub struct UserFull {
pub user_id: i64,
pub date_registered: String,
pub date_registered: Option<String>,
pub date_imported: Option<String>,
pub administrator: bool,
pub username: String,
pub email: String,
Expand Down
8 changes: 4 additions & 4 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 @@ -67,17 +67,17 @@ impl SqliteDatabaseV2_0_0 {
})
}

pub async fn insert_user(
pub async fn insert_imported_user(
&self,
user_id: i64,
date_registered: &str,
date_imported: &str,
administrator: bool,
) -> Result<i64, sqlx::Error> {
query(
"INSERT INTO torrust_users (user_id, date_registered, administrator) VALUES (?, ?, ?)",
"INSERT INTO torrust_users (user_id, date_imported, administrator) VALUES (?, ?, ?)",
)
.bind(user_id)
.bind(date_registered)
.bind(date_imported)
.bind(administrator)
.execute(&self.pool)
.await
Expand Down
7 changes: 4 additions & 3 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 @@ -3,7 +3,8 @@
//! NOTES for `torrust_users` table transfer:
//!
//! - In v2, the table `torrust_user` contains a field `date_registered` non existing in v1.
//! It's used the day when the upgrade command is executed.
//! We changed that columns to allow NULL. WE also added the new column `date_imported` with
//! the datetime when the upgrader was executed.
//! - In v2, the table `torrust_user_profiles` contains two new fields: `bio` and `avatar`.
//! Empty string is used as default value.

Expand Down Expand Up @@ -129,10 +130,10 @@ async fn transfer_user_data(
&user.user_id, &user.username
);

let default_data_registered = today_iso8601();
let date_imported = today_iso8601();

let id = dest_database
.insert_user(user.user_id, &default_data_registered, user.administrator)
.insert_imported_user(user.user_id, &date_imported, user.administrator)
.await
.unwrap();

Expand Down

0 comments on commit 99edf52

Please sign in to comment.