Skip to content

Commit

Permalink
fix(migrate): close conn/tx on error
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed May 30, 2022
1 parent b9c768c commit 7b168ea
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions migrate/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,30 @@ func NewSQLMigrationFunc(fsys fs.FS, name string) MigrationFunc {
idb = conn
}

var retErr error

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

if conn, ok := idb.(bun.Conn); ok {
retErr = conn.Close()
return
}

panic("not reached")
}()

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

if tx, ok := idb.(bun.Tx); ok {
return tx.Commit()
} else if conn, ok := idb.(bun.Conn); ok {
return conn.Close()
}

panic("not reached")
return retErr
}
}

Expand Down

0 comments on commit 7b168ea

Please sign in to comment.