diff options
Diffstat (limited to 'internal/services')
| -rw-r--r-- | internal/services/migration.go | 252 | ||||
| -rw-r--r-- | internal/services/migration_dependency_graph.go | 191 | ||||
| -rw-r--r-- | internal/services/migration_dependency_graph_test.go | 377 | ||||
| -rw-r--r-- | internal/services/migration_executor.go | 66 | ||||
| -rw-r--r-- | internal/services/migration_executor_test.go | 230 | ||||
| -rw-r--r-- | internal/services/migration_query_service.go | 90 | ||||
| -rw-r--r-- | internal/services/migration_query_service_test.go | 309 | ||||
| -rw-r--r-- | internal/services/migration_test.go | 771 |
8 files changed, 1715 insertions, 571 deletions
diff --git a/internal/services/migration.go b/internal/services/migration.go index ddfb8c2..ba43ff7 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -2,7 +2,6 @@ package services import ( "fmt" - "sort" "strconv" "time" @@ -12,11 +11,17 @@ import ( "git.sr.ht/~eugenetriguba/bolt/internal/repositories" ) +// MigrationService provides the primary interface for migration operations. +// It coordinates with the lower-level components to apply, revert, create, +// and list out migrations while also managing presentation concerns like +// output and timing. type MigrationService struct { - dbRepo repositories.MigrationDBRepo - fsRepo repositories.MigrationFsRepo - cfg configloader.Config - outputter output.Outputter + executor *MigrationExecutor + queryService *MigrationQueryService + depGraph *MigrationDependencyGraph + fsRepo repositories.MigrationFsRepo + cfg configloader.Config + outputter output.Outputter } func NewMigrationService( @@ -25,29 +30,32 @@ func NewMigrationService( cfg configloader.Config, outputter output.Outputter, ) MigrationService { + depGraph := NewMigrationDependencyGraph(cfg) + executor := NewMigrationExecutor(dbRepo, fsRepo) + queryService := NewMigrationQueryService(dbRepo, fsRepo, depGraph) return MigrationService{ - dbRepo: dbRepo, - fsRepo: fsRepo, - cfg: cfg, - outputter: outputter, + executor: executor, + queryService: queryService, + depGraph: depGraph, + fsRepo: fsRepo, + cfg: cfg, + outputter: outputter, } } -type sortOrder int - -const ( - SortOrderDesc sortOrder = iota - SortOrderAsc -) - func (ms MigrationService) ApplyAllMigrations() error { migrations, err := ms.ListMigrations(SortOrderAsc) if err != nil { return err } + migrationsByVersion := getMigrationsByVersion(migrations) for _, migration := range migrations { if !migration.Applied { + if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil { + return err + } + err := ms.ApplyMigration(migration) if err != nil { return fmt.Errorf( @@ -62,6 +70,14 @@ func (ms MigrationService) ApplyAllMigrations() error { return nil } +func getMigrationsByVersion(migrations []*models.Migration) map[string]*models.Migration { + result := make(map[string]*models.Migration) + for _, migration := range migrations { + result[migration.Version] = migration + } + return result +} + func (ms MigrationService) ApplyUpToVersion(version string) error { migrations, err := ms.ListMigrations(SortOrderAsc) if err != nil { @@ -79,16 +95,21 @@ func (ms MigrationService) ApplyUpToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if targetMigration.Applied { - // Assumption: If the target migration is applied, all migrations before - // it must also be applied. + // TODO: Check not only that the target migration is applied, but also + // all prior migrations. return fmt.Errorf( "migration with version %s is already applied, nothing to apply", version, ) } + migrationsByVersion := getMigrationsByVersion(migrations) for _, migration := range migrations { if !migration.Applied { + if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil { + return err + } + err := ms.ApplyMigration(migration) if err != nil { return fmt.Errorf( @@ -111,19 +132,9 @@ func (ms MigrationService) ApplyMigration(migration *models.Migration) error { ms.outputter.Output(fmt.Sprintf("Applying migration %s..", migration.Name())) startTime := time.Now() - upgradeScript, err := ms.fsRepo.ReadUpgradeScript(migration) - if err != nil { - return fmt.Errorf("unable to read upgrade script: %w", err) - } - - if upgradeScript.Options.UseTransaction { - err = ms.dbRepo.ApplyWithTx(upgradeScript.Contents, migration) - } else { - err = ms.dbRepo.Apply(upgradeScript.Contents, migration) - } - + err := ms.executor.Apply(migration) if err != nil { - return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err) + return err } ms.outputter.Output( @@ -143,8 +154,13 @@ func (ms MigrationService) RevertAllMigrations() error { return err } + migrationsByVersion := getMigrationsByVersion(migrations) for _, migration := range migrations { if migration.Applied { + if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil { + return err + } + err := ms.RevertMigration(migration) if err != nil { return fmt.Errorf( @@ -176,16 +192,21 @@ func (ms MigrationService) RevertDownToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if !targetMigration.Applied { - // Assumption: Every migration from the latest down to the target - // migration hasn't been applied. + // TODO: Check not only that the target migration is reverted, but also + // all prior migrations. return fmt.Errorf( "migration with version %s isn't applied, nothing to revert", version, ) } + migrationsByVersion := getMigrationsByVersion(migrations) for _, migration := range migrations { if migration.Applied { + if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil { + return err + } + err := ms.RevertMigration(migration) if err != nil { return fmt.Errorf( @@ -208,17 +229,7 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error { ms.outputter.Output(fmt.Sprintf("Reverting migration %s..", migration.Name())) startTime := time.Now() - downgradeScript, err := ms.fsRepo.ReadDowngradeScript(migration) - if err != nil { - return fmt.Errorf("unable to read downgrade script: %w", err) - } - - if downgradeScript.Options.UseTransaction { - err = ms.dbRepo.RevertWithTx(downgradeScript.Contents, migration) - } else { - err = ms.dbRepo.Revert(downgradeScript.Contents, migration) - } - + err := ms.executor.Revert(migration) if err != nil { return err } @@ -247,160 +258,61 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e migration = models.NewSequentialMigration(currentVersion+1, message) } - err := ms.fsRepo.Create(migration) + leafNodes, err := ms.getMigrationLeafNodes() if err != nil { return nil, err } - ms.outputter.Output( - fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message), - ) - return migration, nil -} - -func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { - var currentVersion uint64 = 0 + migration.Dependencies = leafNodes - localMigrations, err := ms.fsRepo.List() + err = ms.fsRepo.Create(migration) if err != nil { - return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err) + return nil, err } - - migrations, err := ms.combineMigrations( - localMigrations, - map[string]*models.Migration{}, - SortOrderDesc, + ms.outputter.Output( + fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message), ) - if err != nil { - return 0, err - } - - if len(migrations) > 0 { - latestMigration := migrations[0] - currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64) - if err != nil { - return 0, err - } - } - - return currentVersion, nil + return migration, nil } -func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) { - // Assumption: All migration versions that have been applied - // to the database exist locally in the list of filesystem migrations. - // This would hold true unless someone decided to apply a migration and, - // for whatever reason, delete it later from their filesystem. If there is - // an applied migration that doesn't exist locally, it would not be shown - // here. +func (ms MigrationService) getMigrationLeafNodes() ([]string, error) { localMigrations, err := ms.fsRepo.List() if err != nil { - return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err) + return nil, fmt.Errorf("unable to list migrations for finding leaf nodes: %w", err) } - appliedMigrations, err := ms.dbRepo.List() + leafNodes, err := ms.depGraph.FindLeafNodes(localMigrations) if err != nil { - return nil, fmt.Errorf( - "unable to list out applied migrations from remote db: %w", - err, - ) + return nil, fmt.Errorf("unable to find leaf nodes for dependencies: %w", err) } + return leafNodes, nil - return ms.combineMigrations(localMigrations, appliedMigrations, order) } -func (ms MigrationService) combineMigrations( - localMigrations map[string]*models.Migration, - appliedMigrations map[string]*models.Migration, - order sortOrder, -) ([]*models.Migration, error) { - migrations := make([]*models.Migration, 0) - for _, localMigration := range localMigrations { - _, ok := appliedMigrations[localMigration.Version] - if ok { - localMigration.Applied = true - } - migrations = append(migrations, localMigration) - } +func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { + var maxVersion uint64 = 0 - err := ms.sortMigrations(migrations, order) + localMigrations, err := ms.fsRepo.List() if err != nil { - return nil, err + return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err) } - return migrations, err -} - -func (ms MigrationService) sortMigrations( - migrations []*models.Migration, - order sortOrder, -) error { - sortErrs := make([]error, 0) - sort.Slice(migrations, func(i, j int) bool { - var comparison bool - var err error - - if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential { - comparison, err = ms.compareSequentialMigrations( - migrations[i], - migrations[j], - order, + for _, migration := range localMigrations { + version, err := strconv.ParseUint(migration.Version, 10, 64) + if err != nil { + return 0, fmt.Errorf( + "migration %s has invalid version for sequential style: %w", + migration.Name(), + err, ) - } else { - comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order) } - - if err != nil { - sortErrs = append(sortErrs, err) + if version > maxVersion { + maxVersion = version } - - return comparison - }) - if len(sortErrs) != 0 { - return fmt.Errorf("unable to sort migrations: %v", sortErrs) - } - return nil -} - -func (ms MigrationService) compareSequentialMigrations( - m1 *models.Migration, - m2 *models.Migration, - order sortOrder, -) (bool, error) { - m1Version, err := strconv.ParseInt(m1.Version, 10, 64) - if err != nil { - return false, err - } - - m2Version, err := strconv.ParseInt(m2.Version, 10, 64) - if err != nil { - return false, err } - if order == SortOrderAsc { - return m1Version < m2Version, nil - } else { - return m1Version > m2Version, nil - } + return maxVersion, nil } -func (ms MigrationService) compareTimestampMigrations( - m1 *models.Migration, - m2 *models.Migration, - order sortOrder, -) (bool, error) { - m1Version, err := time.Parse("20060102150405", m1.Version) - if err != nil { - return false, err - } - - m2Version, err := time.Parse("20060102150405", m2.Version) - if err != nil { - return false, err - } - - if order == SortOrderAsc { - return m1Version.Before(m2Version), nil - } else { - return m1Version.After(m2Version), nil - } +func (ms MigrationService) ListMigrations(order SortOrder) ([]*models.Migration, error) { + return ms.queryService.ListMigrations(order) } diff --git a/internal/services/migration_dependency_graph.go b/internal/services/migration_dependency_graph.go new file mode 100644 index 0000000..3f0e417 --- /dev/null +++ b/internal/services/migration_dependency_graph.go @@ -0,0 +1,191 @@ +package services + +import ( + "fmt" + "slices" + "sort" + "strconv" + "time" + + "git.sr.ht/~eugenetriguba/bolt/internal/configloader" + "git.sr.ht/~eugenetriguba/bolt/internal/models" +) + +// MigrationDependencyGraph handles all dependency graph operations for migrations, +// including topological sorting, dependency validation, and graph analysis. +type MigrationDependencyGraph struct { + cfg configloader.Config +} + +func NewMigrationDependencyGraph(cfg configloader.Config) *MigrationDependencyGraph { + return &MigrationDependencyGraph{cfg: cfg} +} + +// TopologicalSort performs a topological sort on migrations based on their dependencies. +// It uses Kahn's algorithm to detect cycles and order migrations. When multiple migrations +// have the same topological level, they are sorted by version for determinism. `migrations` +// is expected to be a map of version -> Migration. +func (g *MigrationDependencyGraph) TopologicalSort(migrations map[string]*models.Migration) ([]*models.Migration, error) { + graph := make(map[string][]*models.Migration) + inDegree := make(map[string]int) + + // To start, we need to build the graph and in-degree count. + for _, m := range migrations { + for _, dep := range m.Dependencies { + if _, exists := migrations[dep]; !exists { + return nil, fmt.Errorf( + "migration %s depends on non-existent migration %s", + m.Version, dep, + ) + } + graph[dep] = append(graph[dep], m) + inDegree[m.Version]++ + } + } + + // Next step of Kahn's algorithm is to find the vertices with in-degree of 0 + // Often, this will simply be one migration (the first migration), but there + // could be multiple independent migrations which would result in multiple + // starting points. + queue := make([]*models.Migration, 0) + for _, m := range migrations { + if inDegree[m.Version] == 0 { + queue = append(queue, m) + } + } + // Within each topological level, we want to sort the migrations by the version + // for determinism, so it's easier to reason about how migrations will execute. + g.sortByVersion(queue) + + result := make([]*models.Migration, 0) + for len(queue) > 0 { + current := queue[0] + queue = queue[1:] + result = append(result, current) + + newlyAvailable := make([]*models.Migration, 0) + for _, neighbor := range graph[current.Version] { + inDegree[neighbor.Version]-- + if inDegree[neighbor.Version] == 0 { + newlyAvailable = append(newlyAvailable, neighbor) + } + } + + g.sortByVersion(newlyAvailable) + queue = append(queue, newlyAvailable...) + g.sortByVersion(queue) + } + + if len(result) != len(migrations) { + return nil, fmt.Errorf("circular dependency detected in migrations") + } + + return result, nil +} + +// ReverseTopologicalSort returns migrations in reverse topological order (for reverting). +func (g *MigrationDependencyGraph) ReverseTopologicalSort(migrations map[string]*models.Migration) ([]*models.Migration, error) { + sorted, err := g.TopologicalSort(migrations) + if err != nil { + return nil, err + } + reversed := make([]*models.Migration, len(sorted)) + for i, m := range sorted { + reversed[len(sorted)-1-i] = m + } + return reversed, nil +} + +// sortByVersion sorts migrations by version number deterministically +func (g *MigrationDependencyGraph) sortByVersion(migrations []*models.Migration) { + sort.Slice(migrations, func(i, j int) bool { + if g.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential { + v1, _ := strconv.ParseInt(migrations[i].Version, 10, 64) + v2, _ := strconv.ParseInt(migrations[j].Version, 10, 64) + return v1 < v2 + } + // Note: If not sequential, we assume timestamp style. + t1, _ := time.Parse("20060102150405", migrations[i].Version) + t2, _ := time.Parse("20060102150405", migrations[j].Version) + return t1.Before(t2) + }) +} + +// CheckDependenciesApplied verifies that all dependencies of a migration have been applied. +func (g *MigrationDependencyGraph) CheckDependenciesApplied( + migration *models.Migration, + allMigrations map[string]*models.Migration, +) error { + for _, depVersion := range migration.Dependencies { + dep, exists := allMigrations[depVersion] + if !exists { + return fmt.Errorf( + "cannot apply migration %s: dependency %s does not exist", + migration.Version, depVersion, + ) + } + if !dep.Applied { + return fmt.Errorf( + "cannot apply migration %s: dependency %s has not been applied", + migration.Version, depVersion, + ) + } + } + + return nil +} + +// CheckNoDependentsApplied verifies that no applied migrations depend on this migration. +func (g *MigrationDependencyGraph) CheckNoDependentsApplied( + migration *models.Migration, + allMigrations map[string]*models.Migration, +) error { + for _, m := range allMigrations { + if m.Version == migration.Version { + continue + } + if m.Applied && slices.Contains(m.Dependencies, migration.Version) { + return fmt.Errorf( + "cannot revert migration %s: migration %s still applied and depends on it", + migration.Version, m.Version, + ) + } + } + + return nil +} + +// FindLeafNodes finds all migrations that no other migration depends on. These are +// the "leaf" nodes in the dependency graph. If there are circular dependency in the +// migration graph, an error is returned. +func (g *MigrationDependencyGraph) FindLeafNodes(localMigrations map[string]*models.Migration) ([]string, error) { + if len(localMigrations) == 0 { + return []string{}, nil + } + + // Validate there are no circular dependencies by building + // out the dependency graph via topological sort. + _, err := g.TopologicalSort(localMigrations) + if err != nil { + return nil, err + } + + // What are all the migrations that have dependencies? + dependedUpon := make(map[string]bool) + for _, migration := range localMigrations { + for _, dep := range migration.Dependencies { + dependedUpon[dep] = true + } + } + + // What are all the migrations that no other migration depends on? + // These are the leaf nodes. + leafNodes := make([]string, 0) + for _, migration := range localMigrations { + if !dependedUpon[migration.Version] { + leafNodes = append(leafNodes, migration.Version) + } + } + + return leafNodes, nil +} diff --git a/internal/services/migration_dependency_graph_test.go b/internal/services/migration_dependency_graph_test.go new file mode 100644 index 0000000..29d36db --- /dev/null +++ b/internal/services/migration_dependency_graph_test.go @@ -0,0 +1,377 @@ +package services + +import ( + "testing" + + "git.sr.ht/~eugenetriguba/bolt/internal/configloader" + "git.sr.ht/~eugenetriguba/bolt/internal/models" + "git.sr.ht/~eugenetriguba/checkmate/assert" + "git.sr.ht/~eugenetriguba/checkmate/check" +) + +func TestTopologicalSort_ThreeMigrationsWithDependencies(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + migrations := map[string]*models.Migration{ + "003": {Version: "003", Dependencies: []string{"001", "002"}}, + "002": {Version: "002", Dependencies: []string{"001"}}, + "001": {Version: "001", Dependencies: []string{}}, + } + + sorted, err := graph.TopologicalSort(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(sorted), 3) + assert.Equal(t, sorted[0].Version, "001") + assert.Equal(t, sorted[1].Version, "002") + assert.Equal(t, sorted[2].Version, "003") +} + +func TestTopologicalSort_CircularDependency(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + migrations := map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{"002"}}, + "002": {Version: "002", Dependencies: []string{"001"}}, + } + + _, err := graph.TopologicalSort(migrations) + + assert.ErrorContains(t, err, "circular dependency detected") +} + +func TestTopologicalSort_NonExistentDependency(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + migrations := map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{"002"}}, + } + + _, err := graph.TopologicalSort(migrations) + + assert.ErrorContains(t, err, "depends on non-existent migration") +} + +func TestTopologicalSort_EmptyList(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + migrations := map[string]*models.Migration{} + + sorted, err := graph.TopologicalSort(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(sorted), 0) +} + +func TestTopologicalSort_DiamondDependency(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + // Diamond: 001 -> 002, 001 -> 003, 002 -> 004, 003 -> 004 + 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"}}, + } + + sorted, err := graph.TopologicalSort(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(sorted), 4) + assert.Equal(t, sorted[0].Version, "001") + // 002 and 003 can be in either order (both depend only on 001) + // But our implementation sorts deterministically + assert.Equal(t, sorted[1].Version, "002") + assert.Equal(t, sorted[2].Version, "003") + assert.Equal(t, sorted[3].Version, "004") +} + +func TestTopologicalSort_TimestampVersionStyle(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }) + migrations := map[string]*models.Migration{ + "20250101120000": {Version: "20250101120000", Dependencies: []string{}}, + "20250101130000": {Version: "20250101130000", Dependencies: []string{"20250101120000"}}, + "20250101110000": {Version: "20250101110000", Dependencies: []string{}}, + } + + sorted, err := graph.TopologicalSort(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(sorted), 3) + // Should be sorted by timestamp among peers + assert.Equal(t, sorted[0].Version, "20250101110000") + assert.Equal(t, sorted[1].Version, "20250101120000") + assert.Equal(t, sorted[2].Version, "20250101130000") +} + +func TestReverseTopologicalSort(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + migrations := map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{}}, + "002": {Version: "002", Dependencies: []string{"001"}}, + "003": {Version: "003", Dependencies: []string{"001", "002"}}, + } + + reversed, err := graph.ReverseTopologicalSort(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(reversed), 3) + assert.Equal(t, reversed[0].Version, "003") + assert.Equal(t, reversed[1].Version, "002") + assert.Equal(t, reversed[2].Version, "001") +} + +func TestReverseTopologicalSort_CircularDependency(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + migrations := map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{"002"}}, + "002": {Version: "002", Dependencies: []string{"001"}}, + } + + _, err := graph.ReverseTopologicalSort(migrations) + + assert.ErrorContains(t, err, "circular dependency detected") +} + +func TestCheckDependenciesApplied_AllApplied(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true, Dependencies: []string{}}, + "002": {Version: "002", Applied: true, Dependencies: []string{"001"}}, + "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}}, + } + + err := graph.CheckDependenciesApplied(allMigrations["003"], allMigrations) + + assert.Nil(t, err) +} + +func TestCheckDependenciesApplied_DependencyNotApplied(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true, Dependencies: []string{}}, + "002": {Version: "002", Applied: false, Dependencies: []string{"001"}}, + "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}}, + } + + err := graph.CheckDependenciesApplied(allMigrations["003"], allMigrations) + + assert.ErrorContains(t, err, "cannot apply migration 003") + assert.ErrorContains(t, err, "dependency 002 has not been applied") +} + +func TestCheckDependenciesApplied_DependencyDoesNotExist(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true, Dependencies: []string{}}, + "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}}, + } + + err := graph.CheckDependenciesApplied(allMigrations["003"], allMigrations) + + assert.ErrorContains(t, err, "cannot apply migration 003") + assert.ErrorContains(t, err, "dependency 002 does not exist") +} + +func TestCheckDependenciesApplied_NoDependencies(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false, Dependencies: []string{}}, + } + + err := graph.CheckDependenciesApplied(allMigrations["001"], allMigrations) + + assert.Nil(t, err) +} + +func TestCheckNoDependentsApplied_NoDependents(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true, Dependencies: []string{}}, + "002": {Version: "002", Applied: true, Dependencies: []string{"001"}}, + "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}}, + } + + err := graph.CheckNoDependentsApplied(allMigrations["002"], allMigrations) + + assert.Nil(t, err) +} + +func TestCheckNoDependentsApplied_HasAppliedDependent(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true, Dependencies: []string{}}, + "002": {Version: "002", Applied: true, Dependencies: []string{"001"}}, + "003": {Version: "003", Applied: true, Dependencies: []string{"001", "002"}}, + } + + err := graph.CheckNoDependentsApplied(allMigrations["002"], allMigrations) + + assert.ErrorContains(t, err, "cannot revert migration 002") + assert.ErrorContains(t, err, "migration 003 still applied and depends on it") +} + +func TestCheckNoDependentsApplied_HasUnappliedDependent(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true, Dependencies: []string{}}, + "002": {Version: "002", Applied: true, Dependencies: []string{"001"}}, + "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}}, + } + + err := graph.CheckNoDependentsApplied(allMigrations["002"], allMigrations) + + assert.Nil(t, err) +} + +func TestCheckNoDependentsApplied_RootMigration(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + allMigrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: true, Dependencies: []string{}}, + "002": {Version: "002", Applied: true, Dependencies: []string{"001"}}, + } + + err := graph.CheckNoDependentsApplied(allMigrations["001"], allMigrations) + + assert.ErrorContains(t, err, "cannot revert migration 001") + assert.ErrorContains(t, err, "migration 002 still applied and depends on it") +} + +func TestFindLeafNodes_NoMigrations(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + + leafNodes, err := graph.FindLeafNodes(map[string]*models.Migration{}) + + assert.Nil(t, err) + assert.DeepEqual(t, leafNodes, []string{}) +} + +func TestFindLeafNodes_SingleMigration(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + migrations := map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{}}, + } + + leafNodes, err := graph.FindLeafNodes(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(leafNodes), 1) + assert.Contains(t, leafNodes, "001") +} + +func TestFindLeafNodes_LinearChain(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + migrations := map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{}}, + "002": {Version: "002", Dependencies: []string{"001"}}, + "003": {Version: "003", Dependencies: []string{"002"}}, + } + + leafNodes, err := graph.FindLeafNodes(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(leafNodes), 1) + assert.Contains(t, leafNodes, "003") +} + +func TestFindLeafNodes_ParallelBranches(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + migrations := map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{}}, + "002": {Version: "002", Dependencies: []string{"001"}}, + "003": {Version: "003", Dependencies: []string{"001"}}, + } + + leafNodes, err := graph.FindLeafNodes(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(leafNodes), 2) + assert.Contains(t, leafNodes, "002") + assert.Contains(t, leafNodes, "003") +} + +func TestFindLeafNodes_ComplexGraph(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{}) + // Diamond shape: 001 -> 002, 001 -> 003, 002 -> 004, 003 -> 004 + // Plus independent branch: 005 + 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{}}, + } + + leafNodes, err := graph.FindLeafNodes(migrations) + + assert.Nil(t, err) + assert.Equal(t, len(leafNodes), 2) + assert.Contains(t, leafNodes, "004") + assert.Contains(t, leafNodes, "005") +} + +func TestSortByVersion_Sequential(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }) + migrations := []*models.Migration{ + {Version: "003"}, + {Version: "001"}, + {Version: "002"}, + } + + graph.sortByVersion(migrations) + + check.Equal(t, migrations[0].Version, "001") + check.Equal(t, migrations[1].Version, "002") + check.Equal(t, migrations[2].Version, "003") +} + +func TestSortByVersion_Timestamp(t *testing.T) { + graph := NewMigrationDependencyGraph(configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }) + migrations := []*models.Migration{ + {Version: "20250101130000"}, + {Version: "20250101110000"}, + {Version: "20250101120000"}, + } + + graph.sortByVersion(migrations) + + check.Equal(t, migrations[0].Version, "20250101110000") + check.Equal(t, migrations[1].Version, "20250101120000") + check.Equal(t, migrations[2].Version, "20250101130000") +} diff --git a/internal/services/migration_executor.go b/internal/services/migration_executor.go new file mode 100644 index 0000000..e9f827e --- /dev/null +++ b/internal/services/migration_executor.go @@ -0,0 +1,66 @@ +package services + +import ( + "fmt" + + "git.sr.ht/~eugenetriguba/bolt/internal/models" + "git.sr.ht/~eugenetriguba/bolt/internal/repositories" +) + +// MigrationExecutor handles the core execution logic for applying and reverting migrations. +type MigrationExecutor struct { + dbRepo repositories.MigrationDBRepo + fsRepo repositories.MigrationFsRepo +} + +func NewMigrationExecutor( + dbRepo repositories.MigrationDBRepo, + fsRepo repositories.MigrationFsRepo, +) *MigrationExecutor { + return &MigrationExecutor{ + dbRepo: dbRepo, + fsRepo: fsRepo, + } +} + +// Apply applies a migration by reading the upgrade script and executing it. +// It automatically handles transaction wrapping based on the script options. +func (e *MigrationExecutor) Apply(migration *models.Migration) error { + upgradeScript, err := e.fsRepo.ReadUpgradeScript(migration) + if err != nil { + return fmt.Errorf("unable to read upgrade script: %w", err) + } + + if upgradeScript.Options.UseTransaction { + err = e.dbRepo.ApplyWithTx(upgradeScript.Contents, migration) + } else { + err = e.dbRepo.Apply(upgradeScript.Contents, migration) + } + + if err != nil { + return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err) + } + + return nil +} + +// Revert reverts a migration by reading the downgrade script and executing it. +// It automatically handles transaction wrapping based on the script options. +func (e *MigrationExecutor) Revert(migration *models.Migration) error { + downgradeScript, err := e.fsRepo.ReadDowngradeScript(migration) + if err != nil { + return fmt.Errorf("unable to read downgrade script: %w", err) + } + + if downgradeScript.Options.UseTransaction { + err = e.dbRepo.RevertWithTx(downgradeScript.Contents, migration) + } else { + err = e.dbRepo.Revert(downgradeScript.Contents, migration) + } + + if err != nil { + return err + } + + return nil +} diff --git a/internal/services/migration_executor_test.go b/internal/services/migration_executor_test.go new file mode 100644 index 0000000..00e7402 --- /dev/null +++ b/internal/services/migration_executor_test.go @@ -0,0 +1,230 @@ +package services + +import ( + "errors" + "testing" + + "git.sr.ht/~eugenetriguba/bolt/internal/bolttest" + "git.sr.ht/~eugenetriguba/bolt/internal/models" + "git.sr.ht/~eugenetriguba/bolt/internal/sqlparse" + "git.sr.ht/~eugenetriguba/checkmate/assert" +) + +func TestExecutorApply_WithTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Contents: "CREATE TABLE tmp(id INT PRIMARY KEY);", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ + Err: nil, + }, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Apply(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) +} + +func TestExecutorApply_WithoutTransaction(t *testing.T) { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Contents: "CREATE TABLE tmp(id INT PRIMARY KEY);", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + Err: nil, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyReturnValue: bolttest.ApplyReturnValue{ + Err: nil, + }, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Apply(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 1) +} + +func TestExecutorApply_ReadUpgradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{}, + Err: expectedErr, + }, + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Apply(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestExecutorApply_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}, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Apply(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestExecutorApply_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}, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Apply(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestExecutorRevert_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, + }, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Revert(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) +} + +func TestExecutorRevert_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, + }, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Revert(&models.Migration{}) + + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertCallCount, 1) +} + +func TestExecutorRevert_ReadDowngradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{}, + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Revert(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestExecutorRevert_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}, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Revert(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} + +func TestExecutorRevert_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}, + } + + executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo) + err := executor.Revert(&models.Migration{}) + + assert.ErrorIs(t, err, expectedErr) +} diff --git a/internal/services/migration_query_service.go b/internal/services/migration_query_service.go new file mode 100644 index 0000000..f505727 --- /dev/null +++ b/internal/services/migration_query_service.go @@ -0,0 +1,90 @@ +package services + +import ( + "fmt" + + "git.sr.ht/~eugenetriguba/bolt/internal/models" + "git.sr.ht/~eugenetriguba/bolt/internal/repositories" +) + +type SortOrder int + +const ( + SortOrderDesc SortOrder = iota + SortOrderAsc +) + +// MigrationQueryService handles querying and listing migrations from +// both the filesystem and database sources. +type MigrationQueryService struct { + dbRepo repositories.MigrationDBRepo + fsRepo repositories.MigrationFsRepo + depGraph *MigrationDependencyGraph +} + +func NewMigrationQueryService( + dbRepo repositories.MigrationDBRepo, + fsRepo repositories.MigrationFsRepo, + depGraph *MigrationDependencyGraph, +) *MigrationQueryService { + return &MigrationQueryService{ + dbRepo: dbRepo, + fsRepo: fsRepo, + depGraph: depGraph, + } +} + +// ListMigrations retrieves and combines migrations from both the filesystem and database, +// returning them in the specified order (topological). +func (s *MigrationQueryService) ListMigrations(order SortOrder) ([]*models.Migration, error) { + // Assumption: All migration versions that have been applied + // to the database exist locally in the list of filesystem migrations. + // This would hold true unless someone decided to apply a migration and, + // for whatever reason, delete it later from their filesystem. If there is + // an applied migration that doesn't exist locally, it would not be shown + // here. + localMigrations, err := s.fsRepo.List() + if err != nil { + return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err) + } + + appliedMigrations, err := s.dbRepo.List() + if err != nil { + return nil, fmt.Errorf( + "unable to list out applied migrations from remote db: %w", + err, + ) + } + + return s.CombineMigrations(localMigrations, appliedMigrations, order) +} + +// CombineMigrations combines local filesystem migrations with applied database migrations, +// marking migrations as applied where appropriate and sorting them in the specified order. +func (s *MigrationQueryService) CombineMigrations( + localMigrations map[string]*models.Migration, + appliedMigrations map[string]*models.Migration, + order SortOrder, +) ([]*models.Migration, error) { + migrations := make(map[string]*models.Migration, 0) + for _, localMigration := range localMigrations { + _, ok := appliedMigrations[localMigration.Version] + if ok { + localMigration.Applied = true + } + migrations[localMigration.Version] = localMigration + } + + var sorted []*models.Migration + var err error + if order == SortOrderAsc { + sorted, err = s.depGraph.TopologicalSort(migrations) + } else { + sorted, err = s.depGraph.ReverseTopologicalSort(migrations) + } + if err != nil { + return nil, err + } + + return sorted, nil +} diff --git a/internal/services/migration_query_service_test.go b/internal/services/migration_query_service_test.go new file mode 100644 index 0000000..af48777 --- /dev/null +++ b/internal/services/migration_query_service_test.go @@ -0,0 +1,309 @@ +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) +} diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go index 09d33a3..c931dc9 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,16 +85,12 @@ func TestListMigrations(t *testing.T) { sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{}, }, - // Ensure that migrations are sorted in asc order for sequential migrations + // Ensure that migrations are sorted by dependencies (topological order) { localFilesystemMigrations: []*models.Migration{ - {Version: "20000"}, - {Version: "10000"}, - {Version: "1010"}, - {Version: "1009"}, - {Version: "190"}, - {Version: "110"}, - {Version: "001"}, + {Version: "003", Dependencies: []string{"001", "002"}}, + {Version: "002", Dependencies: []string{"001"}}, + {Version: "001", Dependencies: []string{}}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ @@ -104,25 +100,17 @@ func TestListMigrations(t *testing.T) { }, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ - {Version: "001"}, - {Version: "110"}, - {Version: "190"}, - {Version: "1009"}, - {Version: "1010"}, - {Version: "10000"}, - {Version: "20000"}, + {Version: "001", Dependencies: []string{}}, + {Version: "002", Dependencies: []string{"001"}}, + {Version: "003", Dependencies: []string{"001", "002"}}, }, }, - // Ensure that migrations are sorted in desc order for sequential migrations + // Ensure that migrations are sorted in reverse topological order for desc { localFilesystemMigrations: []*models.Migration{ - {Version: "001"}, - {Version: "110"}, - {Version: "190"}, - {Version: "1009"}, - {Version: "1010"}, - {Version: "10000"}, - {Version: "20000"}, + {Version: "001", Dependencies: []string{}}, + {Version: "002", Dependencies: []string{"001"}}, + {Version: "003", Dependencies: []string{"001", "002"}}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ @@ -132,69 +120,9 @@ func TestListMigrations(t *testing.T) { }, sortOrder: SortOrderDesc, expectedMigrations: []*models.Migration{ - {Version: "20000"}, - {Version: "10000"}, - {Version: "1010"}, - {Version: "1009"}, - {Version: "190"}, - {Version: "110"}, - {Version: "001"}, - }, - }, - // Ensure that migrations are sorted in asc order for timestamp migrations - { - localFilesystemMigrations: []*models.Migration{ - {Version: "20080509220905"}, - {Version: "20080509220405"}, - {Version: "20080509150405"}, - {Version: "20080502150405"}, - {Version: "20080102150405"}, - {Version: "20070102150405"}, - {Version: "20060102150405"}, - }, - remoteAppliedMigrations: []*models.Migration{}, - cfg: configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleTimestamp, - }, - }, - sortOrder: SortOrderAsc, - expectedMigrations: []*models.Migration{ - {Version: "20060102150405"}, - {Version: "20070102150405"}, - {Version: "20080102150405"}, - {Version: "20080502150405"}, - {Version: "20080509150405"}, - {Version: "20080509220405"}, - {Version: "20080509220905"}, - }, - }, - // Ensure that migrations are sorted in desc order for timestamp migrations - { - localFilesystemMigrations: []*models.Migration{ - {Version: "20060102150405"}, - {Version: "20070102150405"}, - {Version: "20080102150405"}, - {Version: "20080502150405"}, - {Version: "20080509150405"}, - {Version: "20080509220405"}, - {Version: "20080509220905"}, - }, - remoteAppliedMigrations: []*models.Migration{}, - cfg: configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleTimestamp, - }, - }, - sortOrder: SortOrderDesc, - expectedMigrations: []*models.Migration{ - {Version: "20080509220905"}, - {Version: "20080509220405"}, - {Version: "20080509150405"}, - {Version: "20080502150405"}, - {Version: "20080102150405"}, - {Version: "20070102150405"}, - {Version: "20060102150405"}, + {Version: "003", Dependencies: []string{"001", "002"}}, + {Version: "002", Dependencies: []string{"001"}}, + {Version: "001", Dependencies: []string{}}, }, }, } @@ -235,127 +163,6 @@ 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{ @@ -580,140 +387,169 @@ func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { assert.Nil(t, migration) } -func TestApplyMigration_WithTransaction(t *testing.T) { +func TestCreateMigration_FirstMigrationNoDependencies(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, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{}, + Err: nil, }, - } - - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ + CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyMigration(&models.Migration{}) + migration, err := svc.CreateMigration("first migration") assert.Nil(t, err) - assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1) - assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) + assert.Equal(t, len(migration.Dependencies), 0) } -func TestApplyMigration_WithoutTransaction(t *testing.T) { +func TestCreateMigration_SecondMigrationDependsOnFirst(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}, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "001": {Version: "001", Dependencies: []string{}}, }, Err: nil, }, - } - - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ApplyReturnValue: bolttest.ApplyReturnValue{ + CreateReturnValue: bolttest.CreateReturnValue{ Err: nil, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyMigration(&models.Migration{}) + migration, err := svc.CreateMigration("second migration") assert.Nil(t, err) - assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) - assert.Equal(t, migrationDbRepo.ApplyCallCount, 1) + assert.Equal(t, len(migration.Dependencies), 1) + assert.Contains(t, migration.Dependencies, "001") } -func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { - expectedErr := errors.New("error!") +func TestCreateMigration_LinearChainDependsOnLatest(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - Script: sqlparse.MigrationScript{}, - Err: expectedErr, + 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{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyMigration(&models.Migration{}) + migration, err := svc.CreateMigration("fourth migration") - assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, err) + assert.Equal(t, len(migration.Dependencies), 1) + assert.Contains(t, migration.Dependencies, "003") } -func TestApplyMigration_ApplyWithTxErr(t *testing.T) { +func TestCreateMigration_ParallelBranchesDependsOnBoth(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Options: sqlparse.ExecutionOptions{ - UseTransaction: true, - }, + 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, + }, } - expectedErr := errors.New("error!") - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{Err: expectedErr}, - } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyMigration(&models.Migration{}) + migration, err := svc.CreateMigration("merge migration") - assert.ErrorIs(t, err, expectedErr) + assert.Nil(t, err) + assert.Equal(t, len(migration.Dependencies), 2) + assert.Contains(t, migration.Dependencies, "002") + assert.Contains(t, migration.Dependencies, "003") } -func TestApplyMigration_ApplyErr(t *testing.T) { +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{ - ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Options: sqlparse.ExecutionOptions{ - UseTransaction: false, - }, + 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, + }, } - expectedErr := errors.New("error!") - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ApplyReturnValue: bolttest.ApplyReturnValue{Err: expectedErr}, - } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyMigration(&models.Migration{}) + migration, err := svc.CreateMigration("new migration") - assert.ErrorIs(t, err, expectedErr) + 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) { @@ -918,163 +754,242 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) } -func TestRevertMigration_WithTransaction(t *testing.T) { +func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Contents: "DROP TABLE tmp;", - Options: sqlparse.ExecutionOptions{UseTransaction: true}, - }, - Err: nil, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, }, } + svc := NewMigrationService( + &bolttest.MockMigrationDBRepo{}, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{ - Err: nil, + 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( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertMigration(&models.Migration{}) + err := svc.RevertDownToVersion("002") - assert.Nil(t, err) - assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1) - assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.ErrorContains(t, err, "migration with version 002 does not exist") } -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, - }, +func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - RevertReturnValue: bolttest.RevertReturnValue{ - Err: nil, + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, }, } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertMigration(&models.Migration{}) + err := svc.RevertDownToVersion("001") - assert.Nil(t, err) - assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) - assert.Equal(t, migrationDbRepo.RevertCallCount, 1) + assert.ErrorContains(t, err, "migration with version 001 isn't applied") } -func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) { +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{ - Script: sqlparse.MigrationScript{}, - Err: expectedErr, + Err: expectedErr, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertMigration(&models.Migration{}) + err := svc.RevertDownToVersion("001") assert.ErrorIs(t, err, expectedErr) } -func TestRevertMigration_RevertWithTxErr(t *testing.T) { +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{ - ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Options: sqlparse.ExecutionOptions{ - UseTransaction: true, - }, - }, - Err: nil, + 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!") - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{Err: expectedErr}, + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertMigration(&models.Migration{}) + err := svc.RevertAllMigrations() assert.ErrorIs(t, err, expectedErr) } -func TestRevertMigration_RevertErr(t *testing.T) { +func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { + migrations := map[string]*models.Migration{ + "001": {Version: "001", Applied: false}, + "002": {Version: "002", Applied: false}, + } migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Options: sqlparse.ExecutionOptions{ - UseTransaction: false, - }, + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, }, - 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!") - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - RevertReturnValue: bolttest.RevertReturnValue{Err: expectedErr}, + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: migrations, + 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.RevertMigration(&models.Migration{}) + err := svc.RevertAllMigrations() assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) } -func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { - expectedErr := errors.New("error!") +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{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + 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 TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { +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{ - "001": {Version: "001", Applied: true}, + "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1082,21 +997,29 @@ func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { Err: nil, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{}, + Err: nil, + }, + } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("002") + err := svc.ApplyAllMigrations() - assert.ErrorContains(t, err, "migration with version 002 does not exist") + assert.ErrorContains(t, err, "non-existent migration 001") } -func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { +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{ - "001": {Version: "001", Applied: false}, + "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1104,49 +1027,62 @@ func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { Err: nil, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{}, + Err: nil, + }, + } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.ApplyUpToVersion("002") - assert.ErrorContains(t, err, "migration with version 001 isn't applied") + assert.ErrorContains(t, err, "non-existent migration 001") } -func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { +func TestRevertAllMigrations_CheckNoDependentsAppliedErr(t *testing.T) { + // Test CheckNoDependentsApplied error (line 173) migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, + "001": {Version: "001", Dependencies: []string{}, Applied: true}, + "002": {Version: "002", Dependencies: []string{"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{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + "001": {Version: "001", Applied: true}, + "002": {Version: "002", Applied: true}, + }, + Err: nil, }, } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertAllMigrations() - assert.ErrorIs(t, err, expectedErr) + assert.ErrorContains(t, err, "cannot revert migration") } -func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { +func TestRevertDownToVersion_CheckNoDependentsAppliedErr(t *testing.T) { + // Test CheckNoDependentsApplied error in RevertDownToVersion (line 223) migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, - "003": {Version: "003", Applied: false}, + "001": {Version: "001", Dependencies: []string{}, Applied: true}, + "002": {Version: "002", Dependencies: []string{"001"}, Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1154,102 +1090,135 @@ func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { Err: nil, }, } - svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, - migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, + 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.Nil(t, err) - assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2) + assert.ErrorContains(t, err, "cannot revert migration") } -func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) { - expectedErr := errors.New("error!") +func TestCreateMigration_FindLeafNodesErr(t *testing.T) { + // Test FindLeafNodes error when existing migrations have circular dependency (line 285) migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ - Migrations: nil, - Err: expectedErr, + 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{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + migration, err := svc.CreateMigration("new migration") - assert.ErrorIs(t, err, expectedErr) + assert.NotNil(t, err) + assert.ErrorContains(t, err, "circular dependency") + assert.Nil(t, migration) } -func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { +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", Applied: false}, - "002": {Version: "002", Applied: false}, + "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, + }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.ApplyAllMigrations() - assert.Nil(t, err) - assert.Equal(t, migrationDbRepo.RevertCallCount, 0) - assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) + // 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 TestRevertAllMigrations_RevertMigrationErr(t *testing.T) { +func TestApplyUpToVersion_DependencyNotYetAppliedInLoop(t *testing.T) { + // Same test for ApplyUpToVersion migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, + "001": {Version: "001", Dependencies: []string{}, Applied: false}, + "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, } - expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, - ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - Err: expectedErr, + 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{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.ApplyUpToVersion("002") - assert.ErrorIs(t, err, expectedErr) - assert.Equal(t, migrationDbRepo.RevertCallCount, 0) - assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) + assert.ErrorContains(t, err, "dependency 001 has not been applied") } |