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
+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) {
|
||||
|
||||
Reference in new issue
Block a user