cli: address review feedback for embedded-postgres

- Close embedded DB on shutdown to avoid leaking the Postgres process
  and temp directory
- Use fs.remove instead of deprecated fs.rmdir with recursive option
- Guard against absolute config paths in readDatabaseClient
- Forward embedded-postgres error logs to console.error

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
Made-with: Cursor
This commit is contained in:
Patrik Oldsberg
2026-04-01 21:36:04 +02:00
parent ae1cdd9e9f
commit 1f88d2624b
2 changed files with 10 additions and 5 deletions
@@ -66,12 +66,14 @@ export async function runBackend(options: RunBackendOptions) {
const extraEnv: Record<string, string> = {};
let embeddedDb: Awaited<ReturnType<typeof startEmbeddedDb>> | undefined;
const dbClient = await readDatabaseClient(options.configPaths);
if (dbClient === 'embedded-postgres') {
const db = await startEmbeddedDb();
embeddedDb = await startEmbeddedDb();
extraEnv.APP_CONFIG_backend_database = JSON.stringify({
client: 'pg',
connection: db.connection,
connection: embeddedDb.connection,
});
}
@@ -205,6 +207,7 @@ export async function runBackend(options: RunBackendOptions) {
});
}
await embeddedDb?.close();
resolveExitPromise();
}
@@ -224,7 +227,7 @@ async function readDatabaseClient(
allowMissingDefaultConfig: true,
argv: (configPaths ?? []).flatMap(p => [
'--config',
resolvePath(rootDir, p),
isAbsolutePath(p) ? p : resolvePath(rootDir, p),
]),
});
@@ -45,7 +45,9 @@ export async function startEmbeddedDb() {
password,
port,
persistent: false,
onError() {},
onError(messageOrError) {
console.error(`[embedded-postgres]`, messageOrError);
},
onLog() {},
});
@@ -64,7 +66,7 @@ export async function startEmbeddedDb() {
},
async close() {
await pg.stop();
await fs.rmdir(tmpDir, { recursive: true, maxRetries: 3 });
await fs.remove(tmpDir);
},
};
}