Skip to content

feat(webapp,database): opt-in per-client Prisma driver adapters - #4539

Open
ericallam wants to merge 2 commits into
mainfrom
feat/prisma-adapter-pg-per-client-toggles
Open

feat(webapp,database): opt-in per-client Prisma driver adapters#4539
ericallam wants to merge 2 commits into
mainfrom
feat/prisma-adapter-pg-per-client-toggles

Conversation

@ericallam

Copy link
Copy Markdown
Member

What

Adds an opt-in path to run each Prisma client through @prisma/adapter-pg (the node-postgres driver) instead of the built-in engine driver, controlled by a per-client env var, all off by default:

env var client
CONTROL_PLANE_DATABASE_WRITER_DRIVER_ADAPTER control-plane writer
CONTROL_PLANE_DATABASE_REPLICA_DRIVER_ADAPTER control-plane replica
RUN_OPS_DATABASE_WRITER_DRIVER_ADAPTER new run-ops writer
RUN_OPS_DATABASE_REPLICA_DRIVER_ADAPTER new run-ops replica
RUN_OPS_LEGACY_DATABASE_WRITER_DRIVER_ADAPTER legacy run-ops writer
RUN_OPS_LEGACY_DATABASE_REPLICA_DRIVER_ADAPTER legacy run-ops replica

With every flag unset the construction path is byte-identical to today (datasources URL + Rust engine), so this is inert until a flag is turned on. Per-client granularity allows enabling the adapter only where it's wanted.

How

  • Enables the driverAdapters preview feature on both schemas (@trigger.dev/database and @internal/run-ops-database). This keeps the Rust query engine — it does NOT add queryCompiler — so query behavior, result types, and engine tracing spans are unchanged.
  • A shared buildDriverAdapterPool builds each client's pg.Pool with an explicit max, a bounded connectionTimeoutMillis (the node-postgres pool otherwise waits unbounded on acquire), and an onPoolError handler (an unhandled idle-connection error would otherwise crash the process). Threaded through all four client builders via a useDriverAdapter flag.
  • Adds @prisma/adapter-pg + @types/pg to the webapp; pg is already pinned at 8.15.6 (adapter-pg 6.x requires pg < 8.17).

Connect-failure handling (the important correctness/security bit)

Under the adapter an unreachable DB no longer surfaces as PrismaClientInitializationError / P1001; it becomes a P2010 "Database not reachable: " (or a raw ECONNREFUSED/ENOTFOUND-class error). Two handlers are updated so a client on the adapter behaves like today:

  • isInfrastructureError now recognizes those shapes (P2010 with a connectivity message, and raw connectivity errno codes). Without this, the DB hostname would leak into API-client-facing errors and the failure would go unlogged. Security-relevant.
  • isPrismaRetriableError treats the adapter's pool-acquire timeout ("timeout exceeded when trying to connect") as retriable, preserving the P2024 retry behavior the adapter otherwise drops.

Evidence

Validated on an isolated stack that mirrors the production DB topology (chained PgBouncers in front of writer + reader):

  • Behavioral parity: raw-query results and Prisma error codes/meta are byte-identical between the engine driver and the adapter across the queried shapes (unique-constraint meta.target, record-not-found, transaction-timeout, serialization-failure, etc.).
  • Feature matrix: a full 380-project queue-ay pass shows no adapter-caused regressions — pass/fail parity between adapter-off and adapter-on, with the residual failures being pre-existing known-failures/flakes common to both.

Rollout / rollback

All flags default off; enable per client via env var, roll back by unsetting and redeploying (no data migration). Recommended first target is a single writer; enable one client at a time.

Follow-ups (not in this PR)

  • $metrics-based pool observability is removed under the adapter (the Prometheus route + db.pool.connections.* instruments); the metrics replacement (via pg.Pool counters) lands in a separate PR.
  • Note for operators: on the adapter path, interactive-transaction maxWait does not bound pool acquisition — connectionTimeoutMillis does.

refs TRI-13039

Add per-client env vars to route each Prisma client through @prisma/adapter-pg
(node-postgres) instead of the built-in engine driver. All default off, so
behavior is unchanged unless a flag is set:

