From 28c727501f53a9d1761b5e33d9739490f3b3a5e9 Mon Sep 17 00:00:00 2001 From: Odboy Date: Fri, 18 Sep 2026 21:14:53 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20=E7=BB=9F=E4=B8=80=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E8=AE=B0=E5=BD=95=E4=B8=8E=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E6=97=B6=E9=97=B4=E6=9D=A5=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - saveHistory 接收调用方传入的数据库时间,不再使用 time.Now() - 更新、删除、回滚先取 dao.Now(),避免取时间失败时产生无效历史 - 历史表 created_at 升级为 datetime(3),新增迁移脚本 --- server/internal/handler/config.go | 41 ++++++++++--------- server/internal/model/app_config.go | 2 +- sql/kenaito_conf.sql | 2 +- ...migrate_app_config_history_datetime_ms.sql | 5 +++ 4 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 sql/migrate_app_config_history_datetime_ms.sql diff --git a/server/internal/handler/config.go b/server/internal/handler/config.go index b0cce72..85373c4 100644 --- a/server/internal/handler/config.go +++ b/server/internal/handler/config.go @@ -278,19 +278,20 @@ func (h *ConfigHandler) Update(c *gin.Context) { return } - // save history - h.saveHistory(&config, userInfo.Name, "update") - - originalContents := config.Contents - config.Contents = req.Contents - config.Version = req.Version - config.UpdatedBy = userInfo.Name now, err := dao.Now() if err != nil { logger.SugaredLogger.Errorf("[ConfigHandler.Update] get database time error: %v", err) c.JSON(http.StatusInternalServerError, model.WrapperResponse{Code: 500, Msg: "保存失败"}) return } + + // save history + h.saveHistory(&config, userInfo.Name, "update", now) + + originalContents := config.Contents + config.Contents = req.Contents + config.Version = req.Version + config.UpdatedBy = userInfo.Name config.UpdatedAt = &now dao.Engine.ID(config.Id).Cols("contents", "version", "updated_by", "updated_at").Update(&config) @@ -316,17 +317,18 @@ func (h *ConfigHandler) Delete(c *gin.Context) { return } - // 记录删除前的配置内容 - h.saveHistory(&config, userInfo.Name, "delete") - - config.Enabled = false - config.UpdatedBy = userInfo.Name now, err := dao.Now() if err != nil { logger.SugaredLogger.Errorf("[ConfigHandler.Delete] get database time error: %v", err) c.JSON(http.StatusInternalServerError, model.WrapperResponse{Code: 500, Msg: "删除失败"}) return } + + // 记录删除前的配置内容 + h.saveHistory(&config, userInfo.Name, "delete", now) + + config.Enabled = false + config.UpdatedBy = userInfo.Name config.UpdatedAt = &now dao.Engine.ID(config.Id).Cols("enabled", "updated_by", "updated_at").Update(&config) @@ -431,17 +433,18 @@ func (h *ConfigHandler) Rollback(c *gin.Context) { return } - // 记录回滚前的配置内容 - h.saveHistory(&config, userInfo.Name, "rollback") - - config.Contents = history.Contents - config.UpdatedBy = userInfo.Name now, err := dao.Now() if err != nil { logger.SugaredLogger.Errorf("[ConfigHandler.Rollback] get database time error: %v", err) c.JSON(http.StatusInternalServerError, model.WrapperResponse{Code: 500, Msg: "回滚失败"}) return } + + // 记录回滚前的配置内容 + h.saveHistory(&config, userInfo.Name, "rollback", now) + + config.Contents = history.Contents + config.UpdatedBy = userInfo.Name config.UpdatedAt = &now dao.Engine.ID(config.Id).Cols("contents", "updated_by", "updated_at").Update(&config) } @@ -460,7 +463,7 @@ func (h *ConfigHandler) buildAppNames(config *model.AppConfig) string { return "" } -func (h *ConfigHandler) saveHistory(config *model.AppConfig, operator, operate string) { +func (h *ConfigHandler) saveHistory(config *model.AppConfig, operator, operate string, createdAt time.Time) { history := model.AppConfigHistory{ OriginId: config.Id, AppId: config.AppId, @@ -471,7 +474,7 @@ func (h *ConfigHandler) saveHistory(config *model.AppConfig, operator, operate s Type: config.Type, Operate: operate, Contents: config.Contents, - CreatedAt: func() *time.Time { t := time.Now(); return &t }(), + CreatedAt: &createdAt, CreatedBy: operator, } dao.Engine.Insert(&history) diff --git a/server/internal/model/app_config.go b/server/internal/model/app_config.go index 2f707b7..8ee856d 100644 --- a/server/internal/model/app_config.go +++ b/server/internal/model/app_config.go @@ -39,7 +39,7 @@ type AppConfigHistory struct { Operate string `xorm:"varchar(16)" json:"operate"` // update, delete, rollback Contents string `xorm:"text" json:"contents"` ActiveContents string `xorm:"-" json:"activeContents,omitempty"` - CreatedAt *time.Time `xorm:"datetime" json:"createdAt"` + CreatedAt *time.Time `xorm:"datetime(3)" json:"createdAt"` CreatedBy string `xorm:"varchar(32)" json:"createdBy"` } diff --git a/sql/kenaito_conf.sql b/sql/kenaito_conf.sql index c2e8672..17f19b5 100644 --- a/sql/kenaito_conf.sql +++ b/sql/kenaito_conf.sql @@ -152,7 +152,7 @@ CREATE TABLE `app_config_history` ( `type` smallint DEFAULT NULL, `operate` varchar(16) DEFAULT NULL, `contents` text, - `created_at` datetime DEFAULT NULL, + `created_at` datetime(3) DEFAULT NULL, `created_by` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=utf8mb3; diff --git a/sql/migrate_app_config_history_datetime_ms.sql b/sql/migrate_app_config_history_datetime_ms.sql new file mode 100644 index 0000000..2d3a38c --- /dev/null +++ b/sql/migrate_app_config_history_datetime_ms.sql @@ -0,0 +1,5 @@ +-- 将配置历史时间字段升级为毫秒精度,与 app_config 保持一致。 +-- 新库由 xorm Sync2 按模型定义建表,已有环境执行本脚本升级。 + +ALTER TABLE `app_config_history` + MODIFY COLUMN `created_at` DATETIME(3) NULL DEFAULT NULL;