Files
SupplierSystem/server/tests/migrationDdlIdempotency.test.ts
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

51 lines
2.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
/**
* DATA-6(2026-10-19 T4):迁移文件统一幂等。
* 文本扫描:所有 migrations/*.sql 的裸 DDL(CREATE TABLE / CREATE [UNIQUE] INDEX / ADD COLUMN
* 无 `IF NOT EXISTS`)必须为 0(ADD CONSTRAINT 不在此列,Postgres 不支持 IF NOT EXISTS)。
* 已应用过的旧迁移也修(不改文件名/顺序,只影响新装库)。
*/
const MIGRATIONS_DIR = join(__dirname, '../../migrations');
function stripLineComments(sql: string): string {
return sql
.split('\n')
.map((line) => line.replace(/--.*$/, ''))
.join('\n');
}
describe('DATA-6:迁移 DDL 幂等扫描', () => {
it('裸 CREATE TABLE / CREATE INDEX / ADD COLUMN 数量 = 0', () => {
const files = readdirSync(MIGRATIONS_DIR).filter((f) => f.endsWith('.sql'));
const offenders: string[] = [];
const patterns: Array<[string, RegExp]> = [
['CREATE TABLE', /\bCREATE\s+TABLE\s+(?!IF\s+NOT\s+EXISTS)/i],
['CREATE INDEX', /\bCREATE\s+(?:UNIQUE\s+)?INDEX\s+(?!IF\s+NOT\s+EXISTS)/i],
['ADD COLUMN', /\bADD\s+COLUMN\s+(?!IF\s+NOT\s+EXISTS)/i],
];
for (const file of files) {
const sql = stripLineComments(readFileSync(join(MIGRATIONS_DIR, file), 'utf8'));
for (const [label, re] of patterns) {
if (re.test(sql)) offenders.push(`${file}: ${label}`);
}
}
expect(offenders).toEqual([]);
});
it('审计点名的文件确实带上了 IF NOT EXISTS(防回退)', () => {
const expectContains = (file: string, needle: string) => {
expect(readFileSync(join(MIGRATIONS_DIR, file), 'utf8')).toContain(needle);
};
expectContains('20260606_070000_create_customer_image_aliases.sql', 'CREATE INDEX IF NOT EXISTS idx_customer_image_aliases_customer');
expectContains('20260902_create_order_image_vectors.sql', 'CREATE INDEX IF NOT EXISTS idx_order_image_vectors_hash');
expectContains('20260773_tenant_schema_per_branch.sql', 'CREATE TABLE IF NOT EXISTS %I.%I (LIKE');
expectContains('20260850_processing_demand_sheet.sql', 'CREATE TABLE IF NOT EXISTS %I.processing_demand_items');
expectContains('20260850_processing_demand_sheet.sql', 'CREATE TABLE IF NOT EXISTS %I.processing_demand_picks');
expectContains('20260904_order_image_vectors_parse_session.sql', 'CREATE TABLE IF NOT EXISTS %I.order_image_vectors');
});
});