Skip to content

Commit

Permalink
fix: add opt.pgPort, honor $PGPORT 🐛💥
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Jul 15, 2024
1 parent 4450159 commit 96c8b49
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 12 additions & 5 deletions import.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const importGtfsAtomically = async (cfg) => {
connectDownloadScriptToStdout,
importScriptVerbose,
connectImportScriptToStdout,
pgHost, pgUser, pgPassword, pgMetaDatabase, pgOpts,
pgHost, pgPort, pgUser, pgPassword, pgMetaDatabase, pgOpts,
databaseNamePrefix,
schemaName,
pathToImportScript,
Expand All @@ -48,6 +48,7 @@ const importGtfsAtomically = async (cfg) => {
importScriptVerbose: true,
connectImportScriptToStdout: true,
pgHost: null,
pgPort: null,
pgUser: null,
pgPassword: null,
pgMetaDatabase: process.env.PGDATABASE || null,
Expand Down Expand Up @@ -102,11 +103,11 @@ const importGtfsAtomically = async (cfg) => {
// Thus, a newly created database also won't be removed if the transaction fails or is aborted, so we
// have to drop it manually when cleaning up failed/aborted imports.
const dbMngmtClient = await connectToMetaDatabase({
pgHost, pgUser, pgPassword, pgMetaDatabase, pgOpts,
pgHost, pgPort, pgUser, pgPassword, pgMetaDatabase, pgOpts,
})

const client = await connectToMetaDatabase({
pgHost, pgUser, pgPassword, pgMetaDatabase, pgOpts,
pgHost, pgPort, pgUser, pgPassword, pgMetaDatabase, pgOpts,
})

// We only ever keep one row in `latest_import`, which contains NULL in the beginning.
Expand Down Expand Up @@ -198,6 +199,9 @@ const importGtfsAtomically = async (cfg) => {
if (pgHost !== null) {
_importEnv.PGHOST = pgHost
}
if (pgPort !== null) {
_importEnv.PGPORT = pgPort
}
if (pgUser !== null) {
_importEnv.PGUSER = pgUser
}
Expand Down Expand Up @@ -237,15 +241,18 @@ const importGtfsAtomically = async (cfg) => {
// https://www.postgresql.org/docs/15/libpq-connect.html#id-1.7.3.8.3.5
const {
PGHOST,
PGPORT,
POSTGREST_USER,
POSTGREST_PASSWORD,
} = process.env
ok(PGHOST, 'missing/empty $PGHOST')
ok(PGPORT, 'missing/empty $PGPORT')
// todo: why `POSTGREST_`? rename to e.g. `PGBOUNCER_`?
ok(POSTGREST_USER, 'missing/empty $POSTGREST_USER')
ok(POSTGREST_PASSWORD, 'missing/empty $POSTGREST_PASSWORD')

const dsn = `gtfs=host=${PGHOST} dbname=${dbName} user=${POSTGREST_USER} password=${POSTGREST_PASSWORD}`
const logDsn = `gtfs=host=${PGHOST} dbname=${dbName} user=${POSTGREST_USER} password=${POSTGREST_PASSWORD.slice(0, 2)}${POSTGREST_PASSWORD.slice(-2)}`
const dsn = `gtfs=host=${PGHOST} port=${PGPORT} dbname=${dbName} user=${POSTGREST_USER} password=${POSTGREST_PASSWORD}`
const logDsn = `gtfs=host=${PGHOST} port=${PGPORT} dbname=${dbName} user=${POSTGREST_USER} password=${POSTGREST_PASSWORD.slice(0, 2)}${POSTGREST_PASSWORD.slice(-2)}`
logger.debug(`writing "${logDsn}" into env file ${pathToDsnFile}`)
await writeFile(pathToDsnFile, dsn)
}
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ deepStrictEqual(
const connectToMetaDatabase = async (cfg) => {
const {
pgHost,
pgPort,
pgUser,
pgPassword,
pgMetaDatabase,
pgOpts,
} = {
pgHost: null,
pgPort: null,
pgUser: null,
pgPassword: null,
pgMetaDatabase: null,
Expand All @@ -90,6 +92,11 @@ const connectToMetaDatabase = async (cfg) => {
} else if (process.env.PGHOST) {
pgConfig.host = process.env.PGHOST
}
if (pgPort !== null) {
pgConfig.port = pgPort
} else if (process.env.PGPORT) {
pgConfig.port = process.env.PGPORT
}
if (pgUser !== null) {
pgConfig.user = pgUser
} else if (process.env.PGUSER) {
Expand Down

0 comments on commit 96c8b49

Please sign in to comment.