Skip to content

Enhance /api/healthz with DB connectivity check#1072

Merged
catinhere merged 1 commit intomasterfrom
feat/health-endpoint-db-check
Apr 14, 2026
Merged

Enhance /api/healthz with DB connectivity check#1072
catinhere merged 1 commit intomasterfrom
feat/health-endpoint-db-check

Conversation

@xcompass
Copy link
Copy Markdown
Member

@xcompass xcompass commented Apr 10, 2026

Summary

  • Replace static {'status': 'OK'} response with a real health check that verifies database connectivity
  • Return 503 Service Unavailable (with error detail) when any check fails, so monitoring services can detect outages
  • Add tests covering the healthy path, DB-down path, and unauthenticated access

Changes

compair/api/healthz.py

  • Execute SELECT 1 against the database to confirm connectivity
  • Response body now includes a checks map ({"db": "ok"|"error"}) for granular visibility
  • HTTP status is 200 when all checks pass, 503 otherwise

compair/tests/api/test_healthz.py (new)

  • test_healthz_returns_200_when_db_is_up — happy path
  • test_healthz_returns_503_when_db_is_down — DB failure mocked via mock.patch
  • test_healthz_requires_no_authentication — confirms endpoint is publicly reachable

Test plan

  • Run docker exec -it compair_app_1 python -m pytest compair/tests/api/test_healthz.py -v
  • Hit GET /api/healthz on a running instance and confirm 200 {"status": "ok", "checks": {"db": "ok"}}
  • Confirm monitoring service receives 503 when the DB is unreachable

- Execute SELECT 1 against the database to verify connectivity
- Return 503 with error detail when any check fails
- Add test coverage for healthy, DB-down, and unauthenticated cases
@xcompass xcompass requested a review from catinhere April 10, 2026 23:44
catinhere added a commit that referenced this pull request Apr 13, 2026
Copy link
Copy Markdown
Contributor

@catinhere catinhere left a comment

Choose a reason for hiding this comment

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

LGTM and executed everything under the test plan

@catinhere catinhere merged commit 97b8ca4 into master Apr 14, 2026
8 checks passed
@catinhere catinhere deleted the feat/health-endpoint-db-check branch April 14, 2026 19:16
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