package services import ( "errors" "testing" "git.eugenetriguba.com/bolt/cli/internal/bolttest" "git.eugenetriguba.com/bolt/cli/internal/models" "git.eugenetriguba.com/bolt/cli/internal/sqlparse" "git.eugenetriguba.com/checkmate/assert" "git.eugenetriguba.com/checkmate/check" ) func TestListMigrations(t *testing.T) { type test struct { localFilesystemMigrations []*models.Migration remoteAppliedMigrations []*models.Migration sortOrder sortOrder expectedMigrations []*models.Migration } tests := []test{ // Ensure no local or remote applied migrations means none are returned. { localFilesystemMigrations: []*models.Migration{}, remoteAppliedMigrations: []*models.Migration{}, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{}, }, // Ensure one local fs migration shows up as expected. { localFilesystemMigrations: []*models.Migration{ {Version: "0001", Applied: false}, }, remoteAppliedMigrations: []*models.Migration{}, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ {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: "0001", Applied: false}, }, remoteAppliedMigrations: []*models.Migration{ {Version: "0001", Applied: true}, }, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ {Version: "0001", 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: "0001", Applied: true}, }, 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: "0001"}, }, remoteAppliedMigrations: []*models.Migration{}, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ {Version: "0001"}, {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: "0001"}, {Version: "110"}, {Version: "190"}, {Version: "1009"}, {Version: "1010"}, {Version: "10000"}, {Version: "20000"}, }, remoteAppliedMigrations: []*models.Migration{}, sortOrder: SortOrderDesc, expectedMigrations: []*models.Migration{ {Version: "20000"}, {Version: "10000"}, {Version: "1010"}, {Version: "1009"}, {Version: "190"}, {Version: "110"}, {Version: "0001"}, }, }, } 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, 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, 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, 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 } tests := []test{ { m1: models.Migration{Version: "abc"}, m2: models.Migration{Version: "123"}, }, { m1: models.Migration{Version: "123"}, m2: models.Migration{Version: "abc"}, }, } 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, 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(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) check.Equal(t, migration.Version, "0001") check.Equal(t, migration.Message, "new migration") check.Equal(t, migration.Applied, false) } func TestCreateMigration_NilDbRepo(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } svc := NewMigrationService( nil, migrationFsRepo, bolttest.NullOutputter{}, ) migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) check.Equal(t, migration.Version, "0001") 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, 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{ ListReturnValue: bolttest.ListReturnValue{ Err: expectedErr, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) migration, err := svc.CreateMigration("new migration") assert.ErrorIs(t, err, expectedErr) assert.Nil(t, migration) } func TestCreateMigration_VersionIncrements(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "0001": {Version: "0001"}, }, Err: nil, }, CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) assert.Equal(t, migration.Version, "0002") } func TestCreateMigration_VersionIncrementsLatestVersion(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "0001": {Version: "0001"}, "0002": {Version: "0002"}, "0003": {Version: "0003"}, }, Err: nil, }, CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) migration, err := svc.CreateMigration("new migration") assert.Nil(t, err) assert.Equal(t, migration.Version, "0004") } func TestCreateMigration_VersionIncrementsParsingErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "abc": {Version: "abc"}, }, Err: nil, }, CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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{ Script: sqlparse.MigrationScript{ Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", Options: sqlparse.ExecutionOptions{UseTransaction: true}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ Err: nil, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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{ Script: sqlparse.MigrationScript{ Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", Options: sqlparse.ExecutionOptions{UseTransaction: false}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ApplyReturnValue: bolttest.ApplyReturnValue{ Err: nil, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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{ Script: sqlparse.MigrationScript{}, Err: expectedErr, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.ApplyMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) } func TestApplyMigration_ApplyWithTxErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ Script: sqlparse.MigrationScript{ Options: sqlparse.ExecutionOptions{ UseTransaction: true, }, }, Err: nil, }, } expectedErr := errors.New("error!") migrationDbRepo := &bolttest.MockMigrationDBRepo{ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{Err: expectedErr}, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.ApplyMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) } func TestApplyMigration_ApplyErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ Script: sqlparse.MigrationScript{ Options: sqlparse.ExecutionOptions{ UseTransaction: false, }, }, Err: nil, }, } expectedErr := errors.New("error!") migrationDbRepo := &bolttest.MockMigrationDBRepo{ ApplyReturnValue: bolttest.ApplyReturnValue{Err: expectedErr}, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.ApplyMigration(&models.Migration{}) 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, bolttest.NullOutputter{}, ) err := svc.ApplyAllMigrations() assert.ErrorIs(t, err, expectedErr) } func TestApplyAllMigrations_AppliesNothingIfAllApplied(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: true}, "0002": {Version: "0002", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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{ "0001": {Version: "0001", 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, 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, bolttest.NullOutputter{}, ) err := svc.ApplyUpToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestApplyUpToVersion_TargetMigrationNotFound(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.ApplyUpToVersion("0002") assert.ErrorContains(t, err, "migration with version 0002 does not exist") } func TestApplyUpToVersion_TargetMigrationAlreadyApplied(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.ApplyUpToVersion("0001") assert.ErrorContains(t, err, "migration with version 0001 is already applied") } func TestApplyUpToVersion_ApplyMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", 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, bolttest.NullOutputter{}, ) err := svc.ApplyUpToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: false}, "0002": {Version: "0002", Applied: false}, "0003": {Version: "0003", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.ApplyUpToVersion("0002") assert.Nil(t, err) assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) } func TestRevertMigration_WithTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ Script: sqlparse.MigrationScript{ Contents: "DROP TABLE tmp;", Options: sqlparse.ExecutionOptions{UseTransaction: true}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{ Err: nil, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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{ Script: sqlparse.MigrationScript{ Contents: "DROP TABLE tmp;", Options: sqlparse.ExecutionOptions{UseTransaction: false}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ RevertReturnValue: bolttest.RevertReturnValue{ Err: nil, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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{ Script: sqlparse.MigrationScript{}, Err: expectedErr, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.RevertMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) } func TestRevertMigration_RevertWithTxErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ Script: sqlparse.MigrationScript{ Options: sqlparse.ExecutionOptions{ UseTransaction: true, }, }, Err: nil, }, } expectedErr := errors.New("error!") migrationDbRepo := &bolttest.MockMigrationDBRepo{ RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{Err: expectedErr}, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.RevertMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) } func TestRevertMigration_RevertErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ Script: sqlparse.MigrationScript{ Options: sqlparse.ExecutionOptions{ UseTransaction: false, }, }, Err: nil, }, } expectedErr := errors.New("error!") migrationDbRepo := &bolttest.MockMigrationDBRepo{ RevertReturnValue: bolttest.RevertReturnValue{Err: expectedErr}, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("0002") assert.ErrorContains(t, err, "migration with version 0002 does not exist") } func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("0001") assert.ErrorContains(t, err, "migration with version 0001 isn't applied") } func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", 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, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("0001") assert.ErrorIs(t, err, expectedErr) } func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: true}, "0002": {Version: "0002", Applied: true}, "0003": {Version: "0003", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("0001") 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, bolttest.NullOutputter{}, ) err := svc.RevertAllMigrations() assert.ErrorIs(t, err, expectedErr) } func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { migrations := map[string]*models.Migration{ "0001": {Version: "0001", Applied: false}, "0002": {Version: "0002", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, 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{ "0001": {Version: "0001", 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, bolttest.NullOutputter{}, ) err := svc.RevertAllMigrations() assert.ErrorIs(t, err, expectedErr) assert.Equal(t, migrationDbRepo.RevertCallCount, 0) assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) }