monzo-ingestion/migration/src/m20240603_162500_make_title_optional.rs
Joshua Coles 046ce44d23
All checks were successful
Build and Publish Docker Container / build (push) Successful in 10m2s
rustfmt
2024-06-03 18:41:32 +01:00

62 lines
1.5 KiB
Rust

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(
TableAlterStatement::new()
.table(Transaction::Table)
.modify_column(ColumnDef::new(Transaction::Title).string().null())
.to_owned(),
)
.await?;
// Set all empty string titles to null
manager
.get_connection()
.execute_unprepared(
r#"
update transaction
set title = null
where title = ''
"#,
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Set all null titles to empty string when reverting
manager
.get_connection()
.execute_unprepared(
r#"
update transaction
set title = ''
where title is null
"#,
)
.await?;
manager
.alter_table(
TableAlterStatement::new()
.table(Transaction::Table)
.modify_column(ColumnDef::new(Transaction::Title).string().not_null())
.to_owned(),
)
.await
}
}
#[derive(DeriveIden)]
enum Transaction {
Table,
Title,
}