{"record":{"id":"ebedefec5e2d3f93","repo":"flipped-aurora/gin-vue-admin","slug":"db-cannot-be-empty","errorCode":null,"errorMessage":"db Cannot be empty","messagePattern":"db Cannot be empty","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/task/clearTable.go","lineNumber":40,"sourceCode":"\t\tTableName:    \"sys_operation_records\",\n\t\tCompareField: \"created_at\",\n\t\tInterval:     \"2160h\",\n\t})\n\n\tClearTableDetail = append(ClearTableDetail, common.ClearDB{\n\t\tTableName:    \"jwt_blacklists\",\n\t\tCompareField: \"created_at\",\n\t\tInterval:     \"168h\",\n\t})\n\n\tClearTableDetail = append(ClearTableDetail, common.ClearDB{\n\t\tTableName:    \"sys_timed_task_logs\",\n\t\tCompareField: \"created_at\",\n\t\tInterval:     \"720h\", // 执行日志保留 30 天\n\t})\n\n\tif db == nil {\n\t\treturn errors.New(\"db Cannot be empty\")\n\t}\n\n\tfor _, detail := range ClearTableDetail {\n\t\tduration, err := time.ParseDuration(detail.Interval)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif duration < 0 {\n\t\t\treturn errors.New(\"parse duration < 0\")\n\t\t}\n\t\terr = db.Debug().Exec(fmt.Sprintf(\"DELETE FROM %s WHERE %s < ?\", detail.TableName, detail.CompareField), time.Now().Add(-duration)).Error\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/task/clearTable.go#L22-L58","documentation":"The ClearTable timed task deletes rows older than a configured interval from ClearTableDetail tables. Before executing, it validates that the injected *gorm.DB is non-nil; if nil, it returns errors.New(\"db Cannot be empty\"). This prevents nil-pointer panics when the task runs without a database connection.","triggerScenarios":"Registering/running the ClearTable task (clearTable.go:40) when the *gorm.DB parameter passed in is nil — typically because the task was invoked without a DB argument or the DB was never initialized (config missing, connection failed) before task execution.","commonSituations":"Task invoked in unit tests without setting global.GVA_DB; DB initialization failed silently at startup (bad DSN) and the nil handle propagated; custom task runners passing no db argument.","solutions":["Ensure the database initializes successfully at startup (check config.yaml DSN and startup logs for init errors).","Pass a valid *gorm.DB (e.g. global.GVA_DB) when invoking ClearTable from a custom runner.","In tests, initialize the DB (e.g. testutil.NewMemoryDB) before invoking the task.","Do not enable the timed task until DB init is confirmed."],"exampleFix":"// before\nClearTable(nil) // task runner without db\n// after\nif global.GVA_DB != nil { ClearTable(global.GVA_DB) }","handlingStrategy":"validation","validationCode":"// guard before invoking the task\nif global.GVA_DB == nil {\n  return errors.New(\"database not initialized; skip ClearTable\")\n}\nClearTable(global.GVA_DB)","typeGuard":"func dbReady(db *gorm.DB) bool { return db != nil }","tryCatchPattern":"if err := ClearTable(db); err != nil {\n  if err.Error() == \"db Cannot be empty\" {\n    logger.Error(\"ClearTable ran without a DB handle; check startup init\")\n  }\n  return err\n}","preventionTips":["Verify DB initialization succeeded in startup logs before enabling timed tasks.","In tests, initialize a memory DB (testutil.NewMemoryDB) before invoking tasks.","Never call ClearTable with a nil or zero-value *gorm.DB."],"tags":["task","timed-task","gorm","database","nil-check"],"backgroundTag":"nil-database-handle","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}