Harden embedded postgres adoption on startup
This commit is contained in:
@@ -50,6 +50,21 @@ export function createDb(url: string) {
|
|||||||
return drizzlePg(sql, { schema });
|
return drizzlePg(sql, { schema });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getPostgresDataDirectory(url: string): Promise<string | null> {
|
||||||
|
const sql = createUtilitySql(url);
|
||||||
|
try {
|
||||||
|
const rows = await sql<{ data_directory: string | null }[]>`
|
||||||
|
SELECT current_setting('data_directory', true) AS data_directory
|
||||||
|
`;
|
||||||
|
const actual = rows[0]?.data_directory;
|
||||||
|
return typeof actual === "string" && actual.length > 0 ? actual : null;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
await sql.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function listMigrationFiles(): Promise<string[]> {
|
async function listMigrationFiles(): Promise<string[]> {
|
||||||
const entries = await readdir(MIGRATIONS_FOLDER, { withFileTypes: true });
|
const entries = await readdir(MIGRATIONS_FOLDER, { withFileTypes: true });
|
||||||
return entries
|
return entries
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export {
|
export {
|
||||||
createDb,
|
createDb,
|
||||||
|
getPostgresDataDirectory,
|
||||||
ensurePostgresDatabase,
|
ensurePostgresDatabase,
|
||||||
inspectMigrations,
|
inspectMigrations,
|
||||||
applyPendingMigrations,
|
applyPendingMigrations,
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { existsSync, readFileSync, rmSync } from "node:fs";
|
import { existsSync, readFileSync, rmSync } from "node:fs";
|
||||||
import { createServer } from "node:net";
|
import { createServer } from "node:net";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import postgres from "postgres";
|
import { ensurePostgresDatabase, getPostgresDataDirectory } from "./client.js";
|
||||||
import { ensurePostgresDatabase } from "./client.js";
|
|
||||||
import { resolveDatabaseTarget } from "./runtime-config.js";
|
import { resolveDatabaseTarget } from "./runtime-config.js";
|
||||||
|
|
||||||
type EmbeddedPostgresInstance = {
|
type EmbeddedPostgresInstance = {
|
||||||
@@ -99,25 +98,6 @@ async function loadEmbeddedPostgresCtor(): Promise<EmbeddedPostgresCtor> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function matchesEmbeddedDataDir(
|
|
||||||
adminConnectionString: string,
|
|
||||||
expectedDataDir: string,
|
|
||||||
): Promise<boolean> {
|
|
||||||
const sql = postgres(adminConnectionString, { max: 1, onnotice: () => {} });
|
|
||||||
try {
|
|
||||||
const rows = await sql<{ data_directory: string | null }[]>`
|
|
||||||
SELECT current_setting('data_directory', true) AS data_directory
|
|
||||||
`;
|
|
||||||
const actual = rows[0]?.data_directory;
|
|
||||||
if (typeof actual !== "string" || actual.length === 0) return false;
|
|
||||||
return path.resolve(actual) === path.resolve(expectedDataDir);
|
|
||||||
} catch {
|
|
||||||
return false;
|
|
||||||
} finally {
|
|
||||||
await sql.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ensureEmbeddedPostgresConnection(
|
async function ensureEmbeddedPostgresConnection(
|
||||||
dataDir: string,
|
dataDir: string,
|
||||||
preferredPort: number,
|
preferredPort: number,
|
||||||
@@ -132,7 +112,10 @@ async function ensureEmbeddedPostgresConnection(
|
|||||||
|
|
||||||
if (!runningPid && existsSync(pgVersionFile)) {
|
if (!runningPid && existsSync(pgVersionFile)) {
|
||||||
try {
|
try {
|
||||||
const matchesDataDir = await matchesEmbeddedDataDir(preferredAdminConnectionString, dataDir);
|
const actualDataDir = await getPostgresDataDirectory(preferredAdminConnectionString);
|
||||||
|
const matchesDataDir =
|
||||||
|
typeof actualDataDir === "string" &&
|
||||||
|
path.resolve(actualDataDir) === path.resolve(dataDir);
|
||||||
if (!matchesDataDir) {
|
if (!matchesDataDir) {
|
||||||
throw new Error("reachable postgres does not use the expected embedded data directory");
|
throw new Error("reachable postgres does not use the expected embedded data directory");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import { and, eq } from "drizzle-orm";
|
|||||||
import {
|
import {
|
||||||
createDb,
|
createDb,
|
||||||
ensurePostgresDatabase,
|
ensurePostgresDatabase,
|
||||||
|
getPostgresDataDirectory,
|
||||||
inspectMigrations,
|
inspectMigrations,
|
||||||
applyPendingMigrations,
|
applyPendingMigrations,
|
||||||
reconcilePendingMigrationHistory,
|
reconcilePendingMigrationHistory,
|
||||||
@@ -322,6 +323,13 @@ export async function startServer(): Promise<StartedServer> {
|
|||||||
} else {
|
} else {
|
||||||
const configuredAdminConnectionString = `postgres://paperclip:paperclip@127.0.0.1:${configuredPort}/postgres`;
|
const configuredAdminConnectionString = `postgres://paperclip:paperclip@127.0.0.1:${configuredPort}/postgres`;
|
||||||
try {
|
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");
|
await ensurePostgresDatabase(configuredAdminConnectionString, "paperclip");
|
||||||
logger.warn(
|
logger.warn(
|
||||||
`Embedded PostgreSQL appears to already be reachable without a pid file; reusing existing server on configured port ${configuredPort}`,
|
`Embedded PostgreSQL appears to already be reachable without a pid file; reusing existing server on configured port ${configuredPort}`,
|
||||||
|
|||||||
Reference in New Issue
Block a user