generated from holaplex/hub-rust-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add created_at and created_by to collections backfill based on …
…drop. add new fields to collection when drop created.
- Loading branch information
Showing
9 changed files
with
102 additions
and
139 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
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
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
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
63 changes: 63 additions & 0 deletions
63
migration/src/m20230718_111347_add_created_at_and_created_by_columns_to_collections.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,63 @@ | ||
use sea_orm::{ConnectionTrait, Statement}; | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Collections::Table) | ||
.add_column_if_not_exists(ColumnDef::new(Collections::CreatedBy).uuid()) | ||
.add_column_if_not_exists( | ||
ColumnDef::new(Collections::CreatedAt) | ||
.timestamp_with_time_zone() | ||
.extra("default now()".to_string()), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
let db = manager.get_connection(); | ||
|
||
let stmt = Statement::from_string( | ||
manager.get_database_backend(), | ||
r#"UPDATE collections SET created_by = drops.created_by, created_at = drops.created_at FROM drops WHERE drops.collection_id = collections.id;"#.to_string(), | ||
); | ||
|
||
db.execute(stmt).await?; | ||
|
||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Collections::Table) | ||
.modify_column(ColumnDef::new(Collections::CreatedBy).not_null()) | ||
.modify_column(ColumnDef::new(Collections::CreatedAt).not_null()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Collections::Table) | ||
.drop_column(Collections::CreatedBy) | ||
.drop_column(Collections::CreatedAt) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
} | ||
|
||
/// Learn more at https://docs.rs/sea-query#iden | ||
#[derive(Iden)] | ||
enum Collections { | ||
Table, | ||
CreatedBy, | ||
CreatedAt, | ||
} |