Skip to content

Commit

Permalink
feat: Add timestamps for when email change is confirmed
Browse files Browse the repository at this point in the history
  • Loading branch information
spencewenski committed Dec 26, 2024
1 parent 8ce0be6 commit 4b40eb3
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use crate::migration::user::User;
use sea_orm_migration::{prelude::*, schema::*};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.alter_table(alter_table_add_columns()).await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.alter_table(alter_table_drop_columns()).await
}
}

fn alter_table_add_columns() -> TableAlterStatement {
Table::alter()
.table(User::Table)
.add_column_if_not_exists(timestamp_with_time_zone_null(
User::EmailChangeNewConfirmedAt,
))
.add_column_if_not_exists(timestamp_with_time_zone_null(
User::EmailChangeCurrentConfirmedAt,
))
.to_owned()
}

fn alter_table_drop_columns() -> TableAlterStatement {
Table::alter()
.table(User::Table)
.drop_column(User::EmailChangeNewConfirmedAt)
.drop_column(User::EmailChangeCurrentConfirmedAt)
.to_owned()
}

#[cfg(test)]
mod tests {
use insta::assert_snapshot;
use sea_orm::sea_query::PostgresQueryBuilder;

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn alter_table_add_columns() {
let query = super::alter_table_add_columns();

assert_snapshot!(query.to_string(PostgresQueryBuilder));
}

#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn alter_table_drop_columns() {
let query = super::alter_table_drop_columns();

assert_snapshot!(query.to_string(PostgresQueryBuilder));
}
}
6 changes: 6 additions & 0 deletions src/migration/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod m20240729_002549_password_updated_at_function;
pub mod m20240729_002615_password_updated_at_trigger;
pub mod m20241022_072216_case_insensitive_username_email;
pub mod m20241226_080735_pending_email;
mod m20241226_203420_email_change_validation_timestamps;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -49,6 +50,10 @@ pub enum User {
DeletedAt,
/// The user's new email address that has not yet been confirmed.
PendingEmail,
/// The timestamp when the new email address was confirmed.
EmailChangeNewConfirmedAt,
/// The timestamp when the user confirmed the email change was authorized.
EmailChangeCurrentConfirmedAt,
}

/// The collection of migrations defined to create a `user` table. Relevant [MigrationTrait]s
Expand Down Expand Up @@ -77,6 +82,7 @@ impl MigratorTrait for UserMigrator {
Box::new(m20241022_065427_case_insensitive_collation::Migration),
Box::new(m20241022_072216_case_insensitive_username_email::Migration),
Box::new(m20241226_080735_pending_email::Migration),
Box::new(m20241226_203420_email_change_validation_timestamps::Migration),
]
}
}

0 comments on commit 4b40eb3

Please sign in to comment.