Skip to content

Commit f43bd25

Browse files
committed
refactor(pg): make active major list the single source of truth
1 parent 13aee6c commit f43bd25

14 files changed

Lines changed: 63 additions & 45 deletions

File tree

cli/build/util.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"pig/cli/ext"
66
"pig/internal/config"
7-
"slices"
87
"strconv"
98
"strings"
109
)
@@ -46,8 +45,8 @@ func parsePgVersions(pgVersions string) ([]int, error) {
4645
}
4746

4847
// Validate active/supported PostgreSQL major versions
49-
if !slices.Contains(ext.PostgresActiveMajorVersions, ver) {
50-
return nil, fmt.Errorf("PG version %d is not supported (valid: %s)", ver, ext.PostgresActiveVersionString)
48+
if !ext.IsActivePGMajor(ver) {
49+
return nil, fmt.Errorf("PG version %d is not supported (valid: %s)", ver, ext.PostgresActiveVersionString())
5150
}
5251

5352
if !seen[ver] {

cli/ext/alias_dynamic.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func parseCategoryAlias(alias string, targetPgVer int) (spec categoryAliasSpec,
9191
return categoryAliasSpec{}, false
9292
}
9393
if targetPgVer == 0 {
94-
targetPgVer = PostgresLatestMajorVersion
94+
targetPgVer = PostgresLatestMajorVersion()
9595
}
9696
return categoryAliasSpec{
9797
category: category,
@@ -113,7 +113,7 @@ func parseCategoryAlias(alias string, targetPgVer int) (spec categoryAliasSpec,
113113
if err != nil {
114114
return categoryAliasSpec{}, false
115115
}
116-
if !slices.Contains(PostgresActiveMajorVersions, ver) {
116+
if !IsActivePGMajor(ver) {
117117
return categoryAliasSpec{}, false
118118
}
119119

@@ -172,7 +172,7 @@ func buildCategoryPackageList(spec categoryAliasSpec, matrixOS, matrixArch strin
172172
seen := make(map[string]struct{})
173173
selectPG := spec.targetPG
174174
if spec.isPgsql {
175-
selectPG = PostgresLatestMajorVersion
175+
selectPG = PostgresLatestMajorVersion()
176176
}
177177

178178
for _, ext := range Catalog.Extensions {
@@ -272,11 +272,11 @@ func applyCategoryPackageSpecialCase(ext *Extension, pkgName string, pgVer int)
272272
}
273273

274274
func rewriteLatestCategoryPkgToTarget(pkg string, targetPG int) string {
275-
if targetPG == PostgresLatestMajorVersion {
275+
if targetPG == PostgresLatestMajorVersion() {
276276
return pkg
277277
}
278278

279-
latest := strconv.Itoa(PostgresLatestMajorVersion)
279+
latest := strconv.Itoa(PostgresLatestMajorVersion())
280280
target := strconv.Itoa(targetPG)
281281

282282
switch config.OSType {

cli/ext/extension.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,6 @@ func CompactVersion(pgVers []string) string {
8888
return fmt.Sprintf("%d-%d", filteredVers[0], filteredVers[len(filteredVers)-1])
8989
}
9090

91-
// isActivePGMajor checks whether the PostgreSQL major version is in active support window.
92-
func isActivePGMajor(ver int) bool {
93-
return slices.Contains(PostgresActiveMajorVersions, ver)
94-
}
95-
9691
// filterActivePGVersionStrings keeps supported PG majors in input order, deduplicated.
9792
func filterActivePGVersionStrings(pgVers []string) []string {
9893
filtered := make([]string, 0, len(pgVers))
@@ -103,7 +98,7 @@ func filterActivePGVersionStrings(pgVers []string) []string {
10398
continue
10499
}
105100
verInt, err := strconv.Atoi(ver)
106-
if err != nil || !isActivePGMajor(verInt) || seen[verInt] {
101+
if err != nil || !IsActivePGMajor(verInt) || seen[verInt] {
107102
continue
108103
}
109104
filtered = append(filtered, strconv.Itoa(verInt))

cli/ext/import.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func ImportExtensionsResult(pgVer int, names []string, importPath string) *outpu
2222
}
2323

2424
if pgVer == 0 {
25-
logrus.Debugf("no PostgreSQL version specified, set target version to the latest major version: %d", PostgresLatestMajorVersion)
26-
pgVer = PostgresLatestMajorVersion
25+
logrus.Debugf("no PostgreSQL version specified, set target version to the latest major version: %d", PostgresLatestMajorVersion())
26+
pgVer = PostgresLatestMajorVersion()
2727
}
2828

2929
if importPath == "" {

cli/ext/matrix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (m PkgMatrix) PGList() []int {
230230
}
231231
seen := make(map[int]bool)
232232
for _, e := range m {
233-
if e != nil && isActivePGMajor(e.PG) {
233+
if e != nil && IsActivePGMajor(e.PG) {
234234
seen[e.PG] = true
235235
}
236236
}

cli/ext/operation.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ func prepareExtensionPkgOp(opts preparePkgOpOptions) (*preparedPkgOp, *output.Re
4343

4444
pgVer := opts.PgVersion
4545
if pgVer == 0 {
46-
logrus.Debugf("using latest postgres version: %d by default", PostgresLatestMajorVersion)
47-
pgVer = PostgresLatestMajorVersion
46+
logrus.Debugf("using latest postgres version: %d by default", PostgresLatestMajorVersion())
47+
pgVer = PostgresLatestMajorVersion()
4848
}
4949

5050
// Check OS support.

cli/ext/pgsql.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"path/filepath"
88
"pig/internal/config"
99
"pig/internal/utils"
10+
"slices"
11+
"sort"
1012
"strconv"
1113
"strings"
1214
"text/tabwriter"
@@ -39,19 +41,41 @@ var (
3941
)
4042

4143
var (
42-
PostgresActiveMajorVersions = []int{18, 17, 16, 15, 14}
43-
PostgresActiveMajorVersionsAsc = []int{14, 15, 16, 17, 18}
44-
PostgresLatestMajorVersion = 18
45-
PostgresActiveVersionString = formatActiveVersions()
44+
// PostgresActiveMajorVersions is the single source of truth for active PG majors.
45+
PostgresActiveMajorVersions = []int{18, 17, 16, 15, 14}
4646
PostgresElSearchPath = []string{"/usr/pgsql-%s/bin/pg_config"}
4747
PostgresDEBSearchPath = []string{"/usr/lib/postgresql/%s/bin/pg_config"}
4848
PostgresMACSearchPath = []string{"/opt/homebrew/opt/postgresql@%s/bin/pg_config"}
4949
)
5050

51-
// formatActiveVersions returns a comma-separated string of active PG versions (ascending).
52-
func formatActiveVersions() string {
53-
parts := make([]string, len(PostgresActiveMajorVersionsAsc))
54-
for i, v := range PostgresActiveMajorVersionsAsc {
51+
// IsActivePGMajor checks whether the PostgreSQL major version is in active support window.
52+
func IsActivePGMajor(ver int) bool {
53+
return slices.Contains(PostgresActiveMajorVersions, ver)
54+
}
55+
56+
// PostgresLatestMajorVersion returns the maximum active PG major version.
57+
func PostgresLatestMajorVersion() int {
58+
latest := 0
59+
for _, v := range PostgresActiveMajorVersions {
60+
if v > latest {
61+
latest = v
62+
}
63+
}
64+
return latest
65+
}
66+
67+
// PostgresActiveMajorVersionsAsc returns active PG versions sorted ascending.
68+
func PostgresActiveMajorVersionsAsc() []int {
69+
versions := append([]int(nil), PostgresActiveMajorVersions...)
70+
sort.Ints(versions)
71+
return versions
72+
}
73+
74+
// PostgresActiveVersionString returns a comma-separated string of active PG versions (ascending).
75+
func PostgresActiveVersionString() string {
76+
asc := PostgresActiveMajorVersionsAsc()
77+
parts := make([]string, len(asc))
78+
for i, v := range asc {
5579
parts[i] = strconv.Itoa(v)
5680
}
5781
return strings.Join(parts, ",")

cli/ext/plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func BuildAddPlan(pgVer int, names []string, yes bool) *output.Plan {
106106
}
107107

108108
if pgVer == 0 {
109-
pgVer = PostgresLatestMajorVersion
109+
pgVer = PostgresLatestMajorVersion()
110110
}
111111

112112
resolved, notFound := resolvePlanExtensions(pgVer, names, true)
@@ -299,7 +299,7 @@ func BuildRmPlan(pgVer int, names []string, yes bool) *output.Plan {
299299
}
300300

301301
if pgVer == 0 {
302-
pgVer = PostgresLatestMajorVersion
302+
pgVer = PostgresLatestMajorVersion()
303303
}
304304

305305
resolved, notFound := resolvePlanExtensions(pgVer, names, false)

cli/ext/result_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,13 +1625,13 @@ func TestAddExtensionsDefaultPgVersion(t *testing.T) {
16251625
t.Fatal("expected non-nil result")
16261626
}
16271627

1628-
// Should use PostgresLatestMajorVersion by default
1628+
// Should use PostgresLatestMajorVersion() by default
16291629
data, ok := result.Data.(*ExtensionAddData)
16301630
if !ok {
16311631
t.Fatal("expected data to be *ExtensionAddData")
16321632
}
1633-
if data.PgVersion != PostgresLatestMajorVersion {
1634-
t.Errorf("expected PgVersion=%d, got %d", PostgresLatestMajorVersion, data.PgVersion)
1633+
if data.PgVersion != PostgresLatestMajorVersion() {
1634+
t.Errorf("expected PgVersion=%d, got %d", PostgresLatestMajorVersion(), data.PgVersion)
16351635
}
16361636
}
16371637

@@ -2270,13 +2270,13 @@ func TestRmExtensionsDefaultPgVersion(t *testing.T) {
22702270
t.Fatal("expected non-nil result")
22712271
}
22722272

2273-
// Should use PostgresLatestMajorVersion by default
2273+
// Should use PostgresLatestMajorVersion() by default
22742274
data, ok := result.Data.(*ExtensionRmData)
22752275
if !ok {
22762276
t.Fatal("expected data to be *ExtensionRmData")
22772277
}
2278-
if data.PgVersion != PostgresLatestMajorVersion {
2279-
t.Errorf("expected PgVersion=%d, got %d", PostgresLatestMajorVersion, data.PgVersion)
2278+
if data.PgVersion != PostgresLatestMajorVersion() {
2279+
t.Errorf("expected PgVersion=%d, got %d", PostgresLatestMajorVersion(), data.PgVersion)
22802280
}
22812281
}
22822282

@@ -2466,13 +2466,13 @@ func TestUpgradeExtensionsDefaultPgVersion(t *testing.T) {
24662466
t.Fatal("expected non-nil result")
24672467
}
24682468

2469-
// Should use PostgresLatestMajorVersion by default
2469+
// Should use PostgresLatestMajorVersion() by default
24702470
data, ok := result.Data.(*ExtensionUpdateData)
24712471
if !ok {
24722472
t.Fatal("expected data to be *ExtensionUpdateData")
24732473
}
2474-
if data.PgVersion != PostgresLatestMajorVersion {
2475-
t.Errorf("expected PgVersion=%d, got %d", PostgresLatestMajorVersion, data.PgVersion)
2474+
if data.PgVersion != PostgresLatestMajorVersion() {
2475+
t.Errorf("expected PgVersion=%d, got %d", PostgresLatestMajorVersion(), data.PgVersion)
24762476
}
24772477
}
24782478

cli/ext/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func UpgradeExtensions(pgVer int, names []string, yes bool) *output.Result {
1616
// Safety: never auto-upgrade everything. Without explicit targets, do nothing.
1717
if len(names) == 0 {
1818
if pgVer == 0 {
19-
pgVer = PostgresLatestMajorVersion
19+
pgVer = PostgresLatestMajorVersion()
2020
}
2121
data := &ExtensionUpdateData{
2222
PgVersion: pgVer,

0 commit comments

Comments
 (0)