Skip to content

Commit

Permalink
rollback tx after exec failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagleft committed Oct 20, 2024
1 parent 580ed43 commit 491f0b4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,23 @@ func (m *MigrationHandler) runTx(scriptName string, sqlQuery string) error {
// exec query
_, err = tx.Exec(sqlQuery)
if err != nil {
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf(
"failed to finish tx & rollback script %q: %w. after exec: %w",
scriptName, rErr, err,
)
}
return fmt.Errorf("failed to exec script %q: %w", scriptName, err)
}

// commit tx
err = tx.Commit()
if err != nil {
if err := tx.Rollback(); err != nil {
return fmt.Errorf("failed to finish tx & rollback script %q: %w", scriptName, err)
if rErr := tx.Rollback(); rErr != nil {
return fmt.Errorf(
"failed to finish tx & rollback script %q: %w. after commit: %w",
scriptName, rErr, err,
)
}
return fmt.Errorf("failed to commit script %q tx: %w", scriptName, err)
}
Expand Down

0 comments on commit 491f0b4

Please sign in to comment.