- 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 超长修复
51 lines
2.4 KiB
TypeScript
51 lines
2.4 KiB
TypeScript
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');
|
||
});
|
||
});
|