aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-11-13 22:13:14 -0800
committerEugene Triguba <hello@eugenetriguba.com>2026-11-13 22:13:14 -0800
commit9656e14fc758053f34f5cb0e906093e2a18f6162 (patch)
tree3d7799305de92be6a09bcd12c29fe0843b7b8cab
parentRemove migration dependencies (diff)
downloadcli-9656e14fc758053f34f5cb0e906093e2a18f6162.zip
Remove timestamp version style in favor of always using sequential
-rw-r--r--CHANGELOG.md2
-rw-r--r--README.md10
-rw-r--r--internal/commands/down.go1
-rw-r--r--internal/commands/new.go1
-rw-r--r--internal/commands/status.go1
-rw-r--r--internal/commands/up.go1
-rw-r--r--internal/configloader/configloader.go34
-rw-r--r--internal/configloader/configloader_test.go21
-rw-r--r--internal/models/migration.go15
-rw-r--r--internal/models/migration_test.go35
-rw-r--r--internal/repositories/migration_db_repo_test.go17
-rw-r--r--internal/repositories/migration_fs_repo.go6
-rw-r--r--internal/repositories/migration_fs_repo_test.go29
-rw-r--r--internal/services/migration.go130
-rw-r--r--internal/services/migration_test.go257
-rw-r--r--man/bolt-down.12
-rw-r--r--man/bolt-migration.514
-rw-r--r--man/bolt-new.16
-rw-r--r--man/bolt-status.17
-rw-r--r--man/bolt-tutorial.732
-rw-r--r--man/bolt-up.12
-rw-r--r--man/bolt.12
-rw-r--r--man/bolt.toml.515
23 files changed, 124 insertions, 516 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3c69a9d..ed96f26 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **Migration dependencies**: The `-- depends:` directive and dependency-based
ordering added in 0.12.0 have been removed.
+- **Timestamp versions**: Migrations are now always versioned sequentially. The
+ `version_style` option and `BOLT_MIGRATIONS_VERSION_STYLE` have been removed.
## [0.13.1] - 2026-02-12
diff --git a/README.md b/README.md
index f20ea41..532cd3a 100644
--- a/README.md
+++ b/README.md
@@ -13,7 +13,7 @@ Bolt aims to be a simple and reliable tool that can work anywhere you need to
run database migrations.
- Schema migrations are written in plain SQL.
-- Migrations can be versioned sequentially or by creation timestamp.
+- Migrations are versioned sequentially.
- All migrations run in transactions by default.
- Supports up and down migrations and jumping to any particular schema version.
- Supports PostgreSQL, MySQL, SQL Server, and SQLite3.
@@ -39,10 +39,10 @@ DROP TABLE posts;
```bash
$ bolt status
-Version Message Applied
-20240316145038 create_users_table X
-20240317120000 create_posts_table X
-20240318093000 add_email_index
+Version Message Applied
+001 create_users_table X
+002 create_posts_table X
+003 add_email_index
```
## Installation
diff --git a/internal/commands/down.go b/internal/commands/down.go
index affa02b..3dc537b 100644
--- a/internal/commands/down.go
+++ b/internal/commands/down.go
@@ -76,7 +76,6 @@ func (cmd *DownCmd) Execute(
migrationService := services.NewMigrationService(
migrationDBRepo,
migrationFsRepo,
- *cfg,
consoleOutputter,
)
diff --git a/internal/commands/new.go b/internal/commands/new.go
index b1c41aa..762ca4d 100644
--- a/internal/commands/new.go
+++ b/internal/commands/new.go
@@ -64,7 +64,6 @@ func (cmd *NewCmd) Execute(
migrationService := services.NewMigrationService(
nil,
migrationFsRepo,
- *cfg,
consoleOutputter,
)
diff --git a/internal/commands/status.go b/internal/commands/status.go
index cc561bb..5146d92 100644
--- a/internal/commands/status.go
+++ b/internal/commands/status.go
@@ -66,7 +66,6 @@ func (m *StatusCmd) Execute(
migrationService := services.NewMigrationService(
migrationDBRepo,
migrationFsRepo,
- *cfg,
consoleOutputter,
)
diff --git a/internal/commands/up.go b/internal/commands/up.go
index 87a4059..5bb4e18 100644
--- a/internal/commands/up.go
+++ b/internal/commands/up.go
@@ -76,7 +76,6 @@ func (cmd *UpCmd) Execute(
migrationService := services.NewMigrationService(
migrationDBRepo,
migrationFsRepo,
- *cfg,
consoleOutputter,
)
diff --git a/internal/configloader/configloader.go b/internal/configloader/configloader.go
index 81a9564..1d36ae8 100644
--- a/internal/configloader/configloader.go
+++ b/internal/configloader/configloader.go
@@ -10,31 +10,16 @@ package configloader
import (
"errors"
- "fmt"
"os"
"path/filepath"
- "reflect"
"runtime"
"git.sr.ht/~eugenetriguba/envelope"
"github.com/BurntSushi/toml"
)
-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},
- )
+var ErrConfigFileNotFound = errors.New(
+ "bolt configuration file not found in current directory or any parent directories",
)
// Config represents the application configuration settings.
@@ -50,8 +35,7 @@ type Config struct {
}
type MigrationsConfig struct {
- DirectoryPath string `toml:"directory_path" env:"DIR_PATH"`
- VersionStyle VersionStyle `toml:"version_style" env:"VERSION_STYLE"`
+ DirectoryPath string `toml:"directory_path" env:"DIR_PATH"`
}
type ConnectionConfig struct {
@@ -60,12 +44,6 @@ type ConnectionConfig struct {
MigrationsTable string `toml:"migrations_table" env:"MIGRATIONS_TABLE"`
}
-func init() {
- envelope.RegisterTypeParser(reflect.TypeOf(VersionStyleSequential), envelope.TypeParserFunc(func(value string) (any, error) {
- return VersionStyle(value), nil
- }))
-}
-
func NewConfig() (*Config, error) {
filePath, err := findConfigFilePath()
if err != nil && !errors.Is(err, ErrConfigFileNotFound) {
@@ -75,7 +53,6 @@ func NewConfig() (*Config, error) {
cfg := Config{
Migrations: MigrationsConfig{
DirectoryPath: "migrations",
- VersionStyle: VersionStyleTimestamp,
},
Connection: ConnectionConfig{
MigrationsTable: "bolt_migrations",
@@ -93,11 +70,6 @@ func NewConfig() (*Config, error) {
return nil, err
}
- if cfg.Migrations.VersionStyle != VersionStyleSequential &&
- cfg.Migrations.VersionStyle != VersionStyleTimestamp {
- return nil, ErrInvalidVersionStyle
- }
-
return &cfg, nil
}
diff --git a/internal/configloader/configloader_test.go b/internal/configloader/configloader_test.go
index e634c15..4112580 100644
--- a/internal/configloader/configloader_test.go
+++ b/internal/configloader/configloader_test.go
@@ -18,34 +18,18 @@ func TestNewConfigDefaults(t *testing.T) {
assert.Nil(t, err)
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) {
- fileCfg := configloader.Config{
- Migrations: configloader.MigrationsConfig{
- DirectoryPath: "myfancymigrations",
- VersionStyle: "invalid",
- },
- }
- bolttest.CreateConfigFile(t, &fileCfg, "bolt.toml")
-
- _, err := configloader.NewConfig()
- assert.ErrorIs(t, err, configloader.ErrInvalidVersionStyle)
-}
-
func TestNewConfigFindsFileAndPopulatesConfigStruct(t *testing.T) {
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{
DSN: "postgresql://testuser:testpassword@testhost:1234/testdb",
@@ -67,7 +51,6 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
fileCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "cfgmigrations",
- VersionStyle: configloader.VersionStyleSequential,
},
Connection: configloader.ConnectionConfig{
DSN: "mysql://testuser:testpassword@testhost:1234/testdb",
@@ -80,7 +63,6 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
envCfg := configloader.Config{
Migrations: configloader.MigrationsConfig{
DirectoryPath: "envmigrations",
- VersionStyle: configloader.VersionStyleTimestamp,
},
Connection: configloader.ConnectionConfig{
DSN: "postgresql://envtestuser:envtestpassword@envtesthost:4321/envtestdb",
@@ -88,7 +70,6 @@ func TestNewConfigCanBeOverridenByEnvVars(t *testing.T) {
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_DSN", envCfg.Connection.DSN)
t.Setenv("BOLT_DB_DRIVER", envCfg.Connection.Driver)
@@ -104,11 +85,9 @@ func TestNewConfigSearchesParentDirectories(t *testing.T) {
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: "differentmigrationsdir",
- VersionStyle: configloader.VersionStyleSequential,
},
Connection: configloader.ConnectionConfig{
MigrationsTable: "migration_table",
diff --git a/internal/models/migration.go b/internal/models/migration.go
index ed48073..44608f9 100644
--- a/internal/models/migration.go
+++ b/internal/models/migration.go
@@ -3,7 +3,6 @@ package models
import (
"fmt"
"strings"
- "time"
)
type Migration struct {
@@ -12,19 +11,7 @@ type Migration struct {
Applied bool
}
-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,
- }
-}
-
-func NewSequentialMigration(version uint64, message string) *Migration {
+func NewMigration(version uint64, message string) *Migration {
return &Migration{
Version: fmt.Sprintf("%03d", version),
Message: message,
diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go
index aae1cba..87b98e4 100644
--- a/internal/models/migration_test.go
+++ b/internal/models/migration_test.go
@@ -2,30 +2,19 @@ package models_test
import (
"testing"
- "time"
"git.sr.ht/~eugenetriguba/bolt/internal/models"
"git.sr.ht/~eugenetriguba/checkmate/check"
)
-func TestNewTimestampMigration(t *testing.T) {
- var timestamp = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
-
- m := models.NewTimestampMigration(timestamp, "test message")
-
- check.Equal(t, m.Version, "20200101000000")
- check.Equal(t, m.Message, "test message")
- check.Equal(t, m.Applied, false)
-}
-
-func TestNewSequentialMigration(t *testing.T) {
- m := models.NewSequentialMigration(1, "test message")
+func TestNewMigration(t *testing.T) {
+ m := models.NewMigration(1, "test message")
check.Equal(t, m.Version, "001")
check.Equal(t, m.Message, "test message")
check.Equal(t, m.Applied, false)
- m = models.NewSequentialMigration(100, "test message")
+ m = models.NewMigration(100, "test message")
check.Equal(t, m.Version, "100")
check.Equal(t, m.Message, "test message")
@@ -41,28 +30,14 @@ func TestName(t *testing.T) {
testCases := []test{
// Underscores are used to separate version and message.
{
- migration: models.NewSequentialMigration(1, "test_message"),
+ migration: models.NewMigration(1, "test_message"),
expectedName: "001_test_message",
},
- {
- migration: models.NewTimestampMigration(
- time.Date(1234, 1, 2, 3, 4, 5, 6, time.UTC),
- "test_message",
- ),
- expectedName: "12340102030405_test_message",
- },
// Leading/Trailing spaces trimmed, lowercased, and spaces converted to underscores.
{
- migration: models.NewSequentialMigration(1, " teST mesSAGE "),
+ migration: models.NewMigration(1, " teST mesSAGE "),
expectedName: "001_test_message",
},
- {
- migration: models.NewTimestampMigration(
- time.Date(1234, 1, 2, 3, 4, 5, 6, time.UTC),
- " teST mesSAGE ",
- ),
- expectedName: "12340102030405_test_message",
- },
}
for _, tc := range testCases {
diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go
index 8098e03..6787d3d 100644
--- a/internal/repositories/migration_db_repo_test.go
+++ b/internal/repositories/migration_db_repo_test.go
@@ -4,7 +4,6 @@ import (
"database/sql"
"errors"
"testing"
- "time"
"git.sr.ht/~eugenetriguba/bolt/internal/bolttest"
"git.sr.ht/~eugenetriguba/bolt/internal/models"
@@ -181,7 +180,7 @@ func TestApply(t *testing.T) {
repo, err := repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
err = repo.Apply(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration)
assert.Nil(t, err)
assert.Equal(t, migration.Applied, true)
@@ -198,7 +197,7 @@ func TestApply_MalformedSql(t *testing.T) {
db := bolttest.NewTestDB(t)
repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
err = repo.Apply("this is not SQL", migration)
@@ -210,7 +209,7 @@ func TestApplyWithTx_ExecErr(t *testing.T) {
db := bolttest.NewTestDB(t)
repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
err = repo.ApplyWithTx("SELECT 1 FROM abc123donotexist;", migration)
@@ -222,7 +221,7 @@ func TestApplyWithTx_SuccessfullyApplied(t *testing.T) {
repo, err := repositories.NewMigrationDBRepo("bolt_migrations", testdb)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
err = repo.ApplyWithTx(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`, migration)
assert.Nil(t, err)
assert.Equal(t, migration.Applied, true)
@@ -243,7 +242,7 @@ func TestRevert(t *testing.T) {
_, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
_, err = testdb.Exec(
"INSERT INTO bolt_migrations(version) VALUES(?)",
migration.Version,
@@ -268,7 +267,7 @@ func TestRevert_MalformedSql(t *testing.T) {
db := bolttest.NewTestDB(t)
repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
migration.Applied = true
err = repo.Revert("this is not SQL", migration)
@@ -280,7 +279,7 @@ func TestRevertWithTx_ExecErr(t *testing.T) {
db := bolttest.NewTestDB(t)
repo, err := repositories.NewMigrationDBRepo("bolt_migrations", db)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
err = repo.RevertWithTx("DROP TABLE abc123donotexist;", migration)
@@ -295,7 +294,7 @@ func TestRevertWithTx_SuccessfullyReverted(t *testing.T) {
_, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "test")
+ migration := models.NewMigration(1, "test")
_, err = testdb.Exec(
"INSERT INTO bolt_migrations(version) VALUES(?)",
migration.Version,
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index 72057ec..b328c90 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -90,11 +90,13 @@ func (mr migrationFsRepo) List() (map[string]*models.Migration, error) {
if err != nil {
return nil, err
}
- _, exists := migrations[migration.Version]
+ existing, exists := migrations[migration.Version]
if exists {
return nil, fmt.Errorf(
- "%w: a local migration file with version %s already exists",
+ "%w: %s and %s both have version %s; renumber one of them",
ErrMigrationVersionConflict,
+ existing.Name(),
+ migration.Name(),
migration.Version,
)
}
diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go
index 7625c06..c3c71c4 100644
--- a/internal/repositories/migration_fs_repo_test.go
+++ b/internal/repositories/migration_fs_repo_test.go
@@ -5,7 +5,6 @@ import (
"os"
"path/filepath"
"testing"
- "time"
"git.sr.ht/~eugenetriguba/bolt/internal/configloader"
"git.sr.ht/~eugenetriguba/bolt/internal/models"
@@ -54,7 +53,7 @@ func TestCreate_SuccessfullyCreated(t *testing.T) {
migrationsConfig := configloader.MigrationsConfig{DirectoryPath: tempDir}
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "add users table")
+ migration := models.NewMigration(1, "add users table")
err = repo.Create(migration)
assert.Nil(t, err)
@@ -68,7 +67,7 @@ func TestCreate_FailsToCreateMigration(t *testing.T) {
&configloader.MigrationsConfig{DirectoryPath: tempDir},
)
assert.Nil(t, err)
- migration := models.NewTimestampMigration(time.Now(), "add users table")
+ migration := models.NewMigration(1, "add users table")
_, err = os.OpenFile(
filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name())),
os.O_CREATE,
@@ -88,7 +87,7 @@ func TestReadUpgradeScript_SuccessfullyRead(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
- migration := models.NewSequentialMigration(1, "add users table")
+ migration := models.NewMigration(1, "add users table")
err = repo.Create(migration)
assert.Nil(t, err)
@@ -109,7 +108,7 @@ func TestReadUpgradeScript_FileDoesNotExist(t *testing.T) {
migrationsConfig := configloader.MigrationsConfig{DirectoryPath: t.TempDir()}
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
- migration := models.NewSequentialMigration(1, "add users table")
+ migration := models.NewMigration(1, "add users table")
_, err = repo.ReadUpgradeScript(migration)
@@ -123,7 +122,7 @@ func TestReadDowngradeScript_SuccessfullyRead(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
- migration := models.NewSequentialMigration(1, "add users table")
+ migration := models.NewMigration(1, "add users table")
err = repo.Create(migration)
assert.Nil(t, err)
@@ -144,7 +143,7 @@ func TestReadDowngradeScript_FileDoesNotExist(t *testing.T) {
migrationsConfig := configloader.MigrationsConfig{DirectoryPath: t.TempDir()}
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
- migration := models.NewSequentialMigration(1, "add users table")
+ migration := models.NewMigration(1, "add users table")
_, err = repo.ReadDowngradeScript(migration)
@@ -158,14 +157,8 @@ func TestList_Success(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
- migration1 := models.NewTimestampMigration(
- time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC),
- "migration_1",
- )
- migration2 := models.NewTimestampMigration(
- time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC),
- "migration_2",
- )
+ migration1 := models.NewMigration(1, "migration_1")
+ migration2 := models.NewMigration(2, "migration_2")
err = repo.Create(migration1)
assert.Nil(t, err)
repo.Create(migration2)
@@ -215,8 +208,8 @@ func TestList_DuplicateMigrationVersion(t *testing.T) {
migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir}
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
- migration1 := models.NewSequentialMigration(1, "migration_1")
- migration2 := models.NewSequentialMigration(1, "migration_2")
+ migration1 := models.NewMigration(1, "migration_1")
+ migration2 := models.NewMigration(1, "migration_2")
err = repo.Create(migration1)
assert.Nil(t, err)
repo.Create(migration2)
@@ -228,6 +221,6 @@ func TestList_DuplicateMigrationVersion(t *testing.T) {
assert.ErrorContains(
t,
err,
- "a local migration file with version 001 already exists",
+ "001_migration_1 and 001_migration_2 both have version 001; renumber one of them",
)
}
diff --git a/internal/services/migration.go b/internal/services/migration.go
index ddfb8c2..a861176 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -6,7 +6,6 @@ import (
"strconv"
"time"
- "git.sr.ht/~eugenetriguba/bolt/internal/configloader"
"git.sr.ht/~eugenetriguba/bolt/internal/models"
"git.sr.ht/~eugenetriguba/bolt/internal/output"
"git.sr.ht/~eugenetriguba/bolt/internal/repositories"
@@ -15,20 +14,17 @@ import (
type MigrationService struct {
dbRepo repositories.MigrationDBRepo
fsRepo repositories.MigrationFsRepo
- cfg configloader.Config
outputter output.Outputter
}
func NewMigrationService(
dbRepo repositories.MigrationDBRepo,
fsRepo repositories.MigrationFsRepo,
- cfg configloader.Config,
outputter output.Outputter,
) MigrationService {
return MigrationService{
dbRepo: dbRepo,
fsRepo: fsRepo,
- cfg: cfg,
outputter: outputter,
}
}
@@ -235,19 +231,13 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error {
}
func (ms MigrationService) CreateMigration(message string) (*models.Migration, error) {
- var migration *models.Migration
-
- if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleTimestamp {
- migration = models.NewTimestampMigration(time.Now(), message)
- } else {
- currentVersion, err := ms.getCurrentSequentialMigrationVersion()
- if err != nil {
- return nil, err
- }
- migration = models.NewSequentialMigration(currentVersion+1, message)
+ currentVersion, err := ms.getCurrentMigrationVersion()
+ if err != nil {
+ return nil, err
}
+ migration := models.NewMigration(currentVersion+1, message)
- err := ms.fsRepo.Create(migration)
+ err = ms.fsRepo.Create(migration)
if err != nil {
return nil, err
}
@@ -257,32 +247,37 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e
return migration, nil
}
-func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
- var currentVersion uint64 = 0
+func (ms MigrationService) getCurrentMigrationVersion() (uint64, error) {
+ var maxVersion uint64 = 0
localMigrations, err := ms.fsRepo.List()
if err != nil {
return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
}
- migrations, err := ms.combineMigrations(
- localMigrations,
- map[string]*models.Migration{},
- SortOrderDesc,
- )
- if err != nil {
- return 0, err
- }
-
- if len(migrations) > 0 {
- latestMigration := migrations[0]
- currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64)
+ for _, migration := range localMigrations {
+ version, err := parseVersion(migration)
if err != nil {
return 0, err
}
+ if version > maxVersion {
+ maxVersion = version
+ }
}
- return currentVersion, nil
+ return maxVersion, nil
+}
+
+func parseVersion(migration *models.Migration) (uint64, error) {
+ version, err := strconv.ParseUint(migration.Version, 10, 64)
+ if err != nil {
+ return 0, fmt.Errorf(
+ "migration %s has an invalid version: expected a number: %w",
+ migration.Name(),
+ err,
+ )
+ }
+ return version, nil
}
func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
@@ -334,73 +329,20 @@ func (ms MigrationService) sortMigrations(
migrations []*models.Migration,
order sortOrder,
) error {
- sortErrs := make([]error, 0)
- sort.Slice(migrations, func(i, j int) bool {
- var comparison bool
- var err error
-
- if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential {
- comparison, err = ms.compareSequentialMigrations(
- migrations[i],
- migrations[j],
- order,
- )
- } else {
- comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order)
- }
-
+ versions := make(map[*models.Migration]uint64, len(migrations))
+ for _, migration := range migrations {
+ version, err := parseVersion(migration)
if err != nil {
- sortErrs = append(sortErrs, err)
+ return fmt.Errorf("unable to sort migrations: %w", err)
}
+ versions[migration] = version
+ }
- return comparison
+ sort.Slice(migrations, func(i, j int) bool {
+ if order == SortOrderAsc {
+ return versions[migrations[i]] < versions[migrations[j]]
+ }
+ return versions[migrations[i]] > versions[migrations[j]]
})
- if len(sortErrs) != 0 {
- return fmt.Errorf("unable to sort migrations: %v", sortErrs)
- }
return nil
}
-
-func (ms MigrationService) compareSequentialMigrations(
- m1 *models.Migration,
- m2 *models.Migration,
- order sortOrder,
-) (bool, error) {
- m1Version, err := strconv.ParseInt(m1.Version, 10, 64)
- if err != nil {
- return false, err
- }
-
- m2Version, err := strconv.ParseInt(m2.Version, 10, 64)
- if err != nil {
- return false, err
- }
-
- if order == SortOrderAsc {
- return m1Version < m2Version, nil
- } else {
- return m1Version > m2Version, nil
- }
-}
-
-func (ms MigrationService) compareTimestampMigrations(
- m1 *models.Migration,
- m2 *models.Migration,
- order sortOrder,
-) (bool, error) {
- m1Version, err := time.Parse("20060102150405", m1.Version)
- if err != nil {
- return false, err
- }
-
- m2Version, err := time.Parse("20060102150405", m2.Version)
- if err != nil {
- return false, err
- }
-
- if order == SortOrderAsc {
- return m1Version.Before(m2Version), nil
- } else {
- return m1Version.After(m2Version), nil
- }
-}
diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go
index 09d33a3..0496299 100644
--- a/internal/services/migration_test.go
+++ b/internal/services/migration_test.go
@@ -3,10 +3,9 @@ package services
import (
"errors"
"testing"
- "time"
"git.sr.ht/~eugenetriguba/bolt/internal/bolttest"
- "git.sr.ht/~eugenetriguba/bolt/internal/configloader"
+
"git.sr.ht/~eugenetriguba/bolt/internal/models"
"git.sr.ht/~eugenetriguba/bolt/internal/sqlparse"
"git.sr.ht/~eugenetriguba/checkmate/assert"
@@ -17,7 +16,6 @@ func TestListMigrations(t *testing.T) {
type test struct {
localFilesystemMigrations []*models.Migration
remoteAppliedMigrations []*models.Migration
- cfg configloader.Config
sortOrder sortOrder
expectedMigrations []*models.Migration
}
@@ -27,13 +25,8 @@ func TestListMigrations(t *testing.T) {
{
localFilesystemMigrations: []*models.Migration{},
remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
- sortOrder: SortOrderAsc,
- expectedMigrations: []*models.Migration{},
+ sortOrder: SortOrderAsc,
+ expectedMigrations: []*models.Migration{},
},
// Ensure one local fs migration shows up as expected.
{
@@ -41,12 +34,7 @@ func TestListMigrations(t *testing.T) {
{Version: "001", Applied: false},
},
remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
- sortOrder: SortOrderAsc,
+ sortOrder: SortOrderAsc,
expectedMigrations: []*models.Migration{
{Version: "001", Applied: false},
},
@@ -60,11 +48,6 @@ func TestListMigrations(t *testing.T) {
remoteAppliedMigrations: []*models.Migration{
{Version: "001", Applied: true},
},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
sortOrder: SortOrderAsc,
expectedMigrations: []*models.Migration{
{Version: "001", Applied: true},
@@ -77,11 +60,6 @@ func TestListMigrations(t *testing.T) {
remoteAppliedMigrations: []*models.Migration{
{Version: "001", Applied: true},
},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
sortOrder: SortOrderAsc,
expectedMigrations: []*models.Migration{},
},
@@ -97,12 +75,7 @@ func TestListMigrations(t *testing.T) {
{Version: "001"},
},
remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
- sortOrder: SortOrderAsc,
+ sortOrder: SortOrderAsc,
expectedMigrations: []*models.Migration{
{Version: "001"},
{Version: "110"},
@@ -125,12 +98,7 @@ func TestListMigrations(t *testing.T) {
{Version: "20000"},
},
remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
- sortOrder: SortOrderDesc,
+ sortOrder: SortOrderDesc,
expectedMigrations: []*models.Migration{
{Version: "20000"},
{Version: "10000"},
@@ -141,62 +109,6 @@ func TestListMigrations(t *testing.T) {
{Version: "001"},
},
},
- // Ensure that migrations are sorted in asc order for timestamp migrations
- {
- localFilesystemMigrations: []*models.Migration{
- {Version: "20080509220905"},
- {Version: "20080509220405"},
- {Version: "20080509150405"},
- {Version: "20080502150405"},
- {Version: "20080102150405"},
- {Version: "20070102150405"},
- {Version: "20060102150405"},
- },
- remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleTimestamp,
- },
- },
- sortOrder: SortOrderAsc,
- expectedMigrations: []*models.Migration{
- {Version: "20060102150405"},
- {Version: "20070102150405"},
- {Version: "20080102150405"},
- {Version: "20080502150405"},
- {Version: "20080509150405"},
- {Version: "20080509220405"},
- {Version: "20080509220905"},
- },
- },
- // Ensure that migrations are sorted in desc order for timestamp migrations
- {
- localFilesystemMigrations: []*models.Migration{
- {Version: "20060102150405"},
- {Version: "20070102150405"},
- {Version: "20080102150405"},
- {Version: "20080502150405"},
- {Version: "20080509150405"},
- {Version: "20080509220405"},
- {Version: "20080509220905"},
- },
- remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleTimestamp,
- },
- },
- sortOrder: SortOrderDesc,
- expectedMigrations: []*models.Migration{
- {Version: "20080509220905"},
- {Version: "20080509220405"},
- {Version: "20080509150405"},
- {Version: "20080502150405"},
- {Version: "20080102150405"},
- {Version: "20070102150405"},
- {Version: "20060102150405"},
- },
- },
}
for _, tc := range tests {
@@ -225,7 +137,6 @@ func TestListMigrations(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- tc.cfg,
bolttest.NullOutputter{},
)
migrations, err := svc.ListMigrations(tc.sortOrder)
@@ -253,7 +164,6 @@ func TestListMigrations_FsRepoListError(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
migrations, err := svc.ListMigrations(SortOrderAsc)
@@ -281,7 +191,6 @@ func TestListMigrations_DbRepoListError(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
migrations, err := svc.ListMigrations(SortOrderAsc)
@@ -293,31 +202,18 @@ func TestListMigrations_DbRepoListError(t *testing.T) {
func TestListMigrations_ParseErrDuringSort(t *testing.T) {
type test struct {
- m1 models.Migration
- m2 models.Migration
- versionStyle configloader.VersionStyle
+ m1 models.Migration
+ m2 models.Migration
}
tests := []test{
{
- m1: models.Migration{Version: "abc"},
- m2: models.Migration{Version: "123"},
- versionStyle: configloader.VersionStyleSequential,
- },
- {
- m1: models.Migration{Version: "123"},
- m2: models.Migration{Version: "abc"},
- versionStyle: configloader.VersionStyleSequential,
- },
- {
- m1: models.Migration{Version: "20060102150405"},
- m2: models.Migration{Version: "abc"},
- versionStyle: configloader.VersionStyleTimestamp,
+ m1: models.Migration{Version: "abc"},
+ m2: models.Migration{Version: "123"},
},
{
- m1: models.Migration{Version: "abc"},
- m2: models.Migration{Version: "20060102150405"},
- versionStyle: configloader.VersionStyleTimestamp,
+ m1: models.Migration{Version: "123"},
+ m2: models.Migration{Version: "abc"},
},
}
@@ -341,11 +237,6 @@ func TestListMigrations_ParseErrDuringSort(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: tc.versionStyle,
- },
- },
bolttest.NullOutputter{},
)
migrations, err := svc.ListMigrations(SortOrderAsc)
@@ -356,38 +247,7 @@ func TestListMigrations_ParseErrDuringSort(t *testing.T) {
}
}
-func TestCreateMigration_VersionStyleTimestamp(t *testing.T) {
- migrationFsRepo := &bolttest.MockMigrationFsRepo{
- CreateReturnValue: bolttest.CreateReturnValue{
- Err: nil,
- },
- }
- migrationDbRepo := &bolttest.MockMigrationDBRepo{}
- svc := NewMigrationService(
- migrationDbRepo,
- migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleTimestamp,
- },
- },
- bolttest.NullOutputter{},
- )
-
- beforeCreateTs := time.Now().Add(30 * -time.Second)
- migration, err := svc.CreateMigration("new migration")
- afterCreateTs := time.Now().Add(30 * time.Second)
-
- assert.Nil(t, err)
- version, err := time.ParseInLocation("20060102150405", migration.Version, time.Local)
- assert.Nil(t, err)
- check.True(t, beforeCreateTs.Before(version))
- check.True(t, afterCreateTs.After(version))
- check.Equal(t, migration.Message, "new migration")
- check.Equal(t, migration.Applied, false)
-}
-
-func TestCreateMigration_VersionStyleSequential(t *testing.T) {
+func TestCreateMigration(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
CreateReturnValue: bolttest.CreateReturnValue{
Err: nil,
@@ -397,11 +257,6 @@ func TestCreateMigration_VersionStyleSequential(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -422,11 +277,6 @@ func TestCreateMigration_NilDbRepo(t *testing.T) {
svc := NewMigrationService(
nil,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -449,11 +299,6 @@ func TestCreateMigration_CreateErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -474,11 +319,6 @@ func TestCreateMigration_LatestErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -488,7 +328,7 @@ func TestCreateMigration_LatestErr(t *testing.T) {
assert.Nil(t, migration)
}
-func TestCreateMigration_SequentialVersionIncrements(t *testing.T) {
+func TestCreateMigration_VersionIncrements(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
Migrations: map[string]*models.Migration{
@@ -504,11 +344,6 @@ func TestCreateMigration_SequentialVersionIncrements(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -518,7 +353,7 @@ func TestCreateMigration_SequentialVersionIncrements(t *testing.T) {
assert.Equal(t, migration.Version, "002")
}
-func TestCreateMigration_SequentialVersionIncrementsLatestVersion(t *testing.T) {
+func TestCreateMigration_VersionIncrementsLatestVersion(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
Migrations: map[string]*models.Migration{
@@ -536,11 +371,6 @@ func TestCreateMigration_SequentialVersionIncrementsLatestVersion(t *testing.T)
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -550,7 +380,7 @@ func TestCreateMigration_SequentialVersionIncrementsLatestVersion(t *testing.T)
assert.Equal(t, migration.Version, "004")
}
-func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) {
+func TestCreateMigration_VersionIncrementsParsingErr(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
Migrations: map[string]*models.Migration{
@@ -566,11 +396,6 @@ func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -599,7 +424,6 @@ func TestApplyMigration_WithTransaction(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -629,7 +453,6 @@ func TestApplyMigration_WithoutTransaction(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -653,7 +476,6 @@ func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -680,7 +502,6 @@ func TestApplyMigration_ApplyWithTxErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -707,7 +528,6 @@ func TestApplyMigration_ApplyErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -727,7 +547,6 @@ func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -751,11 +570,6 @@ func TestApplyAllMigrations_AppliesNothingIfAllApplied(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -784,11 +598,6 @@ func TestApplyAllMigrations_ApplyMigrationErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -810,7 +619,6 @@ func TestApplyUpToVersion_ListMigrationsErr(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -832,7 +640,6 @@ func TestApplyUpToVersion_TargetMigrationNotFound(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -854,7 +661,6 @@ func TestApplyUpToVersion_TargetMigrationAlreadyApplied(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -880,7 +686,6 @@ func TestApplyUpToVersion_ApplyMigrationErr(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -904,11 +709,6 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -937,7 +737,6 @@ func TestRevertMigration_WithTransaction(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -966,7 +765,6 @@ func TestRevertMigration_WithoutTransaction(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -989,7 +787,6 @@ func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1016,7 +813,6 @@ func TestRevertMigration_RevertWithTxErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1043,7 +839,6 @@ func TestRevertMigration_RevertErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1063,7 +858,6 @@ func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1085,7 +879,6 @@ func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1107,7 +900,6 @@ func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1133,7 +925,6 @@ func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1157,11 +948,6 @@ func TestRevertDownToVersion_RevertsMigrations(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -1182,7 +968,6 @@ func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) {
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
bolttest.NullOutputter{},
)
@@ -1206,11 +991,6 @@ func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
@@ -1239,11 +1019,6 @@ func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) {
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
bolttest.NullOutputter{},
)
diff --git a/man/bolt-down.1 b/man/bolt-down.1
index 18be02c..e99eeb1 100644
--- a/man/bolt-down.1
+++ b/man/bolt-down.1
@@ -34,7 +34,7 @@ Revert all applied migrations:
.Dl $ bolt down
.Pp
Revert migrations down to a specific version:
-.Dl $ bolt down -v 20240316145038
+.Dl $ bolt down -v 002
.Sh SEE ALSO
.Xr bolt 1 ,
.Xr bolt-status 1 ,
diff --git a/man/bolt-migration.5 b/man/bolt-migration.5
index 92728e8..92d1f37 100644
--- a/man/bolt-migration.5
+++ b/man/bolt-migration.5
@@ -15,13 +15,13 @@ Migration files are named using the pattern:
.Pp
Where
.Ar version
-is either a timestamp (e.g.,
-.Li 20240316145038 )
-or sequential number (e.g.,
-.Li 001 )
-depending on the configured version style. See
-.Xr bolt.toml 5
-for configuration details.
+is a sequential number (e.g.,
+.Li 001 ) .
+.Xr bolt-new 1
+picks the next number automatically.
+.Pp
+Each version must be unique. If two migrations exist with the
+same version, Bolt refuses to run until one of them is renumbered.
.Sh DIRECTIVES
Migration files use comment directives to separate sections. All directives are
case-insensitive.
diff --git a/man/bolt-new.1 b/man/bolt-new.1
index 4e83be0..d44367a 100644
--- a/man/bolt-new.1
+++ b/man/bolt-new.1
@@ -12,9 +12,9 @@
.Nm
creates a new database migration file in the migrations directory.
.Pp
-The migration file is named using the configured version style (timestamp
-or sequential) followed by the message. For example:
-.Pa 20240316145038_create_users_table.sql .
+The migration file is named with the next sequential version followed by
+the message. For example:
+.Pa 001_create_users_table.sql .
.Sh OPTIONS
.Bl -tag -width Ds
.It Fl m Ar message , Fl -message Ar message
diff --git a/man/bolt-status.1 b/man/bolt-status.1
index e07c404..c1677c2 100644
--- a/man/bolt-status.1
+++ b/man/bolt-status.1
@@ -11,7 +11,6 @@
displays all discovered migrations and indicates which ones have been applied
to the database.
.Pp
-Migrations are shown in the order they would be executed, by version.
The output includes:
.Bl -bullet -offset Ds -compact
.It
@@ -29,9 +28,9 @@ exits 0 on success, and >0 if an error occurs.
.Sh EXAMPLES
.Bd -literal -offset indent
$ bolt status
-Version Message Applied
-20240316145038 create_users_table X
-20240317120000 add_email_index
+Version Message Applied
+001 create_users_table X
+002 add_email_index
.Ed
.Sh SEE ALSO
.Xr bolt 1 ,
diff --git a/man/bolt-tutorial.7 b/man/bolt-tutorial.7
index 0a137a0..ae2446e 100644
--- a/man/bolt-tutorial.7
+++ b/man/bolt-tutorial.7
@@ -16,6 +16,19 @@ Bolt installed
.It
A database available (PostgreSQL, MySQL, SQL Server, or SQLite)
.El
+.Sh CONFIGURING BOLT
+Create a
+.Pa bolt.toml
+file:
+.Bd -literal -offset indent
+[database]
+dsn = "bolt.db"
+driver = "sqlite3"
+.Ed
+.Pp
+See
+.Xr bolt.toml 5
+for all configuration options.
.Sh CREATING YOUR FIRST MIGRATION
Create a migration with:
.Dl $ bolt new -m \(dqcreate users table\(dq
@@ -23,7 +36,7 @@ Create a migration with:
This creates a
.Pa migrations/
directory with a file like:
-.Dl 20240316145038_create_users_table.sql
+.Dl 001_create_users_table.sql
.Sh WRITING MIGRATION SCRIPTS
Edit the migration file to add SQL statements:
.Bd -literal -offset indent
@@ -45,25 +58,12 @@ section runs when applying the migration.
The
.Cm -- migrate:down
section runs when reverting it.
-.Sh CONFIGURING BOLT
-Create a
-.Pa bolt.toml
-file:
-.Bd -literal -offset indent
-[database]
-dsn = "postgresql://user:password@localhost:5432/mydb?sslmode=disable"
-driver = "postgresql"
-.Ed
-.Pp
-See
-.Xr bolt.toml 5
-for all configuration options.
.Sh APPLYING MIGRATIONS
Apply all pending migrations:
.Dl $ bolt up
.Pp
Apply up to a specific version:
-.Dl $ bolt up -v 20240316145038
+.Dl $ bolt up -v 001
.Sh CHECKING STATUS
View migration status:
.Dl $ bolt status
@@ -74,7 +74,7 @@ Revert the most recent migration:
.Dl $ bolt down
.Pp
Revert to a specific version:
-.Dl $ bolt down -v 20240316145038
+.Dl $ bolt down -v 001
.Sh SEE ALSO
.Xr bolt 1 ,
.Xr bolt.toml 5 ,
diff --git a/man/bolt-up.1 b/man/bolt-up.1
index 19b41e9..df149b9 100644
--- a/man/bolt-up.1
+++ b/man/bolt-up.1
@@ -35,7 +35,7 @@ Apply all pending migrations:
.Dl $ bolt up
.Pp
Apply migrations up to a specific version:
-.Dl $ bolt up -v 20240316145038
+.Dl $ bolt up -v 002
.Sh SEE ALSO
.Xr bolt 1 ,
.Xr bolt-down 1 ,
diff --git a/man/bolt.1 b/man/bolt.1
index 74d65d9..67a86fe 100644
--- a/man/bolt.1
+++ b/man/bolt.1
@@ -16,7 +16,7 @@ is a command-line tool for database migrations.
.It
Schema migrations are written in plain SQL.
.It
-Migrations can be versioned sequentially or by creation timestamp.
+Migrations are versioned sequentially.
.It
All migrations run in transactions by default.
.It
diff --git a/man/bolt.toml.5 b/man/bolt.toml.5
index 15858ff..ef80875 100644
--- a/man/bolt.toml.5
+++ b/man/bolt.toml.5
@@ -31,17 +31,6 @@ absolute path.
.Pp
Environment variable:
.Ev BOLT_MIGRATIONS_DIR_PATH
-.It Cm version_style
-The migration versioning style.
-.Qq timestamp
-(default) uses timestamps like
-.Li 20240316145038 .
-.Qq sequential
-uses incrementing integers like
-.Li 001 , 002 , 003 .
-.Pp
-Environment variable:
-.Ev BOLT_MIGRATIONS_VERSION_STYLE
.El
.Sh DATABASE SECTION
.Bl -tag -width Ds
@@ -74,10 +63,9 @@ All configuration options can be set via environment variables. When both
a configuration file setting and environment variable are present, the
environment variable takes precedence.
.Pp
-.Bl -column "BOLT_MIGRATIONS_VERSION_STYLE" "migrations.version_style" -offset indent
+.Bl -column "BOLT_MIGRATIONS_DIR_PATH" "migrations.directory_path" -offset indent
.It Sy Environment Variable Ta Sy Configuration Key
.It Ev BOLT_MIGRATIONS_DIR_PATH Ta Cm migrations.directory_path
-.It Ev BOLT_MIGRATIONS_VERSION_STYLE Ta Cm migrations.version_style
.It Ev BOLT_DB_DSN Ta Cm database.dsn
.It Ev BOLT_DB_DRIVER Ta Cm database.driver
.It Ev BOLT_DB_MIGRATIONS_TABLE Ta Cm database.migrations_table
@@ -87,7 +75,6 @@ A complete configuration file:
.Bd -literal -offset indent
[migrations]
directory_path = "migrations"
-version_style = "timestamp"
[database]
dsn = "postgresql://user:password@localhost:5432/mydb?sslmode=disable"