From f8764b248ce8ab7e3fe9b187fa41a8a4aaeb015d Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 26 Oct 2026 20:44:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(b1b-c7):=20/api/health=20=E8=A1=A5=20baseT?= =?UTF-8?q?ables=20=E2=80=94=E2=80=94=20W5=20=E5=A5=91=E7=BA=A6=E7=9A=84?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E5=B1=82=E8=A1=A8=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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) --- src/tdc/controller/health.rs | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) 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, })) }