From ad844342baae1b28907a7549f8d03041e088eb5e Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Thu, 17 Oct 2024 12:14:32 +0200 Subject: [PATCH] chore: switch pq to pgx --- bun/bunconnect/connect.go | 2 +- bun/bunconnect/flags.go | 13 +++++++++++-- bun/bunconnect/iam.go | 12 ++++++++---- bun/bunmigrate/command.go | 2 +- go.mod | 1 - migrations/migrator.go | 5 ++--- platform/postgres/errors.go | 10 +++++----- testing/platform/clickhousetesting/clickhouse.go | 1 - testing/platform/pgtesting/postgres.go | 9 ++++----- 9 files changed, 32 insertions(+), 23 deletions(-) diff --git a/bun/bunconnect/connect.go b/bun/bunconnect/connect.go index 94b935d4..c1ca27ad 100644 --- a/bun/bunconnect/connect.go +++ b/bun/bunconnect/connect.go @@ -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 } diff --git a/bun/bunconnect/flags.go b/bun/bunconnect/flags.go index a3357f2c..fd339eea 100644 --- a/bun/bunconnect/flags.go +++ b/bun/bunconnect/flags.go @@ -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" @@ -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 } } diff --git a/bun/bunconnect/iam.go b/bun/bunconnect/iam.go index 0821e92b..22e8024c 100644 --- a/bun/bunconnect/iam.go +++ b/bun/bunconnect/iam.go @@ -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" ) @@ -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 { diff --git a/bun/bunmigrate/command.go b/bun/bunmigrate/command.go index 25b1db21..1e043cb6 100644 --- a/bun/bunmigrate/command.go +++ b/bun/bunmigrate/command.go @@ -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 diff --git a/go.mod b/go.mod index 6c3810dd..13c64dca 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/migrations/migrator.go b/migrations/migrator.go index 48940ade..5fce32dd 100644 --- a/migrations/migrator.go +++ b/migrations/migrator.go @@ -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" @@ -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 diff --git a/platform/postgres/errors.go b/platform/postgres/errors.go index 734bcb3d..35e45925 100644 --- a/platform/postgres/errors.go +++ b/platform/postgres/errors.go @@ -3,7 +3,7 @@ package postgres import ( "database/sql" - "github.com/lib/pq" + "github.com/jackc/pgx/v5/pgconn" "github.com/pkg/errors" ) @@ -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) @@ -45,7 +45,7 @@ func IsNotFoundError(err error) bool { } type ErrConstraintsFailed struct { - err *pq.Error + err *pgconn.PgError } func (e ErrConstraintsFailed) Error() string { @@ -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, } diff --git a/testing/platform/clickhousetesting/clickhouse.go b/testing/platform/clickhousetesting/clickhouse.go index 094cc2b7..8b651885 100644 --- a/testing/platform/clickhousetesting/clickhouse.go +++ b/testing/platform/clickhousetesting/clickhouse.go @@ -13,7 +13,6 @@ import ( "github.com/formancehq/go-libs/v2/testing/docker" - _ "github.com/lib/pq" "github.com/stretchr/testify/require" ) diff --git a/testing/platform/pgtesting/postgres.go b/testing/platform/pgtesting/postgres.go index 24876edb..631ff64d 100644 --- a/testing/platform/pgtesting/postgres.go +++ b/testing/platform/pgtesting/postgres.go @@ -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" ) @@ -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()) @@ -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()) @@ -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()) @@ -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") }