From d2186ed58d907a81648aa62ffa17ec20fb2d213e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 18 Aug 2024 14:51:00 -0400 Subject: GH-4: Add configurable migrations table name (#6) * Add configurable migrations table name * Update codecov to be informational * Add mockdb and couple of extra tests * remove extra files * Add list query error test * Add readme update for custom migration table --- internal/configloader/configloader.go | 16 +++++++---- internal/configloader/configloader_test.go | 46 ++++++++++++++++++------------ 2 files changed, 38 insertions(+), 24 deletions(-) (limited to 'internal/configloader') diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index c13977a..0f724a6 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -54,12 +54,13 @@ type MigrationsConfig struct { } type ConnectionConfig struct { - Host string `toml:"host" envconfig:"BOLT_DB_HOST"` - Port string `toml:"port" envconfig:"BOLT_DB_PORT"` - User string `toml:"user" envconfig:"BOLT_DB_USER"` - Password string `toml:"password" envconfig:"BOLT_DB_PASSWORD"` - DBName string `toml:"dbname" envconfig:"BOLT_DB_NAME"` - Driver string `toml:"driver" envconfig:"BOLT_DB_DRIVER"` + Host string `toml:"host" envconfig:"BOLT_DB_HOST"` + Port string `toml:"port" envconfig:"BOLT_DB_PORT"` + User string `toml:"user" envconfig:"BOLT_DB_USER"` + Password string `toml:"password" envconfig:"BOLT_DB_PASSWORD"` + DBName string `toml:"dbname" envconfig:"BOLT_DB_NAME"` + Driver string `toml:"driver" envconfig:"BOLT_DB_DRIVER"` + MigrationsTable string `toml:"migrations_table" envconfig:"BOLT_DB_MIGRATIONS_TABLE"` } func NewConfig() (*Config, error) { @@ -73,6 +74,9 @@ func NewConfig() (*Config, error) { DirectoryPath: "migrations", VersionStyle: VersionStyleTimestamp, }, + Connection: ConnectionConfig{ + MigrationsTable: "bolt_migrations", + }, } if !errors.Is(err, ErrConfigFileNotFound) { _, err = toml.DecodeFile(filePath, &cfg) diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go index 89c4368..352f243 100644 --- a/internal/configloader/configloader_test.go +++ b/internal/configloader/configloader_test.go @@ -19,6 +19,7 @@ func TestNewConfigDefaults(t *testing.T) { check.Equal(t, cfg.Migrations.DirectoryPath, "migrations") check.Equal(t, cfg.Migrations.VersionStyle, configloader.VersionStyleTimestamp) + check.Equal(t, cfg.Connection.MigrationsTable, "bolt_migrations") } func TestNewConfigWithInvalidVersionStyle(t *testing.T) { @@ -41,6 +42,7 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { bolttest.UnsetEnv(t, "BOLT_DB_PASSWORD") bolttest.UnsetEnv(t, "BOLT_DB_NAME") 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{ @@ -49,12 +51,13 @@ func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) { VersionStyle: configloader.VersionStyleSequential, }, Connection: configloader.ConnectionConfig{ - Host: "testhost", - Port: "1234", - User: "testuser", - Password: "testpassword", - DBName: "testdb", - Driver: "postgresql", + Host: "testhost", + Port: "1234", + User: "testuser", + Password: "testpassword", + DBName: "testdb", + Driver: "postgresql", + MigrationsTable: "test_table", }, } tmpdir := t.TempDir() @@ -74,12 +77,13 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { VersionStyle: configloader.VersionStyleSequential, }, Connection: configloader.ConnectionConfig{ - Host: "testhost", - Port: "1234", - User: "testuser", - Password: "testpassword", - DBName: "testdb", - Driver: "mysql", + Host: "testhost", + Port: "1234", + User: "testuser", + Password: "testpassword", + DBName: "testdb", + Driver: "mysql", + MigrationsTable: "test_table", }, } bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml") @@ -90,12 +94,13 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { VersionStyle: configloader.VersionStyleTimestamp, }, Connection: configloader.ConnectionConfig{ - Host: "envtesthost", - Port: "4321", - User: "envtestuser", - Password: "envtestpassword", - DBName: "envtestdb", - Driver: "postgresql", + Host: "envtesthost", + Port: "4321", + User: "envtestuser", + Password: "envtestpassword", + DBName: "envtestdb", + Driver: "postgresql", + MigrationsTable: "different_table", }, } t.Setenv("BOLT_MIGRATIONS_VERSION_STYLE", string(envCfg.Migrations.VersionStyle)) @@ -106,6 +111,7 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) { t.Setenv("BOLT_DB_PASSWORD", envCfg.Connection.Password) t.Setenv("BOLT_DB_NAME", envCfg.Connection.DBName) t.Setenv("BOLT_DB_DRIVER", envCfg.Connection.Driver) + t.Setenv("BOLT_DB_MIGRATIONS_TABLE", envCfg.Connection.MigrationsTable) cfg, err := configloader.NewConfig() assert.Nil(t, err) @@ -119,6 +125,7 @@ func TestNewConfigSearchesParentDirectories(t *testing.T) { bolttest.UnsetEnv(t, "BOLT_DB_PASSWORD") bolttest.UnsetEnv(t, "BOLT_DB_NAME") 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{ @@ -126,6 +133,9 @@ func TestNewConfigSearchesParentDirectories(t *testing.T) { DirectoryPath: "differentmigrationsdir", VersionStyle: configloader.VersionStyleSequential, }, + Connection: configloader.ConnectionConfig{ + MigrationsTable: "migration_table", + }, } tmpdir := t.TempDir() bolttest.CreateConfigFile(t, &expectedCfg, filepath.Join(tmpdir, "bolt.toml")) -- cgit v1.2.3