test(db): 空库一键迁移验收 + globalSetup 临时库 + 公共夹具收敛(阶段1验收)
- vitest globalSetup:每次运行 CREATE sf_test_<ts> 临时库 → drizzle-orm 程序化迁移器 全量迁移(apps/api/src/db/migrate.ts,drizzle-kit 无法指向运行时建库)→ 设 DATABASE_URL_TEST 让 getDb() 自动回退到临时库;teardown DROP(WITH FORCE)。 有 default 导出时 vitest 不捕获具名 teardown,故用双具名导出。 - db.full-migrate.test.ts:21 表齐 + 四张不可变触发器在库 + sf_test_* 护栏断言 (current_database 不符即红,杜绝静默连开发库)。 - helpers.ts 收敛:rand/rejectionMessage/createSiteFixture/withImmutableCleanup/ withTestDb,Task 6–12 七文件内联版替换为 import(断言、对照组、清场顺序不变)。 - scripts/migrate-test.sh:docker compose up --wait + pnpm test 一键验收。 - 实证:75 测试全绿(72+3);开发库 21 表行数总和 0(零写入);无 sf_test_* 残留。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -7,18 +7,16 @@
|
||||
// 3. query_key 参与唯一:同 exact key 下 query_key='p' 与 NULL 共存;同 'p' 第二条拒绝;
|
||||
// 4. route_kind enum 六值(html/asset/redirect/gone/dynamic/unresolved)往返 + 第七值拒绝;
|
||||
// 5. query_policy enum 四值(ignore/exact/preserve/dynamic)往返 + 第五值拒绝。
|
||||
// 生命周期纪律:随机后缀建 site + finally 反序清理(route_registry → sites);本表无不可变触发器。
|
||||
// Task 13 起夹具走公共 helpers(createSiteFixture/withTestDb/rejectionMessage);
|
||||
// 生命周期纪律不变:随机后缀建 site + finally 反序清理(route_registry → sites);本表无不可变触发器。
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { getDb } from '../../apps/api/src/db/client.js'
|
||||
import { createSiteFixture, withTestDb, rejectionMessage, type Db } from './helpers.js'
|
||||
|
||||
type Db = ReturnType<typeof getDb>
|
||||
type Fixture = { siteId: string; rand: string }
|
||||
|
||||
async function setupRoutesFixture(db: Db): Promise<Fixture> {
|
||||
const rand = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
|
||||
const site = (await db.execute(`insert into sites (id, key, name, default_locale, production_origin)
|
||||
values (gen_random_uuid(), 'routes-${rand}', 'RoutesFixture', 'en', 'https://routes.example') returning id`)) as any
|
||||
return { siteId: site[0].id, rand }
|
||||
const site = await createSiteFixture(db, 'routes')
|
||||
return { siteId: site.siteId, rand: site.rand }
|
||||
}
|
||||
|
||||
// 反序清理:route_registry(无不可变触发器,直接删)→ sites(级联挡在 site 前,先删子表)。
|
||||
@@ -28,25 +26,14 @@ async function cleanupRoutesFixture(db: Db, f: Fixture) {
|
||||
}
|
||||
|
||||
async function withRoutesFixture(fn: (db: Db, f: Fixture) => Promise<void>) {
|
||||
const db = getDb()
|
||||
const f = await setupRoutesFixture(db)
|
||||
try {
|
||||
await fn(db, f)
|
||||
} finally {
|
||||
await cleanupRoutesFixture(db, f)
|
||||
await (db as any).$client.end()
|
||||
}
|
||||
}
|
||||
|
||||
// drizzle 会把 PostgresError 包成 DrizzleQueryError(message 只有 "Failed query: ..."),
|
||||
// 服务端原始错误文本在 cause 里——断言约束/枚举身份时必须看 cause。
|
||||
async function rejectionMessage(db: Db, query: string): Promise<string> {
|
||||
try {
|
||||
await db.execute(query)
|
||||
} catch (e: any) {
|
||||
return String(e?.cause?.message ?? e?.message ?? e)
|
||||
}
|
||||
throw new Error(`expected query to be rejected: ${query}`)
|
||||
await withTestDb(async (db) => {
|
||||
const f = await setupRoutesFixture(db)
|
||||
try {
|
||||
await fn(db, f)
|
||||
} finally {
|
||||
await cleanupRoutesFixture(db, f)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 探测插入:除显式指定的列外全部换新值(随机 suffix 的 id/path/collision_key),保证被测
|
||||
|
||||
Reference in New Issue
Block a user