Skip to content

feat: Add DB index for health lookup hot path - #708

Open
Ugooweb wants to merge 1 commit into
Predictify-org:mainfrom
Ugooweb:feature/issue-b037-health-index
Open

feat: Add DB index for health lookup hot path #708
Ugooweb wants to merge 1 commit into
Predictify-org:mainfrom
Ugooweb:feature/issue-b037-health-index

Conversation

@Ugooweb

@Ugooweb Ugooweb commented Jul 28, 2026

Copy link
Copy Markdown

Closes #611

This PR replaces the mock in-memory state used by the health API endpoints with a proper database lookup. To ensure the new hot path is performant, it also adds an EXPLAIN-verified index on the specific filter columns.

Problem Statement

The /api/health endpoints were using a mock memory state (currentHealthState) which wouldn't scale or synchronize correctly across instances. While the /mutations endpoint persisted an audit log (action = "health.state_mutation"), the GET request didn't fetch the active state from the database. Furthermore, querying audit_logs by action and sorting by creation date lacked an index, which would eventually lead to slow sequential scans.

Solution

  1. Replaced Memory State: Updated src/routes/health.ts to fetch the latest state from the audit_logs table.
  2. Added DB Index: Created migrations/health_index.sql to add a specific index on (action, created_at DESC) in audit_logs to optimize the lookup query.
  3. Updated Tests: Rewrote the health route tests to mock the database layer and properly test the DB interactions.

Changes

1. migrations/health_index.sql

  • ✅ Added UP migration to create idx_audit_logs_action_created_at index on audit_logs.
  • ✅ Added DOWN migration for rollback capability.

2. src/routes/health.ts

  • ✅ Replaced currentHealthState with a database query on audit_logs.
  • ✅ Configured GET / to query the latest health.state_mutation and return the afterState.
  • ✅ Configured POST /mutations to retrieve the current state from the database before applying updates.

3. tests/health.test.ts

  • ✅ Added Drizzle ORM mocks for db.select() to simulate database responses.
  • ✅ Added test cases ensuring the GET /health endpoint correctly handles the database state.
  • ✅ Added test cases verifying the POST /health/mutations functionality.

Security Considerations

  • ✅ Avoided modifying any unrelated endpoints or adding unnecessary permissions.
  • ✅ Safe rollback included in the raw SQL migration logic.

Acceptance Criteria

  • Index Created: Added migration + rollback for (action, created_at).
  • Hot Path Updated: Implemented the DB lookup to replace the mock.
  • Tests Added: Rewrote and focused the tests for 90%+ coverage.
  • Linting Passed: Followed

@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@Ugooweb Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Add DB index for health lookup hot path [b#037]

1 participant