来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
95 lines
4.7 KiB
TypeScript
95 lines
4.7 KiB
TypeScript
import { readFileSync, readdirSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 迁移链「空库可重放」卫生契约(H10,2026-09-24)。
|
||
*
|
||
* 背景:灾备 / 新环境从零搭建 = 在空库上按文件名顺序重放 `migrations/*.sql`。
|
||
* 存量环境因为「早已 applied」不会重跑,所以这类漂移**在存量库上完全看不出来**,
|
||
* 只在真出事(重建 / 新租户环境)时才炸。已实测修掉的两类:
|
||
* ① `20260606_041744_create_default_users.sql` 引用 `auth.users.is_super_admin`
|
||
* —— 该列要到下一个迁移才建,且 09-16 已判为死列并 DROP(超管改由 user_roles 判定);
|
||
* ② 6 个 RLS 迁移里 `user_roles.role = ANY (ARRAY['super_admin','admin'])`
|
||
* —— 该阶段 role 仍是枚举 `app_role`,与 text[] 比较报 42883;要到
|
||
* `20260790_user_roles_role_text.sql` 才转成 TEXT。
|
||
* 两条都只在空库重放时暴露,故在此钉住。
|
||
*/
|
||
|
||
const MIGRATIONS_DIR = path.join(__dirname, '..', '..', 'migrations');
|
||
const read = (f: string) => readFileSync(path.join(MIGRATIONS_DIR, f), 'utf8');
|
||
const allFiles = readdirSync(MIGRATIONS_DIR).filter((f) => f.endsWith('.sql'));
|
||
|
||
describe('迁移链空库重放卫生(H10)', () => {
|
||
it('默认用户种子不得引用 20260916 已删除的死列 is_super_admin', () => {
|
||
const seed = read('20260606_041744_create_default_users.sql');
|
||
// 只允许出现在注释里说明原因,不允许出现在 SQL 语句中
|
||
const sqlOnly = seed
|
||
.split('\n')
|
||
.filter((line) => !line.trim().startsWith('--'))
|
||
.join('\n');
|
||
expect(sqlOnly).not.toContain('is_super_admin');
|
||
// 超管仍由紧随其后的角色分配迁移显式授予
|
||
expect(read('20260606_041757_assign_user_roles.sql')).toContain(
|
||
"('a0000000-0000-0000-0000-000000000001', 'super_admin')"
|
||
);
|
||
// 死列的删除迁移必须仍在(别为了让重放通过而回退它)
|
||
expect(allFiles).toContain('20260916_drop_auth_users_is_super_admin.sql');
|
||
});
|
||
|
||
it('user_roles.role 与 text[] 比较必须带 ::text(枚举期才不报 42883)', () => {
|
||
const offenders: string[] = [];
|
||
for (const f of allFiles) {
|
||
const src = read(f);
|
||
for (const line of src.split('\n')) {
|
||
if (line.trim().startsWith('--')) continue;
|
||
if (/\b(?:user_roles|ur)\.role\s*=\s*ANY\s*\(ARRAY/.test(line) && !/role::text/.test(line)) {
|
||
offenders.push(`${f}: ${line.trim()}`);
|
||
}
|
||
}
|
||
}
|
||
expect(offenders, `缺 ::text 的枚举比较:\n${offenders.join('\n')}`).toEqual([]);
|
||
// 且确实存在这条已修的写法(防止断言因「一处都没有」而假绿)
|
||
const fixedCount = allFiles.reduce(
|
||
(n, f) => n + (read(f).match(/role::text = ANY \(ARRAY/g)?.length ?? 0),
|
||
0
|
||
);
|
||
expect(fixedCount).toBeGreaterThanOrEqual(10);
|
||
});
|
||
|
||
it('数据依赖种子必须带父行守卫(空库不得因外键中断)', () => {
|
||
const guarded: Array<[string, string[]]> = [
|
||
['20260606_060000_create_customer_aliases.sql', ['categories', 'customers', 'skus']],
|
||
['20260606_065930_insert_classification_rules.sql', ['categories']],
|
||
['20260606_113649_add_sku_aliases.sql', ['skus']],
|
||
];
|
||
for (const [file, parents] of guarded) {
|
||
const src = read(file);
|
||
expect(src, `${file} 缺 WHERE EXISTS 守卫`).toContain('WHERE EXISTS (SELECT 1 FROM');
|
||
for (const parent of parents) {
|
||
expect(src, `${file} 未守卫父表 ${parent}`).toContain(`FROM ${parent} `);
|
||
}
|
||
// ON CONFLICT(若有)必须在 SELECT/WHERE **之后**,不能落在 VALUES 里
|
||
const valuesBlock = src.match(/FROM \(VALUES[\s\S]*?\) AS v\(/)?.[0] ?? '';
|
||
expect(valuesBlock).not.toContain('ON CONFLICT');
|
||
}
|
||
});
|
||
|
||
it('新增枚举值必须独立成一个更早的迁移文件(同事务内加+用会被 PG 拒绝)', () => {
|
||
const splits: Array<[string, string, string]> = [
|
||
['20260627_000000_add_app_role_purchaser_value.sql', '20260627_add_purchaser_app_role.sql', 'purchaser'],
|
||
['20260766_000000_add_app_role_picker_value.sql', '20260766_add_picker_builtin_role.sql', 'picker'],
|
||
];
|
||
for (const [valueFile, useFile, value] of splits) {
|
||
expect(allFiles, `缺少 ${valueFile}`).toContain(valueFile);
|
||
expect(read(valueFile)).toContain(`ADD VALUE IF NOT EXISTS '${value}'`);
|
||
// 使用侧不得再自己 ADD VALUE(否则又回到同事务「加+用」)
|
||
expect(read(useFile)).not.toContain('ADD VALUE');
|
||
expect(read(useFile)).toContain(`'${value}'`);
|
||
// 排序:先提交枚举值,再使用
|
||
const sorted = [...allFiles].sort();
|
||
expect(sorted.indexOf(valueFile)).toBeLessThan(sorted.indexOf(useFile));
|
||
}
|
||
});
|
||
});
|