Skip to content

Commit

Permalink
fix: should rollback if migrate using transaction and got an err (tha…
Browse files Browse the repository at this point in the history
…nks @Bayshark)
  • Loading branch information
vmihailenco committed May 6, 2023
1 parent 1e624cc commit e7a119b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions migrate/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ func Exec(ctx context.Context, db *bun.DB, f io.Reader, isTx bool) error {
}

var retErr error
var execErr error

defer func() {
if tx, ok := idb.(bun.Tx); ok {
retErr = tx.Commit()
if execErr != nil {
retErr = tx.Rollback()
} else {
retErr = tx.Commit()
}
return
}

Expand All @@ -113,8 +118,9 @@ func Exec(ctx context.Context, db *bun.DB, f io.Reader, isTx bool) error {
}()

for _, q := range queries {
if _, err := idb.ExecContext(ctx, q); err != nil {
return err
_, execErr = idb.ExecContext(ctx, q)
if execErr != nil {
return execErr
}
}

Expand Down

0 comments on commit e7a119b

Please sign in to comment.