diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/bolttest/mock_migration_fs_repo.go | 12 | ||||
| -rw-r--r-- | internal/repositories/migration_fs_repo.go | 18 | ||||
| -rw-r--r-- | internal/repositories/migration_fs_repo_test.go | 26 | ||||
| -rw-r--r-- | internal/services/migration.go | 5 | ||||
| -rw-r--r-- | internal/services/migration_test.go | 50 |
5 files changed, 46 insertions, 65 deletions
diff --git a/internal/bolttest/mock_migration_fs_repo.go b/internal/bolttest/mock_migration_fs_repo.go index ae933e3..d392732 100644 --- a/internal/bolttest/mock_migration_fs_repo.go +++ b/internal/bolttest/mock_migration_fs_repo.go @@ -7,8 +7,6 @@ type MockMigrationFsRepo struct { CreateCallCount int ListReturnValue ListReturnValue ListCallCount int - LatestReturnValue LatestReturnValue - LatestCallCount int ReadUpgradeScriptReturnValue ReadUpgradeScriptReturnValue ReadUpgradeScriptCallCount int ReadDowngradeScriptReturnValue ReadDowngradeScriptReturnValue @@ -19,11 +17,6 @@ type CreateReturnValue struct { Err error } -type LatestReturnValue struct { - Migration *models.Migration - Err error -} - type ReadUpgradeScriptReturnValue struct { ScriptContents string Err error @@ -41,11 +34,6 @@ func (repo *MockMigrationFsRepo) List() (map[string]*models.Migration, error) { return repo.ListReturnValue.Migrations, repo.ListReturnValue.Err } -func (repo *MockMigrationFsRepo) Latest() (*models.Migration, error) { - repo.LatestCallCount += 1 - return repo.LatestReturnValue.Migration, repo.LatestReturnValue.Err -} - func (repo *MockMigrationFsRepo) ReadUpgradeScript( migration *models.Migration, ) (string, error) { diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go index 9d6c3aa..e535013 100644 --- a/internal/repositories/migration_fs_repo.go +++ b/internal/repositories/migration_fs_repo.go @@ -26,7 +26,6 @@ func (e *ErrIsNotDir) Error() string { type MigrationFsRepo interface { Create(migration *models.Migration) error List() (map[string]*models.Migration, error) - Latest() (*models.Migration, error) ReadUpgradeScript(migration *models.Migration) (string, error) ReadDowngradeScript(migration *models.Migration) (string, error) } @@ -82,23 +81,6 @@ func (mr migrationFsRepo) Create(migration *models.Migration) error { return nil } -func (mr migrationFsRepo) Latest() (*models.Migration, error) { - // TODO: ReadDir returns migrations sorted by filename. - // To be correct, it seems like we will need to sort these - // migrations by the version style that is configured and then - // grab the latest. - 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 { diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go index d10bddc..32de193 100644 --- a/internal/repositories/migration_fs_repo_test.go +++ b/internal/repositories/migration_fs_repo_test.go @@ -208,29 +208,3 @@ func TestList_InvalidMigrationName(t *testing.T) { "expected a migration directory of the format <version>_<message>", ) } - -func TestLatest_DirDoesNotExist(t *testing.T) { - migrationsDir := filepath.Join(t.TempDir(), "migrations") - migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} - repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.Nil(t, err) - err = os.RemoveAll(migrationsDir) - assert.Nil(t, err) - - _, err = repo.Latest() - - assert.NotNil(t, err) - assert.ErrorContains(t, err, "no such file or directory") -} - -func TestLatest_NoMigrations(t *testing.T) { - migrationsDir := filepath.Join(t.TempDir(), "migrations") - migrationsConfig := configloader.MigrationsConfig{DirectoryPath: migrationsDir} - repo, err := repositories.NewMigrationFsRepo(&migrationsConfig) - assert.Nil(t, err) - - latestMigration, err := repo.Latest() - - assert.Nil(t, err) - assert.Nil(t, latestMigration) -} 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, |