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

1
Cargo.lock generated
View File

@@ -1617,6 +1617,7 @@ dependencies = [
"sea-orm", "sea-orm",
"serde", "serde",
"serde_json", "serde_json",
"sqlx",
"tokio", "tokio",
"tower", "tower",
"tower-http", "tower-http",

View File

@@ -23,6 +23,7 @@ pub struct AuthState {
pub default_tenant_id: Uuid, pub default_tenant_id: Uuid,
pub wechat_appid: String, pub wechat_appid: String,
pub wechat_secret: String, pub wechat_secret: String,
pub wechat_dev_mode: bool,
pub redis: Option<redis::Client>, pub redis: Option<redis::Client>,
} }

View File

@@ -28,6 +28,9 @@ allowed_origins = "http://localhost:5173,http://localhost:5174,http://localhost:
[wechat] [wechat]
appid = "__MUST_SET_VIA_ENV__" appid = "__MUST_SET_VIA_ENV__"
secret = "__MUST_SET_VIA_ENV__" secret = "__MUST_SET_VIA_ENV__"
# dev_mode = true 跳过 jscode2session允许微信开发者工具模拟器登录
# 生产环境必须为 false默认
dev_mode = false
[health] [health]
aes_key = "__MUST_SET_VIA_ENV__" aes_key = "__MUST_SET_VIA_ENV__"

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

View File

@@ -49,7 +49,7 @@ impl MigrationTrait for Migration {
let db = manager.get_connection(); let db = manager.get_connection();
db.execute(sea_orm::Statement::from_string( db.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres, 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?; .await?;
Ok(()) Ok(())
@@ -74,7 +74,7 @@ async fn insert_prompt(
db.execute(sea_orm::Statement::from_string( db.execute(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres, sea_orm::DatabaseBackend::Postgres,
format!( 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", 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 tenant_id, esc(name), esc(description), esc(system_prompt), esc(user_template), esc(model_config), category, sys, sys
), ),

View File

@@ -62,6 +62,8 @@ pub struct CorsConfig {
pub struct WechatConfig { pub struct WechatConfig {
pub appid: String, pub appid: String,
pub secret: String, pub secret: String,
#[serde(default)]
pub dev_mode: bool,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]

View File

@@ -207,7 +207,7 @@ async fn main() -> anyhow::Result<()> {
); );
std::process::exit(1); std::process::exit(1);
} }
if config.wechat.appid == "__MUST_SET_VIA_ENV__" || config.wechat.secret == "__MUST_SET_VIA_ENV__" { if !config.wechat.dev_mode && (config.wechat.appid == "__MUST_SET_VIA_ENV__" || config.wechat.secret == "__MUST_SET_VIA_ENV__") {
tracing::error!( tracing::error!(
"微信凭据为默认占位值,拒绝启动。请设置环境变量 ERP__WECHAT__APPID 和 ERP__WECHAT__SECRET" "微信凭据为默认占位值,拒绝启动。请设置环境变量 ERP__WECHAT__APPID 和 ERP__WECHAT__SECRET"
); );

View File

@@ -57,6 +57,7 @@ impl FromRef<AppState> for erp_auth::AuthState {
default_tenant_id: state.default_tenant_id, default_tenant_id: state.default_tenant_id,
wechat_appid: state.config.wechat.appid.clone(), wechat_appid: state.config.wechat.appid.clone(),
wechat_secret: state.config.wechat.secret.clone(), wechat_secret: state.config.wechat.secret.clone(),
wechat_dev_mode: state.config.wechat.dev_mode,
redis: Some(state.redis.clone()), redis: Some(state.redis.clone()),
} }
} }