From db7de4b31b1836abed822077840665639f0d66da Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 19 Jan 2024 23:27:27 -0500 Subject: Add ability to use sequential or timestamp-based versions --- internal/configloader/configloader.go | 47 +++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'internal/configloader/configloader.go') diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index c50ec24..c5aba2d 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -10,6 +10,7 @@ package configloader import ( "errors" + "fmt" "os" "path/filepath" "runtime" @@ -18,8 +19,21 @@ import ( "github.com/kelseyhightower/envconfig" ) -var errConfigFileNotFound = errors.New( - "bolt configuration file not found in current directory or any parent directories", +type VersionStyle string + +const ( + VersionStyleSequential VersionStyle = "sequential" + VersionStyleTimestamp VersionStyle = "timestamp" +) + +var ( + ErrConfigFileNotFound = errors.New( + "bolt configuration file not found in current directory or any parent directories", + ) + ErrInvalidVersionStyle = fmt.Errorf( + "invalid version style for bolt migrations. supported styles: %v", + []VersionStyle{VersionStyleSequential, VersionStyleTimestamp}, + ) ) // Config represents the application configuration settings. @@ -35,7 +49,16 @@ type Config struct { } type MigrationsConfig struct { - DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"` + DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"` + VersionStyle VersionStyle `toml:"version_style" envconfig:"BOLT_MIGRATIONS_VERSION_STYLE"` +} + +func (mc *MigrationsConfig) Validate() error { + if mc.VersionStyle != VersionStyleSequential && mc.VersionStyle != VersionStyleTimestamp { + return ErrInvalidVersionStyle + } + + return nil } type ConnectionConfig struct { @@ -49,12 +72,17 @@ type ConnectionConfig struct { func NewConfig() (*Config, error) { filePath, err := findConfigFilePath() - if err != nil && !errors.Is(err, errConfigFileNotFound) { + if err != nil && !errors.Is(err, ErrConfigFileNotFound) { return nil, err } - cfg := Config{Migrations: MigrationsConfig{DirectoryPath: "migrations"}} - if !errors.Is(err, errConfigFileNotFound) { + cfg := Config{ + Migrations: MigrationsConfig{ + DirectoryPath: "migrations", + VersionStyle: VersionStyleSequential, + }, + } + if !errors.Is(err, ErrConfigFileNotFound) { _, err = toml.DecodeFile(filePath, &cfg) if err != nil { return nil, err @@ -66,6 +94,11 @@ func NewConfig() (*Config, error) { return nil, err } + err = cfg.Migrations.Validate() + if err != nil { + return nil, err + } + return &cfg, nil } @@ -91,7 +124,7 @@ func findConfigFilePath() (filePath string, err error) { } if dir == rootDir { - return "", errConfigFileNotFound + return "", ErrConfigFileNotFound } dir = filepath.Dir(dir) -- cgit v1.2.3 From b8b23e393dcda33cc98b22f4083977b543861bf2 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 17:29:19 -0500 Subject: Add test for invalid version style in config --- internal/configloader/configloader.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'internal/configloader/configloader.go') diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index c5aba2d..6155d56 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -50,15 +50,7 @@ type Config struct { type MigrationsConfig struct { DirectoryPath string `toml:"directory_path" envconfig:"BOLT_MIGRATIONS_DIR_PATH"` - VersionStyle VersionStyle `toml:"version_style" envconfig:"BOLT_MIGRATIONS_VERSION_STYLE"` -} - -func (mc *MigrationsConfig) Validate() error { - if mc.VersionStyle != VersionStyleSequential && mc.VersionStyle != VersionStyleTimestamp { - return ErrInvalidVersionStyle - } - - return nil + VersionStyle VersionStyle `toml:"version_style" envconfig:"BOLT_MIGRATIONS_VERSION_STYLE"` } type ConnectionConfig struct { @@ -94,9 +86,9 @@ func NewConfig() (*Config, error) { return nil, err } - err = cfg.Migrations.Validate() - if err != nil { - return nil, err + if cfg.Migrations.VersionStyle != VersionStyleSequential && + cfg.Migrations.VersionStyle != VersionStyleTimestamp { + return nil, ErrInvalidVersionStyle } return &cfg, nil -- cgit v1.2.3 From bf1d3b3e1fc42132678b244e479839416a03a6ce Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 29 Feb 2024 19:53:20 -0500 Subject: Add service tests --- internal/configloader/configloader.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'internal/configloader/configloader.go') diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go index 6155d56..c2c05e7 100644 --- a/internal/configloader/configloader.go +++ b/internal/configloader/configloader.go @@ -71,7 +71,7 @@ func NewConfig() (*Config, error) { cfg := Config{ Migrations: MigrationsConfig{ DirectoryPath: "migrations", - VersionStyle: VersionStyleSequential, + VersionStyle: VersionStyleTimestamp, }, } if !errors.Is(err, ErrConfigFileNotFound) { -- cgit v1.2.3