Skip to content

Commit

Permalink
feat: Add User column to store the user's new email before it's confi…
Browse files Browse the repository at this point in the history
…rmed (#536)
  • Loading branch information
spencewenski authored Dec 26, 2024
1 parent 72cb363 commit 8ce0be6
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/migration/user/m20241226_080735_pending_email.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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(string_null(User::PendingEmail))
.to_owned()
}

fn alter_table_drop_columns() -> TableAlterStatement {
Table::alter()
.table(User::Table)
.drop_column(User::PendingEmail)
.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));
}
}
4 changes: 4 additions & 0 deletions src/migration/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod m20240729_000812_password_updated_at;
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;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -46,6 +47,8 @@ pub enum User {
EmailChangeTokenCurrent,
/// When the user was deleted.
DeletedAt,
/// The user's new email address that has not yet been confirmed.
PendingEmail,
}

/// The collection of migrations defined to create a `user` table. Relevant [MigrationTrait]s
Expand Down Expand Up @@ -73,6 +76,7 @@ impl MigratorTrait for UserMigrator {
Box::new(m20240729_002615_password_updated_at_trigger::Migration),
Box::new(m20241022_065427_case_insensitive_collation::Migration),
Box::new(m20241022_072216_case_insensitive_username_email::Migration),
Box::new(m20241226_080735_pending_email::Migration),
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/migration/user/m20241226_080735_pending_email.rs
expression: query.to_string(PostgresQueryBuilder)
snapshot_kind: text
---
ALTER TABLE "user" ADD COLUMN IF NOT EXISTS "pending_email" varchar NULL
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/migration/user/m20241226_080735_pending_email.rs
expression: query.to_string(PostgresQueryBuilder)
snapshot_kind: text
---
ALTER TABLE "user" DROP COLUMN "pending_email"
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: src/migration/user/tests.rs
expression: user_migrations
snapshot_kind: text
---
[
"m20240714_203551_create_user_table_uuid_pk",
Expand All @@ -12,4 +13,5 @@ expression: user_migrations
"m20240729_002615_password_updated_at_trigger",
"m20241022_065427_case_insensitive_collation",
"m20241022_072216_case_insensitive_username_email",
"m20241226_080735_pending_email",
]

0 comments on commit 8ce0be6

Please sign in to comment.