From db7de4b31b1836abed822077840665639f0d66da Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Fri, 19 Jan 2024 23:27:27 -0500 Subject: Add ability to use sequential or timestamp-based versions --- internal/services/migration.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'internal/services') 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 -- cgit v1.2.3 From b8b23e393dcda33cc98b22f4083977b543861bf2 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 28 Jan 2024 17:29:19 -0500 Subject: Add test for invalid version style in config --- internal/services/migration.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'internal/services') diff --git a/internal/services/migration.go b/internal/services/migration.go index 91921bc..6836980 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -198,7 +198,10 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) CreateMigration(versionStyle configloader.VersionStyle, message string) error { +func (ms *MigrationService) CreateMigration( + versionStyle configloader.VersionStyle, + message string, +) error { var migration *models.Migration if versionStyle == configloader.VersionStyleTimestamp { -- cgit v1.2.3 From 052bfc3c1f25ff0f4a9506472c50857dc07a8664 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 25 Feb 2024 15:56:37 -0500 Subject: Add error handling to unhandled errors during migration apply/revert --- internal/services/migration.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'internal/services') diff --git a/internal/services/migration.go b/internal/services/migration.go index 6836980..b6b00bb 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -41,7 +41,14 @@ func (ms *MigrationService) ApplyAllMigrations() error { for _, migration := range migrations { if !migration.Applied { - ms.ApplyMigration(migration) + err := ms.ApplyMigration(migration) + if err != nil { + return fmt.Errorf( + "unable to apply migration %s: %w", + migration.Name(), + err, + ) + } } } @@ -75,7 +82,11 @@ func (ms *MigrationService) ApplyUpToVersion(version string) error { if !migration.Applied { err := ms.ApplyMigration(migration) if err != nil { - return err + return fmt.Errorf( + "unable to apply migration %s: %w", + migration.Name(), + err, + ) } } @@ -124,7 +135,14 @@ func (ms *MigrationService) RevertAllMigrations() error { for _, migration := range migrations { if migration.Applied { - ms.RevertMigration(migration) + err := ms.RevertMigration(migration) + if err != nil { + return fmt.Errorf( + "unable to revert migration %s: %w", + migration.Name(), + err, + ) + } } } @@ -158,7 +176,11 @@ func (ms *MigrationService) RevertDownToVersion(version string) error { if migration.Applied { err := ms.RevertMigration(migration) if err != nil { - return err + return fmt.Errorf( + "unable to revert migration %s: %w", + migration.Name(), + err, + ) } } -- cgit v1.2.3 From bf1d3b3e1fc42132678b244e479839416a03a6ce Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Thu, 29 Feb 2024 19:53:20 -0500 Subject: Add service tests --- internal/services/migration.go | 233 +++++++++++--- internal/services/migration_test.go | 595 ++++++++++++++++++++++++++++++++++++ 2 files changed, 781 insertions(+), 47 deletions(-) create mode 100644 internal/services/migration_test.go (limited to 'internal/services') diff --git a/internal/services/migration.go b/internal/services/migration.go index b6b00bb..48859d6 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -1,29 +1,62 @@ package services import ( + "database/sql" "fmt" "sort" "strconv" + "strings" "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/sqlparse" ) type MigrationService struct { - dbRepo *repositories.MigrationDBRepo - fsRepo *repositories.MigrationFsRepo + dbRepo repositories.MigrationDBRepo + fsRepo repositories.MigrationFsRepo + cfg configloader.Config outputter output.Outputter } func NewMigrationService( - dbRepo *repositories.MigrationDBRepo, - fsRepo *repositories.MigrationFsRepo, + dbRepo repositories.MigrationDBRepo, + fsRepo repositories.MigrationFsRepo, + cfg configloader.Config, outputter output.Outputter, -) *MigrationService { - return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo, outputter: outputter} +) MigrationService { + return MigrationService{ + dbRepo: dbRepo, + fsRepo: fsRepo, + cfg: cfg, + outputter: outputter, + } +} + +func NewMigrationServiceFromConfig( + db *sql.DB, + outputter output.Outputter, + cfg configloader.Config, +) (MigrationService, error) { + migrationDBRepo, err := repositories.NewMigrationDBRepo(db) + if err != nil { + return MigrationService{}, err + } + + migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) + if err != nil { + return MigrationService{}, err + } + + return NewMigrationService( + migrationDBRepo, + migrationFsRepo, + cfg, + outputter, + ), nil } type sortOrder int @@ -33,7 +66,7 @@ const ( SortOrderAsc ) -func (ms *MigrationService) ApplyAllMigrations() error { +func (ms MigrationService) ApplyAllMigrations() error { migrations, err := ms.ListMigrations(SortOrderAsc) if err != nil { return err @@ -55,7 +88,7 @@ func (ms *MigrationService) ApplyAllMigrations() error { return nil } -func (ms *MigrationService) ApplyUpToVersion(version string) error { +func (ms MigrationService) ApplyUpToVersion(version string) error { migrations, err := ms.ListMigrations(SortOrderAsc) if err != nil { return err @@ -98,7 +131,7 @@ func (ms *MigrationService) ApplyUpToVersion(version string) error { return nil } -func (ms *MigrationService) ApplyMigration(migration *models.Migration) error { +func (ms MigrationService) ApplyMigration(migration *models.Migration) error { ms.outputter.Output( fmt.Sprintf( "Applying migration %s_%s..", @@ -112,10 +145,22 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error { return err } - err = ms.dbRepo.Apply(scriptContents, migration) + sqlParser := sqlparse.NewSqlParser() + execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents)) if err != nil { - return err + return fmt.Errorf("unable to parse sql file for upgrade script: %w", err) } + + if execOptions.UseTransaction { + err = ms.dbRepo.ApplyWithTx(scriptContents, migration) + } else { + err = ms.dbRepo.Apply(scriptContents, migration) + } + + if err != nil { + return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err) + } + ms.outputter.Output( fmt.Sprintf( "Successfully applied migration %s_%s!", @@ -127,7 +172,7 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) RevertAllMigrations() error { +func (ms MigrationService) RevertAllMigrations() error { migrations, err := ms.ListMigrations(SortOrderDesc) if err != nil { return err @@ -149,7 +194,7 @@ func (ms *MigrationService) RevertAllMigrations() error { return nil } -func (ms *MigrationService) RevertDownToVersion(version string) error { +func (ms MigrationService) RevertDownToVersion(version string) error { migrations, err := ms.ListMigrations(SortOrderDesc) if err != nil { return err @@ -192,7 +237,7 @@ func (ms *MigrationService) RevertDownToVersion(version string) error { return nil } -func (ms *MigrationService) RevertMigration(migration *models.Migration) error { +func (ms MigrationService) RevertMigration(migration *models.Migration) error { ms.outputter.Output( fmt.Sprintf( "Reverting migration %s_%s..", @@ -205,10 +250,22 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return err } - err = ms.dbRepo.Revert(scriptContents, migration) + sqlParser := sqlparse.NewSqlParser() + execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents)) + if err != nil { + return fmt.Errorf("unable to parse sql file for downgrade script: %w", err) + } + + if execOptions.UseTransaction { + err = ms.dbRepo.ApplyWithTx(scriptContents, migration) + } else { + err = ms.dbRepo.Revert(scriptContents, migration) + } + if err != nil { return err } + ms.outputter.Output( fmt.Sprintf( "Successfully reverted migration %s_%s!", @@ -220,70 +277,152 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error { return nil } -func (ms *MigrationService) CreateMigration( - versionStyle configloader.VersionStyle, - message string, -) error { +func (ms MigrationService) CreateMigration(message string) (*models.Migration, error) { var migration *models.Migration - if versionStyle == configloader.VersionStyleTimestamp { + if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleTimestamp { migration = models.NewTimestampMigration(time.Now(), message) } else { - var currentVerison uint64 = 0 - latestMigration, err := ms.fsRepo.Latest() + currentVersion, err := ms.getCurrentSequentialMigrationVersion() if err != nil { - return err + return nil, err } - if latestMigration != nil { - currentVerison, err = strconv.ParseUint(latestMigration.Version, 10, 64) - if err != nil { - return err - } - } - migration = models.NewSequentialMigration(currentVerison+1, message) + migration = models.NewSequentialMigration(currentVersion+1, message) } err := ms.fsRepo.Create(migration) if err != nil { - return err + return nil, err } ms.outputter.Output( fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message), ) - return nil + return migration, nil +} + +func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { + var currentVersion uint64 = 0 + + latestMigration, err := ms.fsRepo.Latest() + if err != nil { + return 0, err + } + + if latestMigration != nil { + currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64) + if err != nil { + return 0, err + } + } + + return currentVersion, nil } -func (ms *MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) { +func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) { // Assumption: All migration versions that have been applied // to the database exist locally in the list of filesystem migrations. // This would hold true unless someone decided to apply a migration and, - // for whatever reason, delete it later from their filesystem. - migrations, err := ms.fsRepo.List() + // for whatever reason, delete it later from their filesystem. If there is + // an applied migration that doesn't exist locally, it would not be shown + // here. + localMigrations, err := ms.fsRepo.List() if err != nil { - return nil, err + return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err) } appliedMigrations, err := ms.dbRepo.List() if err != nil { - return nil, err + return nil, fmt.Errorf( + "unable to list out applied migrations from remote db: %w", + err, + ) } - var flatMigrations []*models.Migration - for _, migration := range migrations { - _, ok := appliedMigrations[migration.Version] + migrations := make([]*models.Migration, 0) + for _, localMigration := range localMigrations { + _, ok := appliedMigrations[localMigration.Version] if ok { - migration.Applied = true + localMigration.Applied = true } - flatMigrations = append(flatMigrations, migration) + migrations = append(migrations, localMigration) } - sort.Slice(flatMigrations, func(i, j int) bool { - if order == SortOrderAsc { - return flatMigrations[i].Version < flatMigrations[j].Version + err = ms.sortMigrations(migrations, order) + if err != nil { + return nil, err + } + + return migrations, err +} + +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 { - return flatMigrations[i].Version > flatMigrations[j].Version + comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order) } + + if err != nil { + sortErrs = append(sortErrs, err) + } + + return comparison }) + 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 + } - return flatMigrations, nil + 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 new file mode 100644 index 0000000..73045ef --- /dev/null +++ b/internal/services/migration_test.go @@ -0,0 +1,595 @@ +package services + +import ( + "errors" + "testing" + "time" + + "github.com/eugenetriguba/bolt/internal/bolttest" + "github.com/eugenetriguba/bolt/internal/configloader" + "github.com/eugenetriguba/bolt/internal/models" + "github.com/eugenetriguba/checkmate/assert" + "github.com/eugenetriguba/checkmate/check" +) + +func TestListMigrations(t *testing.T) { + type test struct { + localFilesystemMigrations []*models.Migration + remoteAppliedMigrations []*models.Migration + cfg configloader.Config + sortOrder sortOrder + expectedMigrations []*models.Migration + } + + tests := []test{ + // Ensure no local or remote applied migrations means none are returned. + { + localFilesystemMigrations: []*models.Migration{}, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{}, + }, + // Ensure one local fs migration shows up as expected. + { + localFilesystemMigrations: []*models.Migration{ + {Version: "001", Applied: false}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{ + {Version: "001", 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}, + }, + 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}, + }, + }, + // Ensure that any applied migrations that don't exist locally + // do not show up in the response. + { + localFilesystemMigrations: []*models.Migration{}, + remoteAppliedMigrations: []*models.Migration{ + {Version: "001", Applied: true}, + }, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{}, + }, + // Ensure that migrations are sorted in asc order for sequential migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "20000"}, + {Version: "10000"}, + {Version: "1010"}, + {Version: "1009"}, + {Version: "190"}, + {Version: "110"}, + {Version: "001"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{ + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, + }, + }, + // Ensure that migrations are sorted in desc order for sequential migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + sortOrder: SortOrderDesc, + expectedMigrations: []*models.Migration{ + {Version: "20000"}, + {Version: "10000"}, + {Version: "1010"}, + {Version: "1009"}, + {Version: "190"}, + {Version: "110"}, + {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 { + localFsMigrationMap := make(map[string]*models.Migration, 0) + for _, localFsMigration := range tc.localFilesystemMigrations { + localFsMigrationMap[localFsMigration.Version] = localFsMigration + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: localFsMigrationMap, + Err: nil, + }, + } + + remoteAppliedMigrationMap := make(map[string]*models.Migration, 0) + for _, remoteAppliedMigration := range tc.remoteAppliedMigrations { + remoteAppliedMigrationMap[remoteAppliedMigration.Version] = remoteAppliedMigration + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: remoteAppliedMigrationMap, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + tc.cfg, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(tc.sortOrder) + + assert.Nil(t, err) + assert.DeepEqual(t, migrations, tc.expectedMigrations) + } +} + +func TestListMigrations_FsRepoListError(t *testing.T) { + expectedErr := errors.New("fs repo error") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorIs(t, err, expectedErr) + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) +} + +func TestListMigrations_DbRepoListError(t *testing.T) { + expectedErr := errors.New("db repo error") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorIs(t, err, expectedErr) + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) +} + +func TestListMigrations_ParseErrDuringSort(t *testing.T) { + type test struct { + m1 models.Migration + m2 models.Migration + versionStyle configloader.VersionStyle + } + + 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: "20060102150405"}, + versionStyle: configloader.VersionStyleTimestamp, + }, + } + + for _, tc := range tests { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + tc.m1.Version: {Version: tc.m1.Version}, + tc.m2.Version: {Version: tc.m2.Version}, + }, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: tc.versionStyle, + }, + }, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorContains(t, err, "unable to sort migrations") + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) + } +} + +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(-time.Second) + migration, err := svc.CreateMigration("new migration") + afterCreateTs := time.Now().Add(time.Second) + + assert.Nil(t, err) + version, err := time.Parse("20060102150405", migration.Version) + 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) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + 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) + check.Equal(t, migration.Version, "001") + check.Equal(t, migration.Message, "new migration") + check.Equal(t, migration.Applied, false) +} + +func TestCreateMigration_CreateErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + CreateReturnValue: bolttest.CreateReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, migration) +} + +func TestCreateMigration_LatestErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + LatestReturnValue: bolttest.LatestReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + migration, err := svc.CreateMigration("new migration") + + assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, migration) +} + +func TestCreateMigration_SequentialVersionIncrements(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + LatestReturnValue: bolttest.LatestReturnValue{ + Migration: &models.Migration{Version: "001"}, + 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, "002") +} + +func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + LatestReturnValue: bolttest.LatestReturnValue{ + Migration: &models.Migration{Version: "abc"}, + 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.ErrorContains(t, err, `parsing "abc": invalid syntax`) + assert.Nil(t, migration) +} + +func TestApplyMigration_WithTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + ScriptContents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) +} + +func TestApplyMigration_WithoutTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + ScriptContents: "-- bolt: no-transaction\nCREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyReturnValue: bolttest.ApplyReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 1) +} + +func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + ScriptContents: "", + Err: expectedErr, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} -- cgit v1.2.3 From 55d3999d9efdeb0a87959e93d473cb64c8ba8c48 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 07:35:21 -0500 Subject: Add proper tests for ApplyAllMigrations and ApplyUpToVersion --- internal/services/migration.go | 2 + internal/services/migration_test.go | 202 ++++++++++++++++++++++++++++++++++++ 2 files changed, 204 insertions(+) (limited to 'internal/services') diff --git a/internal/services/migration.go b/internal/services/migration.go index 48859d6..c0d043b 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -105,6 +105,8 @@ func (ms MigrationService) ApplyUpToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if targetMigration.Applied { + // Assumption: If the target migration is applied, all migrations before + // it must also be applied. return fmt.Errorf( "migration with version %s is already applied, nothing to apply", version, diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 73045ef..e3d3244 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -593,3 +593,205 @@ func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { assert.ErrorIs(t, err, expectedErr) } + +func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyAllMigrations() + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyAllMigrations_AppliesNothingIfAllApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + "002": {Version: "002", Applied: true}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyAllMigrations() + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) +} + +func TestApplyAllMigrations_ApplyMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyAllMigrations() + + assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) +} + +func TestApplyUpToVersion_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("001") + + assert.ErrorIs(t, err, expectedErr) +} + +func TestApplyUpToVersion_TargetMigrationNotFound(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("002") + + assert.ErrorContains(t, err, "migration with version 002 does not exist") +} + +func TestApplyUpToVersion_TargetMigrationAlreadyApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("001") + + assert.ErrorContains(t, err, "migration with version 001 is already applied") +} + +func TestApplyUpToVersion_ApplyMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("001") + + 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}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.ApplyUpToVersion("002") + + assert.Nil(t, err) + assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) +} -- cgit v1.2.3 From 75a1bfa521ef1e10514330b984f46b84e1cef926 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 08:31:26 -0500 Subject: Remove NewMigrationServiceFromConfig --- internal/services/migration.go | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) (limited to 'internal/services') diff --git a/internal/services/migration.go b/internal/services/migration.go index c0d043b..bc4b9d1 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -1,7 +1,6 @@ package services import ( - "database/sql" "fmt" "sort" "strconv" @@ -36,29 +35,6 @@ func NewMigrationService( } } -func NewMigrationServiceFromConfig( - db *sql.DB, - outputter output.Outputter, - cfg configloader.Config, -) (MigrationService, error) { - migrationDBRepo, err := repositories.NewMigrationDBRepo(db) - if err != nil { - return MigrationService{}, err - } - - migrationFsRepo, err := repositories.NewMigrationFsRepo(&cfg.Migrations) - if err != nil { - return MigrationService{}, err - } - - return NewMigrationService( - migrationDBRepo, - migrationFsRepo, - cfg, - outputter, - ), nil -} - type sortOrder int const ( @@ -259,7 +235,7 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error { } if execOptions.UseTransaction { - err = ms.dbRepo.ApplyWithTx(scriptContents, migration) + err = ms.dbRepo.RevertWithTx(scriptContents, migration) } else { err = ms.dbRepo.Revert(scriptContents, migration) } -- cgit v1.2.3 From 4daf960b457fe1ab27e405c1c1d56e9cfc7c904e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 08:50:27 -0500 Subject: Add proper tests to revert service methods --- internal/services/migration.go | 2 + internal/services/migration_test.go | 276 ++++++++++++++++++++++++++++++++++++ 2 files changed, 278 insertions(+) (limited to 'internal/services') diff --git a/internal/services/migration.go b/internal/services/migration.go index bc4b9d1..446f267 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -189,6 +189,8 @@ func (ms MigrationService) RevertDownToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if !targetMigration.Applied { + // Assumption: Every migration from the latest down to the target + // migration hasn't been applied. return fmt.Errorf( "migration with version %s isn't applied, nothing to revert", version, diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index e3d3244..1087398 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -795,3 +795,279 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { assert.Nil(t, err) assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) } + +func TestRevertMigration_WithTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + ScriptContents: "DROP TABLE tmp;", + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) +} + +func TestRevertMigration_WithoutTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + ScriptContents: "-- bolt: no-transaction\nDROP TABLE tmp;", + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertReturnValue: bolttest.RevertReturnValue{ + Err: nil, + }, + } + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertCallCount, 1) +} + +func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + ScriptContents: "", + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertMigration(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("002") + + assert.ErrorContains(t, err, "migration with version 002 does not exist") +} + +func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + assert.ErrorContains(t, err, "migration with version 001 isn't applied") +} + +func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + 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}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.RevertDownToVersion("001") + + assert.Nil(t, err) + assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2) +} + +func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + + err := svc.RevertAllMigrations() + + assert.ErrorIs(t, err, expectedErr) +} + +func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + "002": {Version: "002", Applied: false}, + } + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.RevertAllMigrations() + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) +} + +func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + } + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, + bolttest.NullOutputter{}, + ) + + err := svc.RevertAllMigrations() + + assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) +} -- cgit v1.2.3 From 018083b9259c2265ad3ea2432f315641e5b996ce Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 10:18:41 -0500 Subject: Clean up fs repo and fs repo tests --- internal/services/migration.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'internal/services') diff --git a/internal/services/migration.go b/internal/services/migration.go index 446f267..a91ae2a 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -335,7 +335,10 @@ func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, return migrations, err } -func (ms MigrationService) sortMigrations(migrations []*models.Migration, order sortOrder) error { +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 -- cgit v1.2.3 From a37df93853def914dbda165cdcf09ae8e0305ec3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 2 Mar 2024 11:04:00 -0500 Subject: Remove Latest() from migration fs repo in favor of using List on service --- internal/services/migration.go | 5 ++-- internal/services/migration_test.go | 50 +++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 9 deletions(-) (limited to 'internal/services') 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, -- cgit v1.2.3