-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Auto-update timestamp columns (#287)
Add utilities and migrations to enable auto-updating timestamp columns of a row whenever the row is updated. Also, add the relevant migrations to the `UserMigrator` to auto-enable the feature for the `user` table. Closes #285
- Loading branch information
1 parent
999f362
commit 9e1ee56
Showing
22 changed files
with
691 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ | |
pub mod check; | ||
pub mod schema; | ||
pub mod timestamp; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/migration/timestamp/m20240723_201404_add_update_timestamp_function.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//! Migration to create a SQL function to update the [Timestamps::UpdatedAt] column for a row | ||
//! with the current timestamp. | ||
//! | ||
//! Note: Currently only supports Postgres. If another DB is used, will do nothing. | ||
use crate::migration::schema::Timestamps; | ||
use crate::migration::timestamp::{ | ||
exec_create_update_timestamp_function, exec_drop_update_timestamp_function, | ||
}; | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
const COLUMN: Timestamps = Timestamps::UpdatedAt; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
exec_create_update_timestamp_function(manager, COLUMN).await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
exec_drop_update_timestamp_function(manager, COLUMN).await | ||
} | ||
} |
Oops, something went wrong.