aboutsummaryrefslogtreecommitdiffstats
path: root/internal
diff options
context:
space:
mode:
Diffstat (limited to 'internal')
-rw-r--r--internal/storage/db.go15
-rw-r--r--internal/storage/db_test.go5
2 files changed, 14 insertions, 6 deletions
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) {