fix: optimize test infrastructure and fix missing brand migration
- Share PGlite instance per test file (TRUNCATE RESTART IDENTITY instead of creating new instance per test) — tests run in ~9s vs minutes - Add missing 'brand' column to items table migration - Fix corrupt 0002 snapshot (merge conflict artifacts) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,16 +1,51 @@
|
||||
import { PGlite } from "@electric-sql/pglite";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { drizzle } from "drizzle-orm/pglite";
|
||||
import { migrate } from "drizzle-orm/pglite/migrator";
|
||||
import * as schema from "../../src/db/schema.ts";
|
||||
|
||||
type Db = ReturnType<typeof drizzle<typeof schema>>;
|
||||
|
||||
export async function createTestDb() {
|
||||
const client = new PGlite();
|
||||
const db = drizzle(client, { schema });
|
||||
// Cache: one PGlite instance per test file (per worker)
|
||||
let cachedClient: PGlite | null = null;
|
||||
let cachedDb: Db | null = null;
|
||||
|
||||
// Apply all migrations to create tables
|
||||
await migrate(db, { migrationsFolder: "./drizzle-pg" });
|
||||
async function getOrCreateDb(): Promise<Db> {
|
||||
if (cachedDb) return cachedDb;
|
||||
cachedClient = new PGlite();
|
||||
cachedDb = drizzle(cachedClient, { schema });
|
||||
await migrate(cachedDb, { migrationsFolder: "./drizzle-pg" });
|
||||
return cachedDb;
|
||||
}
|
||||
|
||||
// Truncation order respects foreign keys (children first)
|
||||
const TRUNCATE_TABLES = [
|
||||
"setup_items",
|
||||
"setups",
|
||||
"thread_candidates",
|
||||
"threads",
|
||||
"items",
|
||||
"global_item_tags",
|
||||
"global_items",
|
||||
"tags",
|
||||
"oauth_tokens",
|
||||
"oauth_codes",
|
||||
"oauth_clients",
|
||||
"api_keys",
|
||||
"settings",
|
||||
"categories",
|
||||
"users",
|
||||
];
|
||||
|
||||
export async function createTestDb() {
|
||||
const db = await getOrCreateDb();
|
||||
|
||||
// Truncate all tables and reset sequences (fast reset, no PGlite restart)
|
||||
for (const t of TRUNCATE_TABLES) {
|
||||
await db.execute(
|
||||
sql.raw(`TRUNCATE TABLE "${t}" RESTART IDENTITY CASCADE`),
|
||||
);
|
||||
}
|
||||
|
||||
// Seed a test user
|
||||
const [user] = await db
|
||||
|
||||
Reference in New Issue
Block a user