Fix/live query record drop on subset unmount #1656
main fromrefs/pull/1656/head·opened 🎯 Changes#
When a row was first inserted by subset query A, write({ type: 'insert' }) cleared the row's pending metadata. setPersistedOwners was called before write, so the ownership it set was immediately overwritten by the insert's metadata-delete.
When a later live query called getHydratedOwnedRowsForQueryBaseline, it read the stale persisted state and overwrote the in-memory rowToQueries map, dropping query A's hash from the owner set. When that query was GC'd, it found zero remaining owners and deleted the row — removing it from unrelated list views.
✅ Checklist#
- [ x ] I have tested this code locally with
pnpm test.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Notes#
Currently facing the below issue with react-db and I believe the below re-ordering of lines fixes it for myself but need to understand the impact elsewhere or if there is a better place to add this fix.
- Live query A contains a list of rows
- Live query B contains a subset of those rows with a different where clause
- When Live Query B unmounts then the rows are removed from the collection completely even with Live Query A still mounted and requires those rows.,
Any new live queries seem to take ownership of existing rows when mounting. Meaning that when the new live query unmounts it causes that record to be removed from the collection even if other useLiveQueries still should have that row existing.
I am unsure if this is a similar impact across the other vue/svelte db packages I am currently only using react-db so any advise if this change would negatively impact other areas would be helpful.
One thing noticed - my team have observed that the row will re-appear in the correct live query if we force a re-render during local development
Summary by CodeRabbit#
- Bug Fixes
- Corrected record syncing to ensure existing rows receive the appropriate update behavior rather than being skipped.
- Fixed ownership tracking to re-apply ownership after writes, improving consistency and preventing unintended row deletion during sibling live query garbage collection.
- Tests
- Added coverage confirming that unmounting a narrowly filtered
useLiveQuerydoes not remove records from another still-mounted, unfiltered listuseLiveQuery.
- Added coverage confirming that unmounting a narrowly filtered
- 1
Closing as superseded after RFC #1657 Phase 0 review. The immediate persisted-metadata clobber described here was addressed more fundamentally by #1626, and #1664 has since refactored the ownership paths touched by this branch. The added React test uses mockSyncCollectionOptions and does not exercise query-db ownership. Broader ownership, provider-lifetime, and destructive-diff semantics should be handled by RFC #1657 PR-5's coverage registry rather than landing this partial ownership expansion. A genuine query-db-level red/green regression test can still be harvested separately if it exposes remaining behavior. Thank you for the investigation and contribution.
KyleAMathews(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
8f2c6426-543f-446f-b2f6-d1e4a9d48fa8📥 Commits
Reviewing files that changed from the base of the PR and between fb9fa21fcfcb2a3cab87f2c8b0dc356d2971730a and 5707c505418d17816cf56012a082a7dcda1e9802.
📒 Files selected for processing (1)
.changeset/evil-maps-shine.md✅ Files skipped from review due to trivial changes (1)
📝 Walkthrough
Walkthrough#
The PR reorders row tracking and ownership persistence in
applySuccessfulResult, and adds a regression test showing that unmounting a narrowuseLiveQuerydoes not remove records from a still-mounted broad query.Changes#
Query Collection Ownership Fix
packages/query-db-collection/src/query.ts,.changeset/evil-maps-shine.mdapplySuccessfulResultnow adds the row before write selection, chooses insert versus update from current synced state and prior ownership, re-reads persisted owners after the write, and the changeset records the minor release and fix note.packages/react-db/tests/useLiveQuery.test.tsxEstimated code review effort: 2 (Simple) | ~10 minutes
Possibly related PRs
Suggested reviewers:
kevin-dp🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
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