Phase 0 安全热修复 (CRITICAL): - 外部化微信 appid/secret 到 ERP__WECHAT__APPID/SECRET 环境变量 - 正确连接 HealthCrypto 到 ERP__HEALTH__AES_KEY/HMAC_KEY 环境变量 - 外部化小程序加密密钥到 TARO_APP_ENCRYPTION_KEY 环境变量 - 移除小程序 auth store 中的敏感信息 console.log Phase 1 安全加固: - 微信自动注册 display_name 添加 sanitize 防止 XSS - 测试数据库凭据改为从 TEST_DB_URL 环境变量读取 Phase 2 代码质量: - 提取 useThemeMode hook 消除 22 处重复暗色模式检测 - 提取共享健康常量到 constants/health.ts - 拆分 patient_service.rs 脱敏函数到 masking.rs - 移除未使用的 i18next/react-i18next 依赖 - 移除未使用的 api/errors.ts 和 erp-auth/anyhow 依赖 Phase 3 测试覆盖: - 新增 5 个患者模块集成测试 (CRUD/租户隔离/验证/软删除) Phase 4 跨平台一致性: - 统一小程序 Patient.birthday → birth_date 匹配后端 - 统一小程序 Appointment.time_slot → start_time/end_time 匹配后端 Phase 5 架构: - 微信登录添加多租户 TODO 注释 - 更新 wiki/infrastructure.md 环境变量文档
96 lines
3.4 KiB
Rust
96 lines
3.4 KiB
Rust
use sea_orm::{Database, ConnectionTrait, Statement, DatabaseBackend};
|
||
|
||
use erp_server_migration::MigratorTrait;
|
||
|
||
/// 测试数据库 — 使用本地 PostgreSQL 创建隔离测试库
|
||
///
|
||
/// 连接本地 PostgreSQL(wiki/infrastructure.md 配置),为每个测试创建独立的测试数据库。
|
||
/// 不依赖 Docker/Testcontainers,与开发环境一致。
|
||
pub struct TestDb {
|
||
db: Option<sea_orm::DatabaseConnection>,
|
||
db_name: String,
|
||
}
|
||
|
||
impl TestDb {
|
||
pub async fn new() -> Self {
|
||
let db_name = format!("erp_test_{}", uuid::Uuid::now_v7().simple());
|
||
|
||
let admin_url = std::env::var("TEST_DB_URL")
|
||
.unwrap_or_else(|_| "postgres://postgres:123123@localhost:5432/postgres".to_string());
|
||
|
||
let admin_db = Database::connect(&admin_url)
|
||
.await
|
||
.expect("连接本地 PostgreSQL 失败,请确认服务正在运行");
|
||
|
||
admin_db
|
||
.execute(Statement::from_string(
|
||
DatabaseBackend::Postgres,
|
||
format!("CREATE DATABASE \"{}\"", db_name),
|
||
))
|
||
.await
|
||
.expect("创建测试数据库失败");
|
||
|
||
drop(admin_db);
|
||
|
||
// 从 admin_url 推导测试库 URL(替换路径部分)
|
||
let test_url = if let Some(pos) = admin_url.rfind('/') {
|
||
format!("{}/{}", &admin_url[..pos], db_name)
|
||
} else {
|
||
format!("postgres://postgres:123123@localhost:5432/{}", db_name)
|
||
};
|
||
let db = Database::connect(&test_url)
|
||
.await
|
||
.expect("连接测试数据库失败");
|
||
|
||
// 运行所有迁移
|
||
erp_server_migration::Migrator::up(&db, None)
|
||
.await
|
||
.expect("执行数据库迁移失败");
|
||
|
||
Self { db: Some(db), db_name }
|
||
}
|
||
|
||
/// 获取数据库连接引用
|
||
pub fn db(&self) -> &sea_orm::DatabaseConnection {
|
||
self.db.as_ref().expect("数据库连接已被释放")
|
||
}
|
||
}
|
||
|
||
impl Drop for TestDb {
|
||
fn drop(&mut self) {
|
||
let db_name = self.db_name.clone();
|
||
self.db.take();
|
||
|
||
// 尝试在独立线程中清理,避免在 tokio runtime 内创建新 runtime
|
||
let _ = std::thread::spawn(move || {
|
||
let rt = tokio::runtime::Builder::new_current_thread()
|
||
.enable_all()
|
||
.build();
|
||
if let Ok(rt) = rt {
|
||
rt.block_on(async {
|
||
let admin_url = std::env::var("TEST_DB_URL")
|
||
.unwrap_or_else(|_| "postgres://postgres:123123@localhost:5432/postgres".to_string());
|
||
if let Ok(admin_db) = Database::connect(&admin_url).await {
|
||
let disconnect_sql = format!(
|
||
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '{}'",
|
||
db_name
|
||
);
|
||
admin_db
|
||
.execute(Statement::from_string(DatabaseBackend::Postgres, disconnect_sql))
|
||
.await
|
||
.ok();
|
||
|
||
admin_db
|
||
.execute(Statement::from_string(
|
||
DatabaseBackend::Postgres,
|
||
format!("DROP DATABASE IF EXISTS \"{}\"", db_name),
|
||
))
|
||
.await
|
||
.ok();
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
}
|