- CONTROL_PLANE_DATABASE_WRITER_DRIVER_ADAPTER
- CONTROL_PLANE_DATABASE_REPLICA_DRIVER_ADAPTER
- RUN_OPS_DATABASE_WRITER_DRIVER_ADAPTER
- RUN_OPS_DATABASE_REPLICA_DRIVER_ADAPTER
- RUN_OPS_LEGACY_DATABASE_WRITER_DRIVER_ADAPTER
- RUN_OPS_LEGACY_DATABASE_REPLICA_DRIVER_ADAPTER

Enables the driverAdapters preview feature on both schemas (keeps the Rust
query engine; does not add queryCompiler). Each adapter pool is built with a
bounded connectionTimeoutMillis and an onPoolError handler.

Handle the connect-failure differences the adapter introduces:
- isInfrastructureError now recognizes the adapter's connect-failure shapes
  (P2010 'not reachable' and raw ECONNREFUSED/ENOTFOUND-class errors) so the DB
  host is still scrubbed from API-client errors and infra failures are logged.
- isPrismaRetriableError treats the adapter pool-acquire timeout as retriable,
  preserving the P2024 retry behavior.

refs TRI-13039

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Aug 8, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: f07ac7c

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai

coderabbitai Bot commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The web application adds optional PostgreSQL driver-adapter support for control-plane, run-ops, and legacy run-ops Prisma writers and replicas. Environment flags independently enable each adapter. Adapter-backed clients use configured PostgreSQL pools. Default behavior continues to use datasource URLs. Prisma generators and package dependencies are updated. Connectivity detection now handles adapter timeout messages, network errors, and connectivity-related P2010 errors.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely describes the opt-in per-client Prisma driver-adapter change.
Description check ✅ Passed The description gives detailed scope, implementation, testing evidence, rollout, rollback, and follow-ups, but omits the template checklist and screenshots.
✨ Finishing Touches 💡 2
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/prisma-adapter-pg-per-client-toggles

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot]

This comment was marked as resolved.

- Pass the per-client resolved connection limit into the adapter pool instead
  of always using DATABASE_CONNECTION_LIMIT, so per-client overrides (e.g.
  RUN_OPS_DATABASE_READ_REPLICA_CONNECTION_LIMIT) are honored on the adapter path.
- Build the adapter pool from the base DSN (drops prisma-only URL params the pg
  driver ignores and the duplicate application_name).
- Scope the connectivity message match to 'database not reachable' so a generic
  'not reachable' error is no longer misclassified as infrastructure.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ericallam
ericallam marked this pull request as ready for review August 8, 2026 18:05

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 4 potential issues.

Open in Devin Review

Comment on lines +446 to +459
const pool = new Pool({
connectionString,
max: connectionLimit,
connectionTimeoutMillis: poolTimeoutSeconds * 1000,
application_name: env.SERVICE_NAME,
});
pool.on("error", (error) => {
logger.error("prisma driver adapter pool error", {
clientType,
error: error instanceof Error ? error.message : String(error),
ignoreError: true,
});
});
return new PrismaPg(pool);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Turning on the new database connection driver makes the app read and write the wrong database schema for installs that use a custom schema

The database schema named in the connection URL is dropped when the new connection driver is built (new Pool({ connectionString }) at apps/webapp/app/db.server.ts:446-451), so an installation configured with a non-default schema silently talks to the default one instead.
Impact: Operators who enable the new driver on a deployment whose connection string names a custom schema get "table does not exist" failures or, worse, reads/writes against an unrelated schema.

Mechanism: `?schema=` is a Prisma-only DSN parameter that node-postgres ignores, and the adapter's `schema` option is never set

With the Rust engine path the DSN is handed to Prisma (datasources: { db: { url } }), and Prisma uses the schema query parameter to set the connection's search_path. The repo relies on this: getDatabaseSchema() (apps/webapp/app/db.server.ts:973-987) reads ?schema= and defaults to public, and .env.example / docs/self-hosting/kubernetes.mdx:262 ship URLs with ?schema=....

