-
Notifications
You must be signed in to change notification settings - Fork 269
fix(db): reject in-place sync row changes without previousValue in development #1988
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KyleAMathews
wants to merge
1
commit into
main
Choose a base branch
from
fix-sync-reused-row-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+205
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/db': patch | ||
| --- | ||
|
|
||
| Throw `SyncRowReusedWithoutPreviousValueError` in development when a sync source changes a row object it already wrote and writes it again without `previousValue`. The collection keeps the written object as the stored row, so the in-place change overwrote the previous value, and live queries could keep the row in a filter it left. Production builds skip the check. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { afterEach, describe, expect, it, vi } from 'vitest' | ||
| import { createCollection } from '../src/collection/index.js' | ||
| import { SyncRowReusedWithoutPreviousValueError } from '../src/errors.js' | ||
| import { createLiveQueryCollection, eq } from '../src/query/index.js' | ||
| import type { SyncConfig } from '../src/types.js' | ||
|
|
||
| /** | ||
| * A sync update tells the Collection a row's new value. Core keeps the | ||
| * object a source writes as the row's stored value, so a source that | ||
| * changes that object in place and writes it again has already overwritten | ||
| * the previous value core would publish. Live queries then see an update | ||
| * whose old and new values are the same object, and a row that left a | ||
| * filter stays in it. A source that reuses its row object must say what the | ||
| * row was through `previousValue`. In development, core rejects the write | ||
| * that omits it; production keeps the cheaper unchecked path. | ||
| */ | ||
| type Row = { id: string; group: string } | ||
|
|
||
| function setup() { | ||
| let sync!: Parameters<SyncConfig<Row, string>[`sync`]>[0] | ||
| const row: Row = { id: `r`, group: `a` } | ||
| const collection = createCollection<Row, string>({ | ||
| id: `sync-reused-row`, | ||
| getKey: (item) => item.id, | ||
| startSync: true, | ||
| sync: { | ||
| rowUpdateMode: `full`, | ||
| sync: (actions) => { | ||
| sync = actions | ||
| actions.begin() | ||
| actions.write({ type: `insert`, value: row }) | ||
| actions.commit() | ||
| actions.markReady() | ||
| }, | ||
| }, | ||
| }) | ||
| const groupA = createLiveQueryCollection({ | ||
| query: (q) => q.from({ r: collection }).where(({ r }) => eq(r.group, `a`)), | ||
| startSync: true, | ||
| }) | ||
| return { sync: () => sync, row, collection, groupA } | ||
| } | ||
|
|
||
| describe(`sync writes of a reused row object`, () => { | ||
| afterEach(() => vi.unstubAllEnvs()) | ||
|
|
||
| it(`rejects an in-place update without previousValue in development`, async () => { | ||
| vi.stubEnv(`NODE_ENV`, `development`) | ||
| const { sync, row, collection, groupA } = setup() | ||
| await groupA.preload() | ||
| row.group = `b` | ||
| sync().begin() | ||
| expect(() => sync().write({ type: `update`, value: row })).toThrow( | ||
| SyncRowReusedWithoutPreviousValueError, | ||
| ) | ||
| await collection.cleanup() | ||
| }) | ||
|
|
||
| it(`accepts an in-place update that names its previous value`, async () => { | ||
| vi.stubEnv(`NODE_ENV`, `development`) | ||
| const { sync, row, collection, groupA } = setup() | ||
| await groupA.preload() | ||
| expect([...groupA.keys()]).toEqual([`r`]) | ||
| const previousValue = { ...row } | ||
| row.group = `b` | ||
| sync().begin() | ||
| sync().write({ type: `update`, value: row, previousValue }) | ||
| sync().commit() | ||
| expect([...groupA.keys()]).toEqual([]) | ||
| await collection.cleanup() | ||
| }) | ||
|
|
||
| it(`accepts an unchanged rewrite after a declared in-place update`, async () => { | ||
| vi.stubEnv(`NODE_ENV`, `development`) | ||
| const { sync, row, collection, groupA } = setup() | ||
| await groupA.preload() | ||
| const previousValue = { ...row } | ||
| row.group = `b` | ||
| sync().begin() | ||
| sync().write({ type: `update`, value: row, previousValue }) | ||
| sync().commit() | ||
| // The live-query Collection rewrites an unchanged row the same way. | ||
| sync().begin() | ||
| expect(() => sync().write({ type: `update`, value: row })).not.toThrow() | ||
| sync().commit() | ||
| await collection.cleanup() | ||
| }) | ||
|
|
||
| it(`accepts an update with a new object`, async () => { | ||
| vi.stubEnv(`NODE_ENV`, `development`) | ||
| const { sync, collection, groupA } = setup() | ||
| await groupA.preload() | ||
| sync().begin() | ||
| sync().write({ type: `update`, value: { id: `r`, group: `b` } }) | ||
| sync().commit() | ||
| expect([...groupA.keys()]).toEqual([]) | ||
| await collection.cleanup() | ||
| }) | ||
|
|
||
| it(`does not check in production`, async () => { | ||
| vi.stubEnv(`NODE_ENV`, `production`) | ||
| const { sync, row, collection, groupA } = setup() | ||
| await groupA.preload() | ||
| row.group = `b` | ||
| sync().begin() | ||
| expect(() => sync().write({ type: `update`, value: row })).not.toThrow() | ||
| sync().commit() | ||
| await collection.cleanup() | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Document the shallow scope of the reused-row check.
The check compares shallow snapshots, so in-place changes inside existing nested objects can bypass the error. Both descriptions should state this limitation.
docs/guides/collection-options-creator.md#L188-L189: qualify the error claim and state that in-place nested-field changes are not detected..changeset/reject-reused-sync-rows.md#L5-L5: narrow the release-note claim to changes visible to the shallow check and state the same limitation.📍 Affects 2 files
docs/guides/collection-options-creator.md#L188-L189(this comment).changeset/reject-reused-sync-rows.md#L5-L5🤖 Prompt for AI Agents