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.
|
* Walks up from the current directory until found.
|
||||||
*
|
*
|
||||||
* @throws Error if repo root cannot be 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
|
// Walk up to 10 levels to find the repo root
|
||||||
for (let i = 0; i < 10; i++) {
|
for (let i = 0; i < 10; i++) {
|
||||||
const migrationsPath = join(current, "db", "migrations");
|
const schemaPath = join(current, "db", "schema.sql");
|
||||||
if (existsSync(migrationsPath)) {
|
if (existsSync(schemaPath)) {
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
const parent = join(current, "..");
|
const parent = join(current, "..");
|
||||||
@@ -164,13 +164,13 @@ function findRepoRoot(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(
|
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.
|
* Applies the database schema from db/schema.sql.
|
||||||
* Creates the database if it doesn't exist.
|
* Creates the database if it doesn't exist, then runs the schema.
|
||||||
* Should be called once before running test suite.
|
* Should be called once before running test suite.
|
||||||
*/
|
*/
|
||||||
export async function runMigrations(): Promise<void> {
|
export async function runMigrations(): Promise<void> {
|
||||||
@@ -180,21 +180,34 @@ export async function runMigrations(): Promise<void> {
|
|||||||
await ensureTestDatabaseExists();
|
await ensureTestDatabaseExists();
|
||||||
|
|
||||||
const repoRoot = findRepoRoot();
|
const repoRoot = findRepoRoot();
|
||||||
|
const schemaPath = join(repoRoot, "db", "schema.sql");
|
||||||
|
|
||||||
const proc = Bun.spawn(["dbmate", "up"], {
|
// Read the schema file
|
||||||
env: { ...process.env, DATABASE_URL: testDbUrl },
|
const schemaFile = Bun.file(schemaPath);
|
||||||
cwd: repoRoot,
|
const schemaSql = await schemaFile.text();
|
||||||
stdout: "pipe",
|
|
||||||
stderr: "pipe",
|
// 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;
|
try {
|
||||||
|
await client.connect();
|
||||||
if (exitCode !== 0) {
|
await client.query(schemaSql);
|
||||||
const stderr = await new Response(proc.stderr).text();
|
} catch (error) {
|
||||||
throw new Error(
|
// Ignore "already exists" errors - schema may have already been applied
|
||||||
`Migration failed with code ${exitCode.toString()}: ${stderr}`,
|
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