Skip to content

Gate startup on a reachable database; add a Postgres healthcheck - #10

Merged
Mwvndva merged 1 commit into
mainfrom
version-1
Jul 11, 2026
Merged

Gate startup on a reachable database; add a Postgres healthcheck#10
Mwvndva merged 1 commit into
mainfrom
version-1

Conversation

@Mwvndva

@Mwvndva Mwvndva commented Jul 11, 2026

Copy link
Copy Markdown
Owner

The pg driver adapter's $connect() is lazy: it never proves the database is reachable, so the API reported "successfully started" and then 500'd every DB-backed request (login, the username check the create-account screen fires on open, the phone backfill) whenever Postgres was not up.

PrismaService now forces a SELECT 1 after connecting and waits for it, retrying while the database is still coming up (30 attempts, 2s apart). If it never answers within the budget it throws, so the process exits and pm2 restarts it instead of serving errors against a dead database. This makes cold boots deterministic once the DB exists rather than racing it.

docker-compose gains a pg_isready healthcheck so docker compose up -d --wait can block until Postgres truly accepts connections, letting a deploy start the API only after the DB is ready.

Summary by CodeRabbit

  • Bug Fixes
    • Improved application startup reliability by waiting for the database to become available before marking the service as ready.
    • Added automatic database connection retries with clear startup warnings and failure handling.
    • Local development environments now wait for PostgreSQL readiness when starting services.

The pg driver adapter's $connect() is lazy: it never proves the database is
reachable, so the API reported "successfully started" and then 500'd every
DB-backed request (login, the username check the create-account screen fires
on open, the phone backfill) whenever Postgres was not up.

PrismaService now forces a `SELECT 1` after connecting and waits for it,
retrying while the database is still coming up (30 attempts, 2s apart). If it
never answers within the budget it throws, so the process exits and pm2
restarts it instead of serving errors against a dead database. This makes cold
boots deterministic once the DB exists rather than racing it.

docker-compose gains a pg_isready healthcheck so `docker compose up -d --wait`
can block until Postgres truly accepts connections, letting a deploy start the
API only after the DB is ready.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 23e1206d-0453-47b2-a75f-fb1d801ffcfa

📥 Commits

Reviewing files that changed from the base of the PR and between 883b938 and 0db1d1a.

📒 Files selected for processing (2)
  • backend/docker-compose.yml
  • backend/src/prisma/prisma.service.ts

📝 Walkthrough

Walkthrough

The local Postgres container now reports readiness through a Docker healthcheck. PrismaService also verifies database availability during module startup, retrying SELECT 1 and logging outcomes before either continuing or throwing.

Changes

Database readiness

Layer / File(s) Summary
Postgres container healthcheck
backend/docker-compose.yml
The postgres service runs pg_isready for memory_db with configured intervals, timeouts, retries, and startup delay.
Prisma startup gate
backend/src/prisma/prisma.service.ts
PrismaService initializes a Nest logger and waits for a successful SELECT 1, retrying up to 30 times with 2-second delays and throwing after the final failure.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant DockerCompose
  participant Postgres
  participant PrismaService
  participant Logger
  DockerCompose->>Postgres: Run pg_isready healthcheck
  Postgres-->>DockerCompose: Report readiness
  PrismaService->>Postgres: Execute SELECT 1 during onModuleInit
  Postgres-->>PrismaService: Return success or failure
  PrismaService->>Logger: Log success or retry warning
  PrismaService->>Postgres: Retry after 2 seconds when unavailable
Loading
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch version-1

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.

@Mwvndva
Mwvndva merged commit 40f7585 into main Jul 11, 2026
1 of 4 checks 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.

2 participants