mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-16 02:40:39 +09:00
Harden embedded postgres adoption on startup
This commit is contained in:
parent
8d5af56fc5
commit
88bf1b23a3
4 changed files with 29 additions and 22 deletions
|
|
@ -1,8 +1,7 @@
|
|||
import { existsSync, readFileSync, rmSync } from "node:fs";
|
||||
import { createServer } from "node:net";
|
||||
import path from "node:path";
|
||||
import postgres from "postgres";
|
||||
import { ensurePostgresDatabase } from "./client.js";
|
||||
import { ensurePostgresDatabase, getPostgresDataDirectory } from "./client.js";
|
||||
import { resolveDatabaseTarget } from "./runtime-config.js";
|
||||
|
||||
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(
|
||||
dataDir: string,
|
||||
preferredPort: number,
|
||||
|
|
@ -132,7 +112,10 @@ async function ensureEmbeddedPostgresConnection(
|
|||
|
||||
if (!runningPid && existsSync(pgVersionFile)) {
|
||||
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) {
|
||||
throw new Error("reachable postgres does not use the expected embedded data directory");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue