Migrate connection string to new connections model#80
Conversation
…model Read `connections[0].endpoints.direct.connectionString` from the Management API response instead of manually constructing a DSN from `apiKeys[0].directConnection` fields. Falls back to the legacy path when `connections` is absent for backward compatibility with older API versions. Closes PTL-1046
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
claim-db-worker | a62d08a | Commit Preview URL Branch Preview URL |
Mar 02 2026, 02:02 PM |
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr80
npx create-pg@pr80
npx create-postgres@pr80Worker URLs
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughAdds typed connection and endpoint interfaces and updates database creation logic to derive connectionString from a Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@create-db/src/core/database.ts`:
- Around line 10-12: The current assignment to directConnDetails branches on
result.data and only reads one of directConnection or ppgDirectConnection, which
can miss credentials present on the other field; update the logic that computes
directConnDetails (the expression that uses result.data, apiKeys,
directConnection and ppgDirectConnection) to check both locations regardless of
envelope and prefer a non-null value (e.g., try apiKeys?.[0]?.directConnection
and if null/undefined fall back to apiKeys?.[0]?.ppgDirectConnection, and do the
same for the result.databases path) so legacy credentials are not missed.
- Around line 142-146: The code assumes database?.connections?.[0] is a Postgres
connection; instead find a valid Postgres connection before using endpoints:
iterate database.connections (or use .find) to locate a connection where
connection.type === 'postgres' or that has endpoints?.direct?.connectionString
|| endpoints?.pooled?.connectionString, assign it to a selectedConnection
variable, then compute connectionString from
selectedConnection.endpoints.direct?.connectionString ??
selectedConnection.endpoints.pooled?.connectionString ??
buildLegacyConnectionString(result, database); update references from connection
to selectedConnection so non-Postgres/accelerate connections are skipped.
The API doesn't guarantee ordering of connections[], so the first entry might be an accelerate connection without direct/pooled endpoints. Use .find() to locate a connection that has a connectionString available.
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr80
npx create-pg@pr80
npx create-postgres@pr80Worker URLs
|
There was a problem hiding this comment.
♻️ Duplicate comments (1)
create-db/src/core/database.ts (1)
10-12:⚠️ Potential issue | 🟠 MajorLegacy fallback still drops valid credentials in mixed legacy shapes.
Line 10–12 still chooses only one legacy field based on envelope (
directConnectionorppgDirectConnection). If the other field is populated,connectionStringresolves tonullincorrectly.Suggested fix
- const apiKeys = database?.apiKeys; - const directConnDetails = result.data - ? apiKeys?.[0]?.directConnection - : result.databases?.[0]?.apiKeys?.[0]?.ppgDirectConnection; + const apiKey = + database?.apiKeys?.[0] ?? result.databases?.[0]?.apiKeys?.[0]; + const directConnDetails = + apiKey?.directConnection ?? apiKey?.ppgDirectConnection;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@create-db/src/core/database.ts` around lines 10 - 12, The assignment to directConnDetails currently picks only one legacy envelope field (directConnection vs ppgDirectConnection) causing valid credentials in the other field to be dropped; update the logic that computes directConnDetails (and any downstream connectionString usage) to check both legacy locations and use the first non-null value (prefer apiKeys?.[0]?.directConnection but fall back to result.databases?.[0]?.apiKeys?.[0]?.ppgDirectConnection), or merge the objects if both exist so no valid credentials are lost; adjust references to directConnDetails so connectionString resolves from that combined/non-null object.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@create-db/src/core/database.ts`:
- Around line 10-12: The assignment to directConnDetails currently picks only
one legacy envelope field (directConnection vs ppgDirectConnection) causing
valid credentials in the other field to be dropped; update the logic that
computes directConnDetails (and any downstream connectionString usage) to check
both legacy locations and use the first non-null value (prefer
apiKeys?.[0]?.directConnection but fall back to
result.databases?.[0]?.apiKeys?.[0]?.ppgDirectConnection), or merge the objects
if both exist so no valid credentials are lost; adjust references to
directConnDetails so connectionString resolves from that combined/non-null
object.
Use connectionString directly from the connections endpoint array. Drop buildLegacyConnectionString helper — the API always returns the connections array with connectionString populated.
|
✅ Preview CLIs & Workers are live! Test the CLIs locally under tag npx create-db@pr80
npx create-pg@pr80
npx create-postgres@pr80Worker URLs
|
## Summary Migrates `prisma init --db` from reading deprecated `database.directConnection` fields to the new `connections[].endpoints.direct.connectionString` from the Management API response ([TML-1927](https://linear.app/prisma-company/issue/TML-1927)). - Reads the connection string directly from `connections[].endpoints.direct.connectionString` — no manual DSN construction needed - Throws if no connection string is found (no legacy fallback — the API always returns the new fields) - Bumps `@prisma/management-api-sdk` from `0.2.0` to `1.12.0` to get the new connection model types - Updates SDK import names to match v1.12.0 exports (`ManagementAPI` -> `ManagementApiSdk`, `createManagementAPI` -> `createManagementApiSdk`) - Bumps `hono` from `4.11.7` to `4.12.3` to fix IP spoofing authentication bypass vulnerability ## Context The Management API shipped a new connection model in [PR #3365](prisma/pdp-control-plane#3365). The deprecated fields (`directConnection`, `apiKeys`) will be removed after a deprecation window. This migration ensures `prisma init --db` works with the new response shape. Related: [PTL-1046](https://linear.app/prisma-company/issue/PTL-1046) — same migration for `prisma/create-db` ([PR #80](prisma/create-db#80)) ## Test plan - [x] `prisma init --db` creates a project and returns a valid connection string (manual test against production API) - [ ] Existing CI tests pass Closes TML-1927 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved database connection detection to prefer available endpoint types and surface a clearer error when no connection string is found. * **Chores** * Updated management API integration to the newer SDK surface. * Bumped devDependencies: @prisma/management-api-sdk to v1.12.0 and hono to v4.12.3. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Summary
Migrates
create-dbfrom reading deprecatedapiKeys[0].directConnectionfields to the newconnections[0].endpoints.direct.connectionStringfrom the Management API response (PTL-1046).connections[0].endpoints.direct.connectionString, falling back topooled.connectionStringapiKeyspath whenconnectionsis absent (for older API versions)postgresql://...DSN construction from the main flowConnectionEndpointandConnectiontype interfaces reflecting the new response shapeContext
PR #3365 added the new connection model to the Management API. The deprecated fields (
apiKeys[0].directConnection.*) will be removed in a future release — this migration must land before that happens.Test plan
npm test(unit tests pass locally)connections[]path is usedapiKeys(noconnections), the legacy fallback still produces a valid connection stringCloses PTL-1046
Summary by CodeRabbit
New Features
Improvements