refactor(db): use version string as primary key for ConfigVersion

-transition `of_config_versions` primary key from `id` to `version` string.
-add database migration files `202606270001_make_version_primary_key.sql` for PostgreSQL and SQLite.
-introduce AfterFind/AfterCreate GORM hooks to preserve JSON backward compatibility.
-refactor API controllers, logics, and front-end typescript definitions to receive `string` parameter.
This commit is contained in:
ryan
2026-06-27 13:38:02 +08:00
parent 13d6966cb5
commit ac7b776378
8 changed files with 11 additions and 87 deletions
+3 -31
View File
@@ -8,29 +8,14 @@ import (
"encoding/json"
"errors"
"strings"
"time"
"github.com/Rain-kl/Wavelet/internal/db"
"github.com/Rain-kl/Wavelet/internal/model"
openrestyrender "github.com/Rain-kl/Wavelet/pkg/render/openresty"
"gorm.io/gorm"
)
type configVersionRecord struct {
ID uint `gorm:"primaryKey"`
Version string `gorm:"column:version"`
SnapshotJSON string `gorm:"column:snapshot_json"`
SupportFilesJSON string `gorm:"column:support_files_json"`
Checksum string `gorm:"column:checksum"`
IsActive bool `gorm:"column:is_active"`
CreatedAt time.Time `gorm:"column:created_at"`
}
func (configVersionRecord) TableName() string {
return "of_config_versions"
}
func getActiveConfigMeta(ctx context.Context) (*ActiveConfigMeta, error) {
version, err := loadActiveConfigVersion(ctx)
version, err := model.GetActiveConfigVersion(ctx)
if err != nil {
return nil, err
}
@@ -41,7 +26,7 @@ func getActiveConfigMeta(ctx context.Context) (*ActiveConfigMeta, error) {
}
func getActiveConfigForAgent(ctx context.Context) (*ConfigResponse, error) {
version, err := loadActiveConfigVersion(ctx)
version, err := model.GetActiveConfigVersion(ctx)
if err != nil {
return nil, err
}
@@ -62,19 +47,6 @@ func getActiveConfigForAgent(ctx context.Context) (*ConfigResponse, error) {
}, nil
}
func loadActiveConfigVersion(ctx context.Context) (*configVersionRecord, error) {
conn := db.DB(ctx)
if conn == nil {
return nil, errors.New("database not initialized")
}
version := &configVersionRecord{}
err := conn.Where("is_active = ?", true).Order("id desc").First(version).Error
if err != nil {
return nil, err
}
return version, nil
}
func sourceSupportFiles(files []SupportFile) []SupportFile {
if len(files) == 0 {
return nil
@@ -129,7 +129,7 @@ func checksumAgentWAFIPGroup(group WAFIPGroup) string {
}
func activeConfigWAFIPGroupIDs(ctx context.Context) ([]uint, error) {
version, err := loadActiveConfigVersion(ctx)
version, err := model.GetActiveConfigVersion(ctx)
if err != nil {
if isActiveConfigNotFound(err) {
return []uint{}, nil
@@ -26,7 +26,7 @@ func setupWAFIPGroupTestDB(t *testing.T) func() {
require.NoError(t, err)
require.NoError(t, sqliteDB.AutoMigrate(
&model.OpenFlareWAFIPGroup{},
&configVersionRecord{},
&model.ConfigVersion{},
))
db.SetDB(sqliteDB)
@@ -55,7 +55,7 @@ func seedActiveConfigWithWAFIPGroup(t *testing.T, ctx context.Context, ipGroupID
snapshotJSON, err := json.Marshal(snapshot)
require.NoError(t, err)
require.NoError(t, db.DB(ctx).Create(&configVersionRecord{
require.NoError(t, db.DB(ctx).Create(&model.ConfigVersion{
Version: "20260618-001",
SnapshotJSON: string(snapshotJSON),
Checksum: "test-checksum",