fix(db): includes orderBy ignored after optimistic update on child collection#1496
fix(db): includes orderBy ignored after optimistic update on child collection#1496comet08 wants to merge 1 commit into
Conversation
…llection to avoid stale sort order Co-Authored-By: Claude <noreply@anthropic.com>
📝 WalkthroughWalkthroughThis patch fixes a bug where ChangesOrderBy Preservation in Included Collections
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. 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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/db/tests/query/includes.test.ts (1)
5372-5451: ⚡ Quick winAdd a nested-includes regression for the second modified path.
This suite covers the direct child include flow, but it never exercises the nested buffer path changed in
setupNestedPipelines(). Astatus -> tasks -> subtasksoptimistic reorder case would close the gap and protect the second branch touched in this PR. As per coding guidelines,Always add unit tests that reproduce a bug before fixing it to ensure the bug is fixed and prevent regressionandTest corner cases including: ... async race conditions.🤖 Prompt for 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. In `@packages/db/tests/query/includes.test.ts` around lines 5372 - 5451, The test suite exercises optimistic reorders for a single child include but doesn't hit the nested buffer path in setupNestedPipelines(); add a new spec that mirrors the existing `orderBy in includes after child collection update` test but with a nested include (status -> tasks -> subtasks) to exercise the second branch: create mockSyncCollectionOptions and createCollection for statuses, tasks, and subtasks, build a live query via createLiveQueryCollection that selects statuses with tasks which in turn include subtasks ordered by position, preload the live query, perform an optimistic swap of subtask positions using subtasks.update(...), assert the in-memory order changes immediately, then use the tasks/subtasksOptions.utils.begin/write/commit flow to simulate server confirmation and finally assert the order remains correct; ensure test names and helper functions mirror existing patterns (e.g., getSubtaskOrder analogous to getTaskOrder) so setupNestedPipelines()’s nested pipeline path is exercised.Source: Coding guidelines
🤖 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.
Nitpick comments:
In `@packages/db/tests/query/includes.test.ts`:
- Around line 5372-5451: The test suite exercises optimistic reorders for a
single child include but doesn't hit the nested buffer path in
setupNestedPipelines(); add a new spec that mirrors the existing `orderBy in
includes after child collection update` test but with a nested include (status
-> tasks -> subtasks) to exercise the second branch: create
mockSyncCollectionOptions and createCollection for statuses, tasks, and
subtasks, build a live query via createLiveQueryCollection that selects statuses
with tasks which in turn include subtasks ordered by position, preload the live
query, perform an optimistic swap of subtask positions using
subtasks.update(...), assert the in-memory order changes immediately, then use
the tasks/subtasksOptions.utils.begin/write/commit flow to simulate server
confirmation and finally assert the order remains correct; ensure test names and
helper functions mirror existing patterns (e.g., getSubtaskOrder analogous to
getTaskOrder) so setupNestedPipelines()’s nested pipeline path is exercised.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 26dee569-e9b0-41fa-9ee8-e20d3607d68d
📒 Files selected for processing (3)
.changeset/twelve-jars-lead.mdpackages/db/src/query/live/collection-config-builder.tspackages/db/tests/query/includes.test.ts
Closes #1444
🎯 Changes
When a child collection item is updated, the D2 graph emits a
(-1, +1)pair. If the delete (-1) message arrives first, an accumulator entry is created with the oldorderByIndex. The subsequent insert (+1) message updatedvaluecorrectly but never updatedorderByIndex— so the child collection's comparator kept using the stale positional index.This caused incorrect sort order immediately after optimistic updates on child collections, with ordering only correcting itself after server confirmation.
The parent pipeline (
accumulateChanges) already handled this correctly. This fix applies the sameorderByIndexupdate logic to the child pipeline accumulator and the nested pipeline accumulator for consistency.✅ Checklist
pnpm test.🚀 Release Impact
Summary by CodeRabbit
Bug Fixes
Tests