diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-09 10:41:46 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-09 10:41:46 -0500 |
| commit | 64b73944a6849c0b376bffa33b44f9ee6ac20a51 (patch) | |
| tree | 55c55aaf83eb829d1c522ce173bf1d1edd8de019 | |
| parent | Release v0.3.0 (diff) | |
| download | cli-64b73944a6849c0b376bffa33b44f9ee6ac20a51.zip | |
Replace lib/pq with pgx
| -rw-r--r-- | CHANGELOG.md | 6 | ||||
| -rw-r--r-- | go.mod | 12 | ||||
| -rw-r--r-- | go.sum | 32 | ||||
| -rw-r--r-- | internal/storage/db.go | 15 | ||||
| -rw-r--r-- | internal/storage/db_test.go | 5 |
5 files changed, 58 insertions, 12 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 88417e7..6f15e86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +### Changed + +- Replaced the lib/pq postgres driver with pgx. + ## [0.3.0] - 2024-03-02 ### Added @@ -6,8 +6,16 @@ require ( github.com/BurntSushi/toml v1.3.2 github.com/eugenetriguba/checkmate v0.3.2 github.com/google/subcommands v1.2.0 + github.com/jackc/pgx/v5 v5.5.4 github.com/kelseyhightower/envconfig v1.4.0 - github.com/lib/pq v1.10.9 ) -require github.com/google/go-cmp v0.6.0 // indirect +require ( + github.com/google/go-cmp v0.6.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect + golang.org/x/crypto v0.17.0 // indirect + golang.org/x/sync v0.1.0 // indirect + golang.org/x/text v0.14.0 // indirect +) @@ -1,14 +1,38 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -github.com/eugenetriguba/checkmate v0.3.0 h1:JqeefDNUGga9VG+jusq2UlVKCFKBlRN1p5JQbl1CmLI= -github.com/eugenetriguba/checkmate v0.3.0/go.mod h1:IY38Cl4zlNI1dg+wcDYwrbWZfJ9i2/ouWf8C0N8c/do= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eugenetriguba/checkmate v0.3.2 h1:vcALwpLLpt0FBxckjuobNFYtBLtlCT2p55Xk4p0SAhk= github.com/eugenetriguba/checkmate v0.3.2/go.mod h1:IY38Cl4zlNI1dg+wcDYwrbWZfJ9i2/ouWf8C0N8c/do= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +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/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8= +github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= -github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= -github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= +golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/storage/db.go b/internal/storage/db.go index a9e35af..d83e71d 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -4,16 +4,20 @@ import ( "database/sql" "errors" "fmt" + "slices" "github.com/eugenetriguba/bolt/internal/configloader" - _ "github.com/lib/pq" + _ "github.com/jackc/pgx/v5/stdlib" ) +var supportedDrivers = []string{"postgres"} + var ( ErrMalformedConnectionString = errors.New( "malformed database connection parameters provided", ) - ErrUnableToConnect = errors.New("unable to open connection to database") + ErrUnableToConnect = errors.New("unable to open connection to database") + ErrUnsupportedDriver = fmt.Errorf("unsupported driver, supported drivers are %s", supportedDrivers) ) // DBConnect establishes a connection to the database using the driver @@ -24,8 +28,13 @@ var ( // not in a valid format. // - ErrUnableToConnect: Unable to make a connection to the database with // the provided connection parameters. +// - ErrUnsupportedDriver: The provided driver is not supported. func DBConnect(driver string, connectionParams string) (*sql.DB, error) { - db, err := sql.Open(driver, connectionParams) + if !slices.Contains(supportedDrivers, driver) { + return nil, ErrUnsupportedDriver + } + + db, err := sql.Open("pgx", connectionParams) if err != nil { return nil, fmt.Errorf("%w: %v", ErrMalformedConnectionString, err) } diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index e85cc57..eaf9dfe 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -24,15 +24,14 @@ func TestDBConnect_Success(t *testing.T) { func TestDBConnect_BadConnectionString(t *testing.T) { _, err := storage.DBConnect("postgres", "pizza=123") assert.ErrorIs(t, err, storage.ErrUnableToConnect) - assert.ErrorContains(t, err, "connection refused") + assert.ErrorContains(t, err, "unable to open connection to database") } func TestDBConnect_UnsupportedDriver(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "redis") _, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - assert.ErrorIs(t, err, storage.ErrMalformedConnectionString) - assert.ErrorContains(t, err, `unknown driver "redis"`) + assert.ErrorIs(t, err, storage.ErrUnsupportedDriver) } func TestDBConnectionString(t *testing.T) { |