Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Apply database option ordering after adding options https://github.com/siyuan-note/siyuan/issues/18499

Danield3af3d1048c1
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 1bdca208ab..e547ef5106 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -7967,7 +7967,6 @@ func updateAttributeViewColumnOptions(operation *Operation) (err error) {
optionSorts[opt.Name] = i
}
- addNew := false
selectKey, _ := attrView.GetKey(operation.ID)
if nil == selectKey {
return
@@ -7988,21 +7987,37 @@ func updateAttributeViewColumnOptions(operation *Operation) (err error) {
Color: av.FilterColorValue(opt.Color),
Desc: opt.Desc,
})
- addNew = true
}
}
- if !addNew {
- sort.SliceStable(selectKey.Options, func(i, j int) bool {
- return optionSorts[selectKey.Options[i].Name] < optionSorts[selectKey.Options[j].Name]
- })
- }
+ sortAttributeViewColumnOptions(selectKey.Options, optionSorts)
regenAttrViewGroups(attrView)
err = av.SaveAttributeView(attrView)
return
}
+func sortAttributeViewColumnOptions(options []*av.SelectOption, optionSorts map[string]int) {
+ var sortableOptions []*av.SelectOption
+ for _, opt := range options {
+ if _, ok := optionSorts[opt.Name]; ok {
+ sortableOptions = append(sortableOptions, opt)
+ }
+ }
+ sort.SliceStable(sortableOptions, func(i, j int) bool {
+ return optionSorts[sortableOptions[i].Name] < optionSorts[sortableOptions[j].Name]
+ })
+
+ // 仅重排请求中包含的选项,避免过期请求扰动其他请求新增的选项。
+ sortableIndex := 0
+ for i, opt := range options {
+ if _, ok := optionSorts[opt.Name]; ok {
+ options[i] = sortableOptions[sortableIndex]
+ sortableIndex++
+ }
+ }
+}
+
func (tx *Transaction) doRemoveAttrViewColOption(operation *Operation) (ret *TxErr) {
err := removeAttributeViewColumnOption(operation)
if err != nil {
diff --git a/kernel/model/attribute_view_options_test.go b/kernel/model/attribute_view_options_test.go
new file mode 100644
index 0000000000..2dbd907605
--- /dev/null
+++ b/kernel/model/attribute_view_options_test.go
@@ -0,0 +1,110 @@
+// 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 (
+ "reflect"
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/av"
+)
+
+func TestUpdateAttributeViewColumnOptionsAddsAndSorts(t *testing.T) {
+ setupAttributeViewValidationTest(t)
+
+ attrView := av.NewAttributeView("20260802100000-options")
+ selectKey := attrView.KeyValues[1].Key
+ selectKey.Options = []*av.SelectOption{
+ {Name: "Doing", Color: "1"},
+ {Name: "Todo", Color: "2"},
+ }
+ if err := av.SaveAttributeView(attrView); nil != err {
+ t.Fatalf("save attribute view failed: %s", err)
+ }
+
+ targetOptions := []*av.SelectOption{
+ {Name: "Inbox", Color: "1"},
+ {Name: "Todo", Color: "2"},
+ {Name: "Shelved", Color: "3"},
+ {Name: "Doing", Color: "4"},
+ {Name: "Done", Color: "5"},
+ }
+ operation := &Operation{AvID: attrView.ID, ID: selectKey.ID, Data: targetOptions}
+ if err := updateAttributeViewColumnOptions(operation); nil != err {
+ t.Fatalf("update attribute view column options failed: %s", err)
+ }
+
+ parsed, err := av.ParseAttributeView(attrView.ID)
+ if nil != err {
+ t.Fatalf("parse attribute view failed: %s", err)
+ }
+ updatedKey, err := parsed.GetKey(selectKey.ID)
+ if nil != err {
+ t.Fatalf("get select key failed: %s", err)
+ }
+ assertSelectOptionNames(t, updatedKey.Options, []string{"Inbox", "Todo", "Shelved", "Doing", "Done"})
+ if "4" != updatedKey.GetOption("Doing").Color {
+ t.Fatalf("expected existing option color to be updated, got [%s]", updatedKey.GetOption("Doing").Color)
+ }
+}
+
+func TestUpdateAttributeViewColumnOptionsKeepsUnspecifiedOptionsInPlace(t *testing.T) {
+ setupAttributeViewValidationTest(t)
+
+ attrView := av.NewAttributeView("20260802100001-options")
+ selectKey := attrView.KeyValues[1].Key
+ selectKey.Options = []*av.SelectOption{
+ {Name: "First", Color: "1"},
+ {Name: "Second", Color: "2"},
+ {Name: "Concurrent", Color: "3"},
+ }
+ if err := av.SaveAttributeView(attrView); nil != err {
+ t.Fatalf("save attribute view failed: %s", err)
+ }
+
+ targetOptions := []*av.SelectOption{
+ {Name: "Second", Color: "2"},
+ {Name: "First", Color: "1"},
+ {Name: "New", Color: "4"},
+ }
+ operation := &Operation{AvID: attrView.ID, ID: selectKey.ID, Data: targetOptions}
+ if err := updateAttributeViewColumnOptions(operation); nil != err {
+ t.Fatalf("update attribute view column options failed: %s", err)
+ }
+
+ parsed, err := av.ParseAttributeView(attrView.ID)
+ if nil != err {
+ t.Fatalf("parse attribute view failed: %s", err)
+ }
+ updatedKey, err := parsed.GetKey(selectKey.ID)
+ if nil != err {
+ t.Fatalf("get select key failed: %s", err)
+ }
+ assertSelectOptionNames(t, updatedKey.Options, []string{"Second", "First", "Concurrent", "New"})
+}
+
+func assertSelectOptionNames(t *testing.T, options []*av.SelectOption, expected []string) {
+ t.Helper()
+
+ actual := make([]string, 0, len(options))
+ for _, option := range options {
+ actual = append(actual, option.Name)
+ }
+ if !reflect.DeepEqual(expected, actual) {
+ t.Fatalf("expected option names %v, got %v", expected, actual)
+ }
+}