add migration for account and account_category tables
This commit is contained in:
@@ -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<Box<dyn MigrationTrait>> {
|
||||
vec![]
|
||||
vec![
|
||||
//
|
||||
Box::new(m20260526_062833_create_account_table::Migration),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user