From 7026e3861a5712ced6f2006f282c8386b4db106e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 13 Nov 2026 22:20:16 -0800 Subject: Pad new migrations to 4 digits --- CHANGELOG.md | 4 + README.md | 6 +- internal/models/migration.go | 2 +- internal/models/migration_test.go | 8 +- internal/repositories/migration_fs_repo_test.go | 2 +- internal/services/migration_test.go | 100 ++++++++++++------------ man/bolt-down.1 | 2 +- man/bolt-migration.5 | 2 +- man/bolt-new.1 | 2 +- man/bolt-status.1 | 4 +- man/bolt-tutorial.7 | 6 +- man/bolt-up.1 | 2 +- 12 files changed, 72 insertions(+), 68 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed96f26..6ab8e34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- New migrations are zero-padded to four digits. + ### Removed - **Migration dependencies**: The `-- depends:` directive and dependency-based diff --git a/README.md b/README.md index 532cd3a..9aec4f0 100644 --- a/README.md +++ b/README.md @@ -40,9 +40,9 @@ DROP TABLE posts; ```bash $ bolt status Version Message Applied -001 create_users_table X -002 create_posts_table X -003 add_email_index +0001 create_users_table X +0002 create_posts_table X +0003 add_email_index ``` ## Installation diff --git a/internal/models/migration.go b/internal/models/migration.go index 44608f9..74a1536 100644 --- a/internal/models/migration.go +++ b/internal/models/migration.go @@ -13,7 +13,7 @@ type Migration struct { func NewMigration(version uint64, message string) *Migration { return &Migration{ - Version: fmt.Sprintf("%03d", version), + Version: fmt.Sprintf("%04d", version), Message: message, Applied: false, } diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go index 87b98e4..91aed2e 100644 --- a/internal/models/migration_test.go +++ b/internal/models/migration_test.go @@ -10,13 +10,13 @@ import ( func TestNewMigration(t *testing.T) { m := models.NewMigration(1, "test message") - check.Equal(t, m.Version, "001") + check.Equal(t, m.Version, "0001") check.Equal(t, m.Message, "test message") check.Equal(t, m.Applied, false) m = models.NewMigration(100, "test message") - check.Equal(t, m.Version, "100") + check.Equal(t, m.Version, "0100") check.Equal(t, m.Message, "test message") check.Equal(t, m.Applied, false) } @@ -31,12 +31,12 @@ func TestName(t *testing.T) { // Underscores are used to separate version and message. { migration: models.NewMigration(1, "test_message"), - expectedName: "001_test_message", + expectedName: "0001_test_message", }, // Leading/Trailing spaces trimmed, lowercased, and spaces converted to underscores. { migration: models.NewMigration(1, " teST mesSAGE "), - expectedName: "001_test_message", + expectedName: "0001_test_message", }, } diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index c3c71c4..4e0936d 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -221,6 +221,6 @@ func TestList_DuplicateMigrationVersion(t *testing.T) { assert.ErrorContains( t, err, - "001_migration_1 and 001_migration_2 both have version 001; renumber one of them", + "0001_migration_1 and 0001_migration_2 both have version 0001; renumber one of them", ) } diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 0496299..4b5bdf0 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -31,26 +31,26 @@ func TestListMigrations(t *testing.T) { // Ensure one local fs migration shows up as expected. { localFilesystemMigrations: []*models.Migration{ - {Version: "001", Applied: false}, + {Version: "0001", Applied: false}, }, remoteAppliedMigrations: []*models.Migration{}, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ - {Version: "001", Applied: false}, + {Version: "0001", Applied: false}, }, }, // Ensure that one local fs migration and one remote applied migration // for the same version returns one applied migration. { localFilesystemMigrations: []*models.Migration{ - {Version: "001", Applied: false}, + {Version: "0001", Applied: false}, }, remoteAppliedMigrations: []*models.Migration{ - {Version: "001", Applied: true}, + {Version: "0001", Applied: true}, }, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ - {Version: "001", Applied: true}, + {Version: "0001", Applied: true}, }, }, // Ensure that any applied migrations that don't exist locally @@ -58,7 +58,7 @@ func TestListMigrations(t *testing.T) { { localFilesystemMigrations: []*models.Migration{}, remoteAppliedMigrations: []*models.Migration{ - {Version: "001", Applied: true}, + {Version: "0001", Applied: true}, }, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{}, @@ -72,12 +72,12 @@ func TestListMigrations(t *testing.T) { {Version: "1009"}, {Version: "190"}, {Version: "110"}, - {Version: "001"}, + {Version: "0001"}, }, remoteAppliedMigrations: []*models.Migration{}, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ - {Version: "001"}, + {Version: "0001"}, {Version: "110"}, {Version: "190"}, {Version: "1009"}, @@ -89,7 +89,7 @@ func TestListMigrations(t *testing.T) { // Ensure that migrations are sorted in desc order for sequential migrations { localFilesystemMigrations: []*models.Migration{ - {Version: "001"}, + {Version: "0001"}, {Version: "110"}, {Version: "190"}, {Version: "1009"}, @@ -106,7 +106,7 @@ func TestListMigrations(t *testing.T) { {Version: "1009"}, {Version: "190"}, {Version: "110"}, - {Version: "001"}, + {Version: "0001"}, }, }, } @@ -263,7 +263,7 @@ func TestCreateMigration(t *testing.T) { migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) - check.Equal(t, migration.Version, "001") + check.Equal(t, migration.Version, "0001") check.Equal(t, migration.Message, "new migration") check.Equal(t, migration.Applied, false) } @@ -283,7 +283,7 @@ func TestCreateMigration_NilDbRepo(t *testing.T) { migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) - check.Equal(t, migration.Version, "001") + check.Equal(t, migration.Version, "0001") check.Equal(t, migration.Message, "new migration") check.Equal(t, migration.Applied, false) } @@ -332,7 +332,7 @@ func TestCreateMigration_VersionIncrements(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ - "001": {Version: "001"}, + "0001": {Version: "0001"}, }, Err: nil, }, @@ -350,16 +350,16 @@ func TestCreateMigration_VersionIncrements(t *testing.T) { migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) - assert.Equal(t, migration.Version, "002") + assert.Equal(t, migration.Version, "0002") } func TestCreateMigration_VersionIncrementsLatestVersion(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ - "001": {Version: "001"}, - "002": {Version: "002"}, - "003": {Version: "003"}, + "0001": {Version: "0001"}, + "0002": {Version: "0002"}, + "0003": {Version: "0003"}, }, Err: nil, }, @@ -377,7 +377,7 @@ func TestCreateMigration_VersionIncrementsLatestVersion(t *testing.T) { migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) - assert.Equal(t, migration.Version, "004") + assert.Equal(t, migration.Version, "0004") } func TestCreateMigration_VersionIncrementsParsingErr(t *testing.T) { @@ -557,8 +557,8 @@ func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) { func TestApplyAllMigrations_AppliesNothingIfAllApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, + "0001": {Version: "0001", Applied: true}, + "0002": {Version: "0002", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -582,7 +582,7 @@ func TestApplyAllMigrations_AppliesNothingIfAllApplied(t *testing.T) { func TestApplyAllMigrations_ApplyMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, + "0001": {Version: "0001", Applied: false}, } expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ @@ -622,14 +622,14 @@ func TestApplyUpToVersion_ListMigrationsErr(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("001") + err := svc.ApplyUpToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestApplyUpToVersion_TargetMigrationNotFound(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, + "0001": {Version: "0001", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -643,14 +643,14 @@ func TestApplyUpToVersion_TargetMigrationNotFound(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("002") + err := svc.ApplyUpToVersion("0002") - assert.ErrorContains(t, err, "migration with version 002 does not exist") + assert.ErrorContains(t, err, "migration with version 0002 does not exist") } func TestApplyUpToVersion_TargetMigrationAlreadyApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, + "0001": {Version: "0001", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -664,14 +664,14 @@ func TestApplyUpToVersion_TargetMigrationAlreadyApplied(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("001") + err := svc.ApplyUpToVersion("0001") - assert.ErrorContains(t, err, "migration with version 001 is already applied") + assert.ErrorContains(t, err, "migration with version 0001 is already applied") } func TestApplyUpToVersion_ApplyMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, + "0001": {Version: "0001", Applied: false}, } expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ @@ -689,16 +689,16 @@ func TestApplyUpToVersion_ApplyMigrationErr(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("001") + err := svc.ApplyUpToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, - "002": {Version: "002", Applied: false}, - "003": {Version: "003", Applied: false}, + "0001": {Version: "0001", Applied: false}, + "0002": {Version: "0002", Applied: false}, + "0003": {Version: "0003", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -712,7 +712,7 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("002") + err := svc.ApplyUpToVersion("0002") assert.Nil(t, err) assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) @@ -861,14 +861,14 @@ func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertDownToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, + "0001": {Version: "0001", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -882,14 +882,14 @@ func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("002") + err := svc.RevertDownToVersion("0002") - assert.ErrorContains(t, err, "migration with version 002 does not exist") + assert.ErrorContains(t, err, "migration with version 0002 does not exist") } func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, + "0001": {Version: "0001", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -903,14 +903,14 @@ func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertDownToVersion("0001") - assert.ErrorContains(t, err, "migration with version 001 isn't applied") + assert.ErrorContains(t, err, "migration with version 0001 isn't applied") } func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, + "0001": {Version: "0001", Applied: true}, } expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ @@ -928,16 +928,16 @@ func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertDownToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, - "003": {Version: "003", Applied: false}, + "0001": {Version: "0001", Applied: true}, + "0002": {Version: "0002", Applied: true}, + "0003": {Version: "0003", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -951,7 +951,7 @@ func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertDownToVersion("0001") assert.Nil(t, err) assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2) @@ -978,8 +978,8 @@ func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) { func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, - "002": {Version: "002", Applied: false}, + "0001": {Version: "0001", Applied: false}, + "0002": {Version: "0002", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1003,7 +1003,7 @@ func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, + "0001": {Version: "0001", Applied: true}, } expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ diff --git a/man/bolt-down.1 b/man/bolt-down.1 index e99eeb1..135009b 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 002 +.Dl $ bolt down -v 0002 .Sh SEE ALSO .Xr bolt 1 , .Xr bolt-status 1 , diff --git a/man/bolt-migration.5 b/man/bolt-migration.5 index 92d1f37..c0b1913 100644 --- a/man/bolt-migration.5 +++ b/man/bolt-migration.5 @@ -16,7 +16,7 @@ Migration files are named using the pattern: Where .Ar version is a sequential number (e.g., -.Li 001 ) . +.Li 0001 ) . .Xr bolt-new 1 picks the next number automatically. .Pp diff --git a/man/bolt-new.1 b/man/bolt-new.1 index d44367a..014a67a 100644 --- a/man/bolt-new.1 +++ b/man/bolt-new.1 @@ -14,7 +14,7 @@ creates a new database migration file in the migrations directory. .Pp The migration file is named with the next sequential version followed by the message. For example: -.Pa 001_create_users_table.sql . +.Pa 0001_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 c1677c2..0cb9d4f 100644 --- a/man/bolt-status.1 +++ b/man/bolt-status.1 @@ -29,8 +29,8 @@ exits 0 on success, and >0 if an error occurs. .Bd -literal -offset indent $ bolt status Version Message Applied -001 create_users_table X -002 add_email_index +0001 create_users_table X +0002 add_email_index .Ed .Sh SEE ALSO .Xr bolt 1 , diff --git a/man/bolt-tutorial.7 b/man/bolt-tutorial.7 index ae2446e..2448594 100644 --- a/man/bolt-tutorial.7 +++ b/man/bolt-tutorial.7 @@ -36,7 +36,7 @@ Create a migration with: This creates a .Pa migrations/ directory with a file like: -.Dl 001_create_users_table.sql +.Dl 0001_create_users_table.sql .Sh WRITING MIGRATION SCRIPTS Edit the migration file to add SQL statements: .Bd -literal -offset indent @@ -63,7 +63,7 @@ Apply all pending migrations: .Dl $ bolt up .Pp Apply up to a specific version: -.Dl $ bolt up -v 001 +.Dl $ bolt up -v 0001 .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 001 +.Dl $ bolt down -v 0001 .Sh SEE ALSO .Xr bolt 1 , .Xr bolt.toml 5 , diff --git a/man/bolt-up.1 b/man/bolt-up.1 index df149b9..5583aef 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 002 +.Dl $ bolt up -v 0002 .Sh SEE ALSO .Xr bolt 1 , .Xr bolt-down 1 , -- cgit v1.2.3