~bigbes/core-go

33562018fec2053d30ebe5e9fa5f313223d18c3c — Drew DeVault 5 years ago 31fc9fc
database.WithTx: rollback if func returns error

Previously, this only rolled back if the called function panicked. This
updates it to also roll back if the called function returns an error.
2 files changed, 7 insertions(+), 2 deletions(-)

M database/middleware.go
M webhooks/legacy_test.go
M database/middleware.go => database/middleware.go +5 -1
@@ 60,5 60,9 @@ func WithTx(ctx context.Context, opts *sql.TxOptions, fn func(tx *sql.Tx) error)
		}
		tx.Commit()
	}()
	return fn(tx)
	err = fn(tx)
	if err != nil {
		tx.Rollback()
	}
	return err
}

M webhooks/legacy_test.go => webhooks/legacy_test.go +2 -1
@@ 131,7 131,8 @@ func TestDelivery(t *testing.T) {
				"X-Webhook-Event",
				"X-Webhook-Delivery",
			), // Final request headers
			4096)
			4096).
			WillReturnResult(sqlmock.NewResult(1, 1))
	mock.ExpectCommit()

	ctx = database.Context(context.Background(), db)