aboutsummaryrefslogtreecommitdiffstats
path: root/internal/configloader
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-01-19 23:27:27 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2024-01-19 23:27:27 -0500
commitdb7de4b31b1836abed822077840665639f0d66da (patch)
treed58769374a346778ffff35e6cf56a27e6067f237 /internal/configloader
parentFix typo in output when a user has no migrations (diff)
downloadcli-db7de4b31b1836abed822077840665639f0d66da.zip
Add ability to use sequential or timestamp-based versions
Diffstat (limited to 'internal/configloader')
-rw-r--r--internal/configloader/configloader.go47
1 files changed, 40 insertions, 7 deletions
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)