From 0ac6e45e119091e112e0357f990fe70f15b451c1 Mon Sep 17 00:00:00 2001 From: chlins Date: Wed, 23 Apr 2025 16:55:56 +0800 Subject: [PATCH] feat: add the default gc config Signed-off-by: chlins --- manager/database/database.go | 29 +++++++++++++++++++++++++++++ manager/models/config.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/manager/database/database.go b/manager/database/database.go index d54b0e20b44..dc03d812f4c 100644 --- a/manager/database/database.go +++ b/manager/database/database.go @@ -17,6 +17,7 @@ package database import ( + "encoding/json" "errors" "fmt" @@ -200,5 +201,33 @@ func seed(db *gorm.DB) error { } } + // Create default GC config. + var config models.Config + if err := db.Model(models.Config{}).First(&config, models.Config{Name: models.ConfigGC}).Error; err != nil { + if !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } + + gcConfig := &models.GCConfig{ + Audit: &models.GCAuditConfig{ + TTL: models.DefaultGCAuditTTL, + }, + Job: &models.GCJobConfig{ + TTL: models.DefaultGCJobTTL, + }, + } + gcConfigVal, err := json.Marshal(gcConfig) + if err != nil { + return err + } + + if err := db.Model(models.Config{}).Create(&models.Config{ + Name: models.ConfigGC, + Value: string(gcConfigVal), + }).Error; err != nil { + return err + } + } + return nil } diff --git a/manager/models/config.go b/manager/models/config.go index 7fb08542ec0..10d67ab1d88 100644 --- a/manager/models/config.go +++ b/manager/models/config.go @@ -16,6 +16,21 @@ package models +import "time" + +const ( + // ConfigGC is the name of the GC config. + ConfigGC = "gc" +) + +const ( + // DefaultGCJobTTL is the default GC job TTL, by default it is 6 hours. + DefaultGCJobTTL = time.Hour * 6 + + // DefaultGCAuditTTL is the default audit TTL, by default it is 7 days. + DefaultGCAuditTTL = time.Hour * 24 * 7 +) + type Config struct { BaseModel Name string `gorm:"column:name;type:varchar(256);index:uk_config_name,unique;not null;comment:config name" json:"name"` @@ -24,3 +39,16 @@ type Config struct { UserID uint `gorm:"comment:user id" json:"user_id"` User User `json:"user"` } + +type GCConfig struct { + Audit *GCAuditConfig `json:"audit,omitempty"` + Job *GCJobConfig `json:"job,omitempty"` +} + +type GCAuditConfig struct { + TTL time.Duration `json:"ttl"` +} + +type GCJobConfig struct { + TTL time.Duration `json:"ttl"` +}