aboutsummaryrefslogtreecommitdiffstats
path: root/internal
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
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')
-rw-r--r--internal/commands/new.go5
-rw-r--r--internal/configloader/configloader.go47
-rw-r--r--internal/models/migration.go38
-rw-r--r--internal/models/migration_test.go25
-rw-r--r--internal/repositories/migration_db_repo_test.go10
-rw-r--r--internal/repositories/migration_fs_repo.go46
-rw-r--r--internal/repositories/migration_fs_repo_test.go8
-rw-r--r--internal/services/migration.go24
8 files changed, 135 insertions, 68 deletions
diff --git a/internal/commands/new.go b/internal/commands/new.go
index a4eeff4..0c44248 100644
--- a/internal/commands/new.go
+++ b/internal/commands/new.go
@@ -3,10 +3,8 @@ package commands
import (
"context"
"flag"
- "time"
"github.com/eugenetriguba/bolt/internal/configloader"
- "github.com/eugenetriguba/bolt/internal/models"
"github.com/eugenetriguba/bolt/internal/output"
"github.com/eugenetriguba/bolt/internal/repositories"
"github.com/eugenetriguba/bolt/internal/services"
@@ -70,8 +68,7 @@ func (cmd *NewCmd) Execute(
return subcommands.ExitFailure
}
- migration := models.NewMigration(time.Now(), cmd.message)
- err = migrationService.CreateMigration(migration)
+ err = migrationService.CreateMigration(cfg.Migrations.VersionStyle, cmd.message)
if err != nil {
consoleOutputter.Error(err.Error())
return subcommands.ExitFailure
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)
diff --git a/internal/models/migration.go b/internal/models/migration.go
index f647a1b..4007f3c 100644
--- a/internal/models/migration.go
+++ b/internal/models/migration.go
@@ -11,30 +11,22 @@ type Migration struct {
Applied bool
}
-func NewMigration(timestamp time.Time, message string) *Migration {
- version := fmt.Sprintf(
- "%d%02d%02d%02d%02d%02d",
- timestamp.Year(), timestamp.Month(), timestamp.Day(),
- timestamp.Hour(), timestamp.Minute(), timestamp.Second(),
- )
- return &Migration{Version: version, Message: message, Applied: false}
-}
-
-func (m *Migration) String() string {
- checkmark := " "
- if m.Applied {
- checkmark = "x"
+func NewTimestampMigration(version time.Time, message string) *Migration {
+ return &Migration{
+ Version: fmt.Sprintf(
+ "%d%02d%02d%02d%02d%02d",
+ version.Year(), version.Month(), version.Day(),
+ version.Hour(), version.Minute(), version.Second(),
+ ),
+ Message: message,
+ Applied: false,
}
+}
- message := m.Message
- if len(message) > 0 {
- message = fmt.Sprintf("- %s ", message)
+func NewSequentialMigration(version uint64, message string) *Migration {
+ return &Migration{
+ Version: fmt.Sprintf("%03d", version),
+ Message: message,
+ Applied: false,
}
-
- return fmt.Sprintf(
- "%s %s- [%s]",
- m.Version,
- message,
- checkmark,
- )
}
diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go
index 8d1fb06..81068f3 100644
--- a/internal/models/migration_test.go
+++ b/internal/models/migration_test.go
@@ -9,25 +9,26 @@ import (
"github.com/eugenetriguba/bolt/internal/models"
)
-var timestamp = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
+func TestNewTimestampMigration(t *testing.T) {
+ var timestamp = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
-func TestNewMigrationInit(t *testing.T) {
- m := models.NewMigration(timestamp, "test message")
+ m := models.NewTimestampMigration(timestamp, "test message")
assert.Equal(t, m.Version, "20200101000000")
assert.Equal(t, m.Message, "test message")
assert.Equal(t, m.Applied, false)
}
-func TestAppliedNewMigration(t *testing.T) {
- m := models.NewMigration(timestamp, "test_message")
+func TestNewSequentialMigration(t *testing.T) {
+ m := models.NewSequentialMigration(1, "test message")
- assert.Equal(t, m.String(), "20200101000000 - test_message - [ ]")
- m.Applied = true
- assert.Equal(t, m.String(), "20200101000000 - test_message - [x]")
-}
+ assert.Equal(t, m.Version, "001")
+ assert.Equal(t, m.Message, "test message")
+ assert.Equal(t, m.Applied, false)
-func TestEmptyMessageMigration(t *testing.T) {
- m := models.NewMigration(timestamp, "")
- assert.Equal(t, "20200101000000 - [ ]", m.String())
+ m = models.NewSequentialMigration(100, "test message")
+
+ assert.Equal(t, m.Version, "100")
+ assert.Equal(t, m.Message, "test message")
+ assert.Equal(t, m.Applied, false)
}
diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go
index 55b8eb2..dc635b0 100644
--- a/internal/repositories/migration_db_repo_test.go
+++ b/internal/repositories/migration_db_repo_test.go
@@ -131,7 +131,7 @@ func TestMigrationDBRepo_Apply(t *testing.T) {
repo, err := repositories.NewMigrationDBRepo(db)
assert.NilError(t, err)
- migration := models.NewMigration(time.Now(), "test")
+ migration := models.NewTimestampMigration(time.Now(), "test")
err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration)
assert.NilError(t, err)
assert.Equal(t, migration.Applied, true)
@@ -158,7 +158,7 @@ func TestMigrationDBRepo_Revert(t *testing.T) {
_, err = db.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
assert.NilError(t, err)
- migration := models.NewMigration(time.Now(), "test")
+ migration := models.NewTimestampMigration(time.Now(), "test")
_, err = db.Exec(
`INSERT INTO bolt_migrations(version) VALUES ($1);`,
migration.Version,
@@ -194,7 +194,7 @@ func TestMigrationDBRepo_ApplyAndRevertWithoutTransaction(t *testing.T) {
_, err = db.Exec("CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT)")
assert.NilError(t, err)
- migration := models.NewMigration(time.Now(), "test")
+ migration := models.NewTimestampMigration(time.Now(), "test")
// CREATE INDEX CONCURRENTLY cannot run inside a transaction.
err = repo.Apply(
`-- bolt: no-transaction\CREATE INDEX CONCURRENTLY i1 ON tmp(id2);`,
@@ -215,7 +215,7 @@ func TestMigrationDBRepo_ApplyMalformedSql(t *testing.T) {
db := bolttest.NewTestDB(t, "postgres")
repo, err := repositories.NewMigrationDBRepo(db)
assert.NilError(t, err)
- migration := models.NewMigration(time.Now(), "test")
+ migration := models.NewTimestampMigration(time.Now(), "test")
err = repo.Apply("this is not SQL", migration)
assert.ErrorContains(t, err, "syntax error")
@@ -230,7 +230,7 @@ func TestMigrationDBRepo_RevertMalformedSql(t *testing.T) {
db := bolttest.NewTestDB(t, "postgres")
repo, err := repositories.NewMigrationDBRepo(db)
assert.NilError(t, err)
- migration := models.NewMigration(time.Now(), "test")
+ migration := models.NewTimestampMigration(time.Now(), "test")
migration.Applied = true
err = repo.Revert("this is not SQL", migration)
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index 5478b5c..65afe5f 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -3,6 +3,7 @@ package repositories
import (
"errors"
"fmt"
+ "io/fs"
"os"
"path/filepath"
"strings"
@@ -87,6 +88,19 @@ func (mr *MigrationFsRepo) Get(version string) (*models.Migration, error) {
return migration, nil
}
+func (mr *MigrationFsRepo) Latest() (*models.Migration, error) {
+ entries, err := os.ReadDir(mr.migrationsDirPath)
+ if err != nil {
+ return nil, err
+ }
+
+ if len(entries) > 0 {
+ return dirEntryToMigration(entries[len(entries)-1])
+ }
+
+ return nil, nil
+}
+
func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) {
entries, err := os.ReadDir(mr.migrationsDirPath)
if err != nil {
@@ -95,24 +109,32 @@ func (mr *MigrationFsRepo) List() (map[string]*models.Migration, error) {
var migrations = make(map[string]*models.Migration)
for _, entry := range entries {
- parts := strings.SplitN(entry.Name(), "_", 2)
- if len(parts) != 2 {
- return nil, fmt.Errorf(
- "%s is an invalid migration name: expected a "+
- "migration directory of the format <version>_<message>",
- entry.Name(),
- )
- }
- migrations[parts[0]] = &models.Migration{
- Version: parts[0],
- Message: parts[1],
- Applied: false,
+ migration, err := dirEntryToMigration(entry)
+ if err != nil {
+ return nil, err
}
+ migrations[migration.Version] = migration
}
return migrations, nil
}
+func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) {
+ parts := strings.SplitN(entry.Name(), "_", 2)
+ if len(parts) != 2 {
+ return nil, fmt.Errorf(
+ "%s is an invalid migration name: expected a "+
+ "migration directory of the format <version>_<message>",
+ entry.Name(),
+ )
+ }
+ return &models.Migration{
+ Version: parts[0],
+ Message: parts[1],
+ Applied: false,
+ }, nil
+}
+
func (mr *MigrationFsRepo) ReadUpgradeScript(
migration *models.Migration,
) (string, error) {
diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go
index 8fdcd8f..b1659fd 100644
--- a/internal/repositories/migration_fs_repo_test.go
+++ b/internal/repositories/migration_fs_repo_test.go
@@ -45,7 +45,7 @@ func TestMigrationFsRepoCreate(t *testing.T) {
migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir}
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.NilError(t, err)
- migration := models.NewMigration(time.Now(), "add users table")
+ migration := models.NewTimestampMigration(time.Now(), "add users table")
err = repo.Create(migration)
assert.NilError(t, err)
@@ -62,7 +62,7 @@ func TestMigrationFsRepoReadUpgradeAndDowngradeScript(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.NilError(t, err)
- migration := models.NewMigration(time.Now(), "add users table")
+ migration := models.NewTimestampMigration(time.Now(), "add users table")
err = repo.Create(migration)
assert.NilError(t, err)
@@ -94,11 +94,11 @@ func TestMigrationFsRepo_List(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.NilError(t, err)
- migration1 := models.NewMigration(
+ migration1 := models.NewTimestampMigration(
time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC),
"migration_1",
)
- migration2 := models.NewMigration(
+ migration2 := models.NewTimestampMigration(
time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC),
"migration_2",
)
diff --git a/internal/services/migration.go b/internal/services/migration.go
index 8b5898e..91921bc 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -3,7 +3,10 @@ package services
import (
"fmt"
"sort"
+ "strconv"
+ "time"
+ "github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/bolt/internal/models"
"github.com/eugenetriguba/bolt/internal/output"
"github.com/eugenetriguba/bolt/internal/repositories"
@@ -195,7 +198,26 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
return nil
}
-func (ms *MigrationService) CreateMigration(migration *models.Migration) error {
+func (ms *MigrationService) CreateMigration(versionStyle configloader.VersionStyle, message string) error {
+ var migration *models.Migration
+
+ if versionStyle == configloader.VersionStyleTimestamp {
+ migration = models.NewTimestampMigration(time.Now(), message)
+ } else {
+ var currentVerison uint64 = 0
+ latestMigration, err := ms.fsRepo.Latest()
+ if err != nil {
+ return err
+ }
+ if latestMigration != nil {
+ currentVerison, err = strconv.ParseUint(latestMigration.Version, 10, 64)
+ if err != nil {
+ return err
+ }
+ }
+ migration = models.NewSequentialMigration(currentVerison+1, message)
+ }
+
err := ms.fsRepo.Create(migration)
if err != nil {
return err