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.go244
1 files changed, 207 insertions, 37 deletions
diff --git a/internal/services/migration.go b/internal/services/migration.go
index 8b5898e..4be10cc 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -3,24 +3,36 @@ package services
import (
"fmt"
"sort"
+ "strconv"
+ "strings"
+ "time"
+ "github.com/eugenetriguba/bolt/internal/configloader"
"github.com/eugenetriguba/bolt/internal/models"
"github.com/eugenetriguba/bolt/internal/output"
"github.com/eugenetriguba/bolt/internal/repositories"
+ "github.com/eugenetriguba/bolt/internal/sqlparse"
)
type MigrationService struct {
- dbRepo *repositories.MigrationDBRepo
- fsRepo *repositories.MigrationFsRepo
+ dbRepo repositories.MigrationDBRepo
+ fsRepo repositories.MigrationFsRepo
+ cfg configloader.Config
outputter output.Outputter
}
func NewMigrationService(
- dbRepo *repositories.MigrationDBRepo,
- fsRepo *repositories.MigrationFsRepo,
+ dbRepo repositories.MigrationDBRepo,
+ fsRepo repositories.MigrationFsRepo,
+ cfg configloader.Config,
outputter output.Outputter,
-) *MigrationService {
- return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo, outputter: outputter}
+) MigrationService {
+ return MigrationService{
+ dbRepo: dbRepo,
+ fsRepo: fsRepo,
+ cfg: cfg,
+ outputter: outputter,
+ }
}
type sortOrder int
@@ -30,7 +42,7 @@ const (
SortOrderAsc
)
-func (ms *MigrationService) ApplyAllMigrations() error {
+func (ms MigrationService) ApplyAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
@@ -38,14 +50,21 @@ func (ms *MigrationService) ApplyAllMigrations() error {
for _, migration := range migrations {
if !migration.Applied {
- ms.ApplyMigration(migration)
+ err := ms.ApplyMigration(migration)
+ if err != nil {
+ return fmt.Errorf(
+ "unable to apply migration %s: %w",
+ migration.Name(),
+ err,
+ )
+ }
}
}
return nil
}
-func (ms *MigrationService) ApplyUpToVersion(version string) error {
+func (ms MigrationService) ApplyUpToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
@@ -62,6 +81,8 @@ 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.
return fmt.Errorf(
"migration with version %s is already applied, nothing to apply",
version,
@@ -72,7 +93,11 @@ func (ms *MigrationService) ApplyUpToVersion(version string) error {
if !migration.Applied {
err := ms.ApplyMigration(migration)
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to apply migration %s: %w",
+ migration.Name(),
+ err,
+ )
}
}
@@ -84,7 +109,7 @@ func (ms *MigrationService) ApplyUpToVersion(version string) error {
return nil
}
-func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
+func (ms MigrationService) ApplyMigration(migration *models.Migration) error {
ms.outputter.Output(
fmt.Sprintf(
"Applying migration %s_%s..",
@@ -98,10 +123,22 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
return err
}
- err = ms.dbRepo.Apply(scriptContents, migration)
+ sqlParser := sqlparse.NewSqlParser()
+ execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents))
if err != nil {
- return err
+ return fmt.Errorf("unable to parse sql file for upgrade script: %w", err)
+ }
+
+ if execOptions.UseTransaction {
+ err = ms.dbRepo.ApplyWithTx(scriptContents, migration)
+ } else {
+ err = ms.dbRepo.Apply(scriptContents, migration)
+ }
+
+ if err != nil {
+ return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err)
}
+
ms.outputter.Output(
fmt.Sprintf(
"Successfully applied migration %s_%s!",
@@ -113,7 +150,7 @@ func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
return nil
}
-func (ms *MigrationService) RevertAllMigrations() error {
+func (ms MigrationService) RevertAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderDesc)
if err != nil {
return err
@@ -121,14 +158,21 @@ func (ms *MigrationService) RevertAllMigrations() error {
for _, migration := range migrations {
if migration.Applied {
- ms.RevertMigration(migration)
+ err := ms.RevertMigration(migration)
+ if err != nil {
+ return fmt.Errorf(
+ "unable to revert migration %s: %w",
+ migration.Name(),
+ err,
+ )
+ }
}
}
return nil
}
-func (ms *MigrationService) RevertDownToVersion(version string) error {
+func (ms MigrationService) RevertDownToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderDesc)
if err != nil {
return err
@@ -145,6 +189,8 @@ 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.
return fmt.Errorf(
"migration with version %s isn't applied, nothing to revert",
version,
@@ -155,7 +201,11 @@ func (ms *MigrationService) RevertDownToVersion(version string) error {
if migration.Applied {
err := ms.RevertMigration(migration)
if err != nil {
- return err
+ return fmt.Errorf(
+ "unable to revert migration %s: %w",
+ migration.Name(),
+ err,
+ )
}
}
@@ -167,7 +217,7 @@ func (ms *MigrationService) RevertDownToVersion(version string) error {
return nil
}
-func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
+func (ms MigrationService) RevertMigration(migration *models.Migration) error {
ms.outputter.Output(
fmt.Sprintf(
"Reverting migration %s_%s..",
@@ -180,10 +230,22 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
return err
}
- err = ms.dbRepo.Revert(scriptContents, migration)
+ sqlParser := sqlparse.NewSqlParser()
+ execOptions, err := sqlParser.Parse(strings.NewReader(scriptContents))
+ if err != nil {
+ return fmt.Errorf("unable to parse sql file for downgrade script: %w", err)
+ }
+
+ if execOptions.UseTransaction {
+ err = ms.dbRepo.RevertWithTx(scriptContents, migration)
+ } else {
+ err = ms.dbRepo.Revert(scriptContents, migration)
+ }
+
if err != nil {
return err
}
+
ms.outputter.Output(
fmt.Sprintf(
"Successfully reverted migration %s_%s!",
@@ -195,48 +257,156 @@ func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
return nil
}
-func (ms *MigrationService) CreateMigration(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)
+ }
+
err := ms.fsRepo.Create(migration)
if err != nil {
- return err
+ return nil, err
}
ms.outputter.Output(
fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
)
- return nil
+ return migration, nil
}
-func (ms *MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
+func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
+ var currentVersion uint64 = 0
+
+ migrations, err := ms.ListMigrations(SortOrderDesc)
+ 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
+}
+
+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.
- migrations, err := ms.fsRepo.List()
+ // 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 nil, err
+ return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
}
appliedMigrations, err := ms.dbRepo.List()
if err != nil {
- return nil, err
+ return nil, fmt.Errorf(
+ "unable to list out applied migrations from remote db: %w",
+ err,
+ )
}
- var flatMigrations []*models.Migration
- for _, migration := range migrations {
- _, ok := appliedMigrations[migration.Version]
+ migrations := make([]*models.Migration, 0)
+ for _, localMigration := range localMigrations {
+ _, ok := appliedMigrations[localMigration.Version]
if ok {
- migration.Applied = true
+ localMigration.Applied = true
}
- flatMigrations = append(flatMigrations, migration)
+ migrations = append(migrations, localMigration)
}
- sort.Slice(flatMigrations, func(i, j int) bool {
- if order == SortOrderAsc {
- return flatMigrations[i].Version < flatMigrations[j].Version
+ 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 {
- return flatMigrations[i].Version > flatMigrations[j].Version
+ comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order)
+ }
+
+ if err != nil {
+ sortErrs = append(sortErrs, err)
}
+
+ return comparison
})
+ if len(sortErrs) != 0 {
+ return fmt.Errorf("unable to sort migrations: %v", sortErrs)
+ }
+ return nil
+}
- return flatMigrations, 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
+ }
}