- sync_module_permissions 每次启动都确保 admin 拥有所有权限(修复 CRITICAL-001) - 新增迁移 m20260505_000116: 补充 11 项缺失的健康管理菜单(多租户安全) - 修复 000101: UUID 格式错误(缺少第 4 段) - 修复 000104/000106/000107: Expr::val → Expr::cust(SQL 函数不应被引号包裹) - 修复 000109: 外键创建改为 IF NOT EXISTS 模式 - 修复 000110: 表名 critical_alerts → critical_alert(匹配实际表名) - 修复 000111/000112: create_table + create_index 添加 if_not_exists() - 修复 000113: 改为 raw SQL 幂等模式,修正 FK 目标表名 patients → patient
94 lines
3.8 KiB
Rust
94 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> {
|
|
let db = manager.get_connection();
|
|
|
|
db.execute_unprepared(
|
|
r#"
|
|
CREATE TABLE IF NOT EXISTS ble_gateways (
|
|
id UUID PRIMARY KEY,
|
|
tenant_id UUID NOT NULL,
|
|
gateway_id VARCHAR NOT NULL UNIQUE,
|
|
name VARCHAR NOT NULL,
|
|
api_key_hash VARCHAR NOT NULL,
|
|
api_key_prefix VARCHAR NOT NULL,
|
|
status VARCHAR NOT NULL DEFAULT 'active',
|
|
firmware_version VARCHAR,
|
|
ip_address VARCHAR,
|
|
last_heartbeat_at TIMESTAMPTZ,
|
|
metadata JSON,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
created_by UUID,
|
|
updated_by UUID,
|
|
deleted_at TIMESTAMPTZ,
|
|
version INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS gateway_patient_bindings (
|
|
id UUID PRIMARY KEY,
|
|
tenant_id UUID NOT NULL,
|
|
gateway_id_fk UUID NOT NULL,
|
|
patient_id UUID NOT NULL,
|
|
peripheral_mac VARCHAR,
|
|
device_type VARCHAR,
|
|
status VARCHAR NOT NULL DEFAULT 'active',
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
created_by UUID,
|
|
updated_by UUID,
|
|
deleted_at TIMESTAMPTZ,
|
|
version INTEGER NOT NULL DEFAULT 1
|
|
);
|
|
"#,
|
|
)
|
|
.await?;
|
|
|
|
// 索引(幂等)
|
|
let indexes = [
|
|
("idx_ble_gateways_tenant_id", "CREATE INDEX IF NOT EXISTS idx_ble_gateways_tenant_id ON ble_gateways (tenant_id)"),
|
|
("idx_ble_gateways_api_key_prefix", "CREATE INDEX IF NOT EXISTS idx_ble_gateways_api_key_prefix ON ble_gateways (api_key_prefix)"),
|
|
("idx_gateway_patient_bindings_gateway", "CREATE INDEX IF NOT EXISTS idx_gateway_patient_bindings_gateway ON gateway_patient_bindings (gateway_id_fk)"),
|
|
("idx_gateway_patient_bindings_patient", "CREATE INDEX IF NOT EXISTS idx_gateway_patient_bindings_patient ON gateway_patient_bindings (patient_id)"),
|
|
];
|
|
for (_, sql) in &indexes {
|
|
db.execute_unprepared(sql).await.ok();
|
|
}
|
|
|
|
// 外键约束(幂等)
|
|
let fks = [
|
|
("fk_gpb_gateway", "ALTER TABLE gateway_patient_bindings ADD CONSTRAINT fk_gpb_gateway FOREIGN KEY (gateway_id_fk) REFERENCES ble_gateways(id) ON DELETE CASCADE"),
|
|
("fk_gpb_patient", "ALTER TABLE gateway_patient_bindings ADD CONSTRAINT fk_gpb_patient FOREIGN KEY (patient_id) REFERENCES patient(id) ON DELETE CASCADE"),
|
|
];
|
|
for (name, sql) in &fks {
|
|
let check = format!(
|
|
"SELECT COUNT(*) FROM information_schema.table_constraints WHERE constraint_name = '{name}'"
|
|
);
|
|
let result = db.query_one(sea_orm::Statement::from_string(
|
|
sea_orm::DatabaseBackend::Postgres,
|
|
check,
|
|
)).await?;
|
|
let count: i64 = result.unwrap().try_get_by_index::<i64>(0).unwrap_or(0);
|
|
if count == 0 {
|
|
db.execute_unprepared(sql).await?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
let db = manager.get_connection();
|
|
db.execute_unprepared("DROP TABLE IF EXISTS gateway_patient_bindings")
|
|
.await?;
|
|
db.execute_unprepared("DROP TABLE IF EXISTS ble_gateways")
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
}
|