Skip to content
Source
Sign inSign up
TanStack/db

fix(db-sqlite-persistence-core): rows marked with metadata prior to insert get reset causing stale data #1619

Closedtombryden(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/1619/headopened

🎯 Changes

Fixes https://github.com/TanStack/db/issues/1618 - rows in the same transaction marked with metadata get their metadata reset on insert, causing bugs like stale data when used with queryCollectionOptions

✅ Checklist

  • 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).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where stale item metadata could remain after inserts, causing outdated items to persist.
    • Preserved existing row metadata when an insert occurs in the same transaction as an explicit metadata update.
    • Cleared leftover metadata only when no newer metadata update was already queued, improving sync accuracy.
    1. Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 88dc55fc-1760-4629-a8e4-93807b3ab080

      📥 Commits

      Reviewing files that changed from the base of the PR and between 267b36bb3d84dc3c280ed97e81e86d2fa3648dc3 and ff1cf8a2c21430a139669f6790ac6b32e2977974.

      📒 Files selected for processing (3)
      • .changeset/small-tables-listen.md
      • packages/db-sqlite-persistence-core/src/persisted.ts
      • packages/db-sqlite-persistence-core/tests/persisted.test.ts

      📝 Walkthrough

      Walkthrough

      Adjusts SQLite persistence insert handling so a metadata-less insert only clears row metadata when no row-metadata write is already queued for that key. Adds regression tests for preserved metadata and stale metadata clearing, plus a patch changeset.

      Changes

      SQLite row-metadata insert fix

      Layer / File(s)Summary
      Insert metadata guard
      packages/db-sqlite-persistence-core/src/persisted.ts
      The wrapped sync.write insert path only queues a row-metadata delete when the transaction has not already queued a row-metadata write for that key.
      Regression coverage and release note
      packages/db-sqlite-persistence-core/tests/persisted.test.ts, .changeset/small-tables-listen.md
      Tests cover same-transaction metadata preservation and stale metadata clearing, and the patch changeset records the release entry.

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~12 minutes

      Suggested reviewers

      • samwillis

      Poem

      I thump through the metadata grass, one hop keeps old crumbs from the past. If a write is already in flight, I leave its little tag alight. 🐇 Hooray for cleaner burrow paths!

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly describes the main bug fix in db-sqlite-persistence-core and matches the changeset.
      Description check✅ PassedThe description follows the template with changes, checklist, and release impact sections filled in.
      Linked Issues check✅ PassedThe code and tests address issue #1618 by preserving row metadata on insert and clearing stale metadata when needed.
      Out of Scope Changes check✅ PassedThe PR only adds a changeset plus focused code and test updates for the reported metadata bug.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      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
      Comment `@coderabbitai help` to get the list of available commands.
    2. Hi @tombryden, thanks for addressing this issue! The fix seems to be spot on! 🥇

      I'm closing this PR in favor of https://github.com/TanStack/db/pull/1626, i added a higher-level more end-to-end reproduction and cherry picked your fix such that attribution is correct.

    3. Hi @tombryden, thanks for addressing this issue! The fix seems to be spot on! 🥇

      I'm closing this PR in favor of #1626, i added a higher-level more end-to-end reproduction and cherry picked your fix such that attribution is correct.

      Great, thanks!

Sign in to join this conversation.