Skip to content

Migrate connection string to new connections model#80

Merged
kristof-siket merged 3 commits intomainfrom
ptl-1046-migrate-create-db-to-use-new-connection-model-fields
Mar 2, 2026
Merged

Migrate connection string to new connections model#80
kristof-siket merged 3 commits intomainfrom
ptl-1046-migrate-create-db-to-use-new-connection-model-fields

Conversation

@kristof-siket
Copy link
Contributor

@kristof-siket kristof-siket commented Mar 2, 2026

Summary

Migrates create-db from reading deprecated apiKeys[0].directConnection fields to the new connections[0].endpoints.direct.connectionString from the Management API response (PTL-1046).

  • Reads the connection string directly from connections[0].endpoints.direct.connectionString, falling back to pooled.connectionString
  • Preserves backward compatibility by falling back to the legacy apiKeys path when connections is absent (for older API versions)
  • Removes manual postgresql://... DSN construction from the main flow
  • Adds ConnectionEndpoint and Connection type interfaces reflecting the new response shape

Context

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

  • Run npm test (unit tests pass locally)
  • Manually test against production Management API — verify connection string is returned correctly
  • Manually test against preview Management API — verify new connections[] path is used
  • Verify backward compat: when API returns only apiKeys (no connections), the legacy fallback still produces a valid connection string

Closes PTL-1046

Summary by CodeRabbit

  • New Features

    • Support for multiple connection endpoint types (direct, pooled, accelerate) for more flexible connectivity.
  • Improvements

    • Connection string resolution now prefers endpoint-provided connection strings instead of constructing them from individual credentials.
    • Internal flow refined for more reliable detection of available endpoints; no public API changes.

…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
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 2, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

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

@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Preview CLIs & Workers are live!

Test the CLIs locally under tag pr80-ptl-1046-migrate-create-db-to-use-new-connection-model-fields-22573148372:

npx create-db@pr80
npx create-pg@pr80
npx create-postgres@pr80

Worker URLs
• Create-DB Worker: https://create-db-temp.prisma.io
• Claim-DB Worker: https://create-db.prisma.io

These will live as long as this PR exists under tag pr80-ptl-1046-migrate-create-db-to-use-new-connection-model-fields-22573148372.

@coderabbitai
Copy link

coderabbitai bot commented Mar 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0ba6ca2 and a62d08a.

📒 Files selected for processing (1)
  • create-db/src/core/database.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • create-db/src/core/database.ts

Walkthrough

Adds typed connection and endpoint interfaces and updates database creation logic to derive connectionString from a connections entry's endpoints.direct.connectionString or endpoints.pooled.connectionString, falling back to null; removes prior inline direct-connection construction. Public APIs unchanged.

Changes

Cohort / File(s) Summary
Type Definitions
create-db/src/types.ts
Added ConnectionEndpoint and Connection interfaces and extended DatabaseRecord with optional connections?: Connection[].
Database Core Logic
create-db/src/core/database.ts
Changed connection resolution to search database?.connections for a connection with endpoints.direct.connectionString or endpoints.pooled.connectionString; connectionString now taken from those fields (or null). Removed previous inline construction of a direct PostgreSQL connection string; minor import order swap.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 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 (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Migrate connection string to new connections model' accurately describes the primary change: refactoring to use the new connections API model instead of deprecated fields.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

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.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9aeacd4 and fa73f59.

📒 Files selected for processing (2)
  • create-db/src/core/database.ts
  • create-db/src/types.ts

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.
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Preview CLIs & Workers are live!

Test the CLIs locally under tag pr80-ptl-1046-migrate-create-db-to-use-new-connection-model-fields-22573759849:

npx create-db@pr80
npx create-pg@pr80
npx create-postgres@pr80

Worker URLs
• Create-DB Worker: https://create-db-temp.prisma.io
• Claim-DB Worker: https://create-db.prisma.io

These will live as long as this PR exists under tag pr80-ptl-1046-migrate-create-db-to-use-new-connection-model-fields-22573759849.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
create-db/src/core/database.ts (1)

10-12: ⚠️ Potential issue | 🟠 Major

Legacy fallback still drops valid credentials in mixed legacy shapes.

Line 10–12 still chooses only one legacy field based on envelope (directConnection or ppgDirectConnection). If the other field is populated, connectionString resolves to null incorrectly.

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.

ℹ️ Review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fa73f59 and 0ba6ca2.

📒 Files selected for processing (1)
  • create-db/src/core/database.ts

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 2, 2026
Use connectionString directly from the connections endpoint array.
Drop buildLegacyConnectionString helper — the API always returns
the connections array with connectionString populated.
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Preview CLIs & Workers are live!

Test the CLIs locally under tag pr80-ptl-1046-migrate-create-db-to-use-new-connection-model-fields-22579279596:

npx create-db@pr80
npx create-pg@pr80
npx create-postgres@pr80

Worker URLs
• Create-DB Worker: https://create-db-temp.prisma.io
• Claim-DB Worker: https://create-db.prisma.io

These will live as long as this PR exists under tag pr80-ptl-1046-migrate-create-db-to-use-new-connection-model-fields-22579279596.

@kristof-siket kristof-siket merged commit 6660ab8 into main Mar 2, 2026
6 checks passed
kristof-siket added a commit to prisma/prisma that referenced this pull request Mar 3, 2026
## 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 -->
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.

2 participants