package services import ( "errors" "testing" "time" "git.sr.ht/~eugenetriguba/bolt/internal/bolttest" "git.sr.ht/~eugenetriguba/bolt/internal/configloader" "git.sr.ht/~eugenetriguba/bolt/internal/models" "git.sr.ht/~eugenetriguba/bolt/internal/sqlparse" "git.sr.ht/~eugenetriguba/checkmate/assert" "git.sr.ht/~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 by dependencies (topological order) { localFilesystemMigrations: []*models.Migration{ {Version: "003", Dependencies: []string{"001", "002"}}, {Version: "002", Dependencies: []string{"001"}}, {Version: "001", Dependencies: []string{}}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ {Version: "001", Dependencies: []string{}}, {Version: "002", Dependencies: []string{"001"}}, {Version: "003", Dependencies: []string{"001", "002"}}, }, }, // Ensure that migrations are sorted in reverse topological order for desc { localFilesystemMigrations: []*models.Migration{ {Version: "001", Dependencies: []string{}}, {Version: "002", Dependencies: []string{"001"}}, {Version: "003", Dependencies: []string{"001", "002"}}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }, sortOrder: SortOrderDesc, expectedMigrations: []*models.Migration{ {Version: "003", Dependencies: []string{"001", "002"}}, {Version: "002", Dependencies: []string{"001"}}, {Version: "001", Dependencies: []string{}}, }, }, } 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 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(30 * -time.Second) migration, err := svc.CreateMigration("new migration") afterCreateTs := time.Now().Add(30 * time.Second) assert.Nil(t, err) version, err := time.ParseInLocation("20060102150405", migration.Version, time.Local) 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_NilDbRepo(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } svc := NewMigrationService( nil, 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{ ListReturnValue: bolttest.ListReturnValue{ 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{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "001": {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_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{ 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, 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 TestCreateMigration_FirstMigrationNoDependencies(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, 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("first migration") assert.Nil(t, err) assert.Equal(t, len(migration.Dependencies), 0) } func TestCreateMigration_SecondMigrationDependsOnFirst(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "001": {Version: "001", Dependencies: []string{}}, }, 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("second migration") assert.Nil(t, err) assert.Equal(t, len(migration.Dependencies), 1) assert.Contains(t, migration.Dependencies, "001") } func TestCreateMigration_LinearChainDependsOnLatest(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{"002"}}, }, 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("fourth migration") assert.Nil(t, err) assert.Equal(t, len(migration.Dependencies), 1) assert.Contains(t, migration.Dependencies, "003") } func TestCreateMigration_ParallelBranchesDependsOnBoth(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"}}, }, 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("merge migration") assert.Nil(t, err) assert.Equal(t, len(migration.Dependencies), 2) assert.Contains(t, migration.Dependencies, "002") assert.Contains(t, migration.Dependencies, "003") } 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 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{}}, }, 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, len(migration.Dependencies), 2) assert.Contains(t, migration.Dependencies, "004") assert.Contains(t, migration.Dependencies, "005") } 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) } 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) } 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") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: nil, Err: expectedErr, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleTimestamp, }, }, bolttest.NullOutputter{}, ) migration, err := svc.CreateMigration("new migration") 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 migrations := map[string]*models.Migration{ "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) err := svc.ApplyAllMigrations() assert.ErrorContains(t, err, "non-existent migration 001") } func TestApplyUpToVersion_CheckDependenciesAppliedErr(t *testing.T) { // Test CheckDependenciesApplied error in ApplyUpToVersion (line 116) // Migration 002 depends on 001, but 001 doesn't exist migrations := map[string]*models.Migration{ "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) err := svc.ApplyUpToVersion("002") assert.ErrorContains(t, err, "non-existent migration 001") } func TestRevertAllMigrations_CheckNoDependentsAppliedErr(t *testing.T) { // Test CheckNoDependentsApplied error (line 173) migrations := map[string]*models.Migration{ "001": {Version: "001", Dependencies: []string{}, Applied: true}, "002": {Version: "002", Dependencies: []string{"001"}, Applied: true}, } 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, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) err := svc.RevertAllMigrations() assert.ErrorContains(t, err, "cannot revert migration") } func TestRevertDownToVersion_CheckNoDependentsAppliedErr(t *testing.T) { // Test CheckNoDependentsApplied error in RevertDownToVersion (line 223) migrations := map[string]*models.Migration{ "001": {Version: "001", Dependencies: []string{}, Applied: true}, "002": {Version: "002", Dependencies: []string{"001"}, Applied: true}, } 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, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("001") assert.ErrorContains(t, err, "cannot revert migration") } func TestCreateMigration_FindLeafNodesErr(t *testing.T) { // Test FindLeafNodes error when existing migrations have circular dependency (line 285) 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, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleTimestamp, }, }, bolttest.NullOutputter{}, ) migration, err := svc.CreateMigration("new migration") assert.NotNil(t, err) assert.ErrorContains(t, err, "circular dependency") assert.Nil(t, migration) } 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 migrations := map[string]*models.Migration{ "001": {Version: "001", Dependencies: []string{}, Applied: false}, "002": {Version: "002", Dependencies: []string{"001"}, 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, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) err := svc.ApplyAllMigrations() // 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") } func TestApplyUpToVersion_DependencyNotYetAppliedInLoop(t *testing.T) { // Same test for ApplyUpToVersion migrations := map[string]*models.Migration{ "001": {Version: "001", Dependencies: []string{}, Applied: false}, "002": {Version: "002", Dependencies: []string{"001"}, 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, }, } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) err := svc.ApplyUpToVersion("002") assert.ErrorContains(t, err, "dependency 001 has not been applied") }