来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
67 lines
2.7 KiB
JavaScript
67 lines
2.7 KiB
JavaScript
#!/usr/bin/env node
|
|
// 在单事务内执行核心 FK 迁移、检查约束,再完整回滚;不写 schema_migrations。
|
|
import { readFile } from 'node:fs/promises';
|
|
import { fileURLToPath } from 'node:url';
|
|
import pg from 'pg';
|
|
|
|
const migrationUrl = new URL('../../migrations/20260908_tenant_core_foreign_keys.sql', import.meta.url);
|
|
const migrationSql = await readFile(fileURLToPath(migrationUrl), 'utf8');
|
|
const pool = new pg.Pool({ connectionString: process.env.DATABASE_URL });
|
|
const client = await pool.connect();
|
|
|
|
try {
|
|
await client.query('BEGIN');
|
|
await client.query(migrationSql);
|
|
|
|
const expectedSchemas = await client.query(`
|
|
SELECT 'tenant_template'::text AS schema_name
|
|
UNION ALL
|
|
SELECT schema_name
|
|
FROM public.tenant_schema_registry
|
|
WHERE status IN ('active', 'pending')
|
|
AND schema_name ~ '^tenant_[a-z0-9_-]{2,30}$'
|
|
`);
|
|
const constraints = await client.query(`
|
|
SELECT ns.nspname AS schema_name, COUNT(*)::int AS count,
|
|
bool_and(dst_ns.nspname = ns.nspname) AS same_schema,
|
|
bool_and(con.confdeltype = 'c') AS cascades
|
|
FROM pg_constraint con
|
|
JOIN pg_class src ON src.oid = con.conrelid
|
|
JOIN pg_namespace ns ON ns.oid = src.relnamespace
|
|
JOIN pg_class dst ON dst.oid = con.confrelid
|
|
JOIN pg_namespace dst_ns ON dst_ns.oid = dst.relnamespace
|
|
WHERE con.contype = 'f'
|
|
AND ns.nspname = ANY($1::text[])
|
|
AND (
|
|
(src.relname = 'order_items' AND dst.relname = 'orders')
|
|
OR (src.relname = 'order_item_fulfillments' AND dst.relname = 'order_items')
|
|
OR (src.relname = 'picking_items' AND dst.relname = 'picking_tasks')
|
|
)
|
|
GROUP BY ns.nspname
|
|
ORDER BY ns.nspname
|
|
`, [expectedSchemas.rows.map((row) => row.schema_name)]);
|
|
|
|
for (const row of expectedSchemas.rows) {
|
|
const actual = constraints.rows.find((item) => item.schema_name === row.schema_name);
|
|
if (!actual || Number(actual.count) !== 3 || actual.same_schema !== true || actual.cascades !== true) {
|
|
throw new Error(`core FK dry-run failed for ${row.schema_name}: ${JSON.stringify(actual || null)}`);
|
|
}
|
|
}
|
|
console.log(`core FK migration dry-run OK for ${expectedSchemas.rowCount} schema(s)`);
|
|
await client.query('ROLLBACK');
|
|
|
|
const persisted = await client.query(
|
|
`SELECT to_regprocedure('public.ensure_tenant_core_foreign_keys(text)') IS NOT NULL AS exists`
|
|
);
|
|
if (persisted.rows[0]?.exists === true) {
|
|
throw new Error('rollback drill failed: migration function still exists');
|
|
}
|
|
console.log('rollback drill OK: transaction left no migration objects');
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => undefined);
|
|
throw error;
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|