Skip to content

Commit ac852c3

Browse files
committed
transfered postgresql versioncheck to pgsqlHealth
1 parent 551e940 commit ac852c3

File tree

2 files changed

+29
-39
lines changed

2 files changed

+29
-39
lines changed

common/versionCheck/postgres.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,11 @@
11
package common
22

3-
import (
4-
"fmt"
5-
"os/exec"
6-
"strings"
7-
)
8-
93
func PostgresCheck() {
10-
// Check if PostgreSQL is installed by checking the existence of command "psql"
11-
_, err := exec.LookPath("psql")
12-
if err != nil {
13-
addToNotInstalled("PostgreSQL")
14-
return
15-
}
16-
17-
// Get the version of PostgreSQL
18-
out, err := exec.Command("psql", "--version").Output()
19-
if err != nil {
20-
addToVersionErrors(fmt.Errorf("Error getting PostgreSQL version"))
21-
return
22-
}
23-
24-
// Parse the version
25-
// Eg. output
26-
// psql (PostgreSQL) 13.3 (Ubuntu 13.3-1.pgdg20.04+1)
27-
version := strings.Split(string(out), " ")[2]
4+
version := GatherVersion("postgres")
285

29-
oldVersion := GatherVersion("postgres")
30-
31-
if oldVersion != "" && oldVersion != version {
32-
addToUpdated(AppVersion{Name: "PostgreSQL", OldVersion: oldVersion, NewVersion: version})
33-
CreateNews("PostgreSQL", oldVersion, version, false)
34-
} else {
6+
if version != "" {
357
addToNotUpdated(AppVersion{Name: "PostgreSQL", OldVersion: version})
8+
} else {
9+
addToNotInstalled("PostgreSQL")
3610
}
37-
38-
StoreVersion("postgres", version)
3911
}

pgsqlHealth/main.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,31 @@ var healthData *PostgreSQLHealthData
6161
// getPostgresVersionInfo gets PostgreSQL version information
6262
// and determines update status
6363
func getPostgresVersionInfo() (string, bool, string) {
64-
// Get the version of PostgreSQL
65-
out, err := exec.Command("psql", "--version").Output()
66-
if err != nil {
67-
return "", false, "Error getting version"
64+
var version string
65+
66+
if Connection != nil {
67+
var fullVersion string
68+
err := Connection.QueryRow("SHOW server_version;").Scan(&fullVersion)
69+
if err == nil {
70+
version = strings.Split(fullVersion, " ")[0]
71+
}
6872
}
6973

70-
// Parse the version
71-
// Example output: psql (PostgreSQL) 13.3 (Ubuntu 13.3-1.pgdg20.04+1)
72-
version := strings.Split(string(out), " ")[2]
74+
if version == "" {
75+
out, err := exec.Command("psql", "--version").Output()
76+
if err != nil {
77+
return "", false, "Error getting version"
78+
}
79+
80+
// Parse the version
81+
// Example output: psql (PostgreSQL) 13.3 (Ubuntu 13.3-1.pgdg20.04+1)
82+
parts := strings.Split(string(out), " ")
83+
if len(parts) >= 3 {
84+
version = parts[2]
85+
} else {
86+
return "", false, "Error parsing psql version output"
87+
}
88+
}
7389

7490
// Get the previously stored version
7591
oldVersion := versionCheck.GatherVersion("postgres")
@@ -79,6 +95,8 @@ func getPostgresVersionInfo() (string, bool, string) {
7995
}
8096

8197
if oldVersion != "" && oldVersion != version {
98+
versionCheck.CreateNews("PostgreSQL", oldVersion, version, false)
99+
82100
// Don't use fmt.Println for any output here
83101
return version, true, fmt.Sprintf("Updated from %s to %s", oldVersion, version)
84102
}

0 commit comments

Comments
 (0)