Skip to content

Commit b2bae4e

Browse files
committed
MGMT-19930: upgrade the pgx to v5
It is beneficial because compared to v4 (previously used) v5 has optimized memory usage. pgx comes as an indirect dependency from gorm.io/driver/postgres. So this task involves upgrading the postgres driver too.
1 parent 7503bc0 commit b2bae4e

File tree

218 files changed

+27934
-5154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+27934
-5154
lines changed

cmd/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,7 @@ func setupServerForIPXE(serverInfo *servers.ServerInfo, h http.Handler) {
802802
}
803803

804804
func setupDB(log logrus.FieldLogger) *gorm.DB {
805-
dbConnectionStr := fmt.Sprintf("host=%s port=%s user=%s database=%s password=%s sslmode=disable",
806-
Options.DBConfig.Host, Options.DBConfig.Port, Options.DBConfig.User, Options.DBConfig.Name, Options.DBConfig.Pass)
805+
dbConnectionStr := Options.DBConfig.LibpqDSN()
807806
var db *gorm.DB
808807
var err error
809808
// Tries to open a db connection every 2 seconds

go.mod

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ require (
8282
golang.org/x/sys v0.38.0
8383
gopkg.in/ini.v1 v1.67.0
8484
gopkg.in/yaml.v2 v2.4.0
85-
gorm.io/driver/postgres v1.3.5
86-
gorm.io/gorm v1.25.8
85+
gorm.io/driver/postgres v1.6.0
86+
gorm.io/gorm v1.25.10
8787
k8s.io/api v0.34.2
8888
k8s.io/apiextensions-apiserver v0.29.5
8989
k8s.io/apimachinery v0.34.2
@@ -122,9 +122,9 @@ require (
122122
github.com/gorilla/websocket v1.5.4-0.20250319132907-e064f32e3674 // indirect
123123
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
124124
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
125-
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
126125
github.com/jackc/pgio v1.0.0 // indirect
127-
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
126+
github.com/jackc/pgx/v5 v5.6.0 // indirect
127+
github.com/jackc/puddle/v2 v2.2.2 // indirect
128128
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
129129
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
130130
github.com/magiconair/properties v1.8.7 // indirect
@@ -201,11 +201,9 @@ require (
201201
github.com/imdario/mergo v0.3.16 // indirect
202202
github.com/inconshreveable/mousetrap v1.1.0 // indirect
203203
github.com/itchyny/timefmt-go v0.1.3 // indirect
204-
github.com/jackc/pgconn v1.14.3 // indirect
205204
github.com/jackc/pgpassfile v1.0.0 // indirect
206-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
205+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
207206
github.com/jackc/pgtype v1.14.4 // indirect
208-
github.com/jackc/pgx/v4 v4.18.2 // indirect
209207
github.com/jinzhu/inflection v1.0.0 // indirect
210208
github.com/jinzhu/now v1.1.5 // indirect
211209
github.com/jmespath/go-jmespath v0.4.1-0.20220621161143-b0104c826a24 // indirect
@@ -273,7 +271,6 @@ require (
273271

274272
replace (
275273
github.com/google/gnostic-models => github.com/google/gnostic-models v0.6.8
276-
github.com/jackc/pgx/v4 => github.com/jackc/pgx/v4 v4.18.3
277274
github.com/metal3-io/baremetal-operator => github.com/openshift/baremetal-operator v0.0.0-20231019133159-8643f32fea3e
278275
github.com/metal3-io/baremetal-operator/apis => github.com/openshift/baremetal-operator/apis v0.0.0-20231019133159-8643f32fea3e
279276
github.com/metal3-io/baremetal-operator/pkg/hardwareutils => github.com/openshift/baremetal-operator/pkg/hardwareutils v0.0.0-20231019133159-8643f32fea3e

go.sum

Lines changed: 69 additions & 15 deletions
Large diffs are not rendered by default.

internal/common/common_unitest_db.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/google/uuid"
1313
. "github.com/onsi/gomega"
14+
dbpkg "github.com/openshift/assisted-service/pkg/db"
1415
"gorm.io/driver/postgres"
1516
"gorm.io/gorm"
1617
"gorm.io/gorm/logger"
@@ -337,11 +338,7 @@ func DeleteTestDB(db *gorm.DB, dbName string) {
337338

338339
func getDBDSN(dbName string) string {
339340
host, port := getDBContext().GetHostPort()
340-
dsn := fmt.Sprintf("host=%s port=%s user=postgres password=admin sslmode=disable", host, port)
341-
if dbName != "" {
342-
dsn = dsn + fmt.Sprintf(" database=%s", dbName)
343-
}
344-
return dsn
341+
return dbpkg.LibpqDSN(host, port, "postgres", "admin", dbName)
345342
}
346343

347344
func openTestDB(dbName string) (*gorm.DB, error) {

pkg/db/db.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
package db
22

3+
import "fmt"
4+
import "strings"
5+
36
type Config struct {
47
Host string `envconfig:"DB_HOST"`
58
Port string `envconfig:"DB_PORT"`
69
User string `envconfig:"DB_USER"`
710
Pass string `envconfig:"DB_PASS"`
811
Name string `envconfig:"DB_NAME"`
912
}
13+
14+
func escapeConninfoValue(v string) string {
15+
replacer := strings.NewReplacer(`\`, `\\`, `'`, `\'`)
16+
return "'" + replacer.Replace(v) + "'"
17+
}
18+
19+
// LibpqDSN returns a PostgreSQL libpq keyword connection string for GORM/pgx.
20+
// client_encoding=UTF8 is always set so pgx v5 simple-query paths (used by some GORM APIs) work regardless of server defaults.
21+
func LibpqDSN(host, port, user, password, database string) string {
22+
s := fmt.Sprintf("host=%s port=%s user=%s password=%s sslmode=disable client_encoding=UTF8",
23+
host, port, escapeConninfoValue(user), escapeConninfoValue(password))
24+
if database != "" {
25+
s += fmt.Sprintf(" database=%s", escapeConninfoValue(database))
26+
}
27+
return s
28+
}
29+
30+
// LibpqDSN builds a libpq DSN from c using the same rules as the package function.
31+
func (c Config) LibpqDSN() string {
32+
return LibpqDSN(c.Host, c.Port, c.User, c.Pass, c.Name)
33+
}

subsystem/kubeapi/kubeapi_suite_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package kubeapi
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"net/url"
87
"os"
98
"testing"
@@ -19,6 +18,7 @@ import (
1918
"github.com/openshift/assisted-service/client"
2019
"github.com/openshift/assisted-service/models"
2120
"github.com/openshift/assisted-service/pkg/auth"
21+
dbpkg "github.com/openshift/assisted-service/pkg/db"
2222
"github.com/openshift/assisted-service/subsystem/utils_test"
2323
hivev1 "github.com/openshift/hive/apis/hive/v1"
2424
"github.com/sirupsen/logrus"
@@ -108,8 +108,7 @@ func init() {
108108
userClientCfg := clientcfg(auth.UserAuthHeaderWriter("bearer " + Options.TestToken))
109109
agentClientCfg := clientcfg(auth.AgentAuthHeaderWriter(utils_test.FakePS))
110110

111-
db, err := gorm.Open(postgres.Open(fmt.Sprintf("host=%s port=%s user=admin database=installer password=admin sslmode=disable",
112-
Options.DBHost, Options.DBPort)), &gorm.Config{})
111+
db, err := gorm.Open(postgres.Open(dbpkg.LibpqDSN(Options.DBHost, Options.DBPort, "admin", "admin", "installer")), &gorm.Config{})
113112
if err != nil {
114113
logrus.Fatal("Fail to connect to DB, ", err)
115114
}

subsystem/subsystem_suite_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package subsystem
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"net/url"
87
"os"
98
"testing"
@@ -20,6 +19,7 @@ import (
2019
"github.com/openshift/assisted-service/client/versions"
2120
"github.com/openshift/assisted-service/models"
2221
"github.com/openshift/assisted-service/pkg/auth"
22+
dbpkg "github.com/openshift/assisted-service/pkg/db"
2323
"github.com/openshift/assisted-service/subsystem/utils_test"
2424
hivev1 "github.com/openshift/hive/apis/hive/v1"
2525
"github.com/sirupsen/logrus"
@@ -112,8 +112,7 @@ func init() {
112112
log.Fatal(err.Error())
113113
}
114114

115-
db, err := gorm.Open(postgres.Open(fmt.Sprintf("host=%s port=%s user=admin database=installer password=admin sslmode=disable",
116-
Options.DBHost, Options.DBPort)), &gorm.Config{})
115+
db, err := gorm.Open(postgres.Open(dbpkg.LibpqDSN(Options.DBHost, Options.DBPort, "admin", "admin", "installer")), &gorm.Config{})
117116
if err != nil {
118117
logrus.Fatal("Fail to connect to DB, ", err)
119118
}

vendor/github.com/jackc/chunkreader/v2/.travis.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

vendor/github.com/jackc/chunkreader/v2/README.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

vendor/github.com/jackc/chunkreader/v2/chunkreader.go

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)