Files
hms/crates/erp-server/migration/src/m20260501_000098_create_ai_suggestion.rs
iven 577d2a32b1 feat(db): 添加 ai_suggestion 和 ai_risk_threshold 表迁移
- ai_suggestion: AI 建议记录表,含 tenant_id、analysis_id、suggestion_type、
  risk_level、status、params、baseline_snapshot 等字段
- ai_risk_threshold: 租户级风险阈值配置表,按 metric_name + tenant_id 唯一索引
- 两表均包含标准审计字段和 version_lock 乐观锁
2026-05-01 08:04:51 +08:00

99 lines
3.8 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(Alias::new("ai_suggestion"))
.col(
ColumnDef::new(Alias::new("id"))
.uuid()
.not_null()
.primary_key()
.default(Expr::cust("gen_random_uuid()")),
)
.col(ColumnDef::new(Alias::new("tenant_id")).uuid().not_null())
.col(ColumnDef::new(Alias::new("analysis_id")).uuid().not_null())
.col(
ColumnDef::new(Alias::new("suggestion_type"))
.string_len(20)
.not_null(),
)
.col(
ColumnDef::new(Alias::new("risk_level"))
.string_len(10)
.not_null(),
)
.col(ColumnDef::new(Alias::new("params")).json_binary().not_null())
.col(
ColumnDef::new(Alias::new("status"))
.string_len(20)
.not_null()
.default("pending"),
)
.col(ColumnDef::new(Alias::new("workflow_instance_id")).uuid())
.col(ColumnDef::new(Alias::new("action_result")).json_binary())
.col(ColumnDef::new(Alias::new("baseline_snapshot")).json_binary())
.col(ColumnDef::new(Alias::new("reanalysis_id")).uuid())
.col(
ColumnDef::new(Alias::new("created_at"))
.timestamp_with_time_zone()
.default(Expr::cust("NOW()")),
)
.col(
ColumnDef::new(Alias::new("updated_at"))
.timestamp_with_time_zone()
.default(Expr::cust("NOW()")),
)
.col(ColumnDef::new(Alias::new("created_by")).uuid())
.col(ColumnDef::new(Alias::new("updated_by")).uuid())
.col(
ColumnDef::new(Alias::new("deleted_at")).timestamp_with_time_zone(),
)
.col(
ColumnDef::new(Alias::new("version_lock"))
.integer()
.not_null()
.default(1),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_ai_suggestion_tenant_analysis")
.table(Alias::new("ai_suggestion"))
.col(Alias::new("tenant_id"))
.col(Alias::new("analysis_id"))
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx_ai_suggestion_tenant_status")
.table(Alias::new("ai_suggestion"))
.col(Alias::new("tenant_id"))
.col(Alias::new("status"))
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Alias::new("ai_suggestion")).to_owned())
.await
}
}