Initial import of streamline
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { test, expect, describe } from "bun:test";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { getTableConfig } from "drizzle-orm/pg-core";
|
||||
import * as schema from "../src/schema";
|
||||
|
||||
/**
|
||||
* 迁移与 schema.ts 的防漂移测试。
|
||||
*
|
||||
* ## 它挡的是哪一类事故
|
||||
*
|
||||
* 加一列要动两处:写一份 `drizzle/*.sql` 把列加进生产库,再在 `schema.ts` 的表定义
|
||||
* 上声明它。两处只做了一处,后果**完全不报错**:
|
||||
*
|
||||
* - 迁移做了、声明漏了 → drizzle 把那个键**悄悄丢掉**。写路径通常是
|
||||
* `setStatus(id, { …一堆键…, trackingImpact })` 这种形状,SET 子句里还有别的键,
|
||||
* 所以 SQL 合法、任务照常收尾、日志照常打「扫描完成」,只有那个结论人间蒸发。
|
||||
* - 声明做了、迁移漏了 → 读路径反而**能过**(select 不会因为多要一列而炸,实测),
|
||||
* 于是同样安静,直到某天有人去写它。
|
||||
*
|
||||
* 生产上真的发生过一次:`tracking_impact` 在 0013 里加给了 tasks 和 features,而
|
||||
* schema.ts 只声明在 features 和 security 上。埋点扫描对 bug 整整一段时间没有落过库,
|
||||
* 没有任何一条日志、告警或测试变红。
|
||||
*
|
||||
* ## 为什么是解析 SQL 而不是连库
|
||||
*
|
||||
* 连生产库比对当然更准,但那样这个测试就只能在有 `DATABASE_URL` 的机器上跑,CI 上
|
||||
* 会静默跳过 —— 一个会自己跳过的防漂移测试等于没有。迁移文件在仓库里,是离线可得
|
||||
* 的真值,也正是 code review 时人眼会漏掉的那一半。
|
||||
*/
|
||||
|
||||
const MIGRATIONS_DIR = path.resolve(import.meta.dir, "../../drizzle");
|
||||
|
||||
/** `ALTER TABLE "tasks" ADD COLUMN IF NOT EXISTS "base_ref" text;` → [tasks, base_ref] */
|
||||
const ADD_COLUMN_RE =
|
||||
/ALTER\s+TABLE\s+"?(\w+)"?\s+ADD\s+COLUMN\s+(?:IF\s+NOT\s+EXISTS\s+)?"?(\w+)"?/gi;
|
||||
|
||||
/** `ALTER TABLE "features" DROP COLUMN IF EXISTS "deliverable";` → [features, deliverable] */
|
||||
const DROP_COLUMN_RE =
|
||||
/ALTER\s+TABLE\s+"?(\w+)"?\s+DROP\s+COLUMN\s+(?:IF\s+EXISTS\s+)?"?(\w+)"?/gi;
|
||||
|
||||
/** schema.ts 里声明的所有表 → 它们的列名集合。 */
|
||||
function declaredColumns(): Map<string, Set<string>> {
|
||||
const out = new Map<string, Set<string>>();
|
||||
for (const value of Object.values(schema)) {
|
||||
let cfg;
|
||||
try { cfg = getTableConfig(value as never); } catch { continue; }
|
||||
out.set(cfg.name, new Set(cfg.columns.map((c) => c.name)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
describe("迁移与 schema.ts · 每一列都两处都有", () => {
|
||||
const declared = declaredColumns();
|
||||
|
||||
test("schema.ts 里确实解析出了表", () => {
|
||||
expect(declared.size).toBeGreaterThan(5);
|
||||
expect(declared.get("tasks")?.size ?? 0).toBeGreaterThan(20);
|
||||
});
|
||||
|
||||
test("每个 ADD COLUMN 迁移过的列,schema.ts 都声明了", () => {
|
||||
const files = readdirSync(MIGRATIONS_DIR).filter((f) => f.endsWith(".sql")).sort();
|
||||
// 被后续迁移 DROP 掉的列不该再要求 schema.ts 声明(例:0037 删掉了「验证」搬走后
|
||||
// features 上留下的三列)。只认「加在前、删在后」:先删后加的列仍然要声明。
|
||||
const droppedAfter = new Map<string, string>();
|
||||
for (const file of files) {
|
||||
const body = readFileSync(path.join(MIGRATIONS_DIR, file), "utf8").replace(/^\s*--.*$/gm, "");
|
||||
DROP_COLUMN_RE.lastIndex = 0;
|
||||
for (const m of body.matchAll(DROP_COLUMN_RE)) droppedAfter.set(`${m[1]}.${m[2]}`, file);
|
||||
}
|
||||
const missing: string[] = [];
|
||||
for (const file of files) {
|
||||
const sql = readFileSync(path.join(MIGRATIONS_DIR, file), "utf8");
|
||||
// 注释里会出现示例 SQL,先把 `--` 行剥掉,免得把说明文字当成迁移。
|
||||
const body = sql.replace(/^\s*--.*$/gm, "");
|
||||
ADD_COLUMN_RE.lastIndex = 0;
|
||||
for (const m of body.matchAll(ADD_COLUMN_RE)) {
|
||||
const [, table, column] = m;
|
||||
const cols = declared.get(table);
|
||||
// 表本身没在 schema.ts 里声明是另一回事(可能是纯运维表),不在本测试范围。
|
||||
if (!cols) continue;
|
||||
const dropFile = droppedAfter.get(`${table}.${column}`);
|
||||
if (dropFile && dropFile > file) continue;
|
||||
if (!cols.has(column)) missing.push(`${file}: ${table}.${column}`);
|
||||
}
|
||||
}
|
||||
expect(
|
||||
missing,
|
||||
`这些列已经迁移进库、但 schema.ts 没有声明 —— drizzle 会把对应的键悄悄丢掉:\n ${missing.join("\n ")}`,
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user