refactor(b1b-c4): schema 探针通用化 —— 平台层零业务表名
- ensure_schema 的探针从 `SELECT 1 FROM tdc_identity LIMIT 1` 改成 `SELECT count(*)::text FROM pg_catalog.pg_tables WHERE schemaname = 'public'` ⇒ public 下有表就跳过 migrate.sql(单 schema 布局,base_* 与 tdc_* 同处 public) - 新增 tests/schema_init.rs(独立测试二进制,自带一个临时 PGlite): 首启建出 30 张业务表 + 二跑幂等不增删表 - 边界账本删掉已失效条目(CP1 db.rs tdc_identity)
This commit is contained in:
1 parent
8b4721601f
commit
42cb97cabc
3 files changed
+94
-8
No files matched your search
@@ -13,7 +13,6 @@
|
||||
# 文件字段按**路径后缀**匹配 ⇒ 搬家(src/auth.rs → src/platform/auth.rs)不会让条目失效。
|
||||
|
||||
CP1 auth.rs /api/ssw/tdc/ C5:白名单清单待归业务层 src/tdc/support/public_paths.rs(并剪掉 7 条死路径) B1b
|
||||
CP1 db.rs tdc_identity C4:schema 探针待通用化(改查 pg_catalog.pg_tables) B1b
|
||||
CP2 session.rs Identity 假阳性:SessionKind::Identity 是**会话口径**,不是业务实体 tdc_identity 永久
|
||||
CP2 auth.rs SessionKind::Identity 假阳性:Principal::Session 分支里的会话种类,不是业务实体 永久
|
||||
CP4 auth.rs /api/ C5:白名单清单待归业务层(含 /api/oauth/Login 与 /api/health 两条) B1b
|
||||
+19
-7
@@ -307,14 +307,26 @@ pub fn json_key_to_column(key: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// 首次启动初始化 schema:若 `tdc_identity` 已存在则跳过,否则按字面量感知切分逐条 exec。
|
||||
/// 首次启动初始化 schema:`public` schema 里**一张表都没有**才按字面量感知切分逐条 exec。
|
||||
///
|
||||
/// 🔴 探针**通用**(v3 spec §2.3.1 C4)—— 平台层**不写死任何业务表名**:
|
||||
/// `SELECT count(*) FROM pg_catalog.pg_tables WHERE schemaname = 'public'`。
|
||||
/// 单 schema 布局下成立(`base_*` 与 `tdc_*` 同处 `public`)。
|
||||
///
|
||||
/// ⚠️ 语义变化(相对旧探针 `SELECT 1 FROM tdc_identity LIMIT 1`):
|
||||
/// 旧口径是「**某张业务表**在 ⇒ 跳过」;新口径是「**public 下有表** ⇒ 跳过」。
|
||||
/// 两者在「本仓自己建的库」上等价;差别只在**半初始化**的库上
|
||||
/// (旧口径会去跑 `migrate.sql` 然后撞「表已存在」;新口径直接跳过)。
|
||||
pub async fn ensure_schema(db: &PGlite) -> Result<(), PgError> {
|
||||
let probe: &[&(dyn ToSql + Sync)] = &[];
|
||||
if db
|
||||
.query("SELECT 1 FROM tdc_identity LIMIT 1", probe)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
let table_count = query_scalar_text(
|
||||
db,
|
||||
"SELECT count(*)::text FROM pg_catalog.pg_tables WHERE schemaname = 'public'",
|
||||
&[],
|
||||
)
|
||||
.await?
|
||||
.and_then(|s| s.trim().parse::<i64>().ok())
|
||||
.unwrap_or(0);
|
||||
if table_count > 0 {
|
||||
return Ok(());
|
||||
}
|
||||
for stmt in split_sql_statements(MIGRATE_SQL) {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//! Schema 初始化探针(C4)—— **通用**探针,不写死任何业务表名。
|
||||
//!
|
||||
//! 旧口径:`SELECT 1 FROM tdc_identity LIMIT 1`(平台层写死了业务表名 ⇒ 越界)。
|
||||
//! 新口径:`SELECT count(*) FROM pg_catalog.pg_tables WHERE schemaname = 'public'`
|
||||
//! ⇒ 为 0 才执行 `migrate.sql`。
|
||||
//!
|
||||
//! ⚠️ 本测试二进制内**只开一个** PGlite(pglite-rs 每进程只允许一个)⇒ 用 `OnceCell` 共享。
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use pglite::PGlite;
|
||||
use tokio::sync::OnceCell;
|
||||
|
||||
use tdc_connector::platform::db::{ensure_schema, query_scalar_text};
|
||||
|
||||
static DB: OnceCell<Arc<PGlite>> = OnceCell::const_new();
|
||||
|
||||
async fn shared_db() -> Arc<PGlite> {
|
||||
DB.get_or_init(|| async {
|
||||
let db = Arc::new(PGlite::open_temp().await.expect("打开临时 PGlite 失败"));
|
||||
ensure_schema(db.as_ref()).await.expect("建表失败");
|
||||
db
|
||||
})
|
||||
.await
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// `public` schema 里的**业务表**数(排除 `base_*` 之外的都算业务表 —— 本仓只有 tdc_*)。
|
||||
async fn public_table_count(db: &PGlite) -> i64 {
|
||||
let raw = query_scalar_text(
|
||||
db,
|
||||
"SELECT count(*)::text FROM pg_catalog.pg_tables WHERE schemaname = 'public'",
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
.expect("计数失败");
|
||||
raw.and_then(|s| s.trim().parse::<i64>().ok()).unwrap_or(0)
|
||||
}
|
||||
|
||||
/// 🔴 首次启动:空库 ⇒ 探针读到 0 ⇒ `migrate.sql` 全部执行 ⇒ **30 张业务表**到位。
|
||||
#[tokio::test]
|
||||
async fn fresh_database_gets_the_full_schema() {
|
||||
let db = shared_db().await;
|
||||
assert_eq!(
|
||||
public_table_count(db.as_ref()).await,
|
||||
30,
|
||||
"首次启动后 public schema 应有 30 张业务表"
|
||||
);
|
||||
// 关键业务表在位(不是靠表名探测,而是**业务**语义)
|
||||
for t in ["tdc_identity", "tdc_resource", "tdc_product", "tdc_policy"] {
|
||||
let sql = format!(
|
||||
"SELECT count(*)::text FROM pg_catalog.pg_tables \
|
||||
WHERE schemaname = 'public' AND tablename = '{}'",
|
||||
t
|
||||
);
|
||||
let raw = query_scalar_text(db.as_ref(), &sql, &[]).await.expect("查询失败");
|
||||
assert_eq!(
|
||||
raw.and_then(|s| s.trim().parse::<i64>().ok()).unwrap_or(0),
|
||||
1,
|
||||
"表 {} 应存在",
|
||||
t
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// 🔴 二次启动:探针读到 > 0 ⇒ **跳过** `migrate.sql` ⇒ 不报错、表数不变(幂等)。
|
||||
#[tokio::test]
|
||||
async fn second_run_is_a_noop() {
|
||||
let db = shared_db().await;
|
||||
let before = public_table_count(db.as_ref()).await;
|
||||
ensure_schema(db.as_ref()).await.expect("二次初始化不应失败");
|
||||
let after = public_table_count(db.as_ref()).await;
|
||||
assert_eq!(before, after, "二次初始化不应增删表");
|
||||
assert_eq!(after, 30);
|
||||
}
|
||||
Reference in new issue
Block a user