Add some flex migrations

This commit is contained in:
Joshua Coles 2024-10-15 21:03:40 +01:00
parent c3796720b7
commit 3c3b6dc4e6
3 changed files with 120 additions and 0 deletions

View File

@ -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),
]
}
}

View File

@ -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,
}

View File

@ -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,
}