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
+18 -40
View File
@@ -1,11 +1,11 @@
// 内容域集成测试:修订不可变触发器(UPDATE/DELETE 拒绝)+ entry_locales 三元组唯一 +
// 双指针语义(发布=只动 entry_locales 指针,不碰修订行)+ source_records.content_entry_id 回填 FK。
// 生命周期纪律:随机后缀插入 + finally 反序清理;不可变表(content_revisions / source_snapshots)
// 清场走点名 DISABLE TRIGGER → 删本级行 → ENABLE,嵌套 finally 保证触发器必然恢复。
// Task 13 起夹具走公共 helpers(createSiteFixture/withImmutableCleanup/withTestDb/rejectionMessage);
// 生命周期纪律不变:随机后缀插入 + finally 反序清理;不可变表(content_revisions / source_snapshots)
// 清场走 DISABLE→删→ENABLE 封装,嵌套 finally 保证触发器必然恢复。
import { describe, it, expect } from 'vitest'
import { getDb } from '../../apps/api/src/db/client.js'
import { createSiteFixture, withImmutableCleanup, withTestDb, rejectionMessage, type Db } from './helpers.js'
type Db = ReturnType<typeof getDb>
type Fixture = {
siteId: string
entryId: string
@@ -19,10 +19,9 @@ type Fixture = {
// 建链(FK 依赖顺序):site → content_entry → content_revision → entry_locale(current 指针指向该修订,published=NULL);
// 另建 source 链(connection → snapshot → job)供 source_records FK 探针使用。
async function setupContentFixture(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(), 'content-${rand}', 'ContentFixture', 'en', 'https://content.example') returning id`)) as any
const siteId = site[0].id
const site = await createSiteFixture(db, 'content')
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 entryId = entry[0].id
@@ -41,48 +40,27 @@ async function setupContentFixture(db: Db): Promise<Fixture> {
return { siteId, entryId, localeId: locale[0].id, revisionId, connectionId, snapshotId: snap[0].id, importJobId: job[0].id }
}
// 反序清理。source_records 先于 content_entries(新 FK);两张不可变表逐一 DISABLE → 删 → ENABLE(嵌套 finally)。
// 反序清理。source_records 先于 content_entries(新 FK);两张不可变表逐一 DISABLE→删→ENABLE。
async function cleanupContentFixture(db: Db, f: Fixture) {
await db.execute(`delete from source_records where site_id = '${f.siteId}'`)
await db.execute(`delete from entry_locales where site_id = '${f.siteId}'`)
await db.execute(`alter table content_revisions disable trigger content_revisions_immutable`)
try {
await db.execute(`delete from content_revisions where site_id = '${f.siteId}'`)
} finally {
await db.execute(`alter table content_revisions enable trigger content_revisions_immutable`)
}
await withImmutableCleanup(db, 'content_revisions', 'content_revisions_immutable', `site_id = '${f.siteId}'`)
await db.execute(`delete from import_jobs where site_id = '${f.siteId}'`)
await db.execute(`alter table source_snapshots disable trigger source_snapshots_immutable`)
try {
await db.execute(`delete from source_snapshots where site_id = '${f.siteId}'`)
} finally {
await db.execute(`alter table source_snapshots enable trigger source_snapshots_immutable`)
}
await withImmutableCleanup(db, 'source_snapshots', 'source_snapshots_immutable', `site_id = '${f.siteId}'`)
await db.execute(`delete from source_connections where site_id = '${f.siteId}'`)
await db.execute(`delete from content_entries where site_id = '${f.siteId}'`)
await db.execute(`delete from sites where id = '${f.siteId}'`)
}
async function withContentFixture(fn: (db: Db, f: Fixture) => Promise<void>) {
const db = getDb()
const f = await setupContentFixture(db)
try {
await fn(db, f)
} finally {
await cleanupContentFixture(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 setupContentFixture(db)
try {
await fn(db, f)
} finally {
await cleanupContentFixture(db, f)
}
})
}
describe('内容域约束(修订不可变 + 双指针)', () => {