Skip to content

Fix stale food entry delete cache - #1103

Merged
Asherlc merged 2 commits into
mainfrom
thankful-circus
May 8, 2026
Merged

Asherlc merged 2 commits into
mainfrom
thankful-circus

Conversation

@Asherlc

@Asherlc Asherlc commented May 8, 2026

Copy link
Copy Markdown
Owner

Summary

  • Invalidate food and nutrition query caches after food create/update/delete/quickAdd mutations.
  • Add regression coverage for deleting a previously loaded food entry so stale cached byDate data cannot mask deletion.

Tests

  • pnpm lint
  • pnpm tsc --noEmit
  • cd packages/server && pnpm tsc --noEmit
  • cd packages/web && pnpm tsc --noEmit
  • pnpm exec vitest run packages/server/src/routers/food.test.ts packages/server/src/routers/food.integration.test.ts
  • pnpm exec vitest run src/providers/whoop-sync.integration.test.ts
  • pnpm exec vitest run src/providers/apple-health/import.integration.test.ts

Note: pnpm test ran 10,612 passing tests but exited nonzero locally because two integration suites hit the known Testcontainers startup error No host port found for host IP under high load. Both affected suites passed when rerun in isolation.

Summary by CodeRabbit

  • Tests

    • Added integration and unit tests ensuring food queries reflect deletions and that cache-invalidation failures are handled safely.
  • Bug Fixes

    • Food lists now refresh correctly after create, update, delete, and quick-add actions; delete operations remain successful even if background cache invalidation fails (errors are logged/reported).

Copilot AI review requested due to automatic review settings May 8, 2026 05:39
@coderabbitai

coderabbitai Bot commented May 8, 2026

Copy link
Copy Markdown

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 355ee53b-6cb5-4331-bcf9-063e9050ea2d

📥 Commits

Reviewing files that changed from the base of the PR and between a7752e5 and 3f0c9d3.

📒 Files selected for processing (2)
  • packages/server/src/routers/food.test.ts
  • packages/server/src/routers/food.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/server/src/routers/food.ts

📝 Walkthrough

Walkthrough

The PR implements cache invalidation for the food router by introducing a helper function that clears cached food and nutrition data whenever mutations modify entries. Four mutations (create, update, delete, quickAdd) now trigger cache invalidation after persisting changes. An integration test verifies that deleted entries no longer appear in subsequent byDate queries.

Changes

Food Router Cache Invalidation

Layer / File(s) Summary
Cache Dependency Import
packages/server/src/routers/food.ts
queryCache is imported to enable cache invalidation by key prefix.
Cache Invalidation Helper
packages/server/src/routers/food.ts
invalidateFoodCaches(userId) helper function invalidates both food and nutrition cache keys for the given user.
Mutation Cache Integration
packages/server/src/routers/food.ts
create, update, delete, and quickAdd mutations call invalidateFoodCaches(ctx.userId) after completing their repository operations.
Unit Tests: Mocks & Error Handling
packages/server/src/routers/food.test.ts
Adds Vitest mocks for Sentry and queryCache.invalidateByPrefix, a beforeEach reset, and a test that simulates cache invalidation failure asserting delete still succeeds and errors are reported.
Cache Invalidation Verification
packages/server/src/routers/food.integration.test.ts
Integration test confirms that food.byDate reflects deletion by re-querying after food.delete and verifying the deleted entry is absent.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 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 (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and specifically describes the main change: adding cache invalidation for food entry deletions to fix stale data issues.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ 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 thankful-circus

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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

@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

Storybook previews for 640a2947 are ready:

This comment updates automatically on each PR push.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/server/src/routers/food.ts`:
- Around line 47-50: invalidateFoodCaches currently lets cache-invalidation
failures propagate and can turn successful DB commits into client-visible
errors; wrap the invalidation calls (queryCache.invalidateByPrefix in
invalidateFoodCaches) in a try/catch that calls captureException(err) and logs
the error (e.g., processLogger.error) but does NOT rethrow, ensuring the
mutation callers (where invalidateFoodCaches is invoked after DB commits) always
return success when the DB write succeeded; alternatively, move the try/catch
into each caller after the DB commit and call captureException on any failure
while allowing the original success response to be returned.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 997a3ddb-0b96-467b-9b0d-c5e4961cb327

📥 Commits

Reviewing files that changed from the base of the PR and between dd5e7b6 and a7752e5.

📒 Files selected for processing (2)
  • packages/server/src/routers/food.integration.test.ts
  • packages/server/src/routers/food.ts

Comment thread packages/server/src/routers/food.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses stale food/nutrition query results by invalidating relevant tRPC query caches after food mutations, and adds an integration test to prevent regressions where cached food.byDate data can mask a deletion.

Changes:

  • Add invalidateFoodCaches() and call it after food.create, food.update, food.delete, and food.quickAdd mutations.
  • Add an integration regression test ensuring food.byDate returns fresh data after deleting an entry that was previously loaded (and thus potentially cached).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/server/src/routers/food.ts Introduces a helper to invalidate food/nutrition cache prefixes and calls it after food write mutations.
packages/server/src/routers/food.integration.test.ts Adds a regression test covering deletion after an initial cached byDate read.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/server/src/routers/food.ts Outdated
@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown
Contributor

Review app is ready:

This environment runs on a dedicated Hetzner server for PR #1103 and updates on each push.

@Asherlc
Asherlc enabled auto-merge (squash) May 8, 2026 15:19
@Asherlc
Asherlc merged commit aca7d6b into main May 8, 2026
62 checks passed
@Asherlc
Asherlc deleted the thankful-circus branch May 8, 2026 15:26
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