aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-11-13 21:43:11 -0800
committerEugene Triguba <hello@eugenetriguba.com>2026-11-13 21:43:11 -0800
commit28dd0bfc1373746b8c056a211f068da71fdc80b2 (patch)
tree9c04a08501bc5abe7413263f60eaa55400dd162f
parentRelease v0.13.1 (diff)
downloadcli-28dd0bfc1373746b8c056a211f068da71fdc80b2.zip
Remove migration dependencies
-rw-r--r--CHANGELOG.md5
-rw-r--r--Makefile2
-rw-r--r--README.md5
-rw-r--r--internal/bolttest/mock_migration_dependency_graph.go65
-rw-r--r--internal/bolttest/mock_migration_executor.go26
-rw-r--r--internal/bolttest/mock_migration_query_service.go31
-rw-r--r--internal/models/migration.go19
-rw-r--r--internal/models/migration_test.go2
-rw-r--r--internal/repositories/migration_fs_repo.go47
-rw-r--r--internal/repositories/migration_fs_repo_test.go10
-rw-r--r--internal/services/migration.go252
-rw-r--r--internal/services/migration_dependency_graph.go191
-rw-r--r--internal/services/migration_dependency_graph_test.go377
-rw-r--r--internal/services/migration_executor.go66
-rw-r--r--internal/services/migration_executor_test.go230
-rw-r--r--internal/services/migration_query_service.go90
-rw-r--r--internal/services/migration_query_service_test.go309
-rw-r--r--internal/services/migration_test.go771
-rw-r--r--internal/sqlparse/sqlparse.go71
-rw-r--r--internal/sqlparse/sqlparse_test.go105
-rw-r--r--man/bolt-dependencies.7121
-rw-r--r--man/bolt-down.12
-rw-r--r--man/bolt-execution.713
-rw-r--r--man/bolt-migration.534
-rw-r--r--man/bolt-new.112
-rw-r--r--man/bolt-status.13
-rw-r--r--man/bolt-tutorial.722
-rw-r--r--man/bolt-up.14
-rw-r--r--man/bolt.11
29 files changed, 624 insertions, 2262 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index eb55a49..3c69a9d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Removed
+
+- **Migration dependencies**: The `-- depends:` directive and dependency-based
+ ordering added in 0.12.0 have been removed.
+
## [0.13.1] - 2026-02-12
### Changed
diff --git a/Makefile b/Makefile
index df60659..16c2c89 100644
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,6 @@ install: build
install -d $(DESTDIR)$(MANDIR)/man7
install -m 644 man/bolt-tutorial.7 $(DESTDIR)$(MANDIR)/man7/
install -m 644 man/bolt-databases.7 $(DESTDIR)$(MANDIR)/man7/
- install -m 644 man/bolt-dependencies.7 $(DESTDIR)$(MANDIR)/man7/
install -m 644 man/bolt-execution.7 $(DESTDIR)$(MANDIR)/man7/
uninstall:
@@ -42,7 +41,6 @@ uninstall:
rm -f $(DESTDIR)$(MANDIR)/man5/bolt-migration.5
rm -f $(DESTDIR)$(MANDIR)/man7/bolt-tutorial.7
rm -f $(DESTDIR)$(MANDIR)/man7/bolt-databases.7
- rm -f $(DESTDIR)$(MANDIR)/man7/bolt-dependencies.7
rm -f $(DESTDIR)$(MANDIR)/man7/bolt-execution.7
clean:
diff --git a/README.md b/README.md
index 12f4e77..f20ea41 100644
--- a/README.md
+++ b/README.md
@@ -21,11 +21,9 @@ run database migrations.
## What It Looks Like
Migrations are separated by "up" and "down" comments to delineate what runs when
-you apply the migration and what runs when you revert it. Additionally,
-migrations have dependencies which are what determine the execution order.
+you apply the migration and what runs when you revert it.
```sql
--- depends: 20240316145038
-- migrate:up
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
@@ -108,7 +106,6 @@ After installation, documentation is available via man pages.
|------|-------------|
| bolt-tutorial(7) | Getting started tutorial |
| bolt-databases(7) | Database drivers and DSN formats |
-| bolt-dependencies(7) | Migration dependency system |
| bolt-execution(7) | How migrations are executed |
## License
diff --git a/internal/bolttest/mock_migration_dependency_graph.go b/internal/bolttest/mock_migration_dependency_graph.go
deleted file mode 100644
index 1d73ca4..0000000
--- a/internal/bolttest/mock_migration_dependency_graph.go
+++ /dev/null
@@ -1,65 +0,0 @@
-package bolttest
-
-import "git.sr.ht/~eugenetriguba/bolt/internal/models"
-
-type MockMigrationDependencyGraph struct {
- TopologicalSortReturnValue TopologicalSortReturnValue
- TopologicalSortCallCount int
- ReverseTopologicalSortReturnValue ReverseTopologicalSortReturnValue
- ReverseTopologicalSortCallCount int
- CheckDependenciesAppliedReturnValue CheckDependenciesAppliedReturnValue
- CheckDependenciesAppliedCallCount int
- CheckNoDependentsAppliedReturnValue CheckNoDependentsAppliedReturnValue
- CheckNoDependentsAppliedCallCount int
- FindLeafNodesReturnValue FindLeafNodesReturnValue
- FindLeafNodesCallCount int
-}
-
-type TopologicalSortReturnValue struct {
- Migrations []*models.Migration
- Err error
-}
-
-type ReverseTopologicalSortReturnValue = TopologicalSortReturnValue
-
-type CheckDependenciesAppliedReturnValue struct {
- Err error
-}
-
-type CheckNoDependentsAppliedReturnValue = CheckDependenciesAppliedReturnValue
-
-type FindLeafNodesReturnValue struct {
- LeafNodes []string
- Err error
-}
-
-func (g *MockMigrationDependencyGraph) TopologicalSort(migrations []*models.Migration) ([]*models.Migration, error) {
- g.TopologicalSortCallCount++
- return g.TopologicalSortReturnValue.Migrations, g.TopologicalSortReturnValue.Err
-}
-
-func (g *MockMigrationDependencyGraph) ReverseTopologicalSort(migrations []*models.Migration) ([]*models.Migration, error) {
- g.ReverseTopologicalSortCallCount++
- return g.ReverseTopologicalSortReturnValue.Migrations, g.ReverseTopologicalSortReturnValue.Err
-}
-
-func (g *MockMigrationDependencyGraph) CheckDependenciesApplied(
- migration *models.Migration,
- allMigrations []*models.Migration,
-) error {
- g.CheckDependenciesAppliedCallCount++
- return g.CheckDependenciesAppliedReturnValue.Err
-}
-
-func (g *MockMigrationDependencyGraph) CheckNoDependentsApplied(
- migration *models.Migration,
- allMigrations []*models.Migration,
-) error {
- g.CheckNoDependentsAppliedCallCount++
- return g.CheckNoDependentsAppliedReturnValue.Err
-}
-
-func (g *MockMigrationDependencyGraph) FindLeafNodes(localMigrations map[string]*models.Migration) ([]string, error) {
- g.FindLeafNodesCallCount++
- return g.FindLeafNodesReturnValue.LeafNodes, g.FindLeafNodesReturnValue.Err
-}
diff --git a/internal/bolttest/mock_migration_executor.go b/internal/bolttest/mock_migration_executor.go
deleted file mode 100644
index 74eda4f..0000000
--- a/internal/bolttest/mock_migration_executor.go
+++ /dev/null
@@ -1,26 +0,0 @@
-package bolttest
-
-import "git.sr.ht/~eugenetriguba/bolt/internal/models"
-
-type MockMigrationExecutor struct {
- ApplyReturnValue ExecutorApplyReturnValue
- ApplyCallCount int
- RevertReturnValue ExecutorRevertReturnValue
- RevertCallCount int
-}
-
-type ExecutorApplyReturnValue struct {
- Err error
-}
-
-type ExecutorRevertReturnValue = ExecutorApplyReturnValue
-
-func (e *MockMigrationExecutor) Apply(migration *models.Migration) error {
- e.ApplyCallCount++
- return e.ApplyReturnValue.Err
-}
-
-func (e *MockMigrationExecutor) Revert(migration *models.Migration) error {
- e.RevertCallCount++
- return e.RevertReturnValue.Err
-}
diff --git a/internal/bolttest/mock_migration_query_service.go b/internal/bolttest/mock_migration_query_service.go
deleted file mode 100644
index 9200929..0000000
--- a/internal/bolttest/mock_migration_query_service.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package bolttest
-
-import "git.sr.ht/~eugenetriguba/bolt/internal/models"
-
-type MockMigrationQueryService struct {
- ListMigrationsReturnValue QueryListMigrationsReturnValue
- ListMigrationsCallCount int
- CombineMigrationsReturnValue QueryCombineMigrationsReturnValue
- CombineMigrationsCallCount int
-}
-
-type QueryListMigrationsReturnValue struct {
- Migrations []*models.Migration
- Err error
-}
-
-type QueryCombineMigrationsReturnValue = QueryListMigrationsReturnValue
-
-func (s *MockMigrationQueryService) ListMigrations(order interface{}) ([]*models.Migration, error) {
- s.ListMigrationsCallCount++
- return s.ListMigrationsReturnValue.Migrations, s.ListMigrationsReturnValue.Err
-}
-
-func (s *MockMigrationQueryService) CombineMigrations(
- localMigrations map[string]*models.Migration,
- appliedMigrations map[string]*models.Migration,
- order interface{},
-) ([]*models.Migration, error) {
- s.CombineMigrationsCallCount++
- return s.CombineMigrationsReturnValue.Migrations, s.CombineMigrationsReturnValue.Err
-}
diff --git a/internal/models/migration.go b/internal/models/migration.go
index baf5de9..ed48073 100644
--- a/internal/models/migration.go
+++ b/internal/models/migration.go
@@ -7,10 +7,9 @@ import (
)
type Migration struct {
- Version string
- Message string
- Applied bool
- Dependencies []string
+ Version string
+ Message string
+ Applied bool
}
func NewTimestampMigration(version time.Time, message string) *Migration {
@@ -20,18 +19,16 @@ func NewTimestampMigration(version time.Time, message string) *Migration {
version.Year(), version.Month(), version.Day(),
version.Hour(), version.Minute(), version.Second(),
),
- Message: message,
- Applied: false,
- Dependencies: []string{},
+ Message: message,
+ Applied: false,
}
}
func NewSequentialMigration(version uint64, message string) *Migration {
return &Migration{
- Version: fmt.Sprintf("%03d", version),
- Message: message,
- Applied: false,
- Dependencies: []string{},
+ Version: fmt.Sprintf("%03d", version),
+ Message: message,
+ Applied: false,
}
}
diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go
index d7c1d6d..aae1cba 100644
--- a/internal/models/migration_test.go
+++ b/internal/models/migration_test.go
@@ -16,7 +16,6 @@ func TestNewTimestampMigration(t *testing.T) {
check.Equal(t, m.Version, "20200101000000")
check.Equal(t, m.Message, "test message")
check.Equal(t, m.Applied, false)
- check.DeepEqual(t, m.Dependencies, []string{})
}
func TestNewSequentialMigration(t *testing.T) {
@@ -31,7 +30,6 @@ func TestNewSequentialMigration(t *testing.T) {
check.Equal(t, m.Version, "100")
check.Equal(t, m.Message, "test message")
check.Equal(t, m.Applied, false)
- check.DeepEqual(t, m.Dependencies, []string{})
}
func TestName(t *testing.T) {
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index b300dad..72057ec 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -71,15 +71,7 @@ func (mr migrationFsRepo) Create(migration *models.Migration) error {
}
defer file.Close()
- var template strings.Builder
- if len(migration.Dependencies) > 0 {
- template.WriteString("-- depends: ")
- template.WriteString(strings.Join(migration.Dependencies, ", "))
- template.WriteString("\n")
- }
- template.WriteString("-- migrate:up\n\n-- migrate:down\n")
-
- _, err = file.WriteString(template.String())
+ _, err = file.WriteString("-- migrate:up\n\n-- migrate:down\n")
if err != nil {
return fmt.Errorf("unable to write template to new migration script: %w", err)
}
@@ -94,7 +86,7 @@ func (mr migrationFsRepo) List() (map[string]*models.Migration, error) {
var migrations = make(map[string]*models.Migration, 0)
for _, entry := range entries {
- migration, err := mr.dirEntryToMigration(entry)
+ migration, err := dirEntryToMigration(entry)
if err != nil {
return nil, err
}
@@ -106,13 +98,14 @@ func (mr migrationFsRepo) List() (map[string]*models.Migration, error) {
migration.Version,
)
}
+
migrations[migration.Version] = migration
}
return migrations, nil
}
-func (mr migrationFsRepo) dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) {
+func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) {
parts := strings.SplitN(entry.Name(), "_", 2)
if len(parts) != 2 {
return nil, fmt.Errorf(
@@ -123,35 +116,11 @@ func (mr migrationFsRepo) dirEntryToMigration(entry fs.DirEntry) (*models.Migrat
}
version := parts[0]
message := strings.TrimSuffix(parts[1], filepath.Ext(parts[1])) // Remove .sql
- migration := &models.Migration{
+ return &models.Migration{
Version: version,
Message: message,
Applied: false,
- }
-
- scriptPath := filepath.Join(mr.migrationsDirPath, entry.Name())
- dependencies, err := mr.parseDependenciesFromFile(scriptPath)
- if err != nil {
- return nil, fmt.Errorf("unable to parse dependencies from %s: %w", entry.Name(), err)
- }
- migration.Dependencies = dependencies
-
- return migration, nil
-}
-
-func (mr migrationFsRepo) parseDependenciesFromFile(scriptPath string) ([]string, error) {
- scriptContents, err := mr.readScriptContents(scriptPath)
- if err != nil {
- return nil, err
- }
-
- sqlParser := sqlparse.NewSqlParser()
- migration, err := sqlParser.Parse(strings.NewReader(scriptContents))
- if err != nil {
- return nil, err
- }
-
- return migration.Dependencies, nil
+ }, nil
}
func (mr migrationFsRepo) ReadUpgradeScript(
@@ -188,8 +157,7 @@ func (mr migrationFsRepo) getMigrationScripts(
)
}
sqlParser := sqlparse.NewSqlParser()
- migration, err := sqlParser.Parse(strings.NewReader(scriptContents))
- return migration.UpgradeScript, migration.DowngradeScript, err
+ return sqlParser.Parse(strings.NewReader(scriptContents))
}
func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error) {
@@ -197,5 +165,6 @@ func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error)
if err != nil {
return "", err
}
+
return string(contents), nil
}
diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go
index facfd34..7625c06 100644
--- a/internal/repositories/migration_fs_repo_test.go
+++ b/internal/repositories/migration_fs_repo_test.go
@@ -55,17 +55,11 @@ func TestCreate_SuccessfullyCreated(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "add users table")
- migration.Dependencies = append(migration.Dependencies, "001")
err = repo.Create(migration)
assert.Nil(t, err)
- path := filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name()))
- assertFileExists(t, path)
- contents, err := os.ReadFile(path)
- assert.Nil(t, err)
- assert.Equal(t, "-- depends: 001\n-- migrate:up\n\n-- migrate:down\n", string(contents))
-
+ assertFileExists(t, filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name())))
}
func TestCreate_FailsToCreateMigration(t *testing.T) {
@@ -168,12 +162,10 @@ func TestList_Success(t *testing.T) {
time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC),
"migration_1",
)
- migration1.Dependencies = []string{"001", "002"}
migration2 := models.NewTimestampMigration(
time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC),
"migration_2",
)
- migration2.Dependencies = []string{"003"}
err = repo.Create(migration1)
assert.Nil(t, err)
repo.Create(migration2)
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)
}
diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go
index a43d354..90ebc2f 100644
--- a/internal/sqlparse/sqlparse.go
+++ b/internal/sqlparse/sqlparse.go
@@ -8,13 +8,7 @@ import (
)
type SqlParser interface {
- Parse(reader io.Reader) (Migration, error)
-}
-
-type Migration struct {
- UpgradeScript MigrationScript
- DowngradeScript MigrationScript
- Dependencies []string
+ Parse(reader io.Reader) (MigrationScript, MigrationScript, error)
}
type MigrationScript struct {
@@ -35,7 +29,6 @@ const (
const (
upgradeScriptDeliminator = "-- migrate:up"
downgradeScriptDeliminator = "-- migrate:down"
- dependsDirective = "-- depends:"
transactionOptionName = "transaction"
)
@@ -47,11 +40,12 @@ func NewSqlParser() SqlParser {
return &sqlParser{}
}
-// Parse parses out the migration's upgrade and downgrade scripts,
-// any custom execution options with those scripts, and migration dependencies.
-// It returns the upgrade script, downgrade script, dependencies, and any error if one occurred.
-func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) {
- migration := Migration{Dependencies: []string{}}
+// Parse parses out the migration's upgrade and downgrade scripts
+// and any custom execution options with those scripts. It returns
+// the upgrade script, downgrade script, and any error if one occurred.
+func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript, error) {
+ upgradeScript := MigrationScript{}
+ downgradeScript := MigrationScript{}
scanner := bufio.NewScanner(reader)
currentSection := unknownSection
@@ -61,30 +55,25 @@ func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) {
if strings.HasPrefix(loweredLine, upgradeScriptDeliminator) {
currentSection = upgradeScriptSection
- migration.UpgradeScript.Options = parseExecutionOptions(line)
+ upgradeScript.Options = parseExecutionOptions(line)
} else if strings.HasPrefix(loweredLine, downgradeScriptDeliminator) {
currentSection = downgradeScriptSection
- migration.DowngradeScript.Options = parseExecutionOptions(line)
+ downgradeScript.Options = parseExecutionOptions(line)
} else if currentSection == upgradeScriptSection {
- migration.UpgradeScript.Contents += line + "\n"
+ upgradeScript.Contents += line + "\n"
} else if currentSection == downgradeScriptSection {
- migration.DowngradeScript.Contents += line + "\n"
- }
-
- if strings.HasPrefix(loweredLine, dependsDirective) {
- migration.Dependencies = append(migration.Dependencies, parseDependencies(line)...)
+ downgradeScript.Contents += line + "\n"
}
-
}
if err := scanner.Err(); err != nil {
- return Migration{}, fmt.Errorf(
+ return MigrationScript{}, MigrationScript{}, fmt.Errorf(
"parsing sql file encountered an error: %w",
err,
)
}
- return migration, nil
+ return upgradeScript, downgradeScript, nil
}
// parseExecutionOptions extracts the execution options for a
@@ -92,10 +81,10 @@ func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) {
func parseExecutionOptions(line string) ExecutionOptions {
options := ExecutionOptions{UseTransaction: true}
- parts := strings.SplitSeq(line, " ")
- for part := range parts {
+ parts := strings.Split(line, " ")
+ for _, part := range parts {
part = strings.ToLower(part)
- if strings.Contains(part, fmt.Sprintf("%s:", transactionOptionName)) {
+ if strings.Contains(part, "transaction:") {
option := strings.Split(part, ":")[1]
options.UseTransaction = option != "false"
}
@@ -103,31 +92,3 @@ func parseExecutionOptions(line string) ExecutionOptions {
return options
}
-
-// parseDependencies extracts migration dependencies from a depends directive.
-// Expected format: -- depends: 001, 002, 003
-func parseDependencies(line string) []string {
- dependencies := []string{}
-
- loweredLine := strings.ToLower(line)
- prefixIdx := strings.Index(loweredLine, dependsDirective)
- if prefixIdx == -1 {
- return dependencies
- }
-
- depsStr := line[prefixIdx+len(dependsDirective):]
- depsStr = strings.TrimSpace(depsStr)
- if depsStr == "" {
- return dependencies
- }
-
- deps := strings.SplitSeq(depsStr, ",")
- for dep := range deps {
- dep = strings.TrimSpace(dep)
- if dep != "" {
- dependencies = append(dependencies, dep)
- }
- }
-
- return dependencies
-}
diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go
index 20ba72f..5cbbc07 100644
--- a/internal/sqlparse/sqlparse_test.go
+++ b/internal/sqlparse/sqlparse_test.go
@@ -182,12 +182,12 @@ func TestSqlParser_Parse(t *testing.T) {
for _, tc := range testCases {
sqlParser := sqlparse.NewSqlParser()
- migration, err := sqlParser.Parse(
+ upgradeScript, downgradeScript, err := sqlParser.Parse(
strings.NewReader(tc.migration),
)
assert.Nil(t, err)
- check.DeepEqual(t, migration.UpgradeScript, tc.expectedUpgradeScript)
- check.DeepEqual(t, migration.DowngradeScript, tc.expectedDowngradeScript)
+ check.DeepEqual(t, upgradeScript, tc.expectedUpgradeScript)
+ check.DeepEqual(t, downgradeScript, tc.expectedDowngradeScript)
}
}
@@ -215,104 +215,7 @@ func TestSqlParserParseError(t *testing.T) {
ErrCond: " ",
}
sqlParser := sqlparse.NewSqlParser()
- _, err := sqlParser.Parse(reader)
+ _, _, err := sqlParser.Parse(reader)
assert.NotNil(t, err)
assert.ErrorContains(t, err, "unwanted input encountered")
}
-
-func TestSqlParser_ParseDependencies(t *testing.T) {
- testCases := []struct {
- name string
- migration string
- expectedDependencies []string
- }{
- {
- name: "no dependencies",
- migration: "-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{},
- },
- {
- name: "single dependency",
- migration: "-- depends: 001\n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{"001"},
- },
- {
- name: "multiple dependencies",
- migration: "-- depends: 001, 002, 003\n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{"001", "002", "003"},
- },
- {
- name: "multiple dependencies without spaces",
- migration: "-- depends: 001,002,003\n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{"001", "002", "003"},
- },
- {
- name: "case insensitive depends",
- migration: "-- DEPENDS: 001, 002\n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{"001", "002"},
- },
- {
- name: "mixed case depends",
- migration: "-- Depends: 001\n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{"001"},
- },
- {
- name: "empty depends directive",
- migration: "-- depends: \n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{},
- },
- {
- name: "depends with trailing comma",
- migration: "-- depends: 001, 002,\n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{"001", "002"},
- },
- {
- name: "depends with extra whitespace",
- migration: "-- depends: 001 , 002 , 003 \n-- migrate:up\nCREATE TABLE users(id int);",
- expectedDependencies: []string{"001", "002", "003"},
- },
- {
- name: "timestamp version dependencies",
- migration: "-- depends: 20240316145038, 20240317120000\n-- migrate:up\nCREATE TABLE posts(id int);",
- expectedDependencies: []string{"20240316145038", "20240317120000"},
- },
- {
- name: "multiple depends directives",
- migration: `-- depends: 001
--- depends: 002
--- migrate:up
-CREATE TABLE users(id int);`,
- expectedDependencies: []string{"001", "002"},
- },
- {
- name: "depends before up and down",
- migration: `-- depends: 001, 002
--- migrate:up
-CREATE TABLE users(id int);
--- migrate:down
-DROP TABLE users;`,
- expectedDependencies: []string{"001", "002"},
- },
- {
- name: "depends anywhere in file",
- migration: `-- depends: 004
--- migrate:up
--- depends: 003
-CREATE TABLE users(id int);
--- migrate:down
--- depends: 002
-DROP TABLE users;
--- depends: 001`,
- expectedDependencies: []string{"004", "003", "002", "001"},
- },
- }
-
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- sqlParser := sqlparse.NewSqlParser()
- migration, err := sqlParser.Parse(strings.NewReader(tc.migration))
- assert.Nil(t, err)
- check.DeepEqual(t, migration.Dependencies, tc.expectedDependencies)
- })
- }
-}
diff --git a/man/bolt-dependencies.7 b/man/bolt-dependencies.7
deleted file mode 100644
index 4188fcb..0000000
--- a/man/bolt-dependencies.7
+++ /dev/null
@@ -1,121 +0,0 @@
-.Dd February 12, 2026
-.Dt BOLT-DEPENDENCIES 7
-.Os
-.Sh NAME
-.Nm bolt-dependencies
-.Nd migration dependency system
-.Sh DESCRIPTION
-Migration dependencies solve a fundamental problem in collaborative database
-development: ensuring migrations run in the correct order regardless of when
-they were created or merged.
-.Sh THE PROBLEM
-.Ss Sequential Versions
-Developer A creates migration
-.Li 002
-for feature X on branch A. Developer B creates migration
-.Li 002
-for feature Y on branch B. When branches merge, there is a filename conflict.
-.Ss Timestamp Versions
-Developer A creates migration
-.Li 20251013214743
-on branch A. Developer B creates migration
-.Li 20251013214799
-on branch B. Developer B merges first. Developer A merges second.
-.Pp
-In this environment, both migrations apply successfully. However, when a
-fresh database is set up (staging, production, new developer), migrations
-run in timestamp order: A's migration runs first, then B's. This is the
-opposite order from the original development environment. If B's migration
-depends on A's changes, the fresh setup fails.
-.Sh THE SOLUTION
-Instead of relying on version numbers, migrations explicitly declare which
-other migrations they depend on using the
-.Cm -- depends:
-directive:
-.Bd -literal -offset indent
--- depends: 20240316145038
--- migrate:up
-CREATE TABLE posts (
- user_id INTEGER REFERENCES users(id)
-);
-.Ed
-.Pp
-This creates a dependency graph where Bolt determines execution order based
-on what migrations need to run before others, not when they were created.
-.Sh AUTOMATIC POPULATION
-When you create a new migration with
-.Xr bolt-new 1 ,
-Bolt automatically populates the
-.Cm -- depends:
-directive with current
-.Dq leaf
-migrations (migrations that no other migration depends on).
-.Pp
-This means:
-.Bl -bullet -offset Ds -compact
-.It
-The first migration has no dependencies.
-.It
-The second migration depends on the first.
-.It
-Migrations created in parallel branches will both be listed as dependencies
-in subsequent migrations after merging.
-.El
-.Sh EXECUTION ORDER
-Bolt uses topological sorting to determine execution order:
-.Bl -enum -offset Ds -compact
-.It
-Migrations with no dependencies run first.
-.It
-Once a migration completes, migrations depending only on completed
-migrations become eligible.
-.It
-When multiple migrations are eligible, version number is used as a
-tiebreaker for deterministic ordering.
-.El
-.Sh VALIDATION
-Bolt validates the dependency graph before execution:
-.Bl -tag -width Ds
-.It Before applying
-Verifies all dependencies exist and have been applied. This step detects
-any circular dependencies.
-.It Before reverting
-Ensures no other applied migrations depend on the migration being reverted.
-.El
-.Sh EXAMPLES
-No dependencies (root migration):
-.Bd -literal -offset indent
--- migrate:up
-CREATE TABLE users (id SERIAL PRIMARY KEY);
-
--- migrate:down
-DROP TABLE users;
-.Ed
-.Pp
-Single dependency:
-.Bd -literal -offset indent
--- depends: 20240316145038
--- migrate:up
-CREATE TABLE posts (user_id INTEGER REFERENCES users(id));
-
--- migrate:down
-DROP TABLE posts;
-.Ed
-.Pp
-Multiple dependencies:
-.Bd -literal -offset indent
--- depends: 20240316145038, 20240317120000
--- migrate:up
-CREATE TABLE comments (
- post_id INTEGER REFERENCES posts(id),
- user_id INTEGER REFERENCES users(id)
-);
-
--- migrate:down
-DROP TABLE comments;
-.Ed
-.Sh SEE ALSO
-.Xr bolt 1 ,
-.Xr bolt-new 1 ,
-.Xr bolt-migration 5 ,
-.Xr bolt-execution 7
diff --git a/man/bolt-down.1 b/man/bolt-down.1
index 5a331d4..18be02c 100644
--- a/man/bolt-down.1
+++ b/man/bolt-down.1
@@ -11,7 +11,7 @@
.Sh DESCRIPTION
.Nm
reverts applied migrations from the database. Migrations are reverted in
-reverse dependency order.
+reverse version order, newest first.
.Pp
By default, all applied migrations are reverted. Use the
.Fl v
diff --git a/man/bolt-execution.7 b/man/bolt-execution.7
index 75494ac..5e5d64c 100644
--- a/man/bolt-execution.7
+++ b/man/bolt-execution.7
@@ -61,13 +61,13 @@ Bolt scans the migrations directory for
files.
.It
.Sy Parsing :
-Each file is parsed to extract the version, message, and dependencies.
+Each file name is parsed to extract the version and message.
.It
.Sy Comparison :
Bolt queries the tracking table to find unapplied migrations.
.It
.Sy Ordering :
-Pending migrations are ordered by their dependencies.
+Pending migrations are ordered by version, oldest first.
.It
.Sy Execution :
For each pending migration:
@@ -77,8 +77,6 @@ The
.Cm -- migrate:up
section is extracted.
.It
-All dependencies are verified to be applied.
-.It
A transaction is started (unless disabled).
.It
The SQL is executed.
@@ -106,7 +104,7 @@ Bolt queries the tracking table for applied migrations.
Applied versions are matched against local files.
.It
.Sy Ordering :
-Applied migrations are ordered in reverse dependency order.
+Applied migrations are ordered by version, newest first.
.It
.Sy Execution :
For the target migration(s):
@@ -116,8 +114,6 @@ The
.Cm -- migrate:down
section is extracted.
.It
-Bolt verifies no other applied migrations depend on this one.
-.It
A transaction is started (unless disabled).
.It
The SQL is executed.
@@ -140,5 +136,4 @@ Migration files should be committed to version control and not deleted.
.Xr bolt 1 ,
.Xr bolt-down 1 ,
.Xr bolt-up 1 ,
-.Xr bolt-migration 5 ,
-.Xr bolt-dependencies 7
+.Xr bolt-migration 5
diff --git a/man/bolt-migration.5 b/man/bolt-migration.5
index dd5080e..92728e8 100644
--- a/man/bolt-migration.5
+++ b/man/bolt-migration.5
@@ -26,9 +26,6 @@ for configuration details.
Migration files use comment directives to separate sections. All directives are
case-insensitive.
.Bl -tag -width Ds
-.It Cm -- depends: Ar version1, version2, ...
-Declares dependencies on other migrations. Dependencies determine execution
-order. Multiple versions are separated by commas.
.It Cm -- migrate:up Op Ar options
Marks the beginning of the upgrade section. All SQL statements following
this directive until the next directive or end of file are executed when
@@ -53,7 +50,7 @@ inside a transaction, such as
in PostgreSQL.
.El
.Sh EXAMPLES
-A basic migration with no dependencies:
+A basic migration:
.Bd -literal -offset indent
-- migrate:up
CREATE TABLE users (
@@ -65,36 +62,8 @@ CREATE TABLE users (
DROP TABLE users;
.Ed
.Pp
-A migration with a single dependency:
-.Bd -literal -offset indent
--- depends: 20240316145038
--- migrate:up
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL REFERENCES users(id)
-);
-
--- migrate:down
-DROP TABLE posts;
-.Ed
-.Pp
-A migration with multiple dependencies:
-.Bd -literal -offset indent
--- depends: 20240316145038, 20240317120000
--- migrate:up
-CREATE TABLE comments (
- id SERIAL PRIMARY KEY,
- post_id INTEGER NOT NULL REFERENCES posts(id),
- user_id INTEGER NOT NULL REFERENCES users(id)
-);
-
--- migrate:down
-DROP TABLE comments;
-.Ed
-.Pp
A non-transactional migration:
.Bd -literal -offset indent
--- depends: 20240316145038
-- migrate:up transaction:false
CREATE INDEX CONCURRENTLY idx_users_email ON users(email);
@@ -105,5 +74,4 @@ DROP INDEX CONCURRENTLY idx_users_email;
.Xr bolt 1 ,
.Xr bolt-new 1 ,
.Xr bolt.toml 5 ,
-.Xr bolt-dependencies 7 ,
.Xr bolt-execution 7
diff --git a/man/bolt-new.1 b/man/bolt-new.1
index c9662a5..4e83be0 100644
--- a/man/bolt-new.1
+++ b/man/bolt-new.1
@@ -15,15 +15,6 @@ creates a new database migration file in the migrations directory.
The migration file is named using the configured version style (timestamp
or sequential) followed by the message. For example:
.Pa 20240316145038_create_users_table.sql .
-.Pp
-When other migrations exist,
-.Nm
-automatically populates the
-.Ql -- depends:
-directive with the current leaf migrations (migrations that no other
-migration depends on). See
-.Xr bolt-dependencies 7
-for details on how dependencies work.
.Sh OPTIONS
.Bl -tag -width Ds
.It Fl m Ar message , Fl -message Ar message
@@ -41,5 +32,4 @@ Create a migration with the default message:
.Dl $ bolt new
.Sh SEE ALSO
.Xr bolt 1 ,
-.Xr bolt-migration 5 ,
-.Xr bolt-dependencies 7
+.Xr bolt-migration 5
diff --git a/man/bolt-status.1 b/man/bolt-status.1
index 10a60bc..e07c404 100644
--- a/man/bolt-status.1
+++ b/man/bolt-status.1
@@ -11,8 +11,7 @@
displays all discovered migrations and indicates which ones have been applied
to the database.
.Pp
-Migrations are shown in the order they would be executed, based on their
-dependency relationships.
+Migrations are shown in the order they would be executed, by version.
The output includes:
.Bl -bullet -offset Ds -compact
.It
diff --git a/man/bolt-tutorial.7 b/man/bolt-tutorial.7
index 83933aa..0a137a0 100644
--- a/man/bolt-tutorial.7
+++ b/man/bolt-tutorial.7
@@ -75,27 +75,7 @@ Revert the most recent migration:
.Pp
Revert to a specific version:
.Dl $ bolt down -v 20240316145038
-.Sh WORKING WITH DEPENDENCIES
-When you create a second migration, Bolt automatically adds a dependency
-on existing migrations:
-.Bd -literal -offset indent
--- depends: 20240316145038
--- migrate:up
-CREATE TABLE posts (
- id SERIAL PRIMARY KEY,
- user_id INTEGER NOT NULL REFERENCES users(id),
- title VARCHAR(255) NOT NULL
-);
-
--- migrate:down
-DROP TABLE posts;
-.Ed
-.Pp
-Dependencies ensure migrations run in the correct order. See
-.Xr bolt-dependencies 7
-for details.
.Sh SEE ALSO
.Xr bolt 1 ,
.Xr bolt.toml 5 ,
-.Xr bolt-migration 5 ,
-.Xr bolt-dependencies 7
+.Xr bolt-migration 5
diff --git a/man/bolt-up.1 b/man/bolt-up.1
index c8729b4..19b41e9 100644
--- a/man/bolt-up.1
+++ b/man/bolt-up.1
@@ -11,9 +11,7 @@
.Sh DESCRIPTION
.Nm
applies pending migrations to the database. Migrations are executed in
-dependency order as determined by the
-.Ql -- depends:
-directives in each migration file.
+version order, oldest first.
.Pp
By default, all pending migrations are applied. Use the
.Fl v
diff --git a/man/bolt.1 b/man/bolt.1
index 4ab6d64..74d65d9 100644
--- a/man/bolt.1
+++ b/man/bolt.1
@@ -55,7 +55,6 @@ exits 0 on success, and >0 if an error occurs.
.Xr bolt-migration 5 ,
.Xr bolt.toml 5 ,
.Xr bolt-databases 7 ,
-.Xr bolt-dependencies 7 ,
.Xr bolt-execution 7 ,
.Xr bolt-tutorial 7
.Sh AUTHOR