From c60041a1bba0453f71dd6d27f8d6ce48db01b6cc Mon Sep 17 00:00:00 2001 From: igm Date: Mon, 12 Jan 2026 17:12:10 +0800 Subject: [PATCH] Replace dbmate with direct schema.sql execution in tests Instead of running dbmate migrations, tests now directly execute the db/schema.sql file on the test database. This is faster and removes the dbmate dependency from tests. Co-Authored-By: Claude Opus 4.5 --- packages/testing/test-helpers/src/test-db.ts | 49 +++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/packages/testing/test-helpers/src/test-db.ts b/packages/testing/test-helpers/src/test-db.ts index 3c89339..4afeff7 100644 --- a/packages/testing/test-helpers/src/test-db.ts +++ b/packages/testing/test-helpers/src/test-db.ts @@ -142,7 +142,7 @@ async function ensureTestDatabaseExists(): Promise { } /** - * Finds the repository root by looking for db/migrations directory. + * Finds the repository root by looking for db/schema.sql file. * Walks up from the current directory until found. * * @throws Error if repo root cannot be found @@ -152,8 +152,8 @@ function findRepoRoot(): string { // Walk up to 10 levels to find the repo root for (let i = 0; i < 10; i++) { - const migrationsPath = join(current, "db", "migrations"); - if (existsSync(migrationsPath)) { + const schemaPath = join(current, "db", "schema.sql"); + if (existsSync(schemaPath)) { return current; } const parent = join(current, ".."); @@ -164,13 +164,13 @@ function findRepoRoot(): string { } throw new Error( - "Could not find repository root (looking for db/migrations directory)", + "Could not find repository root (looking for db/schema.sql file)", ); } /** - * Runs database migrations using dbmate CLI. - * Creates the database if it doesn't exist. + * Applies the database schema from db/schema.sql. + * Creates the database if it doesn't exist, then runs the schema. * Should be called once before running test suite. */ export async function runMigrations(): Promise { @@ -180,21 +180,34 @@ export async function runMigrations(): Promise { await ensureTestDatabaseExists(); const repoRoot = findRepoRoot(); + const schemaPath = join(repoRoot, "db", "schema.sql"); - const proc = Bun.spawn(["dbmate", "up"], { - env: { ...process.env, DATABASE_URL: testDbUrl }, - cwd: repoRoot, - stdout: "pipe", - stderr: "pipe", + // Read the schema file + const schemaFile = Bun.file(schemaPath); + const schemaSql = await schemaFile.text(); + + // Connect to the test database and run the schema + const { host, port, user, password, database } = parsePostgresUrl(testDbUrl); + + const client = new Client({ + host, + port, + user, + password, + database, }); - const exitCode = await proc.exited; - - if (exitCode !== 0) { - const stderr = await new Response(proc.stderr).text(); - throw new Error( - `Migration failed with code ${exitCode.toString()}: ${stderr}`, - ); + try { + await client.connect(); + await client.query(schemaSql); + } catch (error) { + // Ignore "already exists" errors - schema may have already been applied + const message = error instanceof Error ? error.message : String(error); + if (!message.includes("already exists")) { + throw new Error(`Schema application failed: ${message}`); + } + } finally { + await client.end(); } }