Harden embedded postgres adoption on startup

This commit is contained in:
Dotta
2026-03-16 21:03:05 -05:00
parent 8d5af56fc5
commit 88bf1b23a3
4 changed files with 29 additions and 22 deletions

View File

@@ -10,6 +10,7 @@ import { and, eq } from "drizzle-orm";
import {
createDb,
ensurePostgresDatabase,
getPostgresDataDirectory,
inspectMigrations,
applyPendingMigrations,
reconcilePendingMigrationHistory,
@@ -322,6 +323,13 @@ export async function startServer(): Promise<StartedServer> {
} else {
const configuredAdminConnectionString = `postgres://paperclip:paperclip@127.0.0.1:${configuredPort}/postgres`;
try {
const actualDataDir = await getPostgresDataDirectory(configuredAdminConnectionString);
if (
typeof actualDataDir !== "string" ||
resolve(actualDataDir) !== resolve(dataDir)
) {
throw new Error("reachable postgres does not use the expected embedded data directory");
}
await ensurePostgresDatabase(configuredAdminConnectionString, "paperclip");
logger.warn(
`Embedded PostgreSQL appears to already be reachable without a pid file; reusing existing server on configured port ${configuredPort}`,