From c0483d7f47e73a6db3ffd9c98d6efb17a26a53c2 Mon Sep 17 00:00:00 2001 From: GW_MC <72297530+GWMCwing@users.noreply.github.com> Date: Tue, 26 May 2026 09:49:12 +0000 Subject: [PATCH] add migration for account and account_category tables --- crates/migration/src/lib.rs | 6 +- .../m20260526_062833_create_account_table.rs | 80 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 crates/migration/src/m20260526_062833_create_account_table.rs diff --git a/crates/migration/src/lib.rs b/crates/migration/src/lib.rs index c09a379..0dbd873 100644 --- a/crates/migration/src/lib.rs +++ b/crates/migration/src/lib.rs @@ -1,11 +1,15 @@ pub use sea_orm_migration::prelude::*; +mod m20260526_062833_create_account_table; pub struct Migrator; #[async_trait::async_trait] impl MigratorTrait for Migrator { fn migrations() -> Vec> { - vec![] + vec![ + // + Box::new(m20260526_062833_create_account_table::Migration), + ] } } diff --git a/crates/migration/src/m20260526_062833_create_account_table.rs b/crates/migration/src/m20260526_062833_create_account_table.rs new file mode 100644 index 0000000..da698aa --- /dev/null +++ b/crates/migration/src/m20260526_062833_create_account_table.rs @@ -0,0 +1,80 @@ +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_category") + .if_not_exists() + .col(pk_auto("id").not_null()) + .col(string("name").not_null()) + .col(string("account_type").not_null()) + .col(string("created_at").not_null()) + .col(string("updated_at").not_null()) + .to_owned(), + ) + .await?; + + manager + .create_table( + Table::create() + .table("account") + .if_not_exists() + .col(pk_auto("id").not_null()) + // + .col(string("name").not_null()) + .col(integer("account_category_id").not_null()) + // + .col(string("created_at").not_null()) + .col(string("updated_at").not_null()) + // Add foreign key constraint to account_category + .foreign_key( + ForeignKey::create() + .name("fk-account-category") + .from("account", "account_category_id") + .to("account_category", "id"), + ) + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .name("idx-account-account_type") + .table("account_category") + .col("account_type") + .to_owned(), + ) + .await?; + + manager + .create_index( + Index::create() + .name("idx-account-category_id") + .table("account") + .col("account_category_id") + .to_owned(), + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table("account").to_owned()) + .await?; + + manager + .drop_table(Table::drop().table("account_category").to_owned()) + .await?; + + Ok(()) + } +}