From 8975f7e2fc681a0171f20b1f76be579bf76a1614 Mon Sep 17 00:00:00 2001 From: madhavilosetty-intel Date: Thu, 9 Jan 2025 08:26:00 -0800 Subject: [PATCH] fix: reads db url from config if not in env --- cmd/app/main.go | 2 +- cmd/app/main_test.go | 2 +- config/config.go | 1 + config/config.yml | 1 + internal/app/migrate.go | 7 ++++--- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cmd/app/main.go b/cmd/app/main.go index 35d7a14e..b4b3de04 100644 --- a/cmd/app/main.go +++ b/cmd/app/main.go @@ -26,7 +26,7 @@ func main() { log.Fatalf("Config error: %s", err) } - err = initializeAppFunc() + err = initializeAppFunc(cfg) if err != nil { log.Fatalf("App init error: %s", err) } diff --git a/cmd/app/main_test.go b/cmd/app/main_test.go index 985985f5..f88805d6 100644 --- a/cmd/app/main_test.go +++ b/cmd/app/main_test.go @@ -28,7 +28,7 @@ func TestMainFunction(_ *testing.T) { //nolint:paralleltest // cannot have simul return &config.Config{HTTP: config.HTTP{Port: "8080"}, App: config.App{EncryptionKey: "test"}}, nil } - initializeAppFunc = func() error { + initializeAppFunc = func(_ *config.Config) error { return nil } diff --git a/config/config.go b/config/config.go index 32b09ca7..71c7cb1e 100644 --- a/config/config.go +++ b/config/config.go @@ -90,6 +90,7 @@ func NewConfig() (*Config, error) { }, DB: DB{ PoolMax: 2, + URL: "", }, EA: EA{ URL: "http://localhost:8000", diff --git a/config/config.yml b/config/config.yml index 72327178..5fc2377d 100644 --- a/config/config.yml +++ b/config/config.yml @@ -19,6 +19,7 @@ logger: postgres: pool_max: 5 + url: "" ea: url: "http://localhost:8000" diff --git a/internal/app/migrate.go b/internal/app/migrate.go index d700965c..37414eb5 100644 --- a/internal/app/migrate.go +++ b/internal/app/migrate.go @@ -17,6 +17,7 @@ import ( "github.com/golang-migrate/migrate/v4/source" _ "github.com/golang-migrate/migrate/v4/source/file" // for file source "github.com/golang-migrate/migrate/v4/source/iofs" + "github.com/open-amt-cloud-toolkit/console/config" _ "modernc.org/sqlite" // sqlite3 driver ) @@ -35,9 +36,9 @@ func MigrationError(op string) error { return fmt.Errorf("%w: %s", errMigrate, op) } -func Init() error { - databaseURL, ok := os.LookupEnv("DB_URL") - if !ok || databaseURL == "" { +func Init(cfg *config.Config) error { + databaseURL := cfg.DB.URL + if databaseURL == "" { log.Printf("migrate: environment variable not declared: DB_URL -- using embedded database") }