Skip to content

Commit 776c17d

Browse files
committed
Use pg-connection-string for ?sslrootcert support
1 parent 48cff6c commit 776c17d

File tree

6 files changed

+52
-12
lines changed

6 files changed

+52
-12
lines changed

@app/server/__tests__/helpers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Request, Response } from "express";
22
import { ExecutionResult, graphql, GraphQLSchema } from "graphql";
33
import { Pool, PoolClient } from "pg";
4+
import { parse } from "pg-connection-string";
45
import {
56
createPostGraphileSchema,
67
PostGraphileOptions,
@@ -105,9 +106,7 @@ interface ICtx {
105106
let ctx: ICtx | null = null;
106107

107108
export const setup = async () => {
108-
const rootPgPool = new Pool({
109-
connectionString: process.env.TEST_DATABASE_URL,
110-
});
109+
const rootPgPool = new Pool(parse(process.env.TEST_DATABASE_URL) as any);
111110

112111
const options = getPostGraphileOptions({ rootPgPool });
113112
const schema = await createPostGraphileSchema(

@app/server/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"passport": "^0.4.1",
4040
"passport-github2": "^0.1.11",
4141
"pg": "^8.0.3",
42+
"pg-connection-string": "^2.2.0",
4243
"postgraphile": "^4.7.0",
4344
"redis": "^3.0.2",
4445
"source-map-support": "^0.5.13",

@app/server/src/middleware/installDatabasePools.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Express } from "express";
22
import { Pool } from "pg";
3+
import { parse } from "pg-connection-string";
34

45
import { getShutdownActions } from "../app";
56

@@ -25,16 +26,12 @@ function swallowPoolError(_error: Error) {
2526

2627
export default (app: Express) => {
2728
// This pool runs as the database owner, so it can do anything.
28-
const rootPgPool = new Pool({
29-
connectionString: process.env.DATABASE_URL,
30-
});
29+
const rootPgPool = new Pool(parse(process.env.DATABASE_URL!) as any);
3130
rootPgPool.on("error", swallowPoolError);
3231
app.set("rootPgPool", rootPgPool);
3332

3433
// This pool runs as the unprivileged user, it's what PostGraphile uses.
35-
const authPgPool = new Pool({
36-
connectionString: process.env.AUTH_DATABASE_URL,
37-
});
34+
const authPgPool = new Pool(parse(process.env.AUTH_DATABASE_URL!) as any);
3835
authPgPool.on("error", swallowPoolError);
3936
app.set("authPgPool", authPgPool);
4037

@app/worker/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
"private": true,
44
"version": "0.0.0",
55
"scripts": {
6-
"gw": "cd dist && node -r @app/config/env ../node_modules/.bin/graphile-worker",
6+
"gw": "node -r @app/config/env dist/index.js",
77
"build": "tsc -b",
88
"start": "yarn gw",
9-
"dev": "cd dist && node -r @app/config/env --inspect=9757 ../node_modules/.bin/graphile-worker --watch",
9+
"dev": "node -r @app/config/env --inspect=9757 dist/index.js --watch",
1010
"install-db-schema": "mkdir -p dist && yarn gw --schema-only"
1111
},
1212
"dependencies": {
@@ -22,6 +22,8 @@
2222
"lodash": "^4.17.15",
2323
"mjml": "^4.6.2",
2424
"nodemailer": "^6.4.6",
25+
"pg": "^8.0.3",
26+
"pg-connection-string": "^2.2.0",
2527
"tslib": "^1.11.1"
2628
}
2729
}

@app/worker/src/index.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { getTasks, run, runMigrations } from "graphile-worker";
2+
import { Pool } from "pg";
3+
import { parse } from "pg-connection-string";
4+
5+
/*
6+
* Normally you'd use the CLI, but we need to use pg-connection-string >= 2.1.0,
7+
* and pg only uses 0.1.3 so we have to drop to library mode.
8+
*/
9+
10+
async function main() {
11+
const pgPool = new Pool(parse(process.env.DATABASE_URL!) as any);
12+
try {
13+
const options = { pgPool };
14+
const schemaOnly = process.argv.includes("--schema-only");
15+
if (schemaOnly) {
16+
return await runMigrations(options);
17+
}
18+
const watch = process.argv.includes("--watch");
19+
const { tasks, release } = await getTasks(
20+
options,
21+
`${__dirname}/tasks`,
22+
watch
23+
);
24+
try {
25+
const { promise } = await run({
26+
...options,
27+
taskList: tasks,
28+
});
29+
return await promise;
30+
} finally {
31+
release();
32+
}
33+
} finally {
34+
pgPool.end();
35+
}
36+
}
37+
38+
main().catch((err) => {
39+
console.error(err);
40+
process.exit(1);
41+
});

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12329,7 +12329,7 @@ [email protected]:
1232912329
resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-0.1.3.tgz#da1847b20940e42ee1492beaf65d49d91b245df7"
1233012330
integrity sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=
1233112331

12332-
pg-connection-string@^2.0.0, pg-connection-string@^2.1.0:
12332+
pg-connection-string@^2.0.0, pg-connection-string@^2.1.0, pg-connection-string@^2.2.0:
1233312333
version "2.2.0"
1233412334
resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.2.0.tgz#caab4d38a9de4fdc29c9317acceed752897de41c"
1233512335
integrity sha512-xB/+wxcpFipUZOQcSzcgkjcNOosGhEoPSjz06jC89lv1dj7mc9bZv6wLVy8M2fVjP0a/xN0N988YDq1L0FhK3A==

0 commit comments

Comments
 (0)