From aa7a92664ddc8359002a9fbff74e10793f3346d6 Mon Sep 17 00:00:00 2001 From: zhangwenjian Date: Sat, 19 Sep 2026 13:33:42 +0800 Subject: [PATCH] =?UTF-8?q?feat=E2=9C=A8:=20add=20sys=5Fcolumns.col=5Fwidt?= =?UTF-8?q?h=20and=20default=5Fvalue=20(PRD=20010=20F1/F2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Back the code generator's Vue 3 template migration: col_width lets R2's column-width inference be overridden per field, and default_value lets R1/A6's "unconfigured rows still generate a usable page" guarantee hold for generated forms. Both use a sentinel default (0 / "") rather than NULL so "unconfigured" has exactly one representation - see docs-prd/010-代码生成器前端模板迁移Vue3/数据库变更.md §1.1. The migration and the model change land together: AddColumn reads the column definition off tools.SysColumns's own gorm tag, so splitting them across commits would leave one of them failing to compile. --- app/other/models/tools/sys_columns.go | 13 +++++ .../1786700010000_gen_column_layout_fields.go | 56 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 cmd/migrate/migration/version/1786700010000_gen_column_layout_fields.go diff --git a/app/other/models/tools/sys_columns.go b/app/other/models/tools/sys_columns.go index ff6b8ddf..41db6176 100644 --- a/app/other/models/tools/sys_columns.go +++ b/app/other/models/tools/sys_columns.go @@ -45,6 +45,19 @@ type SysColumns struct { CreateBy int `gorm:"column:create_by;size:20;" json:"createBy"` UpdateBy int `gorm:"column:update_By;size:20;" json:"updateBy"` + // ColWidth and DefaultValue back PRD 010 F1/F2 (代码生成器前端模板迁移 Vue 3). + // Both use a sentinel default (0 / "") rather than NULL - see + // docs-prd/010-代码生成器前端模板迁移Vue3/数据库变更.md §1.1: a non-pointer + // int/string field can never read NULL back out, and NULL would give + // "unconfigured" two representations instead of one. Callers test + // ColWidth == 0 / DefaultValue == "" to detect "not configured". + // + // ColWidth deliberately has no gorm size tag: this codebase's "size:N" + // convention on numeric fields maps to a narrow SQL integer type (see + // column_width_test.go), and col_width needs to hold values up to 800. + ColWidth int `gorm:"column:col_width;not null;default:0;comment:table column width in px, 0 = not configured" json:"colWidth"` + DefaultValue string `gorm:"column:default_value;size:255;not null;default:'';comment:form field default value, empty = not configured" json:"defaultValue"` + common.ModelTime } diff --git a/cmd/migrate/migration/version/1786700010000_gen_column_layout_fields.go b/cmd/migrate/migration/version/1786700010000_gen_column_layout_fields.go new file mode 100644 index 00000000..28b81121 --- /dev/null +++ b/cmd/migrate/migration/version/1786700010000_gen_column_layout_fields.go @@ -0,0 +1,56 @@ +package version + +import ( + "runtime" + + "gorm.io/gorm" + + "go-admin/app/other/models/tools" + "go-admin/cmd/migrate/migration" + common "go-admin/common/models" +) + +// Add sys_columns.col_width and sys_columns.default_value for PRD 010 F1/F2 +// (代码生成器前端模板迁移 Vue 3). +// +// col_width backs R2's column-width inference fallback and default_value +// backs R1/A6's "unconfigured rows still generate a usable page" guarantee - +// see docs-prd/010-代码生成器前端模板迁移Vue3/数据库变更.md §1.1 for why both +// defaults are sentinels (0 / "") rather than NULL: a non-pointer Go int/ +// string field can never read NULL back out, and NULL would give +// "unconfigured" two representations instead of one. +// +// Ordered after 1786700003000, so this reads tools.SysColumns (the runtime +// model sys_columns's Update/GetPage/GetSysTablesInfo actually query through) +// rather than cmd/migrate/migration/models, matching every migration in this +// directory since sys_columns was converted - see +// 1786700004000_generator_tables_marker.go and schema_coverage_test.go's +// TestPostConversionMigrationsAvoidFrozenSeedModels. +// +// Hard prerequisite: tools.SysColumns must already declare ColWidth and +// DefaultValue (with the gorm tags in the doc above) by the time this file +// is compiled - AddColumn reads the column definition off the struct's own +// tag, not off anything in this file. Landing this migration without that +// model change first makes HasColumn/AddColumn silently do nothing (the +// field lookup fails and AddColumn returns an error naming the missing +// field), which fails loudly rather than silently - see the "no such field" +// error - so this is caught at migrate time, not left for a report later. +func init() { + _, fileName, _, _ := runtime.Caller(0) + migration.Migrate.SetVersion(migration.GetFilename(fileName), _1786700010000GenColumnLayoutFields) +} + +func _1786700010000GenColumnLayoutFields(db *gorm.DB, version string) error { + m := db.Migrator() + if !m.HasColumn(&tools.SysColumns{}, "ColWidth") { + if err := m.AddColumn(&tools.SysColumns{}, "ColWidth"); err != nil { + return err + } + } + if !m.HasColumn(&tools.SysColumns{}, "DefaultValue") { + if err := m.AddColumn(&tools.SysColumns{}, "DefaultValue"); err != nil { + return err + } + } + return db.Create(&common.Migration{Version: version}).Error +}