aboutsummaryrefslogtreecommitdiffstats
path: root/internal/services/migration.go
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2025-10-18 10:43:39 -0400
committerEugene Triguba <hello@eugenetriguba.com>2025-11-26 11:46:57 -0500
commite5658d1f61415054029b66cd2f703f1f40138460 (patch)
treec609e209f4cebc62dd985d1b773558f47eb73bf6 /internal/services/migration.go
parentAdd is-branch main check for docs deployment (diff)
downloadcli-e5658d1f61415054029b66cd2f703f1f40138460.zip
Add support for migration dependencies
Diffstat (limited to 'internal/services/migration.go')
-rw-r--r--internal/services/migration.go252
1 files changed, 82 insertions, 170 deletions
diff --git a/internal/services/migration.go b/internal/services/migration.go
index ddfb8c2..ba43ff7 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -2,7 +2,6 @@ package services
import (
"fmt"
- "sort"
"strconv"
"time"
@@ -12,11 +11,17 @@ import (
"git.sr.ht/~eugenetriguba/bolt/internal/repositories"
)
+// MigrationService provides the primary interface for migration operations.
+// It coordinates with the lower-level components to apply, revert, create,
+// and list out migrations while also managing presentation concerns like
+// output and timing.
type MigrationService struct {
- dbRepo repositories.MigrationDBRepo
- fsRepo repositories.MigrationFsRepo
- cfg configloader.Config
- outputter output.Outputter
+ executor *MigrationExecutor
+ queryService *MigrationQueryService
+ depGraph *MigrationDependencyGraph
+ fsRepo repositories.MigrationFsRepo
+ cfg configloader.Config
+ outputter output.Outputter
}
func NewMigrationService(
@@ -25,29 +30,32 @@ func NewMigrationService(
cfg configloader.Config,
outputter output.Outputter,
) MigrationService {
+ depGraph := NewMigrationDependencyGraph(cfg)
+ executor := NewMigrationExecutor(dbRepo, fsRepo)
+ queryService := NewMigrationQueryService(dbRepo, fsRepo, depGraph)
return MigrationService{
- dbRepo: dbRepo,
- fsRepo: fsRepo,
- cfg: cfg,
- outputter: outputter,
+ executor: executor,
+ queryService: queryService,
+ depGraph: depGraph,
+ fsRepo: fsRepo,
+ cfg: cfg,
+ outputter: outputter,
}
}
-type sortOrder int
-
-const (
- SortOrderDesc sortOrder = iota
- SortOrderAsc
-)
-
func (ms MigrationService) ApplyAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if !migration.Applied {
+ if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.ApplyMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -62,6 +70,14 @@ func (ms MigrationService) ApplyAllMigrations() error {
return nil
}
+func getMigrationsByVersion(migrations []*models.Migration) map[string]*models.Migration {
+ result := make(map[string]*models.Migration)
+ for _, migration := range migrations {
+ result[migration.Version] = migration
+ }
+ return result
+}
+
func (ms MigrationService) ApplyUpToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
@@ -79,16 +95,21 @@ func (ms MigrationService) ApplyUpToVersion(version string) error {
return fmt.Errorf("migration with version %s does not exist", version)
}
if targetMigration.Applied {
- // Assumption: If the target migration is applied, all migrations before
- // it must also be applied.
+ // TODO: Check not only that the target migration is applied, but also
+ // all prior migrations.
return fmt.Errorf(
"migration with version %s is already applied, nothing to apply",
version,
)
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if !migration.Applied {
+ if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.ApplyMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -111,19 +132,9 @@ func (ms MigrationService) ApplyMigration(migration *models.Migration) error {
ms.outputter.Output(fmt.Sprintf("Applying migration %s..", migration.Name()))
startTime := time.Now()
- upgradeScript, err := ms.fsRepo.ReadUpgradeScript(migration)
- if err != nil {
- return fmt.Errorf("unable to read upgrade script: %w", err)
- }
-
- if upgradeScript.Options.UseTransaction {
- err = ms.dbRepo.ApplyWithTx(upgradeScript.Contents, migration)
- } else {
- err = ms.dbRepo.Apply(upgradeScript.Contents, migration)
- }
-
+ err := ms.executor.Apply(migration)
if err != nil {
- return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err)
+ return err
}
ms.outputter.Output(
@@ -143,8 +154,13 @@ func (ms MigrationService) RevertAllMigrations() error {
return err
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if migration.Applied {
+ if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.RevertMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -176,16 +192,21 @@ func (ms MigrationService) RevertDownToVersion(version string) error {
return fmt.Errorf("migration with version %s does not exist", version)
}
if !targetMigration.Applied {
- // Assumption: Every migration from the latest down to the target
- // migration hasn't been applied.
+ // TODO: Check not only that the target migration is reverted, but also
+ // all prior migrations.
return fmt.Errorf(
"migration with version %s isn't applied, nothing to revert",
version,
)
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if migration.Applied {
+ if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.RevertMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -208,17 +229,7 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error {
ms.outputter.Output(fmt.Sprintf("Reverting migration %s..", migration.Name()))
startTime := time.Now()
- downgradeScript, err := ms.fsRepo.ReadDowngradeScript(migration)
- if err != nil {
- return fmt.Errorf("unable to read downgrade script: %w", err)
- }
-
- if downgradeScript.Options.UseTransaction {
- err = ms.dbRepo.RevertWithTx(downgradeScript.Contents, migration)
- } else {
- err = ms.dbRepo.Revert(downgradeScript.Contents, migration)
- }
-
+ err := ms.executor.Revert(migration)
if err != nil {
return err
}
@@ -247,160 +258,61 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e
migration = models.NewSequentialMigration(currentVersion+1, message)
}
- err := ms.fsRepo.Create(migration)
+ leafNodes, err := ms.getMigrationLeafNodes()
if err != nil {
return nil, err
}
- ms.outputter.Output(
- fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
- )
- return migration, nil
-}
-
-func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
- var currentVersion uint64 = 0
+ migration.Dependencies = leafNodes
- localMigrations, err := ms.fsRepo.List()
+ err = ms.fsRepo.Create(migration)
if err != nil {
- return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
+ return nil, err
}
-
- migrations, err := ms.combineMigrations(
- localMigrations,
- map[string]*models.Migration{},
- SortOrderDesc,
+ ms.outputter.Output(
+ fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
)
- if err != nil {
- return 0, err
- }
-
- if len(migrations) > 0 {
- latestMigration := migrations[0]
- currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64)
- if err != nil {
- return 0, err
- }
- }
-
- return currentVersion, nil
+ return migration, nil
}
-func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
- // Assumption: All migration versions that have been applied
- // to the database exist locally in the list of filesystem migrations.
- // This would hold true unless someone decided to apply a migration and,
- // for whatever reason, delete it later from their filesystem. If there is
- // an applied migration that doesn't exist locally, it would not be shown
- // here.
+func (ms MigrationService) getMigrationLeafNodes() ([]string, error) {
localMigrations, err := ms.fsRepo.List()
if err != nil {
- return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
+ return nil, fmt.Errorf("unable to list migrations for finding leaf nodes: %w", err)
}
- appliedMigrations, err := ms.dbRepo.List()
+ leafNodes, err := ms.depGraph.FindLeafNodes(localMigrations)
if err != nil {
- return nil, fmt.Errorf(
- "unable to list out applied migrations from remote db: %w",
- err,
- )
+ return nil, fmt.Errorf("unable to find leaf nodes for dependencies: %w", err)
}
+ return leafNodes, nil
- return ms.combineMigrations(localMigrations, appliedMigrations, order)
}
-func (ms MigrationService) combineMigrations(
- localMigrations map[string]*models.Migration,
- appliedMigrations map[string]*models.Migration,
- order sortOrder,
-) ([]*models.Migration, error) {
- migrations := make([]*models.Migration, 0)
- for _, localMigration := range localMigrations {
- _, ok := appliedMigrations[localMigration.Version]
- if ok {
- localMigration.Applied = true
- }
- migrations = append(migrations, localMigration)
- }
+func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
+ var maxVersion uint64 = 0
- err := ms.sortMigrations(migrations, order)
+ localMigrations, err := ms.fsRepo.List()
if err != nil {
- return nil, err
+ return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
}
- return migrations, err
-}
-
-func (ms MigrationService) sortMigrations(
- migrations []*models.Migration,
- order sortOrder,
-) error {
- sortErrs := make([]error, 0)
- sort.Slice(migrations, func(i, j int) bool {
- var comparison bool
- var err error
-
- if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential {
- comparison, err = ms.compareSequentialMigrations(
- migrations[i],
- migrations[j],
- order,
+ for _, migration := range localMigrations {
+ version, err := strconv.ParseUint(migration.Version, 10, 64)
+ if err != nil {
+ return 0, fmt.Errorf(
+ "migration %s has invalid version for sequential style: %w",
+ migration.Name(),
+ err,
)
- } else {
- comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order)
}
-
- if err != nil {
- sortErrs = append(sortErrs, err)
+ if version > maxVersion {
+ maxVersion = version
}
-
- return comparison
- })
- if len(sortErrs) != 0 {
- return fmt.Errorf("unable to sort migrations: %v", sortErrs)
- }
- return nil
-}
-
-func (ms MigrationService) compareSequentialMigrations(
- m1 *models.Migration,
- m2 *models.Migration,
- order sortOrder,
-) (bool, error) {
- m1Version, err := strconv.ParseInt(m1.Version, 10, 64)
- if err != nil {
- return false, err
- }
-
- m2Version, err := strconv.ParseInt(m2.Version, 10, 64)
- if err != nil {
- return false, err
}
- if order == SortOrderAsc {
- return m1Version < m2Version, nil
- } else {
- return m1Version > m2Version, nil
- }
+ return maxVersion, nil
}
-func (ms MigrationService) compareTimestampMigrations(
- m1 *models.Migration,
- m2 *models.Migration,
- order sortOrder,
-) (bool, error) {
- m1Version, err := time.Parse("20060102150405", m1.Version)
- if err != nil {
- return false, err
- }
-
- m2Version, err := time.Parse("20060102150405", m2.Version)
- if err != nil {
- return false, err
- }
-
- if order == SortOrderAsc {
- return m1Version.Before(m2Version), nil
- } else {
- return m1Version.After(m2Version), nil
- }
+func (ms MigrationService) ListMigrations(order SortOrder) ([]*models.Migration, error) {
+ return ms.queryService.ListMigrations(order)
}