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