From 33e7f99fdcfbe1a364079cc7b0f91d2f7c032b16 Mon Sep 17 00:00:00 2001 From: huang Date: Mon, 30 Mar 2026 15:09:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=B8=85=E7=90=86=E9=A2=84=E8=A7=88=E6=8E=A5=E5=8F=A3=20panic?= =?UTF-8?q?=20=E5=8F=8A=E8=B7=AF=E7=94=B1=E6=96=87=E6=A1=A3=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E7=BF=BB=E5=80=8D=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. CountOldRecords 改用 GORM .Scan() 替代 .Row().Scan(),避免查询不存在的表时 nil pointer panic 2. polling_cleanup 路由注册的 basePath 参数去掉多余拼接,修复生成的 OpenAPI 文档路径翻倍 --- internal/routes/polling_cleanup.go | 10 ++++------ internal/store/postgres/polling_cleanup_store.go | 7 +++---- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/internal/routes/polling_cleanup.go b/internal/routes/polling_cleanup.go index 89db4e6..3a503bb 100644 --- a/internal/routes/polling_cleanup.go +++ b/internal/routes/polling_cleanup.go @@ -55,8 +55,7 @@ func registerPollingCleanupRoutes(router fiber.Router, handler *admin.PollingCle }) // 清理日志 - logsPath := basePath + "/data-cleanup-logs" - Register(router, doc, logsPath, "GET", "/data-cleanup-logs", handler.ListLogs, RouteSpec{ + Register(router, doc, basePath, "GET", "/data-cleanup-logs", handler.ListLogs, RouteSpec{ Summary: "获取数据清理日志列表", Tags: []string{"轮询管理-数据清理"}, Input: new(dto.ListDataCleanupLogReq), @@ -65,8 +64,7 @@ func registerPollingCleanupRoutes(router fiber.Router, handler *admin.PollingCle }) // 清理操作 - cleanupPath := basePath + "/data-cleanup" - Register(router, doc, cleanupPath+"/preview", "GET", "/data-cleanup/preview", handler.Preview, RouteSpec{ + Register(router, doc, basePath, "GET", "/data-cleanup/preview", handler.Preview, RouteSpec{ Summary: "预览待清理数据", Tags: []string{"轮询管理-数据清理"}, Input: nil, @@ -74,7 +72,7 @@ func registerPollingCleanupRoutes(router fiber.Router, handler *admin.PollingCle Auth: true, }) - Register(router, doc, cleanupPath+"/progress", "GET", "/data-cleanup/progress", handler.GetProgress, RouteSpec{ + Register(router, doc, basePath, "GET", "/data-cleanup/progress", handler.GetProgress, RouteSpec{ Summary: "获取数据清理进度", Tags: []string{"轮询管理-数据清理"}, Input: nil, @@ -82,7 +80,7 @@ func registerPollingCleanupRoutes(router fiber.Router, handler *admin.PollingCle Auth: true, }) - Register(router, doc, cleanupPath+"/trigger", "POST", "/data-cleanup/trigger", handler.TriggerCleanup, RouteSpec{ + Register(router, doc, basePath, "POST", "/data-cleanup/trigger", handler.TriggerCleanup, RouteSpec{ Summary: "手动触发数据清理", Tags: []string{"轮询管理-数据清理"}, Input: new(dto.TriggerDataCleanupReq), diff --git a/internal/store/postgres/polling_cleanup_store.go b/internal/store/postgres/polling_cleanup_store.go index 1c6c735..521554a 100644 --- a/internal/store/postgres/polling_cleanup_store.go +++ b/internal/store/postgres/polling_cleanup_store.go @@ -193,12 +193,11 @@ func (s *DataCleanupLogStore) CountOldRecords(ctx context.Context, tableName str return 0, err } default: - // 对于其他表,使用原始 SQL - row := s.db.WithContext(ctx).Raw( + // 对于其他表,使用原始 SQL(使用 GORM 的 Scan 避免 Row() 返回 nil 导致 panic) + if err := s.db.WithContext(ctx).Raw( "SELECT COUNT(*) FROM "+tableName+" WHERE created_at < ?", cutoffTime, - ).Row() - if err := row.Scan(&count); err != nil { + ).Scan(&count).Error; err != nil { return 0, err } }