An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve database cell identity when pasting https://github.com/siyuan-note/siyuan/issues/18579
Danielfa0276552c48
diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.tsindex 58fb04a759..59d1543d0d 100644--- a/app/src/protyle/render/av/cell.ts+++ b/app/src/protyle/render/av/cell.ts@@ -36,7 +36,7 @@ import {mergeAVBatchRelationValue,setAVBatchDisplayValue} from "./batchValue";-import {genAVDragFillValue} from "./dragFillValue";+import {genAVDragFillValue, rebindAVCellValue} from "./dragFillValue";export {cellValueIsEmpty} from "./cellValue";@@ -940,7 +940,11 @@ export const updateCellsValue = async (protyle: IProtyle, nodeElement: HTMLEleme} else {cellValue = genCellValue(type, newValue, column?.dateFormat);}- cellValue.id = cellId;+ cellValue = rebindAVCellValue(cellValue, {+ id: cellId,+ keyID: colId,+ blockID: rowID,+ });if ((cellValue.type === "date" && typeof cellValue.date === "string") ||(cellValue.type === "relation" && typeof cellValue.relation === "string")) {break;diff --git a/app/src/protyle/render/av/dragFillValue.test.ts b/app/src/protyle/render/av/dragFillValue.test.tsindex ec99536185..0c0370aa8f 100644--- a/app/src/protyle/render/av/dragFillValue.test.ts+++ b/app/src/protyle/render/av/dragFillValue.test.ts@@ -1,8 +1,8 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {genAVDragFillValue} from "./dragFillValue";+import {genAVDragFillValue, rebindAVCellValue} from "./dragFillValue";-describe("genAVDragFillValue", () => {+describe("rebindAVCellValue", () => {it("rebinds copied values to the target database cell", () => {const source = {id: "source-value",@@ -19,7 +19,7 @@ describe("genAVDragFillValue", () => {updatedAt: number;};- assert.deepEqual(genAVDragFillValue(source, {+ assert.deepEqual(rebindAVCellValue(source, {id: "target-value",keyID: "target-key",blockID: "target-row",@@ -33,7 +33,40 @@ describe("genAVDragFillValue", () => {},});assert.equal(source.id, "source-value");+ assert.equal(source.keyID, "source-key");assert.equal(source.blockID, "source-row");assert.equal(source.createdAt, 100);});++ it("keeps the drag fill helper aligned with general cell rebinding", () => {+ const source: IAVCellValue = {+ id: "source-value",+ keyID: "source-key",+ blockID: "source-row",+ type: "block",+ isDetached: true,+ block: {+ content: "",+ },+ };+ const target = {+ id: "target-value",+ keyID: "target-key",+ blockID: "target-row",+ };++ const rebound = rebindAVCellValue(source, target);+ assert.deepEqual(rebound, {+ id: "target-value",+ keyID: "target-key",+ blockID: "target-row",+ type: "block",+ isDetached: true,+ block: {+ content: "",+ },+ });+ assert.deepEqual(genAVDragFillValue(source, target), rebound);+ assert.equal(source.blockID, "source-row");+ });});diff --git a/app/src/protyle/render/av/dragFillValue.ts b/app/src/protyle/render/av/dragFillValue.tsindex b887c6156e..e815f3164b 100644--- a/app/src/protyle/render/av/dragFillValue.ts+++ b/app/src/protyle/render/av/dragFillValue.ts@@ -1,10 +1,10 @@-interface IAVDragFillTarget {+interface IAVCellValueTarget {id: string;keyID: string;blockID: string;}-export const genAVDragFillValue = (source: IAVCellValue, target: IAVDragFillTarget) => {+export const rebindAVCellValue = (source: IAVCellValue, target: IAVCellValueTarget) => {const value = JSON.parse(JSON.stringify(source)) as IAVCellValue & {createdAt?: number;updatedAt?: number;@@ -16,3 +16,5 @@ export const genAVDragFillValue = (source: IAVCellValue, target: IAVDragFillTargdelete value.updatedAt;return value;};++export const genAVDragFillValue = rebindAVCellValue;diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.goindex 3c937faf2b..3e42ed7ec2 100644--- a/kernel/model/attribute_view.go+++ b/kernel/model/attribute_view.go@@ -7626,6 +7626,14 @@ func updateAttributeViewValue(tx *Transaction, attrView *av.AttributeView, keyID}break}+ if nil == val {+ err = av.ErrKeyNotFound+ return+ }++ valueID := val.ID+ valueType := val.Type+ valueCreatedAt := val.CreatedAtisUpdatingBlockKey := av.KeyTypeBlock == val.Typevar oldRelationBlockIDs []string@@ -7641,10 +7649,15 @@ func updateAttributeViewValue(tx *Transaction, attrView *av.AttributeView, keyIDlogging.LogErrorf("marshal value [%+v] failed: %s", valueData, err)return}- if err = gulu.JSON.UnmarshalJSON(data, &val); err != nil {+ if err = gulu.JSON.UnmarshalJSON(data, val); err != nil {logging.LogErrorf("unmarshal data [%s] failed: %s", data, err)return}+ val.ID = valueID+ val.KeyID = keyID+ val.BlockID = itemID+ val.Type = valueType+ val.CreatedAt = valueCreatedAtkey, _ := attrView.GetKey(keyID)diff --git a/kernel/model/attribute_view_fix.go b/kernel/model/attribute_view_fix.goindex 14334a1f29..f251d95b16 100644--- a/kernel/model/attribute_view_fix.go+++ b/kernel/model/attribute_view_fix.go@@ -80,6 +80,10 @@ func checkAttrView(attrView *av.AttributeView, view *av.View) (changed bool) {// 订正字段类型for _, kv := range attrView.KeyValues {for _, v := range kv.Values {+ if v.KeyID != kv.Key.ID {+ v.KeyID = kv.Key.ID+ changed = true+ }if v.Type != kv.Key.Type {v.Type = kv.Key.Typeif av.KeyTypeBlock == v.Type && nil == v.Block {diff --git a/kernel/model/attribute_view_value_identity_test.go b/kernel/model/attribute_view_value_identity_test.gonew file mode 100644index 0000000000..2f3b26a9f0--- /dev/null+++ b/kernel/model/attribute_view_value_identity_test.go@@ -0,0 +1,109 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package model++import (+ "errors"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/av"+)++func TestUpdateAttributeViewValuePreservesTargetIdentity(t *testing.T) {+ const (+ targetValueID = "20260806000000-targetv"+ targetKeyID = "20260806000001-targetk"+ targetItemID = "20260806000002-targeti"+ sourceValueID = "20260806000003-sourcev"+ sourceKeyID = "20260806000004-sourcek"+ sourceItemID = "20260806000005-sourcei"+ targetCreatedAt = int64(100)+ )+ target := &av.Value{+ ID: targetValueID,+ KeyID: targetKeyID,+ BlockID: targetItemID,+ Type: av.KeyTypeBlock,+ IsDetached: true,+ CreatedAt: targetCreatedAt,+ UpdatedAt: 101,+ Block: &av.ValueBlock{Content: "target"},+ }+ attrView := &av.AttributeView{+ ID: "20260806000006-avtest1",+ KeyValues: []*av.KeyValues{{+ Key: &av.Key{ID: targetKeyID, Type: av.KeyTypeBlock},+ Values: []*av.Value{target},+ }},+ }+ source := &av.Value{+ ID: sourceValueID,+ KeyID: sourceKeyID,+ BlockID: sourceItemID,+ Type: av.KeyTypeBlock,+ IsDetached: true,+ CreatedAt: 200,+ UpdatedAt: 201,+ Block: &av.ValueBlock{Content: "source"},+ }++ updated, err := updateAttributeViewValue(nil, attrView, targetKeyID, targetItemID, source, false)+ if nil != err {+ t.Fatal(err)+ }+ if updated != target {+ t.Fatal("updated value should keep the target value instance")+ }+ if targetValueID != updated.ID || targetKeyID != updated.KeyID || targetItemID != updated.BlockID ||+ av.KeyTypeBlock != updated.Type || targetCreatedAt != updated.CreatedAt {+ t.Fatalf("target identity was overwritten: %+v", updated)+ }+ if nil == updated.Block || "source" != updated.Block.Content {+ t.Fatalf("source content was not applied: %+v", updated.Block)+ }+ if source.UpdatedAt == updated.UpdatedAt {+ t.Fatalf("source update time was retained: %d", updated.UpdatedAt)+ }+}++func TestUpdateAttributeViewValueRejectsMissingKey(t *testing.T) {+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{{+ Key: &av.Key{ID: "20260806000000-existing", Type: av.KeyTypeText},+ }}}++ _, err := updateAttributeViewValue(nil, attrView, "20260806000001-missing", "20260806000002-item001",+ &av.Value{Type: av.KeyTypeText, Text: &av.ValueText{Content: "source"}}, false)+ if !errors.Is(err, av.ErrKeyNotFound) {+ t.Fatalf("missing key returned error [%v]", err)+ }+}++func TestCheckAttrViewCorrectsValueKeyID(t *testing.T) {+ const targetKeyID = "20260806000000-targetk"+ value := &av.Value{KeyID: "20260806000001-sourcek", Type: av.KeyTypeText}+ attrView := &av.AttributeView{KeyValues: []*av.KeyValues{{+ Key: &av.Key{ID: targetKeyID, Type: av.KeyTypeText},+ Values: []*av.Value{value},+ }}}++ if !checkAttrView(attrView, &av.View{}) {+ t.Fatal("correcting a value key ID should mark the attribute view as changed")+ }+ if targetKeyID != value.KeyID {+ t.Fatalf("value key ID was not corrected: %s", value.KeyID)+ }+}diff --git a/kernel/sql/av.go b/kernel/sql/av.goindex 11ffaf3d94..eb43e18374 100644--- a/kernel/sql/av.go+++ b/kernel/sql/av.go@@ -784,6 +784,10 @@ func fillAttributeViewKeyValues(attrView *av.AttributeView, collection av.Collec}for keyID, values := range fieldValues {keyValues, _ := attrView.GetKeyValues(keyID)+ if nil == keyValues {+ logging.LogWarnf("attribute view [%s] key [%s] not found while filling values", attrView.ID, keyID)+ continue+ }existingIDs := map[string]bool{}for _, kv := range keyValues.Values {existingIDs[kv.ID] = truediff --git a/kernel/sql/av_value_test.go b/kernel/sql/av_value_test.gonew file mode 100644index 0000000000..dce83bff6c--- /dev/null+++ b/kernel/sql/av_value_test.go@@ -0,0 +1,48 @@+// SiYuan - Refactor your thinking+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package sql++import (+ "testing"++ "github.com/siyuan-note/siyuan/kernel/av"+)++func TestFillAttributeViewKeyValuesSkipsMissingKey(t *testing.T) {+ const existingKeyID = "20260806000000-existing"+ attrView := &av.AttributeView{+ ID: "20260806000001-avtest1",+ KeyValues: []*av.KeyValues{{+ Key: &av.Key{ID: existingKeyID, Type: av.KeyTypeText},+ }},+ }+ table := &av.Table{Rows: []*av.TableRow{{+ ID: "20260806000002-item001",+ Cells: []*av.TableCell{{BaseValue: &av.BaseValue{Value: &av.Value{+ ID: "20260806000003-value01",+ KeyID: "20260806000004-missing",+ BlockID: "20260806000002-item001",+ Type: av.KeyTypeText,+ Text: &av.ValueText{Content: "source"},+ }}}},+ }}}++ fillAttributeViewKeyValues(attrView, table)+ if 0 != len(attrView.KeyValues[0].Values) {+ t.Fatalf("value for a missing key was added to the existing key: %+v", attrView.KeyValues[0].Values)+ }+}