Skip to content

Commit

Permalink
Migraines return errors for proper rollbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
dnnyjns committed May 19, 2019
1 parent edff753 commit d446a55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 12 additions & 5 deletions runner/migraine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"github.com/jinzhu/gorm"
)

type performFn func(*gorm.DB) error

type Migraine struct {
DisableDDL bool `gorm:"-"`
Perform func(*gorm.DB) `gorm:"-"`
Version string `gorm:"size:255;PRIMARY_KEY;NOT NULL"`
DisableDDL bool `gorm:"-"`
Perform performFn `gorm:"-"`
Version string `gorm:"size:255;PRIMARY_KEY;NOT NULL"`
}

func (m Migraine) Run(db *gorm.DB) {
Expand All @@ -16,11 +18,16 @@ func (m Migraine) Run(db *gorm.DB) {
tx = db
} else {
tx = db.Begin()
defer tx.Commit()
}

if err := m.Perform(tx); err != nil {
tx.Rollback()
panic(err)
}
defer tx.Commit()
m.Perform(tx)

if err := tx.Create(&m).Error; err != nil {
tx.Rollback()
panic(err)
}
}
2 changes: 0 additions & 2 deletions runner/runner.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package runner

import (
"fmt"
"sort"

"github.com/jinzhu/gorm"
Expand Down Expand Up @@ -29,7 +28,6 @@ func (r *Runner) Run() {
for _, migraine := range r.Migs {
length := len(persisted)
i := sort.Search(length, func(i int) bool { return persisted[i].Version == migraine.Version })
fmt.Println(i)
if i == length {
migraine.Run(r.Db)
}
Expand Down

0 comments on commit d446a55

Please sign in to comment.