aboutsummaryrefslogtreecommitdiffstats
path: root/internal/services/migration.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/services/migration.go')
-rw-r--r--internal/services/migration.go130
1 files changed, 36 insertions, 94 deletions
diff --git a/internal/services/migration.go b/internal/services/migration.go
index ddfb8c2..a861176 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -6,7 +6,6 @@ import (
"strconv"
"time"
- "git.sr.ht/~eugenetriguba/bolt/internal/configloader"
"git.sr.ht/~eugenetriguba/bolt/internal/models"
"git.sr.ht/~eugenetriguba/bolt/internal/output"
"git.sr.ht/~eugenetriguba/bolt/internal/repositories"
@@ -15,20 +14,17 @@ import (
type MigrationService struct {
dbRepo repositories.MigrationDBRepo
fsRepo repositories.MigrationFsRepo
- cfg configloader.Config
outputter output.Outputter
}
func NewMigrationService(
dbRepo repositories.MigrationDBRepo,
fsRepo repositories.MigrationFsRepo,
- cfg configloader.Config,
outputter output.Outputter,
) MigrationService {
return MigrationService{
dbRepo: dbRepo,
fsRepo: fsRepo,
- cfg: cfg,
outputter: outputter,
}
}
@@ -235,19 +231,13 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error {
}
func (ms MigrationService) CreateMigration(message string) (*models.Migration, error) {
- var migration *models.Migration
-
- if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleTimestamp {
- migration = models.NewTimestampMigration(time.Now(), message)
- } else {
- currentVersion, err := ms.getCurrentSequentialMigrationVersion()
- if err != nil {
- return nil, err
- }
- migration = models.NewSequentialMigration(currentVersion+1, message)
+ currentVersion, err := ms.getCurrentMigrationVersion()
+ if err != nil {
+ return nil, err
}
+ migration := models.NewMigration(currentVersion+1, message)
- err := ms.fsRepo.Create(migration)
+ err = ms.fsRepo.Create(migration)
if err != nil {
return nil, err
}
@@ -257,32 +247,37 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e
return migration, nil
}
-func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
- var currentVersion uint64 = 0
+func (ms MigrationService) getCurrentMigrationVersion() (uint64, error) {
+ var maxVersion uint64 = 0
localMigrations, err := ms.fsRepo.List()
if err != nil {
return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
}
- migrations, err := ms.combineMigrations(
- localMigrations,
- map[string]*models.Migration{},
- SortOrderDesc,
- )
- if err != nil {
- return 0, err
- }
-
- if len(migrations) > 0 {
- latestMigration := migrations[0]
- currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64)
+ for _, migration := range localMigrations {
+ version, err := parseVersion(migration)
if err != nil {
return 0, err
}
+ if version > maxVersion {
+ maxVersion = version
+ }
}
- return currentVersion, nil
+ return maxVersion, nil
+}
+
+func parseVersion(migration *models.Migration) (uint64, error) {
+ version, err := strconv.ParseUint(migration.Version, 10, 64)
+ if err != nil {
+ return 0, fmt.Errorf(
+ "migration %s has an invalid version: expected a number: %w",
+ migration.Name(),
+ err,
+ )
+ }
+ return version, nil
}
func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
@@ -334,73 +329,20 @@ 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)
- }
-
+ versions := make(map[*models.Migration]uint64, len(migrations))
+ for _, migration := range migrations {
+ version, err := parseVersion(migration)
if err != nil {
- sortErrs = append(sortErrs, err)
+ return fmt.Errorf("unable to sort migrations: %w", err)
}
+ versions[migration] = version
+ }
- return comparison
+ sort.Slice(migrations, func(i, j int) bool {
+ if order == SortOrderAsc {
+ return versions[migrations[i]] < versions[migrations[j]]
+ }
+ return versions[migrations[i]] > versions[migrations[j]]
})
- 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
- }
-}
-
-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
- }
-}