diff options
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/db.go | 27 | ||||
| -rw-r--r-- | internal/storage/db_test.go | 14 |
2 files changed, 27 insertions, 14 deletions
diff --git a/internal/storage/db.go b/internal/storage/db.go index 9032dc3..a9e35af 100644 --- a/internal/storage/db.go +++ b/internal/storage/db.go @@ -2,29 +2,40 @@ package storage import ( "database/sql" + "errors" "fmt" "github.com/eugenetriguba/bolt/internal/configloader" _ "github.com/lib/pq" ) +var ( + ErrMalformedConnectionString = errors.New( + "malformed database connection parameters provided", + ) + ErrUnableToConnect = errors.New("unable to open connection to database") +) + // DBConnect establishes a connection to the database using the driver -// and connection information. +// and connection information. Only "postgres" is supported as the driver. // -// Note that only "postgres" is supported as the driver right now. +// The following errors may be returned: +// - ErrMalformedConnectionString: The provided connection parameters are +// not in a valid format. +// - ErrUnableToConnect: Unable to make a connection to the database with +// the provided connection parameters. func DBConnect(driver string, connectionParams string) (*sql.DB, error) { db, err := sql.Open(driver, connectionParams) if err != nil { - return nil, err + return nil, fmt.Errorf("%w: %v", ErrMalformedConnectionString, err) } - // Note: `sql.Open` only validates the connection string we provided - // it is sane. It doesn't actually open up a connection to the database. - // For that, we ping the database to ensure the connection string is fully - // valid. + // Note: `sql.Open` only validates the connection string we provided is sane. + // It doesn't actually open up a connection to the database. For that, we ping + // the database to ensure the connection string is fully valid. err = db.Ping() if err != nil { - return nil, err + return nil, fmt.Errorf("%w: %v", ErrUnableToConnect, err) } return db, nil diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index d9e22d0..e85cc57 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -7,29 +7,31 @@ import ( "github.com/eugenetriguba/bolt/internal/bolttest" "github.com/eugenetriguba/bolt/internal/configloader" "github.com/eugenetriguba/bolt/internal/storage" - "gotest.tools/v3/assert" + "github.com/eugenetriguba/checkmate/assert" ) -func TestDBConnectWithPostgres(t *testing.T) { +func TestDBConnect_Success(t *testing.T) { cfg := bolttest.NewTestConnectionConfig(t, "postgres") conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - assert.NilError(t, err) + assert.Nil(t, err) defer conn.Close() _, err = conn.Exec("SELECT 1;") - assert.NilError(t, err) + assert.Nil(t, err) } -func TestDBConnectMalformedConnectionString(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") } -func TestDBConnectUnsupportedDriver(t *testing.T) { +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"`) } |