Skip to content

Commit 165a7bb

Browse files
feat(db): log which connection each dbFor sub-pool resolved to
One line per role at first use: the dedicated DATABASE_URL_<ROLE> when set, otherwise an explicit fallback message naming the process connection it shares — so a missing/typo'd env var is visible at rollout instead of silent. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NNy9Fzpfc1FdHAzYyb6Ycy
1 parent 950746e commit 165a7bb

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

packages/db/db.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { createLogger } from '@sim/logger'
12
import { drizzle } from 'drizzle-orm/postgres-js'
23
import postgres from 'postgres'
34
import { resolveDbUrl } from './connection-url'
45
import * as schema from './schema'
56
import { instrumentPoolClient } from './tx-tripwire'
67

8+
const logger = createLogger('Db')
9+
710
/**
811
* Per-role pool profiles. Starting numbers — validate against real per-role
912
* process counts (PgBouncer transaction mode, max_connections=200).
@@ -81,6 +84,11 @@ export const dbReplica: typeof db = replicaUrl
8184

8285
const subPoolClients = new Map<SubProcessDbRole, typeof db>()
8386

87+
/** Which env var the process connection came from — named in dbFor fallback logs. */
88+
const processUrlEnvVar = process.env[`DATABASE_URL_${role.toUpperCase()}`]
89+
? `DATABASE_URL_${role.toUpperCase()}`
90+
: 'DATABASE_URL'
91+
8492
/**
8593
* Per-workload drizzle client with its own pool, built lazily on first call and
8694
* cached per role. Unlike the process-wide `db` (selected by `SIM_DB_ROLE`),
@@ -98,11 +106,21 @@ export function dbFor(role: SubProcessDbRole): typeof db {
98106
const existing = subPoolClients.get(role)
99107
if (existing) return existing
100108

101-
const url = process.env[`DATABASE_URL_${role.toUpperCase()}`] ?? connectionString
109+
const keyedEnvVar = `DATABASE_URL_${role.toUpperCase()}`
110+
const keyedUrl = process.env[keyedEnvVar]
111+
const url = keyedUrl ?? connectionString
102112
if (!url) {
103113
throw new Error('Missing DATABASE_URL environment variable')
104114
}
105115

116+
if (keyedUrl) {
117+
logger.info(`'${role}' pool using dedicated ${keyedEnvVar}`)
118+
} else {
119+
logger.info(
120+
`${keyedEnvVar} not set — '${role}' pool falling back to the process connection (${processUrlEnvVar})`
121+
)
122+
}
123+
106124
const subProfile = DB_POOL_PROFILES[role]
107125
const client = drizzle(
108126
instrumentPoolClient(

0 commit comments

Comments
 (0)