diff options
Diffstat (limited to 'internal/services/migration_test.go')
| -rw-r--r-- | internal/services/migration_test.go | 771 |
1 files changed, 401 insertions, 370 deletions
diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index c931dc9..09d33a3 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -18,7 +18,7 @@ func TestListMigrations(t *testing.T) { localFilesystemMigrations []*models.Migration remoteAppliedMigrations []*models.Migration cfg configloader.Config - sortOrder SortOrder + sortOrder sortOrder expectedMigrations []*models.Migration } @@ -85,12 +85,16 @@ func TestListMigrations(t *testing.T) { sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{}, }, - // Ensure that migrations are sorted by dependencies (topological order) + // Ensure that migrations are sorted in asc order for sequential migrations { localFilesystemMigrations: []*models.Migration{ - {Version: "003", Dependencies: []string{"001", "002"}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "001", Dependencies: []string{}}, + {Version: "20000"}, + {Version: "10000"}, + {Version: "1010"}, + {Version: "1009"}, + {Version: "190"}, + {Version: "110"}, + {Version: "001"}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ @@ -100,17 +104,25 @@ func TestListMigrations(t *testing.T) { }, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ - {Version: "001", Dependencies: []string{}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "003", Dependencies: []string{"001", "002"}}, + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, }, }, - // Ensure that migrations are sorted in reverse topological order for desc + // Ensure that migrations are sorted in desc order for sequential migrations { localFilesystemMigrations: []*models.Migration{ - {Version: "001", Dependencies: []string{}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "003", Dependencies: []string{"001", "002"}}, + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ @@ -120,9 +132,69 @@ func TestListMigrations(t *testing.T) { }, sortOrder: SortOrderDesc, expectedMigrations: []*models.Migration{ - {Version: "003", Dependencies: []string{"001", "002"}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "001", Dependencies: []string{}}, + {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"}, }, }, } @@ -163,6 +235,127 @@ func TestListMigrations(t *testing.T) { } } +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{ @@ -387,169 +580,140 @@ func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { assert.Nil(t, migration) } -func TestCreateMigration_FirstMigrationNoDependencies(t *testing.T) { +func TestApplyMigration_WithTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, + 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, }, - CreateReturnValue: bolttest.CreateReturnValue{ + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("first migration") + err := svc.ApplyMigration(&models.Migration{}) assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 0) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) } -func TestCreateMigration_SecondMigrationDependsOnFirst(t *testing.T) { +func TestApplyMigration_WithoutTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, + 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, }, - CreateReturnValue: bolttest.CreateReturnValue{ + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyReturnValue: bolttest.ApplyReturnValue{ Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("second migration") + err := svc.ApplyMigration(&models.Migration{}) assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 1) - assert.Contains(t, migration.Dependencies, "001") + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 1) } -func TestCreateMigration_LinearChainDependsOnLatest(t *testing.T) { +func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - "003": {Version: "003", Dependencies: []string{"002"}}, - }, - Err: nil, - }, - CreateReturnValue: bolttest.CreateReturnValue{ - Err: nil, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{}, + Err: expectedErr, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("fourth migration") + err := svc.ApplyMigration(&models.Migration{}) - assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 1) - assert.Contains(t, migration.Dependencies, "003") + assert.ErrorIs(t, err, expectedErr) } -func TestCreateMigration_ParallelBranchesDependsOnBoth(t *testing.T) { +func TestApplyMigration_ApplyWithTxErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - "003": {Version: "003", Dependencies: []string{"001"}}, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: true, + }, }, Err: nil, }, - CreateReturnValue: bolttest.CreateReturnValue{ - Err: nil, - }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} + expectedErr := errors.New("error!") + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{Err: expectedErr}, + } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("merge migration") + err := svc.ApplyMigration(&models.Migration{}) - assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 2) - assert.Contains(t, migration.Dependencies, "002") - assert.Contains(t, migration.Dependencies, "003") + assert.ErrorIs(t, err, expectedErr) } -func TestCreateMigration_ComplexGraphMultipleLeaves(t *testing.T) { - // Diamond shape: 001 -> 002, 001 -> 003, 002 -> 004, 003 -> 004 - // Plus independent branch: 005 - // Leaf nodes are 004 and 005 +func TestApplyMigration_ApplyErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - "003": {Version: "003", Dependencies: []string{"001"}}, - "004": {Version: "004", Dependencies: []string{"002", "003"}}, - "005": {Version: "005", Dependencies: []string{}}, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: false, + }, }, Err: nil, }, - CreateReturnValue: bolttest.CreateReturnValue{ - Err: nil, - }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} + expectedErr := errors.New("error!") + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyReturnValue: bolttest.ApplyReturnValue{Err: expectedErr}, + } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("new migration") + err := svc.ApplyMigration(&models.Migration{}) - assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 2) - assert.Contains(t, migration.Dependencies, "004") - assert.Contains(t, migration.Dependencies, "005") + assert.ErrorIs(t, err, expectedErr) } func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) { @@ -754,242 +918,163 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) } -func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { - expectedErr := errors.New("error!") +func TestRevertMigration_WithTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: nil, - Err: expectedErr, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Contents: "DROP TABLE tmp;", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + Err: nil, }, } - 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, + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{ + Err: nil, }, } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("002") + err := svc.RevertMigration(&models.Migration{}) - assert.ErrorContains(t, err, "migration with version 002 does not exist") + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) } -func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, - } +func TestRevertMigration_WithoutTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, + 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( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertMigration(&models.Migration{}) - assert.ErrorContains(t, err, "migration with version 001 isn't applied") + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertCallCount, 1) } -func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - } +func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, - }, ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - Err: expectedErr, + Script: sqlparse.MigrationScript{}, + Err: expectedErr, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertMigration(&models.Migration{}) 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}, - } +func TestRevertMigration_RevertWithTxErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, - }, - } - svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, - migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: true, + }, }, + Err: nil, }, - 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, - }, + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{Err: expectedErr}, } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.RevertMigration(&models.Migration{}) 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}, - } +func TestRevertMigration_RevertErr(t *testing.T) { 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, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: false, + }, }, + Err: nil, }, - 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{ + RevertReturnValue: bolttest.RevertReturnValue{Err: expectedErr}, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.RevertMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) - assert.Equal(t, migrationDbRepo.RevertCallCount, 0) - assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) } -func TestCreateMigration_FsRepoListErrForLeafNodes(t *testing.T) { - // Test fsRepo.List error when finding leaf nodes (line 280) - // Using timestamp version style so we only call List once (for leaf nodes) - expectedErr := errors.New("fs error finding leaf nodes") +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{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleTimestamp, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("new migration") + err := svc.RevertDownToVersion("001") assert.ErrorIs(t, err, expectedErr) - assert.ErrorContains(t, err, "unable to list migrations for finding leaf nodes") - assert.Nil(t, migration) } -func TestApplyAllMigrations_CheckDependenciesAppliedErr(t *testing.T) { - // Test CheckDependenciesApplied error (line 66) - // Migration 002 depends on 001, but 001 doesn't exist +func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { migrations := map[string]*models.Migration{ - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -997,29 +1082,21 @@ func TestApplyAllMigrations_CheckDependenciesAppliedErr(t *testing.T) { Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.ApplyAllMigrations() + err := svc.RevertDownToVersion("002") - assert.ErrorContains(t, err, "non-existent migration 001") + assert.ErrorContains(t, err, "migration with version 002 does not exist") } -func TestApplyUpToVersion_CheckDependenciesAppliedErr(t *testing.T) { - // Test CheckDependenciesApplied error in ApplyUpToVersion (line 116) - // Migration 002 depends on 001, but 001 doesn't exist +func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1027,62 +1104,49 @@ func TestApplyUpToVersion_CheckDependenciesAppliedErr(t *testing.T) { Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("002") + err := svc.RevertDownToVersion("001") - assert.ErrorContains(t, err, "non-existent migration 001") + assert.ErrorContains(t, err, "migration with version 001 isn't applied") } -func TestRevertAllMigrations_CheckNoDependentsAppliedErr(t *testing.T) { - // Test CheckNoDependentsApplied error (line 173) +func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: true}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: true}, + "001": {Version: "001", Applied: true}, } + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, - } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, - }, - Err: nil, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, }, } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.RevertDownToVersion("001") - assert.ErrorContains(t, err, "cannot revert migration") + assert.ErrorIs(t, err, expectedErr) } -func TestRevertDownToVersion_CheckNoDependentsAppliedErr(t *testing.T) { - // Test CheckNoDependentsApplied error in RevertDownToVersion (line 223) +func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: true}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: true}, + "001": {Version: "001", Applied: true}, + "002": {Version: "002", Applied: true}, + "003": {Version: "003", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1090,135 +1154,102 @@ func TestRevertDownToVersion_CheckNoDependentsAppliedErr(t *testing.T) { Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, - }, - Err: nil, - }, - } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("001") - assert.ErrorContains(t, err, "cannot revert migration") + assert.Nil(t, err) + assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2) } -func TestCreateMigration_FindLeafNodesErr(t *testing.T) { - // Test FindLeafNodes error when existing migrations have circular dependency (line 285) +func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{"002"}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - }, - Err: nil, + Migrations: nil, + Err: expectedErr, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleTimestamp, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("new migration") + err := svc.RevertAllMigrations() - assert.NotNil(t, err) - assert.ErrorContains(t, err, "circular dependency") - assert.Nil(t, migration) + assert.ErrorIs(t, err, expectedErr) } -func TestApplyAllMigrations_DependencyNotYetAppliedInLoop(t *testing.T) { - // Test that CheckDependenciesApplied fails when a dependency was applied earlier - // in the same ApplyAllMigrations call but the Applied field wasn't updated +func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: false}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: false}, + "002": {Version: "002", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, - ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Contents: "CREATE TABLE test (id INT);", - Options: sqlparse.ExecutionOptions{UseTransaction: true}, - }, - Err: nil, - }, - } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ - Err: nil, - }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyAllMigrations() + err := svc.RevertAllMigrations() - // This should fail because after applying 001, its Applied field in the list - // is still false, so CheckDependenciesApplied for 002 will fail - assert.ErrorContains(t, err, "dependency 001 has not been applied") + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) } -func TestApplyUpToVersion_DependencyNotYetAppliedInLoop(t *testing.T) { - // Same test for ApplyUpToVersion +func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: false}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: true}, } + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, - ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Contents: "CREATE TABLE test (id INT);", - Options: sqlparse.ExecutionOptions{UseTransaction: true}, - }, - Err: nil, - }, - } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ - Err: nil, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("002") + err := svc.RevertAllMigrations() - assert.ErrorContains(t, err, "dependency 001 has not been applied") + assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) } |