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:
浅念
2026-10-20 20:43:13 +08:00
parent 9e0b49885b
commit f1c181de02
13 changed files with 338 additions and 220 deletions
+14 -26
View File
@@ -1,12 +1,12 @@
// 分类域集成测试:taxonomies 三元组唯一(换 object_type 放行对照——同 key 服务不同对象类型是合法的,
// 如 post 与 product 各挂一套 category)+ terms 来源身份唯一 + content_terms 复合主键 +
// terms.parent_term_id 自引用 FK + is_primary 布尔列(Yoast 主分类)。
// 生命周期纪律:随机后缀插入 + finally 反序清理,可重复运行不残留脏数据(本域无不可变表,常规删除即可;
// Task 13 起夹具走公共 helpers(createSiteFixture/withTestDb/rejectionMessage);
// 生命周期纪律不变:随机后缀插入 + finally 反序清理(本域无不可变表,常规删除即可;
// content_terms 依赖 content_entries,fixture 建链照 Task 8 模式只建到 entry 即可)。
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
entryId: string
@@ -17,10 +17,9 @@ type Fixture = {
// 建链(FK 依赖顺序):site → content_entry → taxonomy → term(默认无 parent,层级在各测试内自建)。
async function setupTaxonomyFixture(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(), 'tax-${rand}', 'TaxFixture', 'en', 'https://tax.example') returning id`)) as any
const siteId = site[0].id
const site = await createSiteFixture(db, 'tax')
const rand = site.rand
const siteId = site.siteId
const entry = (await db.execute(`insert into content_entries (id, site_id, type_key, lifecycle_status)
values (gen_random_uuid(), '${siteId}', 'post', 'active') returning id`)) as any
const tax = (await db.execute(`insert into taxonomies (id, site_id, key, object_type, public_base_path)
@@ -47,25 +46,14 @@ async function cleanupTaxonomyFixture(db: Db, f: Fixture) {
}
async function withTaxonomyFixture(fn: (db: Db, f: Fixture) => Promise<void>) {
const db = getDb()
const f = await setupTaxonomyFixture(db)
try {
await fn(db, f)
} finally {
await cleanupTaxonomyFixture(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 setupTaxonomyFixture(db)
try {
await fn(db, f)
} finally {
await cleanupTaxonomyFixture(db, f)
}
})
}
describe('分类域约束', () => {