From 263ce3e4b368a50efee1a53af11a76a12af0a119 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 6 Jan 2024 16:23:02 -0500 Subject: Break up migration repo into fs and db with a service --- internal/storage/db.go | 41 ++++++++++++++++++++++ internal/storage/db_test.go | 55 ++++++++++++++++++++++++++++++ internal/storage/dbtest/dbtest.go | 24 +++++++++++++ internal/storage/storage.go | 41 ---------------------- internal/storage/storage_test.go | 72 --------------------------------------- 5 files changed, 120 insertions(+), 113 deletions(-) create mode 100644 internal/storage/db.go create mode 100644 internal/storage/db_test.go create mode 100644 internal/storage/dbtest/dbtest.go delete mode 100644 internal/storage/storage.go delete mode 100644 internal/storage/storage_test.go (limited to 'internal/storage') diff --git a/internal/storage/db.go b/internal/storage/db.go new file mode 100644 index 0000000..25db283 --- /dev/null +++ b/internal/storage/db.go @@ -0,0 +1,41 @@ +package storage + +import ( + "database/sql" + "fmt" + + "github.com/eugenetriguba/bolt/internal/configloader" + _ "github.com/lib/pq" +) + +// DBConnect establishes a connection to the database using the driver +// and connection information. +// +// Note that only "postgres" is supported as the driver right now. +func DBConnect(driver string, connectionInfo string) (*sql.DB, error) { + db, err := sql.Open(driver, connectionInfo) + if err != nil { + return nil, 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. + err = db.Ping() + if err != nil { + return nil, err + } + + return db, nil +} + +// DBConnectionString generates a properly formatted connection +// string that can be used to establish a database connection. +// Note that it always creates a connection string with sslmode disabled. +func DBConnectionString(cfg *configloader.ConnectionConfig) string { + return fmt.Sprintf( + "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", + cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, + ) +} diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go new file mode 100644 index 0000000..2583cfd --- /dev/null +++ b/internal/storage/db_test.go @@ -0,0 +1,55 @@ +package storage_test + +import ( + "fmt" + "testing" + + "github.com/eugenetriguba/bolt/internal/configloader" + "github.com/eugenetriguba/bolt/internal/storage" + "github.com/eugenetriguba/bolt/internal/storage/dbtest" + "gotest.tools/v3/assert" +) + +func TestDBConnectWithPostgres(t *testing.T) { + cfg, err := dbtest.NewTestConnectionConfig("postgres") + assert.NilError(t, err) + + conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + assert.NilError(t, err) + defer conn.Close() + + _, err = conn.Exec("SELECT 1;") + assert.NilError(t, err) +} + +func TestDBConnectMalformedConnectionString(t *testing.T) { + _, err := storage.DBConnect("postgres", "pizza=123") + assert.ErrorContains(t, err, "connection refused") +} + +func TestDBConnectUnsupportedDriver(t *testing.T) { + cfg, err := dbtest.NewTestConnectionConfig("redis") + assert.NilError(t, err) + + _, err = storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + assert.ErrorContains(t, err, `unknown driver "redis"`) +} + +func TestDBConnectionString(t *testing.T) { + cfg := configloader.ConnectionConfig{ + Driver: "postgres", + Host: "db1", + Port: 5432, + User: "testuser", + Password: "supersecretpassword", + DBName: "testdb", + } + + cs := storage.DBConnectionString(&cfg) + + expectedConnectionString := fmt.Sprintf( + "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", + cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, + ) + assert.Equal(t, cs, expectedConnectionString) +} diff --git a/internal/storage/dbtest/dbtest.go b/internal/storage/dbtest/dbtest.go new file mode 100644 index 0000000..c53c04a --- /dev/null +++ b/internal/storage/dbtest/dbtest.go @@ -0,0 +1,24 @@ +package dbtest + +import ( + "os" + "strconv" + + "github.com/eugenetriguba/bolt/internal/configloader" +) + +func NewTestConnectionConfig(driver string) (*configloader.ConnectionConfig, error) { + port, err := strconv.Atoi(os.Getenv("DB_PORT")) + if err != nil { + return nil, err + } + + return &configloader.ConnectionConfig{ + Driver: driver, + DBName: os.Getenv("DB_NAME"), + Host: os.Getenv("DB_HOST"), + Port: port, + User: os.Getenv("DB_USERNAME"), + Password: os.Getenv("DB_PASSWORD"), + }, nil +} diff --git a/internal/storage/storage.go b/internal/storage/storage.go deleted file mode 100644 index 25db283..0000000 --- a/internal/storage/storage.go +++ /dev/null @@ -1,41 +0,0 @@ -package storage - -import ( - "database/sql" - "fmt" - - "github.com/eugenetriguba/bolt/internal/configloader" - _ "github.com/lib/pq" -) - -// DBConnect establishes a connection to the database using the driver -// and connection information. -// -// Note that only "postgres" is supported as the driver right now. -func DBConnect(driver string, connectionInfo string) (*sql.DB, error) { - db, err := sql.Open(driver, connectionInfo) - if err != nil { - return nil, 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. - err = db.Ping() - if err != nil { - return nil, err - } - - return db, nil -} - -// DBConnectionString generates a properly formatted connection -// string that can be used to establish a database connection. -// Note that it always creates a connection string with sslmode disabled. -func DBConnectionString(cfg *configloader.ConnectionConfig) string { - return fmt.Sprintf( - "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", - cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, - ) -} diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go deleted file mode 100644 index 3b36a34..0000000 --- a/internal/storage/storage_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package storage_test - -import ( - "fmt" - "strings" - "testing" - - "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/bolt/internal/dbtest" - "github.com/eugenetriguba/bolt/internal/storage" -) - -func TestDBConnectWithPostgres(t *testing.T) { - cfg, err := dbtest.NewTestConnectionConfig("postgres") - if err != nil { - t.Fatal(err) - } - - conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - if err != nil { - t.Fatalf("could not connect to postgres db using %s: %s", - storage.DBConnectionString(cfg), - err, - ) - } - defer conn.Close() - - _, err = conn.Exec("SELECT 1;") - if err != nil { - t.Fatalf("could not execute query against postgres db: %s", err) - } -} - -func TestDBConnectMalformedConnectionString(t *testing.T) { - _, err := storage.DBConnect("postgres", "pizza=123") - if err == nil || (err != nil && !strings.Contains(err.Error(), "connection refused")) { - t.Fatalf("expected connection error, got %s", err) - } -} - -func TestDBConnectUnsupportedDriver(t *testing.T) { - cfg, err := dbtest.NewTestConnectionConfig("redis") - if err != nil { - t.Fatal(err) - } - - _, err = storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - if err == nil || (err != nil && !strings.Contains(err.Error(), `unknown driver "redis"`)) { - t.Fatalf("expected bad driver, got %s", err) - } -} - -func TestDBConnectionString(t *testing.T) { - cfg := configloader.ConnectionConfig{ - Driver: "postgres", - Host: "db1", - Port: 5432, - User: "testuser", - Password: "supersecretpassword", - DBName: "testdb", - } - expectedConnectionString := fmt.Sprintf( - "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", - cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, - ) - - cs := storage.DBConnectionString(&cfg) - - if cs != expectedConnectionString { - t.Fatalf("Expected %s, got %s", expectedConnectionString, cs) - } -} -- cgit v1.2.3