Deletions mutations have incorrect `original` and `changes` if they were previously updated

未关闭
#2,447 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
typescript
领域
data

调研方向

Start at Transaction.applyMutations, where pending mutations are merged and where delete mutations get original, modified and changes all set to the deletion-time row. Read how PendingMutation is built for deletes and what collection._state.syncedData holds, using the update-then-delete sandbox to reproduce. Done means a delete after an update reports original from before the transaction, modified from just before deletion, and changes as their diff, with a regression test covering bulk updates plus a delete.

由索引模型根据 Issue 内容生成。

描述

  • I've validated the bug against the latest version of DB packages

Describe the bug
Deletion mutations have incorrect original and changes if they were previously updated.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/p/sandbox/yxwtgj
  2. Click on 'Update'
  3. Click on 'Delete'
  4. Observe that original and changes are incorrect

Expected behavior

  • original should be the data since before the current transaction started,
  • modified should be the data before the deletion (might be the same as original if no updates occurred during the transaction), and
  • changes should be the difference between original and modified.

Additional context
I run the following fixDeletions workaround in Transaction.applyMutations, which I've monkey-patched:

function fixDeletions(
  this: Transaction<any>,
  mutations: PendingMutation<any>[],
): void {
  // Merge via a globalKey-keyed map rather than a findIndex scan per
  // mutation, which is O(n²) for bulk operations (e.g. inserting many rows
  // in one call). Map preserves insertion order, matching the previous
  // replace-in-place / remove / append semantics.
  const merged = new Map<string, PendingMutation<any>>()
  for (const mutation of this.mutations) {
    merged.set(mutation.globalKey, mutation)
  }

  for (const newMutation of mutations) {
    const { globalKey } = newMutation

    if (newMutation.type !== 'delete') {
      merged.set(globalKey, newMutation)
      continue
    }

    // Deletions have `original`, `modified`, and `changes` set to the
    // state at deletion time.
    // `original` should be the actual original, `changes` should be the
    // changes between `original` and `modified`

    const original =
      newMutation.collection._state.syncedData.get(newMutation.key) ?? newMutation.original

    const { modified } = newMutation
    const keys = new Set([...Object.keys(original), ...Object.keys(modified)])
    const changes: Partial<Record<string, unknown>> = {}
    for (const key of keys) {
      if (
        Object.hasOwn(original, key) !== Object.hasOwn(modified, key)
        || !sameRowValue(original[key], modified[key])
      ) {
        changes[key] = modified[key]
      }
    }

    merged.set(globalKey, {
      ...newMutation,
      original,
      changes,
    })
  }

  // Rebuild in place to preserve the array's identity for external holders
  this.mutations.length = 0
  for (const mutation of merged.values()) {
    this.mutations.push(mutation)
  }
}
主要语言
TypeScript
星标
4k
派生
286
平均合并
1 天 8 小时
30 天内合并 PR
105

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/db 的其他 Issue

查看 TanStack/db 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。