fix(sqlite-persistence): preserve query owner metadata on insert (closes #1618)#1626
fix(sqlite-persistence): preserve query owner metadata on insert (closes #1618)#1626kevin-dp wants to merge 4 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
ChangesMetadata reset bug fix and tests
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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. Comment |
More templates
@tanstack/angular-db
@tanstack/browser-db-sqlite-persistence
@tanstack/capacitor-db-sqlite-persistence
@tanstack/cloudflare-durable-objects-db-sqlite-persistence
@tanstack/db
@tanstack/db-ivm
@tanstack/db-sqlite-persistence-core
@tanstack/electric-db-collection
@tanstack/electron-db-sqlite-persistence
@tanstack/expo-db-sqlite-persistence
@tanstack/node-db-sqlite-persistence
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/react-native-db-sqlite-persistence
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/tauri-db-sqlite-persistence
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
A row inserted while the collection is mounted should be persisted together with its query owner metadata, so that after a reload the row is removed from both the live collection and the persisted store once the query no longer returns it. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
4a646e8 to
64e111a
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/query-db-collection/tests/query.test.ts (1)
4981-5000: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winExtract a typed helper for the two session setups instead of using
as any.These two collection-construction blocks are nearly identical, and the casts hide the exact type contract this regression is supposed to lock down. A small helper that takes
queryClient,adapter, and per-session overrides would remove the duplication and keep the test on the typed public surface. As per coding guidelines,**/*.{ts,tsx,js}: Extract common logic into utility functions when identical or near-identical code blocks appear in multiple places, and**/*.{ts,tsx}: Avoid usinganytypes; useunknowninstead when the type is truly unknown, and provide proper type annotations for return values.Also applies to: 5024-5036
🤖 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/query-db-collection/tests/query.test.ts` around lines 4981 - 5000, The two collection setup blocks for the reload-insert cleanup test are duplicating the same construction logic and using casts that obscure the public type contract. Extract a typed helper around createCollection/queryCollectionOptions that accepts the shared inputs like queryClient, adapter, and per-session overrides, then reuse it for both session setups. Remove the as any casts by adding proper type annotations to the helper return value and the options object so the test stays on the typed surface and still exercises the same behavior.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.
Inline comments:
In `@packages/query-db-collection/tests/query.test.ts`:
- Around line 5039-5052: The test in createLiveQueryCollection is not proving
that persisted hydration happens before the second-session refetch clears the
data. Update the query flow in query.test.ts by deferring the second queryFn
response so the persisted row can be asserted as present after
collection2.stateWhenReady() and before invalidation resolves to an empty
result. Use the existing symbols createLiveQueryCollection,
collection2.stateWhenReady(), secondQueryClient.invalidateQueries(), and
adapter2.rows.has(...) to structure the test so it fails if hydration never
occurs and only passes after the deferred fetch is released.
---
Nitpick comments:
In `@packages/query-db-collection/tests/query.test.ts`:
- Around line 4981-5000: The two collection setup blocks for the reload-insert
cleanup test are duplicating the same construction logic and using casts that
obscure the public type contract. Extract a typed helper around
createCollection/queryCollectionOptions that accepts the shared inputs like
queryClient, adapter, and per-session overrides, then reuse it for both session
setups. Remove the as any casts by adding proper type annotations to the helper
return value and the options object so the test stays on the typed surface and
still exercises the same behavior.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0a72bd3e-a762-4698-b3ec-6c6e3954b83b
📒 Files selected for processing (4)
.changeset/small-tables-listen.mdpackages/db-sqlite-persistence-core/src/persisted.tspackages/db-sqlite-persistence-core/tests/persisted.test.tspackages/query-db-collection/tests/query.test.ts
Fixes #1618
Problem
With a SQLite-persisted
queryCollection, an item inserted while the app is open survives forever after a reload, even once the query stops returning it (the server-side row was deleted). Reported in #1618 (ExpoSQLitePersistence, but it reproduces with any SQLite persistence adapter).Root cause: in
db-sqlite-persistence-core, a syncwriteof typeinsertwith no per-row metadata unconditionally queues a metadata reset (delete) for that key. During query reconciliation the query collection stamps the row'sownersmetadata (metadata.row.set) and then writes the metadata-less insert in the same sync transaction — so the insert's reset clobbers the just-written owners. Without persisted owners, the row is orphaned: on reload it belongs to no query and an empty query result can never reclaim it.Approach (red → green)
query-db-collectionthat inserts a row, reloads from the persisted store, and asserts the row is cleaned up once the query no longer returns it.This PR is a companion to #1619 that adds the missing end-to-end coverage; fix authorship is preserved via cherry-pick.
🤖 Generated with Claude Code
Summary by CodeRabbit
Summary by CodeRabbit
Bug Fixes
Tests