diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2026-11-13 21:43:11 -0800 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2026-11-13 21:43:11 -0800 |
| commit | 28dd0bfc1373746b8c056a211f068da71fdc80b2 (patch) | |
| tree | 9c04a08501bc5abe7413263f60eaa55400dd162f /internal/services | |
| parent | Release v0.13.1 (diff) | |
| download | cli-28dd0bfc1373746b8c056a211f068da71fdc80b2.zip | |
Remove migration dependencies
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, 571 insertions, 1715 deletions
diff --git a/internal/services/migration.go b/internal/services/migration.go index ba43ff7..ddfb8c2 100644 --- a/internal/services/migration.go +++ b/internal/services/migration.go @@ -2,6 +2,7 @@ package services import ( "fmt" + "sort" "strconv" "time" @@ -11,17 +12,11 @@ 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 { - executor *MigrationExecutor - queryService *MigrationQueryService - depGraph *MigrationDependencyGraph - fsRepo repositories.MigrationFsRepo - cfg configloader.Config - outputter output.Outputter + dbRepo repositories.MigrationDBRepo + fsRepo repositories.MigrationFsRepo + cfg configloader.Config + outputter output.Outputter } func NewMigrationService( @@ -30,32 +25,29 @@ func NewMigrationService( cfg configloader.Config, outputter output.Outputter, ) MigrationService { - depGraph := NewMigrationDependencyGraph(cfg) - executor := NewMigrationExecutor(dbRepo, fsRepo) - queryService := NewMigrationQueryService(dbRepo, fsRepo, depGraph) return MigrationService{ - executor: executor, - queryService: queryService, - depGraph: depGraph, - fsRepo: fsRepo, - cfg: cfg, - outputter: outputter, + dbRepo: dbRepo, + 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( @@ -70,14 +62,6 @@ 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 { @@ -95,21 +79,16 @@ func (ms MigrationService) ApplyUpToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if targetMigration.Applied { - // TODO: Check not only that the target migration is applied, but also - // all prior migrations. + // Assumption: If the target migration is applied, all migrations before + // it must also be applied. 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( @@ -132,9 +111,19 @@ func (ms MigrationService) ApplyMigration(migration *models.Migration) error { ms.outputter.Output(fmt.Sprintf("Applying migration %s..", migration.Name())) startTime := time.Now() - err := ms.executor.Apply(migration) + upgradeScript, err := ms.fsRepo.ReadUpgradeScript(migration) if err != nil { - return err + 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) + } + + if err != nil { + return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err) } ms.outputter.Output( @@ -154,13 +143,8 @@ 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( @@ -192,21 +176,16 @@ func (ms MigrationService) RevertDownToVersion(version string) error { return fmt.Errorf("migration with version %s does not exist", version) } if !targetMigration.Applied { - // TODO: Check not only that the target migration is reverted, but also - // all prior migrations. + // Assumption: Every migration from the latest down to the target + // migration hasn't been applied. 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( @@ -229,7 +208,17 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error { ms.outputter.Output(fmt.Sprintf("Reverting migration %s..", migration.Name())) startTime := time.Now() - err := ms.executor.Revert(migration) + 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) + } + if err != nil { return err } @@ -258,13 +247,7 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e migration = models.NewSequentialMigration(currentVersion+1, message) } - leafNodes, err := ms.getMigrationLeafNodes() - if err != nil { - return nil, err - } - migration.Dependencies = leafNodes - - err = ms.fsRepo.Create(migration) + err := ms.fsRepo.Create(migration) if err != nil { return nil, err } @@ -274,45 +257,150 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e return migration, nil } -func (ms MigrationService) getMigrationLeafNodes() ([]string, error) { +func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { + var currentVersion uint64 = 0 + localMigrations, err := ms.fsRepo.List() if err != nil { - return nil, fmt.Errorf("unable to list migrations for finding leaf nodes: %w", err) + return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err) } - leafNodes, err := ms.depGraph.FindLeafNodes(localMigrations) + migrations, err := ms.combineMigrations( + localMigrations, + map[string]*models.Migration{}, + SortOrderDesc, + ) if err != nil { - return nil, fmt.Errorf("unable to find leaf nodes for dependencies: %w", err) + return 0, err } - return leafNodes, nil -} + if len(migrations) > 0 { + latestMigration := migrations[0] + currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64) + if err != nil { + return 0, err + } + } -func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) { - var maxVersion uint64 = 0 + return currentVersion, 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. localMigrations, err := ms.fsRepo.List() if err != nil { - return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err) + return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err) } - 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, + appliedMigrations, err := ms.dbRepo.List() + if err != nil { + return nil, fmt.Errorf( + "unable to list out applied migrations from remote db: %w", + err, + ) + } + + 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) + } + + err := ms.sortMigrations(migrations, order) + if err != nil { + return nil, 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, ) + } else { + comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order) } - if version > maxVersion { - maxVersion = version + + if err != nil { + sortErrs = append(sortErrs, err) } + + 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 } - return maxVersion, nil + if order == SortOrderAsc { + return m1Version < m2Version, nil + } else { + return m1Version > m2Version, nil + } } -func (ms MigrationService) ListMigrations(order SortOrder) ([]*models.Migration, error) { - return ms.queryService.ListMigrations(order) +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 + } } diff --git a/internal/services/migration_dependency_graph.go b/internal/services/migration_dependency_graph.go deleted file mode 100644 index 3f0e417..0000000 --- a/internal/services/migration_dependency_graph.go +++ /dev/null @@ -1,191 +0,0 @@ -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 deleted file mode 100644 index 29d36db..0000000 --- a/internal/services/migration_dependency_graph_test.go +++ /dev/null @@ -1,377 +0,0 @@ -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 deleted file mode 100644 index e9f827e..0000000 --- a/internal/services/migration_executor.go +++ /dev/null @@ -1,66 +0,0 @@ -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 deleted file mode 100644 index 00e7402..0000000 --- a/internal/services/migration_executor_test.go +++ /dev/null @@ -1,230 +0,0 @@ -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 deleted file mode 100644 index f505727..0000000 --- a/internal/services/migration_query_service.go +++ /dev/null @@ -1,90 +0,0 @@ -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 deleted file mode 100644 index af48777..0000000 --- a/internal/services/migration_query_service_test.go +++ /dev/null @@ -1,309 +0,0 @@ -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 c931dc9..09d33a3 100644 --- a/internal/services/migration_test.go +++ b/internal/services/migration_test.go @@ -18,7 +18,7 @@ func TestListMigrations(t *testing.T) { localFilesystemMigrations []*models.Migration remoteAppliedMigrations []*models.Migration cfg configloader.Config - sortOrder SortOrder + sortOrder sortOrder expectedMigrations []*models.Migration } @@ -85,12 +85,16 @@ func TestListMigrations(t *testing.T) { sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{}, }, - // Ensure that migrations are sorted by dependencies (topological order) + // Ensure that migrations are sorted in asc order for sequential migrations { localFilesystemMigrations: []*models.Migration{ - {Version: "003", Dependencies: []string{"001", "002"}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "001", Dependencies: []string{}}, + {Version: "20000"}, + {Version: "10000"}, + {Version: "1010"}, + {Version: "1009"}, + {Version: "190"}, + {Version: "110"}, + {Version: "001"}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ @@ -100,17 +104,25 @@ func TestListMigrations(t *testing.T) { }, sortOrder: SortOrderAsc, expectedMigrations: []*models.Migration{ - {Version: "001", Dependencies: []string{}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "003", Dependencies: []string{"001", "002"}}, + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, }, }, - // Ensure that migrations are sorted in reverse topological order for desc + // Ensure that migrations are sorted in desc order for sequential migrations { localFilesystemMigrations: []*models.Migration{ - {Version: "001", Dependencies: []string{}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "003", Dependencies: []string{"001", "002"}}, + {Version: "001"}, + {Version: "110"}, + {Version: "190"}, + {Version: "1009"}, + {Version: "1010"}, + {Version: "10000"}, + {Version: "20000"}, }, remoteAppliedMigrations: []*models.Migration{}, cfg: configloader.Config{ @@ -120,9 +132,69 @@ func TestListMigrations(t *testing.T) { }, sortOrder: SortOrderDesc, expectedMigrations: []*models.Migration{ - {Version: "003", Dependencies: []string{"001", "002"}}, - {Version: "002", Dependencies: []string{"001"}}, - {Version: "001", Dependencies: []string{}}, + {Version: "20000"}, + {Version: "10000"}, + {Version: "1010"}, + {Version: "1009"}, + {Version: "190"}, + {Version: "110"}, + {Version: "001"}, + }, + }, + // Ensure that migrations are sorted in asc order for timestamp migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "20080509220905"}, + {Version: "20080509220405"}, + {Version: "20080509150405"}, + {Version: "20080502150405"}, + {Version: "20080102150405"}, + {Version: "20070102150405"}, + {Version: "20060102150405"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }, + sortOrder: SortOrderAsc, + expectedMigrations: []*models.Migration{ + {Version: "20060102150405"}, + {Version: "20070102150405"}, + {Version: "20080102150405"}, + {Version: "20080502150405"}, + {Version: "20080509150405"}, + {Version: "20080509220405"}, + {Version: "20080509220905"}, + }, + }, + // Ensure that migrations are sorted in desc order for timestamp migrations + { + localFilesystemMigrations: []*models.Migration{ + {Version: "20060102150405"}, + {Version: "20070102150405"}, + {Version: "20080102150405"}, + {Version: "20080502150405"}, + {Version: "20080509150405"}, + {Version: "20080509220405"}, + {Version: "20080509220905"}, + }, + remoteAppliedMigrations: []*models.Migration{}, + cfg: configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleTimestamp, + }, + }, + sortOrder: SortOrderDesc, + expectedMigrations: []*models.Migration{ + {Version: "20080509220905"}, + {Version: "20080509220405"}, + {Version: "20080509150405"}, + {Version: "20080502150405"}, + {Version: "20080102150405"}, + {Version: "20070102150405"}, + {Version: "20060102150405"}, }, }, } @@ -163,6 +235,127 @@ func TestListMigrations(t *testing.T) { } } +func TestListMigrations_FsRepoListError(t *testing.T) { + expectedErr := errors.New("fs repo error") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorIs(t, err, expectedErr) + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) +} + +func TestListMigrations_DbRepoListError(t *testing.T) { + expectedErr := errors.New("db repo error") + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: expectedErr, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{}, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorIs(t, err, expectedErr) + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) +} + +func TestListMigrations_ParseErrDuringSort(t *testing.T) { + type test struct { + m1 models.Migration + m2 models.Migration + versionStyle configloader.VersionStyle + } + + tests := []test{ + { + m1: models.Migration{Version: "abc"}, + m2: models.Migration{Version: "123"}, + versionStyle: configloader.VersionStyleSequential, + }, + { + m1: models.Migration{Version: "123"}, + m2: models.Migration{Version: "abc"}, + versionStyle: configloader.VersionStyleSequential, + }, + { + m1: models.Migration{Version: "20060102150405"}, + m2: models.Migration{Version: "abc"}, + versionStyle: configloader.VersionStyleTimestamp, + }, + { + m1: models.Migration{Version: "abc"}, + m2: models.Migration{Version: "20060102150405"}, + versionStyle: configloader.VersionStyleTimestamp, + }, + } + + for _, tc := range tests { + migrationFsRepo := &bolttest.MockMigrationFsRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: map[string]*models.Migration{ + tc.m1.Version: {Version: tc.m1.Version}, + tc.m2.Version: {Version: tc.m2.Version}, + }, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ListReturnValue: bolttest.ListReturnValue{ + Migrations: nil, + Err: nil, + }, + } + + svc := NewMigrationService( + migrationDbRepo, + migrationFsRepo, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: tc.versionStyle, + }, + }, + bolttest.NullOutputter{}, + ) + migrations, err := svc.ListMigrations(SortOrderAsc) + + assert.ErrorContains(t, err, "unable to sort migrations") + var expectedMigrations []*models.Migration + assert.DeepEqual(t, migrations, expectedMigrations) + } +} + func TestCreateMigration_VersionStyleTimestamp(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ CreateReturnValue: bolttest.CreateReturnValue{ @@ -387,169 +580,140 @@ func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) { assert.Nil(t, migration) } -func TestCreateMigration_FirstMigrationNoDependencies(t *testing.T) { +func TestApplyMigration_WithTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + Err: nil, }, - CreateReturnValue: bolttest.CreateReturnValue{ + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("first migration") + err := svc.ApplyMigration(&models.Migration{}) assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 0) + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 0) } -func TestCreateMigration_SecondMigrationDependsOnFirst(t *testing.T) { +func TestApplyMigration_WithoutTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, }, Err: nil, }, - CreateReturnValue: bolttest.CreateReturnValue{ + } + + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyReturnValue: bolttest.ApplyReturnValue{ Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("second migration") + err := svc.ApplyMigration(&models.Migration{}) assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 1) - assert.Contains(t, migration.Dependencies, "001") + assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.ApplyCallCount, 1) } -func TestCreateMigration_LinearChainDependsOnLatest(t *testing.T) { +func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) { + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - "003": {Version: "003", Dependencies: []string{"002"}}, - }, - Err: nil, - }, - CreateReturnValue: bolttest.CreateReturnValue{ - Err: nil, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{}, + Err: expectedErr, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("fourth migration") + err := svc.ApplyMigration(&models.Migration{}) - assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 1) - assert.Contains(t, migration.Dependencies, "003") + assert.ErrorIs(t, err, expectedErr) } -func TestCreateMigration_ParallelBranchesDependsOnBoth(t *testing.T) { +func TestApplyMigration_ApplyWithTxErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - "003": {Version: "003", Dependencies: []string{"001"}}, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: true, + }, }, Err: nil, }, - CreateReturnValue: bolttest.CreateReturnValue{ - Err: nil, - }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} + expectedErr := errors.New("error!") + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{Err: expectedErr}, + } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("merge migration") + err := svc.ApplyMigration(&models.Migration{}) - assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 2) - assert.Contains(t, migration.Dependencies, "002") - assert.Contains(t, migration.Dependencies, "003") + assert.ErrorIs(t, err, expectedErr) } -func TestCreateMigration_ComplexGraphMultipleLeaves(t *testing.T) { - // Diamond shape: 001 -> 002, 001 -> 003, 002 -> 004, 003 -> 004 - // Plus independent branch: 005 - // Leaf nodes are 004 and 005 +func TestApplyMigration_ApplyErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - "003": {Version: "003", Dependencies: []string{"001"}}, - "004": {Version: "004", Dependencies: []string{"002", "003"}}, - "005": {Version: "005", Dependencies: []string{}}, + ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: false, + }, }, Err: nil, }, - CreateReturnValue: bolttest.CreateReturnValue{ - Err: nil, - }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} + expectedErr := errors.New("error!") + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + ApplyReturnValue: bolttest.ApplyReturnValue{Err: expectedErr}, + } svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("new migration") + err := svc.ApplyMigration(&models.Migration{}) - assert.Nil(t, err) - assert.Equal(t, len(migration.Dependencies), 2) - assert.Contains(t, migration.Dependencies, "004") - assert.Contains(t, migration.Dependencies, "005") + assert.ErrorIs(t, err, expectedErr) } func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) { @@ -754,242 +918,163 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) { assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2) } -func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { - expectedErr := errors.New("error!") +func TestRevertMigration_WithTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: nil, - Err: expectedErr, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Contents: "DROP TABLE tmp;", + Options: sqlparse.ExecutionOptions{UseTransaction: true}, + }, + Err: nil, }, } - svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, - migrationFsRepo, - configloader.Config{}, - bolttest.NullOutputter{}, - ) - err := svc.RevertDownToVersion("001") - - assert.ErrorIs(t, err, expectedErr) -} - -func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - } - migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{ + Err: nil, }, } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("002") + err := svc.RevertMigration(&models.Migration{}) - assert.ErrorContains(t, err, "migration with version 002 does not exist") + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) } -func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, - } +func TestRevertMigration_WithoutTransaction(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Contents: "DROP TABLE tmp;", + Options: sqlparse.ExecutionOptions{UseTransaction: false}, + }, + Err: nil, + }, + } + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertReturnValue: bolttest.RevertReturnValue{ + Err: nil, }, } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertMigration(&models.Migration{}) - assert.ErrorContains(t, err, "migration with version 001 isn't applied") + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertCallCount, 1) } -func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - } +func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) { expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, - }, ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - Err: expectedErr, + Script: sqlparse.MigrationScript{}, + Err: expectedErr, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertDownToVersion("001") + err := svc.RevertMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) } -func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, - "003": {Version: "003", Applied: false}, - } +func TestRevertMigration_RevertWithTxErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, - }, - } - svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, - migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: true, + }, }, + Err: nil, }, - bolttest.NullOutputter{}, - ) - - err := svc.RevertDownToVersion("001") - - assert.Nil(t, err) - assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2) -} - -func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) { + } expectedErr := errors.New("error!") - migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: nil, - Err: expectedErr, - }, + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{Err: expectedErr}, } svc := NewMigrationService( - &bolttest.MockMigrationDBRepo{}, + migrationDbRepo, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.RevertMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) } -func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: false}, - "002": {Version: "002", Applied: false}, - } +func TestRevertMigration_RevertErr(t *testing.T) { migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, - }, - } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} - svc := NewMigrationService( - migrationDbRepo, - migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Script: sqlparse.MigrationScript{ + Options: sqlparse.ExecutionOptions{ + UseTransaction: false, + }, }, + Err: nil, }, - bolttest.NullOutputter{}, - ) - - err := svc.RevertAllMigrations() - - assert.Nil(t, err) - assert.Equal(t, migrationDbRepo.RevertCallCount, 0) - assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) -} - -func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) { - migrations := map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, } expectedErr := errors.New("error!") - migrationFsRepo := &bolttest.MockMigrationFsRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: migrations, - Err: nil, - }, - ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ - Err: expectedErr, - }, + migrationDbRepo := &bolttest.MockMigrationDBRepo{ + RevertReturnValue: bolttest.RevertReturnValue{Err: expectedErr}, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleSequential, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.RevertMigration(&models.Migration{}) assert.ErrorIs(t, err, expectedErr) - assert.Equal(t, migrationDbRepo.RevertCallCount, 0) - assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) } -func TestCreateMigration_FsRepoListErrForLeafNodes(t *testing.T) { - // Test fsRepo.List error when finding leaf nodes (line 280) - // Using timestamp version style so we only call List once (for leaf nodes) - expectedErr := errors.New("fs error finding leaf nodes") +func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: nil, Err: expectedErr, }, } - svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleTimestamp, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("new migration") + err := svc.RevertDownToVersion("001") assert.ErrorIs(t, err, expectedErr) - assert.ErrorContains(t, err, "unable to list migrations for finding leaf nodes") - assert.Nil(t, migration) } -func TestApplyAllMigrations_CheckDependenciesAppliedErr(t *testing.T) { - // Test CheckDependenciesApplied error (line 66) - // Migration 002 depends on 001, but 001 doesn't exist +func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) { migrations := map[string]*models.Migration{ - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: true}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -997,29 +1082,21 @@ func TestApplyAllMigrations_CheckDependenciesAppliedErr(t *testing.T) { Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.ApplyAllMigrations() + err := svc.RevertDownToVersion("002") - assert.ErrorContains(t, err, "non-existent migration 001") + assert.ErrorContains(t, err, "migration with version 002 does not exist") } -func TestApplyUpToVersion_CheckDependenciesAppliedErr(t *testing.T) { - // Test CheckDependenciesApplied error in ApplyUpToVersion (line 116) - // Migration 002 depends on 001, but 001 doesn't exist +func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1027,62 +1104,49 @@ func TestApplyUpToVersion_CheckDependenciesAppliedErr(t *testing.T) { Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("002") + err := svc.RevertDownToVersion("001") - assert.ErrorContains(t, err, "non-existent migration 001") + assert.ErrorContains(t, err, "migration with version 001 isn't applied") } -func TestRevertAllMigrations_CheckNoDependentsAppliedErr(t *testing.T) { - // Test CheckNoDependentsApplied error (line 173) +func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: true}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: true}, + "001": {Version: "001", Applied: true}, } + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, - } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, - }, - Err: nil, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, }, } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, configloader.Config{}, bolttest.NullOutputter{}, ) - err := svc.RevertAllMigrations() + err := svc.RevertDownToVersion("001") - assert.ErrorContains(t, err, "cannot revert migration") + assert.ErrorIs(t, err, expectedErr) } -func TestRevertDownToVersion_CheckNoDependentsAppliedErr(t *testing.T) { - // Test CheckNoDependentsApplied error in RevertDownToVersion (line 223) +func TestRevertDownToVersion_RevertsMigrations(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: true}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: true}, + "001": {Version: "001", Applied: true}, + "002": {Version: "002", Applied: true}, + "003": {Version: "003", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ @@ -1090,135 +1154,102 @@ func TestRevertDownToVersion_CheckNoDependentsAppliedErr(t *testing.T) { Err: nil, }, } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Applied: true}, - "002": {Version: "002", Applied: true}, - }, - Err: nil, - }, - } svc := NewMigrationService( - migrationDbRepo, + &bolttest.MockMigrationDBRepo{}, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) err := svc.RevertDownToVersion("001") - assert.ErrorContains(t, err, "cannot revert migration") + assert.Nil(t, err) + assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2) } -func TestCreateMigration_FindLeafNodesErr(t *testing.T) { - // Test FindLeafNodes error when existing migrations have circular dependency (line 285) +func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) { + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{"002"}}, - "002": {Version: "002", Dependencies: []string{"001"}}, - }, - Err: nil, + Migrations: nil, + Err: expectedErr, }, } svc := NewMigrationService( &bolttest.MockMigrationDBRepo{}, migrationFsRepo, - configloader.Config{ - Migrations: configloader.MigrationsConfig{ - VersionStyle: configloader.VersionStyleTimestamp, - }, - }, + configloader.Config{}, bolttest.NullOutputter{}, ) - migration, err := svc.CreateMigration("new migration") + err := svc.RevertAllMigrations() - assert.NotNil(t, err) - assert.ErrorContains(t, err, "circular dependency") - assert.Nil(t, migration) + assert.ErrorIs(t, err, expectedErr) } -func TestApplyAllMigrations_DependencyNotYetAppliedInLoop(t *testing.T) { - // Test that CheckDependenciesApplied fails when a dependency was applied earlier - // in the same ApplyAllMigrations call but the Applied field wasn't updated +func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: false}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: false}, + "002": {Version: "002", Applied: false}, } migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, - ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Contents: "CREATE TABLE test (id INT);", - Options: sqlparse.ExecutionOptions{UseTransaction: true}, - }, - Err: nil, - }, - } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ - Err: nil, - }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyAllMigrations() + err := svc.RevertAllMigrations() - // This should fail because after applying 001, its Applied field in the list - // is still false, so CheckDependenciesApplied for 002 will fail - assert.ErrorContains(t, err, "dependency 001 has not been applied") + assert.Nil(t, err) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) } -func TestApplyUpToVersion_DependencyNotYetAppliedInLoop(t *testing.T) { - // Same test for ApplyUpToVersion +func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) { migrations := map[string]*models.Migration{ - "001": {Version: "001", Dependencies: []string{}, Applied: false}, - "002": {Version: "002", Dependencies: []string{"001"}, Applied: false}, + "001": {Version: "001", Applied: true}, } + expectedErr := errors.New("error!") migrationFsRepo := &bolttest.MockMigrationFsRepo{ ListReturnValue: bolttest.ListReturnValue{ Migrations: migrations, Err: nil, }, - ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{ - Script: sqlparse.MigrationScript{ - Contents: "CREATE TABLE test (id INT);", - Options: sqlparse.ExecutionOptions{UseTransaction: true}, - }, - Err: nil, - }, - } - migrationDbRepo := &bolttest.MockMigrationDBRepo{ - ListReturnValue: bolttest.ListReturnValue{ - Migrations: map[string]*models.Migration{}, - Err: nil, - }, - ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{ - Err: nil, + ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{ + Err: expectedErr, }, } + migrationDbRepo := &bolttest.MockMigrationDBRepo{} svc := NewMigrationService( migrationDbRepo, migrationFsRepo, - configloader.Config{}, + configloader.Config{ + Migrations: configloader.MigrationsConfig{ + VersionStyle: configloader.VersionStyleSequential, + }, + }, bolttest.NullOutputter{}, ) - err := svc.ApplyUpToVersion("002") + err := svc.RevertAllMigrations() - assert.ErrorContains(t, err, "dependency 001 has not been applied") + assert.ErrorIs(t, err, expectedErr) + assert.Equal(t, migrationDbRepo.RevertCallCount, 0) + assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0) } |