From 3c3b6dc4e670954afac9a5d62ffbf5abdfc96cd6 Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Tue, 15 Oct 2024 21:03:40 +0100 Subject: [PATCH] Add some flex migrations --- migration/src/lib.rs | 4 + ...1015_195220_add_account_to_transactions.rs | 76 +++++++++++++++++++ ...5_200222_add_expenditure_transaction_fk.rs | 40 ++++++++++ 3 files changed, 120 insertions(+) create mode 100644 migration/src/m20241015_195220_add_account_to_transactions.rs create mode 100644 migration/src/m20241015_200222_add_expenditure_transaction_fk.rs diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 03529c4..dc30a72 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -3,6 +3,8 @@ pub use sea_orm_migration::prelude::*; pub mod m20230904_141851_create_monzo_tables; mod m20240529_195030_add_transaction_identity_hash; mod m20240603_162500_make_title_optional; +mod m20241015_195220_add_account_to_transactions; +mod m20241015_200222_add_expenditure_transaction_fk; pub struct Migrator; @@ -17,6 +19,8 @@ impl MigratorTrait for Migrator { Box::new(m20230904_141851_create_monzo_tables::Migration), Box::new(m20240529_195030_add_transaction_identity_hash::Migration), Box::new(m20240603_162500_make_title_optional::Migration), + Box::new(m20241015_195220_add_account_to_transactions::Migration), + Box::new(m20241015_200222_add_expenditure_transaction_fk::Migration), ] } } diff --git a/migration/src/m20241015_195220_add_account_to_transactions.rs b/migration/src/m20241015_195220_add_account_to_transactions.rs new file mode 100644 index 0000000..02a6d2e --- /dev/null +++ b/migration/src/m20241015_195220_add_account_to_transactions.rs @@ -0,0 +1,76 @@ +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, +} diff --git a/migration/src/m20241015_200222_add_expenditure_transaction_fk.rs b/migration/src/m20241015_200222_add_expenditure_transaction_fk.rs new file mode 100644 index 0000000..b9149de --- /dev/null +++ b/migration/src/m20241015_200222_add_expenditure_transaction_fk.rs @@ -0,0 +1,40 @@ +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_foreign_key( + ForeignKey::create() + .name("fk_expenditure_transaction_id") + .from(Expenditure::Table, Expenditure::TransactionId) + .to(Transaction::Table, Transaction::Id) + .on_delete(ForeignKeyAction::Cascade) + .to_owned(), + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager.drop_foreign_key(ForeignKey::drop().name("fk_expenditure_transaction_id").to_owned()).await?; + + Ok(()) + } +} + +#[derive(DeriveIden)] +enum Transaction { + Table, + Id, +} + +#[derive(DeriveIden)] +enum Expenditure { + Table, + TransactionId, +}