Replace dbmate with direct schema.sql execution in tests
Some checks failed
CI / ci (push) Has been cancelled
Some checks failed
CI / ci (push) Has been cancelled
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 <noreply@anthropic.com>
This commit is contained in:
@@ -142,7 +142,7 @@ async function ensureTestDatabaseExists(): Promise<void> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<void> {
|
||||
@@ -180,21 +180,34 @@ export async function runMigrations(): Promise<void> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user