- users with partial unique index on (tenant_id, username) WHERE deleted_at IS NULL - user_credentials, user_tokens with FK cascade - roles, permissions with composite unique (tenant_id, code) - role_permissions, user_roles junction tables - organizations (self-ref tree), departments (tree + org FK), positions - All tables include standard fields: id, tenant_id, timestamps, soft delete, version
107 lines
3.3 KiB
Rust
107 lines
3.3 KiB
Rust
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(Roles::Table)
|
|
.if_not_exists()
|
|
.col(
|
|
ColumnDef::new(Roles::Id)
|
|
.uuid()
|
|
.not_null()
|
|
.primary_key(),
|
|
)
|
|
.col(ColumnDef::new(Roles::TenantId).uuid().not_null())
|
|
.col(ColumnDef::new(Roles::Name).string().not_null())
|
|
.col(ColumnDef::new(Roles::Code).string().not_null())
|
|
.col(ColumnDef::new(Roles::Description).text().null())
|
|
.col(
|
|
ColumnDef::new(Roles::IsSystem)
|
|
.boolean()
|
|
.not_null()
|
|
.default(false),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Roles::CreatedAt)
|
|
.timestamp_with_time_zone()
|
|
.not_null()
|
|
.default(Expr::current_timestamp()),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Roles::UpdatedAt)
|
|
.timestamp_with_time_zone()
|
|
.not_null()
|
|
.default(Expr::current_timestamp()),
|
|
)
|
|
.col(ColumnDef::new(Roles::CreatedBy).uuid().not_null())
|
|
.col(ColumnDef::new(Roles::UpdatedBy).uuid().not_null())
|
|
.col(
|
|
ColumnDef::new(Roles::DeletedAt)
|
|
.timestamp_with_time_zone()
|
|
.null(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Roles::Version)
|
|
.integer()
|
|
.not_null()
|
|
.default(1),
|
|
)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
manager
|
|
.create_index(
|
|
Index::create()
|
|
.name("idx_roles_tenant_id")
|
|
.table(Roles::Table)
|
|
.col(Roles::TenantId)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
manager
|
|
.create_index(
|
|
Index::create()
|
|
.name("idx_roles_tenant_code")
|
|
.table(Roles::Table)
|
|
.col(Roles::TenantId)
|
|
.col(Roles::Code)
|
|
.unique()
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
manager
|
|
.drop_table(Table::drop().table(Roles::Table).to_owned())
|
|
.await
|
|
}
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Roles {
|
|
Table,
|
|
Id,
|
|
TenantId,
|
|
Name,
|
|
Code,
|
|
Description,
|
|
IsSystem,
|
|
CreatedAt,
|
|
UpdatedAt,
|
|
CreatedBy,
|
|
UpdatedBy,
|
|
DeletedAt,
|
|
Version,
|
|
}
|