flipped-aurora/gin-vue-admin · error

db Cannot be empty

Error message

db Cannot be empty

What it means

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.

Source

Thrown at server/task/clearTable.go:40

		TableName:    "sys_operation_records",
		CompareField: "created_at",
		Interval:     "2160h",
	})

	ClearTableDetail = append(ClearTableDetail, common.ClearDB{
		TableName:    "jwt_blacklists",
		CompareField: "created_at",
		Interval:     "168h",
	})

	ClearTableDetail = append(ClearTableDetail, common.ClearDB{
		TableName:    "sys_timed_task_logs",
		CompareField: "created_at",
		Interval:     "720h", // 执行日志保留 30 天
	})

	if db == nil {
		return errors.New("db Cannot be empty")
	}

	for _, detail := range ClearTableDetail {
		duration, err := time.ParseDuration(detail.Interval)
		if err != nil {
			return err
		}
		if duration < 0 {
			return errors.New("parse duration < 0")
		}
		err = db.Debug().Exec(fmt.Sprintf("DELETE FROM %s WHERE %s < ?", detail.TableName, detail.CompareField), time.Now().Add(-duration)).Error
		if err != nil {
			return err
		}
	}
	return nil
}

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Ensure the database initializes successfully at startup (check config.yaml DSN and startup logs for init errors).
  2. Pass a valid *gorm.DB (e.g. global.GVA_DB) when invoking ClearTable from a custom runner.
  3. In tests, initialize the DB (e.g. testutil.NewMemoryDB) before invoking the task.
  4. Do not enable the timed task until DB init is confirmed.

Example fix

// before
ClearTable(nil) // task runner without db
// after
if global.GVA_DB != nil { ClearTable(global.GVA_DB) }
Defensive patterns

Strategy: validation

Validate before calling

// guard before invoking the task
if global.GVA_DB == nil {
  return errors.New("database not initialized; skip ClearTable")
}
ClearTable(global.GVA_DB)

Type guard

func dbReady(db *gorm.DB) bool { return db != nil }

Try / catch

if err := ClearTable(db); err != nil {
  if err.Error() == "db Cannot be empty" {
    logger.Error("ClearTable ran without a DB handle; check startup init")
  }
  return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/ebedefec5e2d3f93. Report an issue: GitHub.