From 5264264333f211c9d40108955e167d28fa73f77d Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 23 Jul 2025 21:39:11 -0400 Subject: Use DSN for handling connection parameters --- internal/configloader/configloader.go | 8 ++----- internal/configloader/configloader_test.go | 37 ++++++------------------------ 2 files changed, 9 insertions(+), 36 deletions(-) (limited to 'internal/configloader') diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index 842dcfb..81a9564 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -55,12 +55,8 @@ type MigrationsConfig struct { } type ConnectionConfig struct { - Host string `toml:"host" env:"HOST"` - Port string `toml:"port" env:"PORT"` - User string `toml:"user" env:"USER"` - Password string `toml:"password" env:"PASSWORD"` - DBName string `toml:"dbname" env:"NAME"` - Driver string `toml:"driver" env:"DRIVER"` + DSN string `toml:"dsn" env:"DSN"` + Driver string `toml:"driver" env:"DRIVER"` MigrationsTable string `toml:"migrations_table" env:"MIGRATIONS_TABLE"` } diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index f988afb..ce738e6 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -36,26 +36,19 @@ func TestNewConfigWithInvalidVersionStyle(t *testing.T) { } func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { - bolttest.UnsetEnv(t, "BOLT_DB_HOST") - bolttest.UnsetEnv(t, "BOLT_DB_PORT") - bolttest.UnsetEnv(t, "BOLT_DB_USER") - bolttest.UnsetEnv(t, "BOLT_DB_PASSWORD") - bolttest.UnsetEnv(t, "BOLT_DB_NAME") + bolttest.UnsetEnv(t, "BOLT_DB_DSN") bolttest.UnsetEnv(t, "BOLT_DB_DRIVER") bolttest.UnsetEnv(t, "BOLT_DB_MIGRATIONS_TABLE") bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH") bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_VERSION_STYLE") + expectedCfg := configloader.Config{ Migrations: configloader.MigrationsConfig{ DirectoryPath: "myfancymigrations", VersionStyle: configloader.VersionStyleSequential, }, Connection: configloader.ConnectionConfig{ - Host: "testhost", - Port: "1234", - User: "testuser", - Password: "testpassword", - DBName: "testdb", + DSN: "postgresql://testuser:testpassword@testhost:1234/testdb", Driver: "postgresql", MigrationsTable: "test_table", }, @@ -77,11 +70,7 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { VersionStyle: configloader.VersionStyleSequential, }, Connection: configloader.ConnectionConfig{ - Host: "testhost", - Port: "1234", - User: "testuser", - Password: "testpassword", - DBName: "testdb", + DSN: "mysql://testuser:testpassword@testhost:1234/testdb", Driver: "mysql", MigrationsTable: "test_table", }, @@ -94,22 +83,14 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { VersionStyle: configloader.VersionStyleTimestamp, }, Connection: configloader.ConnectionConfig{ - Host: "envtesthost", - Port: "4321", - User: "envtestuser", - Password: "envtestpassword", - DBName: "envtestdb", + DSN: "postgresql://envtestuser:envtestpassword@envtesthost:4321/envtestdb", Driver: "postgresql", MigrationsTable: "different_table", }, } t.Setenv("BOLT_MIGRATIONS_VERSION_STYLE", string(envCfg.Migrations.VersionStyle)) t.Setenv("BOLT_MIGRATIONS_DIR_PATH", envCfg.Migrations.DirectoryPath) - t.Setenv("BOLT_DB_HOST", envCfg.Connection.Host) - t.Setenv("BOLT_DB_PORT", envCfg.Connection.Port) - t.Setenv("BOLT_DB_USER", envCfg.Connection.User) - t.Setenv("BOLT_DB_PASSWORD", envCfg.Connection.Password) - t.Setenv("BOLT_DB_NAME", envCfg.Connection.DBName) + t.Setenv("BOLT_DB_DSN", envCfg.Connection.DSN) t.Setenv("BOLT_DB_DRIVER", envCfg.Connection.Driver) t.Setenv("BOLT_DB_MIGRATIONS_TABLE", envCfg.Connection.MigrationsTable) @@ -119,11 +100,7 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { } func TestNewConfigSearchesParentDirectories(t *testing.T) { - bolttest.UnsetEnv(t, "BOLT_DB_HOST") - bolttest.UnsetEnv(t, "BOLT_DB_PORT") - bolttest.UnsetEnv(t, "BOLT_DB_USER") - bolttest.UnsetEnv(t, "BOLT_DB_PASSWORD") - bolttest.UnsetEnv(t, "BOLT_DB_NAME") + bolttest.UnsetEnv(t, "BOLT_DB_DSN") bolttest.UnsetEnv(t, "BOLT_DB_DRIVER") bolttest.UnsetEnv(t, "BOLT_DB_MIGRATIONS_TABLE") bolttest.UnsetEnv(t, "BOLT_MIGRATIONS_DIR_PATH") -- cgit v1.2.3