package services import ( "errors" "testing" "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/checkmate/assert" ) func TestListMigrations_NoMigrations(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.ListMigrations(SortOrderAsc) assert.Nil(t, err) assert.Equal(t, len(migrations), 0) } func TestListMigrations_OneLocalMigration(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "001": {Version: "001", Applied: false}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.ListMigrations(SortOrderAsc) assert.Nil(t, err) assert.Equal(t, len(migrations), 1) assert.Equal(t, migrations[0].Version, "001") assert.Equal(t, migrations[0].Applied, false) } func TestListMigrations_LocalAndAppliedMigrationMerged(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "001": {Version: "001", Applied: false}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "001": {Version: "001", Applied: true}, }, Err: nil, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.ListMigrations(SortOrderAsc) assert.Nil(t, err) assert.Equal(t, len(migrations), 1) assert.Equal(t, migrations[0].Version, "001") assert.Equal(t, migrations[0].Applied, true) } func TestListMigrations_AppliedWithoutLocalNotShown(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "001": {Version: "001", Applied: true}, }, Err: nil, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.ListMigrations(SortOrderAsc) assert.Nil(t, err) assert.Equal(t, len(migrations), 0) } func TestListMigrations_SortedTopologicallyAsc(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{ "003": {Version: "003", Dependencies: []string{"001", "002"}}, "002": {Version: "002", Dependencies: []string{"001"}}, "001": {Version: "001", Dependencies: []string{}}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.ListMigrations(SortOrderAsc) assert.Nil(t, err) assert.Equal(t, len(migrations), 3) assert.Equal(t, migrations[0].Version, "001") assert.Equal(t, migrations[1].Version, "002") assert.Equal(t, migrations[2].Version, "003") } func TestListMigrations_SortedTopologicallyDesc(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", "002"}}, }, Err: nil, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: map[string]*models.Migration{}, Err: nil, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.ListMigrations(SortOrderDesc) assert.Nil(t, err) assert.Equal(t, len(migrations), 3) assert.Equal(t, migrations[0].Version, "003") assert.Equal(t, migrations[1].Version, "002") assert.Equal(t, migrations[2].Version, "001") } 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, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{}) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.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, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{}) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) migrations, err := queryService.ListMigrations(SortOrderAsc) assert.ErrorIs(t, err, expectedErr) var expectedMigrations []*models.Migration assert.DeepEqual(t, migrations, expectedMigrations) } func TestListMigrations_CircularDependency(t *testing.T) { 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, }, } migrationDbRepo := &bolttest.MockMigrationDBRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: nil, Err: nil, }, } depGraph := NewMigrationDependencyGraph(configloader.Config{}) queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph) _, err := queryService.ListMigrations(SortOrderAsc) assert.ErrorContains(t, err, "circular dependency detected") } func TestCombineMigrations_MarksAppliedCorrectly(t *testing.T) { localMigrations := map[string]*models.Migration{ "001": {Version: "001", Applied: false, Dependencies: []string{}}, "002": {Version: "002", Applied: false, Dependencies: []string{"001"}}, } appliedMigrations := map[string]*models.Migration{ "001": {Version: "001", Applied: true}, } depGraph := NewMigrationDependencyGraph(configloader.Config{ Migrations: configloader.MigrationsConfig{ VersionStyle: configloader.VersionStyleSequential, }, }) queryService := NewMigrationQueryService(nil, nil, depGraph) migrations, err := queryService.CombineMigrations(localMigrations, appliedMigrations, SortOrderAsc) assert.Nil(t, err) assert.Equal(t, len(migrations), 2) // Find migrations by version (order is topological) var m001, m002 *models.Migration for _, m := range migrations { if m.Version == "001" { m001 = m } else if m.Version == "002" { m002 = m } } assert.Equal(t, m001.Applied, true) assert.Equal(t, m002.Applied, false) }