mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-11 01:36:37 +08:00
[新增] 优化版本号生成逻辑,确保使用最大日序列号
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"openflare/model"
|
"openflare/model"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -1270,11 +1271,24 @@ func checksumBundle(mainConfig string, routeConfig string, supportFiles []Suppor
|
|||||||
|
|
||||||
func nextVersionNumber(now time.Time) (string, error) {
|
func nextVersionNumber(now time.Time) (string, error) {
|
||||||
prefix := now.Format("20060102")
|
prefix := now.Format("20060102")
|
||||||
var count int64
|
var latest model.ConfigVersion
|
||||||
if err := model.DB.Model(&model.ConfigVersion{}).Where("version LIKE ?", prefix+"-%").Count(&count).Error; err != nil {
|
err := model.DB.
|
||||||
|
Select("version").
|
||||||
|
Where("version LIKE ?", prefix+"-%").
|
||||||
|
Order("version desc").
|
||||||
|
First(&latest).Error
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return fmt.Sprintf("%s-%03d", prefix, 1), nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s-%03d", prefix, count+1), nil
|
suffix := strings.TrimPrefix(latest.Version, prefix+"-")
|
||||||
|
sequence, err := strconv.Atoi(suffix)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("invalid config version sequence %q: %w", latest.Version, err)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s-%03d", prefix, sequence+1), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderHTTPProxyServer(serverNames string, originURL string, originHost string, customHeaders []ProxyRouteCustomHeaderInput, cacheConfig routeCacheConfig, limitConfig routeLimitConfig, upstreamConfig routeUpstreamConfig, powEnabled bool, basicAuthEnabled bool, basicAuthUsername string, basicAuthPassword string, cfg openRestyConfigSnapshot) string {
|
func renderHTTPProxyServer(serverNames string, originURL string, originHost string, customHeaders []ProxyRouteCustomHeaderInput, cacheConfig routeCacheConfig, limitConfig routeLimitConfig, upstreamConfig routeUpstreamConfig, powEnabled bool, basicAuthEnabled bool, basicAuthUsername string, basicAuthPassword string, cfg openRestyConfigSnapshot) string {
|
||||||
|
|||||||
@@ -102,6 +102,42 @@ func TestCreateTLSCertificateAndRenderHTTPSConfig(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNextVersionNumberUsesMaxDailySequence(t *testing.T) {
|
||||||
|
setupServiceTestDB(t)
|
||||||
|
|
||||||
|
seed := []model.ConfigVersion{
|
||||||
|
{
|
||||||
|
Version: "20260526-001",
|
||||||
|
SnapshotJSON: "{}",
|
||||||
|
RenderedConfig: "server {}",
|
||||||
|
SupportFilesJSON: "[]",
|
||||||
|
Checksum: "checksum-1",
|
||||||
|
CreatedBy: "root",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Version: "20260526-003",
|
||||||
|
SnapshotJSON: "{}",
|
||||||
|
RenderedConfig: "server {}",
|
||||||
|
SupportFilesJSON: "[]",
|
||||||
|
Checksum: "checksum-3",
|
||||||
|
CreatedBy: "root",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, version := range seed {
|
||||||
|
if err := model.DB.Create(&version).Error; err != nil {
|
||||||
|
t.Fatalf("failed to seed config version %s: %v", version.Version, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
next, err := nextVersionNumber(time.Date(2026, 5, 26, 12, 0, 0, 0, time.Local))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("nextVersionNumber failed: %v", err)
|
||||||
|
}
|
||||||
|
if next != "20260526-004" {
|
||||||
|
t.Fatalf("expected next version to follow max suffix, got %s", next)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateProxyRouteRejectsHTTPSWithoutCertificate(t *testing.T) {
|
func TestCreateProxyRouteRejectsHTTPSWithoutCertificate(t *testing.T) {
|
||||||
setupServiceTestDB(t)
|
setupServiceTestDB(t)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user