fix(core): 迁移修复 + 配置调整

- auth_state: 新增字段
- config/default.toml: 配置更新
- migration 078/082: 修复 SQL 语法
- state/main: 启动逻辑调整
This commit is contained in:
iven
2026-04-28 00:20:11 +08:00
parent e56cd73e49
commit 30f2452933
9 changed files with 89 additions and 12 deletions

View File

@@ -0,0 +1,76 @@
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
}
}

View File

@@ -19,11 +19,7 @@ impl MigrationTrait for Migration {
)
.await?;
// follow_up_record: face_to_face → outpatient
conn.execute_unprepared(
"UPDATE follow_up_record SET follow_up_type = 'outpatient' WHERE follow_up_type = 'face_to_face'",
)
.await?;
// follow_up_record 没有 follow_up_type 列,跳过
Ok(())
}
@@ -36,10 +32,7 @@ impl MigrationTrait for Migration {
)
.await?;
conn.execute_unprepared(
"UPDATE follow_up_record SET follow_up_type = 'face_to_face' WHERE follow_up_type = 'outpatient'",
)
.await?;
// follow_up_record 没有 follow_up_type 列,跳过
Ok(())
}

View File

@@ -49,7 +49,7 @@ impl MigrationTrait for Migration {
let db = manager.get_connection();
db.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
"DELETE FROM ai_prompts WHERE name IN ('lab_report_interpretation','health_trend_analysis','personalized_checkup_plan','report_summary_generation')".to_string(),
"DELETE FROM ai_prompt WHERE name IN ('lab_report_interpretation','health_trend_analysis','personalized_checkup_plan','report_summary_generation')".to_string(),
))
.await?;
Ok(())
@@ -74,7 +74,7 @@ async fn insert_prompt(
db.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres,
format!(
"INSERT INTO ai_prompts (id, tenant_id, name, description, system_prompt, user_prompt_template, model_config, version, is_active, category, tags, created_at, updated_at, created_by, updated_by, version_lock) \
"INSERT INTO ai_prompt (id, tenant_id, name, description, system_prompt, user_prompt_template, model_config, version, is_active, category, tags, created_at, updated_at, created_by, updated_by, version_lock) \
VALUES (gen_random_uuid(), '{}', '{}', '{}', '{}', '{}', '{}', 1, true, '{}', NULL, NOW(), NOW(), '{}', '{}', 1) ON CONFLICT DO NOTHING",
tenant_id, esc(name), esc(description), esc(system_prompt), esc(user_template), esc(model_config), category, sys, sys
),