mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 14:17:31 +00:00
fix(postgres): connect over a Unix socket
TREK_DB_URL could not name a Unix socket. MikroORM builds the pool from the URL's own host and drops a host parameter, so postgresql://trek@localhost/trek?host=/var/run/postgresql connected to localhost over TCP. A percent-encoded socket path as the host reached the pool, but the client tools got it undecoded in PGHOST, so backups, restores and the safety copy before a migration failed. The server address is now read once, the way libpq reads a connection URL: a host or port parameter takes the place of the URL's own, and a host written into the URL is percent-decoded. The pool and the client tools both get that address. The wiki and .env.example show the socket form.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { pgServerAddress } from './server-address';
|
||||
|
||||
import { execFile, spawn } from 'node:child_process';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
@@ -65,9 +67,10 @@ export function resolvePgTool(tool: PgTool, binDir: string | undefined): string
|
||||
*/
|
||||
export function pgConnectionEnv(url: string): Record<string, string> {
|
||||
const parsed = new URL(url);
|
||||
const address = pgServerAddress(url);
|
||||
const env: Record<string, string> = {
|
||||
PGHOST: parsed.hostname.replace(/^\[(.*)\]$/, '$1'),
|
||||
PGPORT: parsed.port || '5432',
|
||||
PGHOST: address.host,
|
||||
PGPORT: String(address.port ?? 5432),
|
||||
PGDATABASE: decodeURIComponent(parsed.pathname.replace(/^\//, '')),
|
||||
PGCONNECT_TIMEOUT: CONNECT_TIMEOUT_SECONDS,
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { DatabaseEngine } from '../engine';
|
||||
import { redactUrl } from '../redact-url';
|
||||
import { TrekPostgreSqlDriver } from './postgres-driver';
|
||||
import { bootstrapPostgresSchema, defaultPostgresBootstrapDeps, type PostgresBootstrapDeps } from './schema-bootstrap';
|
||||
import { pgServerAddress } from './server-address';
|
||||
import { putTempSchemaLast } from './session-search-path';
|
||||
import { sslOptionFor } from './ssl';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
@@ -84,9 +85,13 @@ export class PostgresEngine implements DatabaseEngine {
|
||||
/** Every new pool connection first puts `pg_temp` last on its `search_path` (`session-search-path.ts`). */
|
||||
ormOptions() {
|
||||
const ssl = sslOptionFor(this.settings.url);
|
||||
// MikroORM takes the address from the URL's authority alone; a `host` parameter (a socket directory) would be lost.
|
||||
const { host, port } = pgServerAddress(this.settings.url);
|
||||
return {
|
||||
driver: TrekPostgreSqlDriver,
|
||||
clientUrl: this.settings.url,
|
||||
host,
|
||||
port,
|
||||
pool: { min: 0, max: this.settings.poolMax },
|
||||
driverOptions: ssl === undefined ? {} : { ssl },
|
||||
onCreateConnection: (connection: unknown) => putTempSchemaLast(connection as DatabaseConnection),
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/** Where the Postgres server listens, as TREK_DB_URL names it. */
|
||||
interface PgServerAddress {
|
||||
/** A host name, an IP address, or the directory of a Unix socket (a path starting with `/`); empty when the URL names none. */
|
||||
host: string;
|
||||
port: number | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* The server address in `url`, read the way libpq reads a connection URL: a
|
||||
* `host` or `port` query parameter takes the place of the URL's own, so
|
||||
* `postgresql://trek@localhost/trek?host=/var/run/postgresql` connects through
|
||||
* the socket in that directory, and a host written into the URL itself is
|
||||
* percent-decoded (`%2Fvar%2Frun%2Fpostgresql`). The app's pool and the client
|
||||
* tools both connect to this one address, so a backup reaches the database
|
||||
* the app runs on.
|
||||
*/
|
||||
export function pgServerAddress(url: string): PgServerAddress {
|
||||
const parsed = new URL(url);
|
||||
const params = parsed.searchParams;
|
||||
const host = params.get('host') ?? decodeURIComponent(parsed.hostname.replace(/^\[(.*)\]$/, '$1'));
|
||||
const rawPort = params.get('port') ?? parsed.port;
|
||||
if (rawPort === '') return { host, port: undefined };
|
||||
const port = Number(rawPort);
|
||||
if (!/^\d+$/.test(rawPort) || port < 1 || port > 65535) {
|
||||
throw new Error(`TREK_DB_URL names port "${rawPort}", which is not a port number.`);
|
||||
}
|
||||
return { host, port };
|
||||
}
|
||||
Reference in New Issue
Block a user