feat(b1b-c7): /api/health 补 baseTables —— W5 契约的账号层表数

- health 控制器(已在业务层 tdc/controller/health.rs)改成**一次查询数两类表**:
  `tables` = public schema 下 tdc_* 条数(P0 口径 30),
  `baseTables` = public schema 下 base_* 条数(B2 建表后为 3,当前 0)
- 两个计数都真查库,与 schema/migrate.sql 逐名对账由 scripts/smoke.sh 守住
  (smoke 里 baseTables 的断言是条件式的 ⇒ 本批断言数仍为 75)
This commit is contained in:
admin committed 2026-10-26 20:44:44 +08:00
1 parent 8618465a9b
commit f8764b248c
1 file changed
+27 -8
+27 -8
View File
@@ -1,4 +1,17 @@
//! 健康检查。
//! 健康检查(**业务层** —— v3 spec §2.3.1 C7 / §2.1 搬家表)。
//!
//! 🔴 **为什么在业务层**:平台层不该知道「业务表」是什么(`tdc_*` / `base_*` 是本仓的业务口径)。
//! 平台层只提供通用的 db 访问封装(`platform::db`),**表的语义归业务层**。
//!
//! ## W5 契约(`/api/health`)
//!
//! | 字段 | 含义 |
//! |---|---|
//! | `tables` | **业务表数** = `public` schema 下 `tdc_*` 表的条数(P0 验收口径:30) |
//! | `baseTables` | 账号层表数 = `public` schema 下 `base_*` 表的条数(B2 之后:3) |
//!
//! 两个计数都**真查库**(不是静态 JSON,也不是从 schema 文件里数)⇒ 与 `schema/migrate.sql`
//! 逐名对账由 `scripts/smoke.sh` 守住。
use axum::extract::State;
use axum::routing::get;
@@ -16,21 +29,26 @@ pub fn router() -> Router<AppState> {
/// ⚠️ 保留 `tdsMode` / `tdsBaseUrl` 两个旧字段(`CLAUDE.md` 记录的既有契约),
/// 避免 `/api/health` 迁到本模块后丢字段。
async fn health(State(state): State<AppState>) -> axum::Json<serde_json::Value> {
let tables = {
// 一次查询同时数两类表(少一次往返)⇒ `tables` = tdc_*,`baseTables` = base_*。
let (tables, base_tables) = {
let _g = state.db_lock.lock().await;
crate::platform::db::query_json(
state.db.as_ref(),
"SELECT count(*)::bigint AS c FROM information_schema.tables \
WHERE table_schema = 'public' AND table_name LIKE 'tdc\\_%'",
"SELECT count(*) FILTER (WHERE table_name LIKE 'tdc\\_%')::bigint AS tdc, \
count(*) FILTER (WHERE table_name LIKE 'base\\_%')::bigint AS base \
FROM information_schema.tables WHERE table_schema = 'public'",
)
.await
.ok()
.and_then(|rows| {
rows.first()
.and_then(|r| r.get("c"))
.and_then(|v| v.as_i64())
rows.first().map(|r| {
(
r.get("tdc").and_then(|v| v.as_i64()).unwrap_or(0),
r.get("base").and_then(|v| v.as_i64()).unwrap_or(0),
)
})
})
.unwrap_or(0)
.unwrap_or((0, 0))
};
axum::Json(serde_json::json!({
"status": "UP",
@@ -41,5 +59,6 @@ async fn health(State(state): State<AppState>) -> axum::Json<serde_json::Value>
"tdsBaseUrl": state.config.tds_base_url.clone().unwrap_or_default(),
"db": "pglite-rs (in-process postgres @ data/tdc.pg)",
"tables": tables,
"baseTables": base_tables,
}))
}