monzo-ingestion/migration/src/m20241015_195220_add_account_to_transactions.rs

77 lines
2.0 KiB
Rust

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
.create_table(
Table::create()
.table(Account::Table)
.if_not_exists()
.col(
ColumnDef::new(Account::Id)
.integer()
.auto_increment()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(Account::Name)
.string()
.not_null(),
)
.to_owned(),
)
.await?;
manager.alter_table(
TableAlterStatement::new()
.table(Transaction::Table)
.add_column(ColumnDef::new(Transaction::AccountId).integer())
.to_owned(),
).await?;
manager
.create_foreign_key(
ForeignKey::create()
.name("fk_transaction_account_id")
.from(Transaction::Table, Transaction::AccountId)
.to(Account::Table, Account::Id)
.on_delete(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager.alter_table(
TableAlterStatement::new()
.table(Transaction::Table)
.drop_column(Transaction::AccountId)
.to_owned(),
).await?;
manager.drop_table(Table::drop().table(Account::Table).to_owned()).await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Account {
Table,
Id,
Name,
}
#[derive(DeriveIden)]
enum Transaction {
Table,
AccountId,
}