[优化] 添加清理历史快照功能

This commit is contained in:
ryan
2026-05-25 16:41:07 +08:00
parent 4f698be0a5
commit 83a426d3d6
6 changed files with 197 additions and 10 deletions
@@ -406,6 +406,34 @@ func ActivateConfigVersion(id uint) (*model.ConfigVersion, error) {
return version, nil
}
func CleanupConfigVersions(keepCount int) (int64, error) {
if keepCount < 3 {
keepCount = 3
}
var versions []model.ConfigVersion
if err := model.DB.Select("id", "is_active").Order("id desc").Find(&versions).Error; err != nil {
return 0, err
}
if len(versions) <= keepCount {
return 0, nil
}
var deleteIDs []uint
for i, v := range versions {
if i < keepCount {
continue
}
if v.IsActive {
continue
}
deleteIDs = append(deleteIDs, v.ID)
}
if len(deleteIDs) == 0 {
return 0, nil
}
result := model.DB.Where("id IN ?", deleteIDs).Delete(&model.ConfigVersion{})
return result.RowsAffected, result.Error
}
func buildCurrentConfigBundle(requireRoutes bool) (*configBundle, error) {
routes, err := model.GetEnabledProxyRoutes()
if err != nil {