func (r *Runner) invokeMigration(isTest bool, m Migration, c *spiffy.DbConnection, optionalTx ...*sql.Tx) (err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("%v", err) } }() if m.IsTransactionIsolated() { err = m.Apply(c, spiffy.OptionalTx(optionalTx...)) return } var tx *sql.Tx tx, err = c.Begin() if err != nil { return err } defer func() { if err == nil { err = exception.Wrap(tx.Commit()) } else { err = exception.WrapMany(err, exception.New(tx.Rollback())) } }() err = m.Apply(c, tx) return }
// Apply wraps the action in a transaction and commits it if there were no errors, rolling back if there were. func (o *Operation) Apply(c *spiffy.DbConnection, optionalTx ...*sql.Tx) (err error) { tx := spiffy.OptionalTx(optionalTx...) err = o.action(o, c, tx) return }