Skip to content

Commit 22db708

Browse files
authored
chore: switch pq to pgx (#138)
1 parent d05fb4b commit 22db708

File tree

9 files changed

+32
-23
lines changed

9 files changed

+32
-23
lines changed

bun/bunconnect/connect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func OpenSQLDB(ctx context.Context, options ConnectionOptions, hooks ...bun.Quer
3737
)
3838
if options.Connector == nil {
3939
logging.FromContext(ctx).Debugf("Opening database with default connector and dsn: '%s'", options.DatabaseSourceName)
40-
sqldb, err = otelsql.Open("postgres", options.DatabaseSourceName)
40+
sqldb, err = otelsql.Open("pgx", options.DatabaseSourceName)
4141
if err != nil {
4242
return nil, err
4343
}

bun/bunconnect/flags.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package bunconnect
33
import (
44
"context"
55
"database/sql/driver"
6+
"fmt"
67
"time"
78

8-
"github.com/lib/pq"
9+
"github.com/jackc/pgx/v5"
10+
"github.com/jackc/pgx/v5/stdlib"
911

1012
"github.com/pkg/errors"
1113
"github.com/spf13/cobra"
@@ -53,7 +55,14 @@ func ConnectionOptionsFromFlags(cmd *cobra.Command) (*ConnectionOptions, error)
5355
}
5456
} else {
5557
connector = func(dsn string) (driver.Connector, error) {
56-
return pq.NewConnector(dsn)
58+
parseConfig, err := pgx.ParseConfig(dsn)
59+
if err != nil {
60+
return nil, err
61+
}
62+
if err != nil {
63+
return nil, fmt.Errorf("failed to parse dsn: %w", err)
64+
}
65+
return stdlib.GetConnector(*parseConfig), nil
5766
}
5867
}
5968

bun/bunconnect/iam.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"database/sql/driver"
66
"fmt"
77

8+
"github.com/jackc/pgx/v5"
9+
"github.com/jackc/pgx/v5/stdlib"
10+
811
"github.com/aws/aws-sdk-go-v2/aws"
912
"github.com/aws/aws-sdk-go-v2/feature/rds/auth"
1013
"github.com/formancehq/go-libs/v2/logging"
1114
_ "github.com/go-sql-driver/mysql"
12-
"github.com/lib/pq"
1315
"github.com/pkg/errors"
1416
"github.com/xo/dburl"
1517
)
@@ -65,12 +67,14 @@ func (i *iamConnector) Connect(ctx context.Context) (driver.Conn, error) {
6567

6668
i.logger.Debugf("IAM: Connect using dsn '%s'", dsn)
6769

68-
pqConnector, err := pq.NewConnector(dsn)
70+
config, err := pgx.ParseConfig(dsn)
6971
if err != nil {
70-
return nil, err
72+
return nil, fmt.Errorf("failed to parse dsn: %w", err)
7173
}
7274

73-
return pqConnector.Connect(ctx)
75+
connector := stdlib.GetConnector(*config)
76+
77+
return connector.Connect(ctx)
7478
}
7579

7680
func (i iamConnector) Driver() driver.Driver {

bun/bunmigrate/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/uptrace/bun"
77

88
// Import the postgres driver.
9-
_ "github.com/lib/pq"
9+
_ "github.com/jackc/pgx/v5/stdlib"
1010
)
1111

