Files
adminandClaude Opus 4.8 70b540f668 feat(b2): 账号层三表入 schema —— base_user / base_role / base_role_relation
影响:/api/health 的 baseTables 从 0 变 3(tables 仍 30);data/ 清空重建。
冒烟断言数 75 → 76(多的那条是 baseTables 对账),其余契约零变化。

改动:
- schema/migrate.sql:末段追加账号层三表(真源 = 上游 j7
  boot/db/j7/V1.0.0__jnpf_base_schema.sql,base_user 在 :17384):
  列类型/列序/96 条 COMMENT ON/3 个索引逐字搬运;
  主键 PRIMARY KEY (f_id, f_tenant_id) 内联(上游在 dump 后置 ALTER 里加,
  pglite 只吃 migrate.sql ⇒ 必须自带)
- tests/schema_init.rs:public 表数 30 → 33,并逐名断言 base_* 三表在位
- src/platform/db.rs:迁移产物单测改成 30 tdc_* + 3 base_* 逐名断言
- scripts/reset-data.sh:提示语改成「30 张 tdc_* + 3 张 base_*」
- docs/known-limitations.md:新增 L2(上游 base_user 的 f_maxkey_user_id
  没建 —— 本仓不实现 MaxKey SSO)
- CLAUDE.md / .workbuddy/memory/2026-10-26.md 同步

设计:tdc_* 侧零 DDL 改动(B0 已逐列对账一致);base_* 只建上游真正用到、
且本仓要用的三张,其余 120 张 base_* 与 base_module/base_authorize 一律不建。

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
2026-10-26 22:58:53 +08:00

92 lines
3.3 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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 里的**表**总数(业务表 `tdc_*` 30 张 + 账号层表 `base_*` 3 张 = 33)。
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 张业务表(tdc_*)+ 3 张账号层表(base_*)**到位(B2 起 33 张)。
#[tokio::test]
async fn fresh_database_gets_the_full_schema() {
let db = shared_db().await;
assert_eq!(
public_table_count(db.as_ref()).await,
33,
"首次启动后 public schema 应有 30 张业务表 + 3 张账号层表"
);
// 关键业务表在位(不是靠表名探测,而是**业务**语义)
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
);
}
// 账号层三表(B2 补的 j7 表)
for t in ["base_user", "base_role", "base_role_relation"] {
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, 33);
}