mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-30 12:51:25 +00:00
Updates(&e) uses GORM's struct form, which skips zero-value fields -
but 0/"" is exactly the sentinel PRD 010 F1/F2 chose for "unconfigured"
(数据库变更.md §1.1). A caller resetting colWidth or defaultValue back
to that sentinel was therefore silently ignored: the row kept its old
value, the API reported success, and reopening the edit form showed the
stale number/string again.
Not fixed by switching to Select("*") - that would also start writing
this struct's other zero-valued fields (Sort, the Pk/Required/... bools),
none of which are part of this change. A second, map-form Updates
scoped to just these two columns writes the sentinel without touching
anything else; map-form Updates does not skip zero values.
TestSysColumnsUpdateClearsSentinelFields pins the regression: seed
150/"active", update to 0/"", read back and assert it stuck. Confirmed
red before this change (got 150/"active" back) and green after.
47 lines
1.6 KiB
Go
47 lines
1.6 KiB
Go
package tools
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// GORM's Updates(struct) skips zero-value fields, and PRD 010 F1/F2 chose 0 /
|
|
// "" as the sentinel for "unconfigured" (docs-prd/010-代码生成器前端模板迁移Vue3/
|
|
// 数据库变更.md §1.1). Put those together and Update can set ColWidth/
|
|
// DefaultValue but never clear them back to the sentinel: the struct-form
|
|
// Updates call silently drops the very values this feature needs to write.
|
|
func TestSysColumnsUpdateClearsSentinelFields(t *testing.T) {
|
|
db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(new(SysColumns)); err != nil {
|
|
t.Fatalf("migrate sys_columns: %v", err)
|
|
}
|
|
|
|
col := SysColumns{TableId: 1, ColumnName: "status", ColWidth: 150, DefaultValue: "active"}
|
|
if _, err := col.Create(db); err != nil {
|
|
t.Fatalf("create: %v", err)
|
|
}
|
|
|
|
// Reset back to the sentinel - the UI action for "go back to inferred
|
|
// width / no default", not merely "never configured".
|
|
update := SysColumns{ColumnId: col.ColumnId, ColWidth: 0, DefaultValue: ""}
|
|
if _, err := update.Update(db); err != nil {
|
|
t.Fatalf("update: %v", err)
|
|
}
|
|
|
|
var got SysColumns
|
|
if err := db.Table("sys_columns").First(&got, col.ColumnId).Error; err != nil {
|
|
t.Fatalf("read back: %v", err)
|
|
}
|
|
if got.ColWidth != 0 {
|
|
t.Errorf("colWidth: want 0 (cleared), got %d - Update() did not write the sentinel back", got.ColWidth)
|
|
}
|
|
if got.DefaultValue != "" {
|
|
t.Errorf("defaultValue: want \"\" (cleared), got %q - Update() did not write the sentinel back", got.DefaultValue)
|
|
}
|
|
}
|