1212
type Executor func(cmd *cobra.Command, args []string, db *bun.DB) error

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ require (
2525
github.com/invopop/jsonschema v0.12.0
2626
github.com/jackc/pgx/v5 v5.7.1
2727
github.com/lestrrat-go/jwx v1.2.30
28-
github.com/lib/pq v1.10.9
2928
github.com/nats-io/nats-server/v2 v2.10.21
3029
github.com/nats-io/nats.go v1.37.0
3130
github.com/olivere/elastic/v7 v7.0.32

migrations/migrator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import (
77
"fmt"
88

99
"github.com/formancehq/go-libs/v2/time"
10-
11-
"github.com/lib/pq"
10+
"github.com/jackc/pgx/v5/pgconn"
1211

1312
"github.com/pkg/errors"
1413
"github.com/uptrace/bun"
@@ -100,7 +99,7 @@ func (m *Migrator) getLastVersion(ctx context.Context, querier interface {
10099
return -1, nil
101100
default:
102101
switch err := err.(type) {
103-
case *pq.Error:
102+
case *pgconn.PgError:
104103
switch err.Code {
105104
case "42P01": // Table not exists
106105
return -1, ErrMissingVersionTable

platform/postgres/errors.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package postgres
33
import (
44
"database/sql"
55

6-
"github.com/lib/pq"
6+
"github.com/jackc/pgx/v5/pgconn"
77
"github.com/pkg/errors"
88
)
99

@@ -15,7 +15,7 @@ func ResolveError(err error) error {
1515
}
1616

1717
switch pge := err.(type) {
18-
case *pq.Error:
18+
case *pgconn.PgError:
1919
switch pge.Code {
2020
case "23505":
2121
return newErrConstraintsFailed(pge)
@@ -45,7 +45,7 @@ func IsNotFoundError(err error) bool {
4545
}
4646

4747
type ErrConstraintsFailed struct {
48-
err *pq.Error
48+
err *pgconn.PgError
4949
}
5050

5151
func (e ErrConstraintsFailed) Error() string {
@@ -62,10 +62,10 @@ func (e ErrConstraintsFailed) Unwrap() error {
6262
}
6363

6464
func (e ErrConstraintsFailed) GetConstraint() string {
65-
return e.err.Constraint
65+
return e.err.ConstraintName
6666
}
6767

68-
func newErrConstraintsFailed(err *pq.Error) ErrConstraintsFailed {
68+
func newErrConstraintsFailed(err *pgconn.PgError) ErrConstraintsFailed {
6969
return ErrConstraintsFailed{
7070
err: err,
7171
}

testing/platform/clickhousetesting/clickhouse.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/formancehq/go-libs/v2/testing/docker"
1515

16-
_ "github.com/lib/pq"
1716
"github.com/stretchr/testify/require"
1817
)
1918

testing/platform/pgtesting/postgres.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"github.com/formancehq/go-libs/v2/testing/docker"
1717
"github.com/google/uuid"
18-
_ "github.com/lib/pq"
1918
"github.com/pkg/errors"
2019
"github.com/stretchr/testify/require"
2120
)
@@ -43,7 +42,7 @@ func (s *Database) ConnectionOptions() bunconnect.ConnectionOptions {
4342
}
4443

4544
func (s *Database) Delete() {
46-
db, err := sql.Open("postgres", s.rootUrl)
45+
db, err := sql.Open("pgx", s.rootUrl)
4746
require.NoError(s.t, err)
4847
defer func() {
4948
require.NoError(s.t, db.Close())
@@ -92,7 +91,7 @@ func (s *PostgresServer) GetDatabaseDSN(databaseName string) string {
9291
}
9392

9493
func (s *PostgresServer) setupDatabase(t T, name string) {
95-
db, err := sql.Open("postgres", s.GetDatabaseDSN(name))
94+
db, err := sql.Open("pgx", s.GetDatabaseDSN(name))
9695
require.NoError(t, err)
9796
defer func() {
9897
require.NoError(t, db.Close())
@@ -105,7 +104,7 @@ func (s *PostgresServer) setupDatabase(t T, name string) {
105104
}
106105

107106
func (s *PostgresServer) NewDatabase(t T) *Database {
108-
db, err := sql.Open("postgres", s.GetDSN())
107+
db, err := sql.Open("pgx", s.GetDSN())
109108
require.NoError(t, err)
110109
defer func() {
111110
require.Nil(t, db.Close())
@@ -240,7 +239,7 @@ func CreatePostgresServer(t T, pool *docker.Pool, opts ...Option) *PostgresServe
240239
resource.GetPort("5432/tcp"),
241240
cfg.InitialDatabaseName,
242241
)
243-
db, err := sql.Open("postgres", dsn)
242+
db, err := sql.Open("pgx", dsn)
244243
if err != nil {
245244
return errors.Wrap(err, "opening database")
246245
}

0 commit comments

Comments
 (0)