diff --git a/src/tdc/controller/health.rs b/src/tdc/controller/health.rs index 4c7cadc..2b85a62 100644 --- a/src/tdc/controller/health.rs +++ b/src/tdc/controller/health.rs @@ -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 { /// ⚠️ 保留 `tdsMode` / `tdsBaseUrl` 两个旧字段(`CLAUDE.md` 记录的既有契约), /// 避免 `/api/health` 迁到本模块后丢字段。 async fn health(State(state): State) -> axum::Json { - 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) -> axum::Json "tdsBaseUrl": state.config.tds_base_url.clone().unwrap_or_default(), "db": "pglite-rs (in-process postgres @ data/tdc.pg)", "tables": tables, + "baseTables": base_tables, })) }