From 4b40eb3f1b79a3c24666f59b90480cae40eb66fa Mon Sep 17 00:00:00 2001 From: Spencer Ferris <3319370+spencewenski@users.noreply.github.com> Date: Thu, 26 Dec 2024 12:41:16 -0800 Subject: [PATCH] feat: Add timestamps for when email change is confirmed --- ...3420_email_change_validation_timestamps.rs | 58 +++++++++++++++++++ src/migration/user/mod.rs | 6 ++ 2 files changed, 64 insertions(+) create mode 100644 src/migration/user/m20241226_203420_email_change_validation_timestamps.rs diff --git a/src/migration/user/m20241226_203420_email_change_validation_timestamps.rs b/src/migration/user/m20241226_203420_email_change_validation_timestamps.rs new file mode 100644 index 0000000..74b4528 --- /dev/null +++ b/src/migration/user/m20241226_203420_email_change_validation_timestamps.rs @@ -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)); + } +} diff --git a/src/migration/user/mod.rs b/src/migration/user/mod.rs index 4de89b2..2042db3 100644 --- a/src/migration/user/mod.rs +++ b/src/migration/user/mod.rs @@ -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; @@ -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 @@ -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), ] } }