On the adapter path the URL is passed straight to pg.Pool. node-postgres only forwards a fixed set of startup parameters (user, database, application_name, options, ...) — schema is not among them, so it is silently discarded. @prisma/adapter-pg@6.14.0 surfaces the schema to the query engine only via its second constructor argument (getConnectionInfo() returns { schemaName: this.options?.schema }), which this code never passes: return new PrismaPg(pool).

Result: with useDriverAdapter on and a DSN such as ...?schema=trigger, all model queries are emitted unqualified and resolve against the session default (public) rather than trigger. Note the raw-SQL helper sqlDatabaseSchema still uses the configured schema, so raw and generated queries would diverge.

Prompt for agents
buildDriverAdapterPool in apps/webapp/app/db.server.ts builds a pg.Pool from the raw DSN and wraps it with `new PrismaPg(pool)`. The DSN's `?schema=` query parameter is meaningful only to Prisma's Rust engine; node-postgres discards it, and @prisma/adapter-pg only learns the schema from its second constructor argument (`new PrismaPg(pool, { schema })`, surfaced through getConnectionInfo().schemaName). Consequently any deployment whose connection string names a non-public schema will run all Prisma model queries against the default search_path once a driver-adapter flag is turned on. Fix by parsing the `schema` search param out of the connection URL for each client (the same way getDatabaseSchema() does for DATABASE_URL, but per-URL since run-ops/legacy DSNs can differ) and passing it as the adapter option; consider also setting the pool's `options: '-c search_path=...'` so raw queries behave identically. Also worth deciding whether an absent schema param should default to 'public' explicitly.
Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

ignoreError: true,
});
});
return new PrismaPg(pool);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 External pg pool is never closed when the Prisma client disconnects

new PrismaPg(pool) is constructed without { disposeExternalPool: true }. In @prisma/adapter-pg@6.14.0 the factory only calls pool.end() when it created the pool itself; for an externally supplied pool it merely removes its error listener on release. So client.$disconnect() (or engine reconnects) will leave the node-postgres pool and its sockets open. Only matters for shutdown/reconnect paths and only when a driver-adapter flag is on, but it is a real difference from the Rust-engine path, which releases its connections on disconnect.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +543 to +552
const client = useDriverAdapter
? new PrismaClient({
adapter: buildDriverAdapterPool(
url,
clientType,
poolTimeout ?? env.DATABASE_POOL_TIMEOUT,
env.DATABASE_CONNECTION_LIMIT
),
log: logConfig,
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Prisma $metrics consumers are not guarded for the adapter path

The PR notes that $metrics-based pool observability goes away under the adapter, but the two existing consumers are unguarded: apps/webapp/app/routes/metrics.ts:17 awaits prisma.$metrics.prometheus() with no try/catch (a throw makes the whole /metrics scrape 500, taking core metrics with it), and the OTEL batch observable callback in apps/webapp/app/v3/tracer.server.ts:552-554 awaits prisma.$metrics.json() inside an async callback whose rejection isn't handled. Worth confirming whether $metrics throws or simply returns pool metrics as zero when the client is constructed with a driver adapter; if it throws, these two call sites should be defensively wrapped in this PR rather than the follow-up.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +43 to +48
if (isPrismaKnownError(error)) {
return retryCodes.includes(error.code);
}

return retryCodes.includes(error.code);
const message = (error as { message?: unknown })?.message;
return typeof message === "string" && ADAPTER_ACQUIRE_TIMEOUT.test(message);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Retriable-error fallback only fires when the acquire timeout is not a coded Prisma error

The new message check is placed after an early return for isPrismaKnownError, which duck-types on any string .code. If Prisma wraps the adapter's "timeout exceeded when trying to connect" into a coded error (e.g. P2010 "Raw query failed", which the companion change in prismaErrors.ts explicitly anticipates for adapter connect failures), the first branch returns false and the pool-acquire retry behaviour the PR is trying to preserve never engages. Checking the message before/independently of the coded branch would make the intent hold in both shapes.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant