Skip to content

Commit

Permalink
chore: switch pq to pgx (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Oct 17, 2024
1 parent d05fb4b commit 22db708
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bun/bunconnect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func OpenSQLDB(ctx context.Context, options ConnectionOptions, hooks ...bun.Quer
)
if options.Connector == nil {
logging.FromContext(ctx).Debugf("Opening database with default connector and dsn: '%s'", options.DatabaseSourceName)
sqldb, err = otelsql.Open("postgres", options.DatabaseSourceName)
sqldb, err = otelsql.Open("pgx", options.DatabaseSourceName)
if err != nil {
return nil, err
}
Expand Down
13 changes: 11 additions & 2 deletions bun/bunconnect/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package bunconnect
import (
"context"
"database/sql/driver"
"fmt"
"time"

"github.com/lib/pq"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,7 +55,14 @@ func ConnectionOptionsFromFlags(cmd *cobra.Command) (*ConnectionOptions, error)
}
} else {
connector = func(dsn string) (driver.Connector, error) {
return pq.NewConnector(dsn)
parseConfig, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, err
}
if err != nil {
return nil, fmt.Errorf("failed to parse dsn: %w", err)
}
return stdlib.GetConnector(*parseConfig), nil
}
}

Expand Down
12 changes: 8 additions & 4 deletions bun/bunconnect/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (
"database/sql/driver"
"fmt"

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

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/rds/auth"
"github.com/formancehq/go-libs/v2/logging"
_ "github.com/go-sql-driver/mysql"
"github.com/lib/pq"
"github.com/pkg/errors"
"github.com/xo/dburl"
)
Expand Down Expand Up @@ -65,12 +67,14 @@ func (i *iamConnector) Connect(ctx context.Context) (driver.Conn, error) {

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

pqConnector, err := pq.NewConnector(dsn)
config, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to parse dsn: %w", err)
}

return pqConnector.Connect(ctx)
connector := stdlib.GetConnector(*config)

return connector.Connect(ctx)
}

func (i iamConnector) Driver() driver.Driver {
Expand Down
2 changes: 1 addition & 1 deletion bun/bunmigrate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/uptrace/bun"

// Import the postgres driver.
_ "github.com/lib/pq"
_ "github.com/jackc/pgx/v5/stdlib"
)

type Executor func(cmd *cobra.Command, args []string, db *bun.DB) error
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ require (
github.com/invopop/jsonschema v0.12.0
github.com/jackc/pgx/v5 v5.7.1
github.com/lestrrat-go/jwx v1.2.30
github.com/lib/pq v1.10.9
github.com/nats-io/nats-server/v2 v2.10.21
github.com/nats-io/nats.go v1.37.0
github.com/olivere/elastic/v7 v7.0.32
Expand Down
5 changes: 2 additions & 3 deletions migrations/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"fmt"

"github.com/formancehq/go-libs/v2/time"

"github.com/lib/pq"
"github.com/jackc/pgx/v5/pgconn"

"github.com/pkg/errors"
"github.com/uptrace/bun"
Expand Down Expand Up @@ -100,7 +99,7 @@ func (m *Migrator) getLastVersion(ctx context.Context, querier interface {
return -1, nil
default:
switch err := err.(type) {
case *pq.Error:
case *pgconn.PgError:
switch err.Code {
case "42P01": // Table not exists
return -1, ErrMissingVersionTable
Expand Down
10 changes: 5 additions & 5 deletions platform/postgres/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package postgres
import (
"database/sql"

"github.com/lib/pq"
"github.com/jackc/pgx/v5/pgconn"
"github.com/pkg/errors"
)

Expand All @@ -15,7 +15,7 @@ func ResolveError(err error) error {
}

switch pge := err.(type) {
case *pq.Error:
case *pgconn.PgError:
switch pge.Code {
case "23505":
return newErrConstraintsFailed(pge)
Expand Down Expand Up @@ -45,7 +45,7 @@ func IsNotFoundError(err error) bool {
}

type ErrConstraintsFailed struct {
err *pq.Error
err *pgconn.PgError
}

func (e ErrConstraintsFailed) Error() string {
Expand All @@ -62,10 +62,10 @@ func (e ErrConstraintsFailed) Unwrap() error {
}

func (e ErrConstraintsFailed) GetConstraint() string {
return e.err.Constraint
return e.err.ConstraintName
}

func newErrConstraintsFailed(err *pq.Error) ErrConstraintsFailed {
func newErrConstraintsFailed(err *pgconn.PgError) ErrConstraintsFailed {
return ErrConstraintsFailed{
err: err,
}
Expand Down
1 change: 0 additions & 1 deletion testing/platform/clickhousetesting/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

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

_ "github.com/lib/pq"
"github.com/stretchr/testify/require"
)

Expand Down
9 changes: 4 additions & 5 deletions testing/platform/pgtesting/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/formancehq/go-libs/v2/testing/docker"
"github.com/google/uuid"
_ "github.com/lib/pq"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -43,7 +42,7 @@ func (s *Database) ConnectionOptions() bunconnect.ConnectionOptions {
}

func (s *Database) Delete() {
db, err := sql.Open("postgres", s.rootUrl)
db, err := sql.Open("pgx", s.rootUrl)
require.NoError(s.t, err)
defer func() {
require.NoError(s.t, db.Close())
Expand Down Expand Up @@ -92,7 +91,7 @@ func (s *PostgresServer) GetDatabaseDSN(databaseName string) string {
}

func (s *PostgresServer) setupDatabase(t T, name string) {
db, err := sql.Open("postgres", s.GetDatabaseDSN(name))
db, err := sql.Open("pgx", s.GetDatabaseDSN(name))
require.NoError(t, err)
defer func() {
require.NoError(t, db.Close())
Expand All @@ -105,7 +104,7 @@ func (s *PostgresServer) setupDatabase(t T, name string) {
}

func (s *PostgresServer) NewDatabase(t T) *Database {
db, err := sql.Open("postgres", s.GetDSN())
db, err := sql.Open("pgx", s.GetDSN())
require.NoError(t, err)
defer func() {
require.Nil(t, db.Close())
Expand Down Expand Up @@ -240,7 +239,7 @@ func CreatePostgresServer(t T, pool *docker.Pool, opts ...Option) *PostgresServe
resource.GetPort("5432/tcp"),
cfg.InitialDatabaseName,
)
db, err := sql.Open("postgres", dsn)
db, err := sql.Open("pgx", dsn)
if err != nil {
return errors.Wrap(err, "opening database")
}
Expand Down

0 comments on commit 22db708

Please sign in to comment.