use sea_orm_migration::prelude::*; #[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(Positions::Table) .if_not_exists() .col( ColumnDef::new(Positions::Id) .uuid() .not_null() .primary_key(), ) .col(ColumnDef::new(Positions::TenantId).uuid().not_null()) .col(ColumnDef::new(Positions::DeptId).uuid().not_null()) .col(ColumnDef::new(Positions::Name).string().not_null()) .col(ColumnDef::new(Positions::Code).string().null()) .col( ColumnDef::new(Positions::Level) .integer() .not_null() .default(0), ) .col( ColumnDef::new(Positions::SortOrder) .integer() .not_null() .default(0), ) .col( ColumnDef::new(Positions::CreatedAt) .timestamp_with_time_zone() .not_null() .default(Expr::current_timestamp()), ) .col( ColumnDef::new(Positions::UpdatedAt) .timestamp_with_time_zone() .not_null() .default(Expr::current_timestamp()), ) .col(ColumnDef::new(Positions::CreatedBy).uuid().not_null()) .col(ColumnDef::new(Positions::UpdatedBy).uuid().not_null()) .col( ColumnDef::new(Positions::DeletedAt) .timestamp_with_time_zone() .null(), ) .col( ColumnDef::new(Positions::Version) .integer() .not_null() .default(1), ) .foreign_key( &mut ForeignKey::create() .name("fk_positions_dept_id") .from(Positions::Table, Positions::DeptId) .to(Departments::Table, Departments::Id) .on_delete(ForeignKeyAction::Restrict) .to_owned(), ) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_positions_tenant_id") .table(Positions::Table) .col(Positions::TenantId) .to_owned(), ) .await?; manager .create_index( Index::create() .name("idx_positions_dept_id") .table(Positions::Table) .col(Positions::DeptId) .to_owned(), ) .await?; Ok(()) } async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .drop_table(Table::drop().table(Positions::Table).to_owned()) .await } } #[derive(DeriveIden)] enum Positions { Table, Id, TenantId, DeptId, Name, Code, Level, SortOrder, CreatedAt, UpdatedAt, CreatedBy, UpdatedBy, DeletedAt, Version, } #[derive(DeriveIden)] enum Departments { Table, Id, }