Skip to content

Fix production DB connectivity by using Prisma Accelerate correctly#23

Merged
maotora merged 3 commits intomainfrom
codex/prod-500-diagnostics
Mar 19, 2026
Merged

Fix production DB connectivity by using Prisma Accelerate correctly#23
maotora merged 3 commits intomainfrom
codex/prod-500-diagnostics

Conversation

@maotora
Copy link
Contributor

@maotora maotora commented Mar 19, 2026

Summary

Production was configured with a Prisma Accelerate DATABASE_URL, but the API runtime was treating that value like a direct PostgreSQL connection string via pg + @prisma/adapter-pg. That left the app booted and serving non-database routes while every Prisma-backed endpoint returned 500.

This PR fixes the runtime split so production uses Prisma Accelerate correctly, while development and test continue using direct PostgreSQL connections.

Root Cause

The deployed service had a DATABASE_URL with the prisma+postgres:// / Accelerate format.

The current runtime path in src/db/prisma.ts instantiated Prisma through PrismaPg, which expects a direct PostgreSQL connection string. As a result, database-backed handlers failed at runtime even though the process itself still responded successfully on /, /health, /openapi.json, and /api-docs.

What Changed

  • Add @prisma/extension-accelerate and use it in production.
  • Route Prisma client creation by environment:
    • production: new PrismaClient({ accelerateUrl: DATABASE_URL }).$extends(withAccelerate())
    • non-production: PrismaPg with a direct PostgreSQL connection.
  • Add DIRECT_DATABASE_URL support for migrations and any raw direct DB access.
  • Update prisma.config.ts so Prisma CLI prefers DIRECT_DATABASE_URL when present.
  • Make db:migrate fail fast with a clear error if an Accelerate-only environment is missing DIRECT_DATABASE_URL.
  • Keep the earlier hardening for production diagnostics:
    • proper Express error middleware signature
    • JSON 503 responses for DB/Accelerate connectivity failures
    • startup readiness check
    • /health now reports database readiness explicitly
  • Document the env split in .env.example and README.md.

Verification

  • pnpm build
  • Production-mode runtime probe using the current Accelerate-backed .env:
    • instantiate the new Prisma client path
    • run SELECT 1 via checkDatabaseConnection()
    • result: success

I did not run the full Jest suite because this workspace does not currently have a direct local PostgreSQL URL configured for the non-production path.

Deploy Notes

Production env:

  • NODE_ENV=production
  • DATABASE_URL=<Prisma Accelerate URL>
  • DIRECT_DATABASE_URL=<direct postgres URL> only if the deployment runs pnpm db:migrate

Development / test env:

  • use a direct PostgreSQL DATABASE_URL, or set DIRECT_DATABASE_URL

Expected Outcome

After deploy, the production service should stop timing out on database-backed routes and resume serving /v1/* and /api/* successfully through Prisma Accelerate.

@maotora maotora merged commit 88866c7 into main Mar 19, 2026
1 check passed
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