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:
@@ -0,0 +1,53 @@
|
||||
// 程序化数据库供给与迁移(Task 13):drizzle-kit migrate 只认 drizzle.config 的
|
||||
// DATABASE_URL(requireDatabaseUrl allowTestUrl:false),无法指向运行时才创建的
|
||||
// 临时库——vitest globalSetup 用本模块对 sf_test_* 临时库完成 CREATE + 全量迁移 + DROP。
|
||||
// 驱动依赖(postgres / drizzle-orm)必须从本包解析:仓库根 node_modules 不含它们,
|
||||
// tests/ 下的调用方无法裸 import,故统一收口在本文件。
|
||||
import { dirname, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import postgres from 'postgres'
|
||||
import { drizzle } from 'drizzle-orm/postgres-js'
|
||||
import { migrate } from 'drizzle-orm/postgres-js/migrator'
|
||||
|
||||
// 本文件位于 apps/api/src/db/ → 上两级是 apps/api/,迁移产物在其 drizzle/ 子目录
|
||||
const defaultMigrationsFolder = join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'drizzle')
|
||||
|
||||
// CREATE/DROP DATABASE 不能走参数绑定(且本身是 DDL),名称先做白名单校验再内插。
|
||||
function assertSafeName(name: string): void {
|
||||
if (!/^[a-z0-9_]+$/i.test(name)) throw new Error(`unsafe database name: ${name}`)
|
||||
}
|
||||
|
||||
/** 对显式连接串的目标库执行 apps/api/drizzle 下的全量迁移(空库 → 最新 schema)。 */
|
||||
export async function migrateDatabase(
|
||||
connectionString: string,
|
||||
migrationsFolder: string = defaultMigrationsFolder,
|
||||
): Promise<void> {
|
||||
const sql = postgres(connectionString, { max: 1 })
|
||||
try {
|
||||
await migrate(drizzle(sql), { migrationsFolder })
|
||||
} finally {
|
||||
await sql.end()
|
||||
}
|
||||
}
|
||||
|
||||
/** 在 adminUrl(指向 postgres 维护库的连接串)所标识的集群里创建新数据库。 */
|
||||
export async function createDatabase(adminConnectionString: string, name: string): Promise<void> {
|
||||
assertSafeName(name)
|
||||
const sql = postgres(adminConnectionString, { max: 1 })
|
||||
try {
|
||||
await sql.unsafe(`create database "${name}"`)
|
||||
} finally {
|
||||
await sql.end()
|
||||
}
|
||||
}
|
||||
|
||||
/** 删除数据库。WITH (FORCE)(PG 13+)先断开目标库上的残余业务连接再删——测试池泄漏时不卡 teardown。 */
|
||||
export async function dropDatabase(adminConnectionString: string, name: string): Promise<void> {
|
||||
assertSafeName(name)
|
||||
const sql = postgres(adminConnectionString, { max: 1 })
|
||||
try {
|
||||
await sql.unsafe(`drop database if exists "${name}" with (force)`)
|
||||
} finally {
|
||||
await sql.end()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user