Skip to content

Commit 5c73038

Browse files
fix(db): attempt parsing cert and db url separately (#1183)
1 parent ed11456 commit 5c73038

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

apps/sim/drizzle.config.ts

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
1+
import fs from 'fs'
2+
import os from 'os'
3+
import path from 'path'
14
import type { Config } from 'drizzle-kit'
25
import { env } from './lib/env'
36

7+
const connectionString = env.POSTGRES_URL ?? env.DATABASE_URL
8+
9+
let sslConfig: { rejectUnauthorized: boolean; ca: string } | undefined
10+
if (env.DATABASE_SSL_CERT) {
11+
const tmpDir = process.env.TMPDIR || os.tmpdir()
12+
const tmpPath = path.join(tmpDir, `sim-db-ca-${process.pid}.crt`)
13+
try {
14+
fs.writeFileSync(tmpPath, env.DATABASE_SSL_CERT, { encoding: 'utf-8', mode: 0o600 })
15+
sslConfig = { rejectUnauthorized: true, ca: tmpPath }
16+
17+
const cleanup = () => {
18+
try {
19+
fs.rmSync(tmpPath)
20+
} catch {}
21+
}
22+
23+
process.once('exit', cleanup)
24+
process.once('SIGINT', () => {
25+
cleanup()
26+
process.exit(0)
27+
})
28+
process.once('SIGTERM', () => {
29+
cleanup()
30+
process.exit(0)
31+
})
32+
} catch {
33+
// If writing fails, leave sslConfig undefined and allow connection to fail fast
34+
}
35+
}
36+
437
export default {
538
schema: './db/schema.ts',
639
out: './db/migrations',
740
dialect: 'postgresql',
841
dbCredentials: {
9-
url: env.DATABASE_URL,
10-
ssl: env.DATABASE_SSL_CERT
11-
? {
12-
rejectUnauthorized: false,
13-
ca: env.DATABASE_SSL_CERT,
14-
}
15-
: undefined,
42+
url: connectionString,
43+
ssl: sslConfig,
1644
},
1745
} satisfies Config

0 commit comments

Comments
 (0)