来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
91 lines
3.3 KiB
JavaScript
91 lines
3.3 KiB
JavaScript
// 阶段 3+:校验 ALS search_path 指向 active tenant;订单数对照 legacy_public(阶段 5 后 public 已无业务表)
|
|
import pg from 'pg';
|
|
|
|
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
|
|
|
|
function assert(cond, msg) {
|
|
if (!cond) throw new Error(msg);
|
|
}
|
|
|
|
const client = await pool.connect();
|
|
try {
|
|
const reg = await client.query(
|
|
`SELECT branch_code, schema_name, status
|
|
FROM public.tenant_schema_registry
|
|
ORDER BY branch_code`
|
|
);
|
|
assert((reg.rowCount || 0) > 0, 'registry empty');
|
|
const active = reg.rows.filter((r) => r.status === 'active');
|
|
console.log(`registry: ${reg.rowCount} total, active=${active.length}`);
|
|
|
|
const hasLegacyOrders = await client.query(
|
|
`SELECT to_regclass('legacy_public.orders') IS NOT NULL AS ok`
|
|
);
|
|
const compareLegacy = Boolean(hasLegacyOrders.rows[0]?.ok);
|
|
|
|
for (const row of active) {
|
|
const schema = row.schema_name;
|
|
assert(/^tenant_[a-z0-9_-]{2,30}$/.test(schema), `bad schema ${schema}`);
|
|
|
|
await client.query('BEGIN');
|
|
await client.query(`SELECT set_config('search_path', $1, true)`, [`${schema}, public`]);
|
|
const path = await client.query(`SELECT current_setting('search_path') AS p`);
|
|
const p = String(path.rows[0].p);
|
|
assert(p.includes(schema), `${row.branch_code} search_path missing schema: ${p}`);
|
|
|
|
const cnt = await client.query(`SELECT COUNT(*)::int AS n FROM orders`);
|
|
const tenantN = Number(cnt.rows[0].n);
|
|
|
|
if (compareLegacy) {
|
|
const leg = await client.query(
|
|
`SELECT COUNT(*)::int AS n FROM legacy_public.orders WHERE branch_code IS NOT DISTINCT FROM $1`,
|
|
[row.branch_code]
|
|
);
|
|
const legacyN = Number(leg.rows[0].n);
|
|
// 阶段 5 后新单只写 tenant,tenant 行数应 ≥ 归档快照
|
|
assert(
|
|
tenantN >= legacyN,
|
|
`${row.branch_code} orders tenant=${tenantN} < legacy=${legacyN}`
|
|
);
|
|
console.log(
|
|
`OK path ${row.branch_code} → ${schema} orders=${tenantN} (legacy_snapshot=${legacyN})`
|
|
);
|
|
} else {
|
|
console.log(`OK path ${row.branch_code} → ${schema} orders=${tenantN}`);
|
|
}
|
|
await client.query('COMMIT');
|
|
}
|
|
|
|
if (active.length >= 2) {
|
|
const a = active[0];
|
|
const b = active[1];
|
|
const c1 = await pool.connect();
|
|
const c2 = await pool.connect();
|
|
try {
|
|
await c1.query('BEGIN');
|
|
await c2.query('BEGIN');
|
|
await c1.query(`SELECT set_config('search_path', $1, true)`, [`${a.schema_name}, public`]);
|
|
await c2.query(`SELECT set_config('search_path', $1, true)`, [`${b.schema_name}, public`]);
|
|
const p1 = await c1.query(`SELECT current_setting('search_path') AS p`);
|
|
const p2 = await c2.query(`SELECT current_setting('search_path') AS p`);
|
|
assert(String(p1.rows[0].p).includes(a.schema_name), 'c1 path leak');
|
|
assert(String(p2.rows[0].p).includes(b.schema_name), 'c2 path leak');
|
|
assert(
|
|
!String(p1.rows[0].p).includes(b.schema_name) || a.schema_name === b.schema_name,
|
|
'c1 contaminated'
|
|
);
|
|
await c1.query('COMMIT');
|
|
await c2.query('COMMIT');
|
|
console.log(`OK concurrent isolation ${a.branch_code} vs ${b.branch_code}`);
|
|
} finally {
|
|
c1.release();
|
|
c2.release();
|
|
}
|
|
}
|
|
|
|
console.log('verify-tenant-workflow-path: OK');
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|