Skip to content

Commit

Permalink
db: switch driver to pgx5 (#3258)
Browse files Browse the repository at this point in the history
* update swo sqlc output to pgx v5

* update swosync to v5

* update swoinfo to use v5

* update swomsg to use pgxpool (v5)

* update harness to use v5

* update app to use v5

* formatting

* swo to v5

* ensure connections are closed

* run tidy

* leave formatting to separate PR
  • Loading branch information
mastercactapus authored Sep 5, 2023
1 parent 15da2c4 commit 0cd7d32
Show file tree
Hide file tree
Showing 29 changed files with 94 additions and 127 deletions.
2 changes: 1 addition & 1 deletion app/listenevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package app
import (
"context"

"github.com/jackc/pgconn"
"github.com/jackc/pgx/v5/pgconn"
"github.com/pkg/errors"
"github.com/target/goalert/permission"
"github.com/target/goalert/util/log"
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ require (
github.com/google/uuid v1.3.0
github.com/gordonklaus/ineffassign v0.0.0-20230610083614-0e73809eb601
github.com/hashicorp/yamux v0.1.1
github.com/jackc/pgconn v1.14.1
github.com/jackc/pgtype v1.14.0
github.com/jackc/pgx/v4 v4.18.1
github.com/jackc/pgx/v5 v5.4.3
github.com/jmespath/go-jmespath v0.4.0
github.com/joho/godotenv v1.5.1
Expand Down Expand Up @@ -85,7 +83,6 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fullstorydev/grpcurl v1.8.7 // indirect
github.com/go-jose/go-jose/v3 v3.0.0 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
Expand All @@ -104,11 +101,11 @@ require (
github.com/ian-kent/linkio v0.0.0-20170807205755-97566b872887 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.1 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v4 v4.18.1 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect
github.com/jhump/protoreflect v1.15.1 // indirect
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF0
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g=
github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
github.com/Masterminds/sprig v2.16.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
Expand Down Expand Up @@ -113,7 +111,6 @@ github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWH
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k=
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
github.com/coreos/go-oidc/v3 v3.6.0 h1:AKVxfYw1Gmkn/w96z0DbT/B/xFnzTd3MkZvWLjF4n/o=
github.com/coreos/go-oidc/v3 v3.6.0/go.mod h1:ZpHUsHBucTUj6WOkrP4E20UPynbLZzhTQ1XKCXkxyPc=
Expand Down Expand Up @@ -174,8 +171,6 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me
github.com/go-test/deep v1.0.4 h1:u2CU3YKy9I2pmu9pX0eq50wCgjfGIt539SqR7FbHiho=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.2.0+incompatible h1:yyYWMnhkhrKwwr8gAOcOCYxOOscHgDS9yZgBrnJfGa0=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
Expand Down Expand Up @@ -297,6 +292,7 @@ github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0=
github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo=
github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk=
github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8=
Expand All @@ -314,10 +310,10 @@ github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c=
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc=
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A=
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg=
Expand Down Expand Up @@ -488,8 +484,6 @@ github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
2 changes: 1 addition & 1 deletion migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"strings"
"time"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/pkg/errors"
"github.com/rubenv/sql-migrate/sqlparse"
"github.com/target/goalert/lock"
Expand Down
2 changes: 1 addition & 1 deletion sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sql:
gen:
go:
out: swo/swodb
sql_package: pgx/v4
sql_package: pgx/v5
- schema:
- migrate/schema.sql
queries:
Expand Down
4 changes: 2 additions & 2 deletions swo/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"database/sql/driver"
"sync"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
)

// Connector is a driver.Connector that will use the old database until the
Expand Down
2 changes: 1 addition & 1 deletion swo/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"sync"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/target/goalert/swo/swogrp"
"github.com/target/goalert/swo/swosync"
)
Expand Down
46 changes: 26 additions & 20 deletions swo/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"

"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/target/goalert/app/lifecycle"
"github.com/target/goalert/swo/swodb"
"github.com/target/goalert/swo/swogrp"
Expand All @@ -23,8 +23,8 @@ import (
type Manager struct {
// sql.DB instance safe for the application to use (instrumented for safe SWO operation)
dbApp *sql.DB
dbMain *sql.DB
dbNext *sql.DB
dbMain *pgxpool.Pool
dbNext *pgxpool.Pool

pauseResume lifecycle.PauseResumer

Expand Down Expand Up @@ -71,15 +71,23 @@ func NewManager(cfg Config) (*Manager, error) {
}.String()
}

mainDB, err := sqldrv.NewDB(cfg.OldDBURL, appStr(ConnTypeMainMgr))
mainURL, err := sqldrv.AppURL(cfg.OldDBURL, appStr(ConnTypeMainMgr))
if err != nil {
return nil, fmt.Errorf("connect to old db: %w", err)
}
mainPool, err := pgxpool.New(context.Background(), mainURL)
if err != nil {
return nil, fmt.Errorf("connect to old db: %w", err)
}
mainAppDBC, err := sqldrv.NewConnector(cfg.OldDBURL, appStr(ConnTypeMainApp))
if err != nil {
return nil, fmt.Errorf("connect to old db: %w", err)
}
nextDB, err := sqldrv.NewDB(cfg.NewDBURL, appStr(ConnTypeNextMgr))
nextURL, err := sqldrv.AppURL(cfg.NewDBURL, appStr(ConnTypeNextMgr))
if err != nil {
return nil, fmt.Errorf("connect to new db: %w", err)
}
nextPool, err := pgxpool.New(context.Background(), nextURL)
if err != nil {
return nil, fmt.Errorf("connect to new db: %w", err)
}
Expand All @@ -91,8 +99,8 @@ func NewManager(cfg Config) (*Manager, error) {
m := &Manager{
Config: cfg,
dbApp: sql.OpenDB(NewConnector(mainAppDBC, nextAppDBC)),
dbMain: mainDB,
dbNext: nextDB,
dbMain: mainPool,
dbNext: nextPool,
}

ctx := cfg.Logger.BackgroundContext()
Expand Down Expand Up @@ -177,19 +185,17 @@ func (m *Manager) withConnFromBoth(ctx context.Context, f func(ctx context.Conte
})
}

func withPGXConn(ctx context.Context, db *sql.DB, runFunc func(context.Context, *pgx.Conn) error) error {
conn, err := db.Conn(ctx)
if err != nil {
return err
}
defer conn.Close()

return conn.Raw(func(driverConn interface{}) error {
conn := driverConn.(*stdlib.Conn).Conn()
defer conn.Close(context.Background())
defer conn.PgConn().Close(context.Background())
func withPGXConn(ctx context.Context, db *pgxpool.Pool, runFunc func(context.Context, *pgx.Conn) error) error {
return db.AcquireFunc(ctx, func(conn *pgxpool.Conn) error {
err := runFunc(ctx, conn.Conn())
if err != nil {
_ = conn.Conn().Close(ctx)
return err
}

return runFunc(ctx, conn)
// Close connection instead of returning it to the pool, to ensure
// locks are released.
return conn.Conn().Close(ctx)
})
}

Expand Down
4 changes: 2 additions & 2 deletions swo/swodb/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions swo/swodb/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions swo/swodb/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion swo/swoinfo/conninfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package swoinfo
import (
"context"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/target/goalert/swo/swodb"
)

Expand Down
6 changes: 3 additions & 3 deletions swo/swoinfo/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"

"github.com/google/uuid"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/target/goalert/swo/swodb"
)

Expand All @@ -22,11 +22,11 @@ func DBInfo(ctx context.Context, conn *pgx.Conn) (*DB, error) {
if err != nil {
return nil, err
}
if info.ID == uuid.Nil {
if !info.ID.Valid {
return nil, fmt.Errorf("no database ID")
}
return &DB{
ID: info.ID,
ID: info.ID.Bytes,
Version: info.Version,
}, nil
}
2 changes: 1 addition & 1 deletion swo/swoinfo/scansequences.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"sort"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/target/goalert/swo/swodb"
)

Expand Down
2 changes: 1 addition & 1 deletion swo/swoinfo/scantables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"sort"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/target/goalert/swo/swodb"
)

Expand Down
Loading

0 comments on commit 0cd7d32

Please sign in to comment.