- auth_state: 新增字段 - config/default.toml: 配置更新 - migration 078/082: 修复 SQL 语法 - state/main: 启动逻辑调整
77 lines
3.4 KiB
Rust
77 lines
3.4 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("alerts"))
|
|
.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("patient_id")).uuid().not_null())
|
|
.col(ColumnDef::new(Alias::new("rule_id")).uuid().not_null())
|
|
.col(ColumnDef::new(Alias::new("severity")).string().not_null())
|
|
.col(ColumnDef::new(Alias::new("title")).string().not_null())
|
|
.col(ColumnDef::new(Alias::new("detail")).json_binary().default(Expr::cust("'{}'::jsonb")))
|
|
.col(ColumnDef::new(Alias::new("status")).string().not_null().default("'pending'"))
|
|
.col(ColumnDef::new(Alias::new("acknowledged_by")).uuid())
|
|
.col(ColumnDef::new(Alias::new("acknowledged_at")).timestamp_with_time_zone())
|
|
.col(ColumnDef::new(Alias::new("resolved_at")).timestamp_with_time_zone())
|
|
.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("deleted_at")).timestamp_with_time_zone())
|
|
.col(ColumnDef::new(Alias::new("version")).integer().not_null().default(1))
|
|
.foreign_key(
|
|
ForeignKey::create()
|
|
.from(Alias::new("alerts"), Alias::new("rule_id"))
|
|
.to(Alias::new("alert_rules"), Alias::new("id"))
|
|
.on_delete(ForeignKeyAction::Restrict)
|
|
)
|
|
.to_owned(),
|
|
).await?;
|
|
|
|
// 按患者查询告警
|
|
manager.create_index(
|
|
Index::create()
|
|
.name("idx_alerts_tenant_patient")
|
|
.table(Alias::new("alerts"))
|
|
.col(Alias::new("tenant_id"))
|
|
.col(Alias::new("patient_id"))
|
|
.col(Alias::new("created_at"))
|
|
.to_owned(),
|
|
).await?;
|
|
|
|
// 按状态筛选
|
|
manager.create_index(
|
|
Index::create()
|
|
.name("idx_alerts_status")
|
|
.table(Alias::new("alerts"))
|
|
.col(Alias::new("tenant_id"))
|
|
.col(Alias::new("status"))
|
|
.col(Alias::new("created_at"))
|
|
.to_owned(),
|
|
).await?;
|
|
|
|
// 冷却期查询 — 同规则同患者
|
|
manager.create_index(
|
|
Index::create()
|
|
.name("idx_alerts_cooldown")
|
|
.table(Alias::new("alerts"))
|
|
.col(Alias::new("tenant_id"))
|
|
.col(Alias::new("patient_id"))
|
|
.col(Alias::new("rule_id"))
|
|
.col(Alias::new("created_at"))
|
|
.to_owned(),
|
|
).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
manager.drop_table(Table::drop().table(Alias::new("alerts")).to_owned()).await
|
|
}
|
|
}
|