diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-02 11:04:00 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-03-02 11:04:00 -0500 |
| commit | a37df93853def914dbda165cdcf09ae8e0305ec3 (patch) | |
| tree | 41fb3a459db99d1f0ec2ed2e00d4ca580a5d0c46 /internal/services | |
| parent | improve wording (diff) | |
| download | cli-a37df93853def914dbda165cdcf09ae8e0305ec3.zip | |
Remove Latest() from migration fs repo in favor of using List on service
Diffstat (limited to 'internal/services')
| -rw-r--r-- | internal/services/migration.go | 5 | ||||
| -rw-r--r-- | internal/services/migration_test.go | 50 |
2 files changed, 46 insertions, 9 deletions
diff --git a/internal/services/migration.go b/internal/services/migration.go index a91ae2a..4be10cc 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -283,12 +283,13 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { var currentVersion uint64 = 0 - latestMigration, err := ms.fsRepo.Latest() + migrations, err := ms.ListMigrations(SortOrderDesc) if err != nil { return 0, err } - if latestMigration != nil { + if len(migrations) > 0 { + latestMigration := migrations[0] currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64) if err != nil { return 0, err diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 1087398..0c30c76 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -440,7 +440,7 @@ func TestCreateMigration_CreateErr(t *testing.T) { func TestCreateMigration_LatestErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ - LatestReturnValue: bolttest.LatestReturnValue{ + ListReturnValue: bolttest.ListReturnValue{ Err: expectedErr, }, } @@ -464,9 +464,11 @@ func TestCreateMigration_LatestErr(t *testing.T) { func TestCreateMigration_SequentialVersionIncrements(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - LatestReturnValue: bolttest.LatestReturnValue{ - Migration: &models.Migration{Version: "001"}, - Err: nil, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "001": {Version: "001"}, + }, + Err: nil, }, CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, @@ -490,11 +492,45 @@ func TestCreateMigration_SequentialVersionIncrements(t *testing.T) { assert.Equal(t, migration.Version, "002") } +func TestCreateMigration_SequentialVersionIncrementsLatestVersion(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "001": {Version: "001"}, + "002": {Version: "002"}, + "003": {Version: "003"}, + }, + Err: nil, + }, + CreateReturnValue: bolttest.CreateReturnValue{ + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.Nil(t, err) + assert.Equal(t, migration.Version, "004") +} + func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - LatestReturnValue: bolttest.LatestReturnValue{ - Migration: &models.Migration{Version: "abc"}, - Err: nil, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "abc": {Version: "abc"}, + }, + Err: nil, }, CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, |