dbutil: Introduce DBTx
Created by: tsenart
This PR introduces dbutil.DBTx
which implements the dbutil.DB
interface
backed by either a sql.DB
or a sql.Tx
, with additional support for
nested transactions using Postgres savepoints.
This allows us to run a suite of integration tests in the context of single transaction (that will be rolled back at the end), even if those tests start transactions themselves.
It also allows us to explicitly rollback to a previous savepoint in tests when we know a given test will result in a SQL level exception / error and proceed safely from there within the context of a parent transaction.
One situation where using a single transaction for integration tests isn't currently supported is when concurrency is involved, due to this limitation: https://github.com/lib/pq/issues/81
That's why we didn't change the integration tests of the Bitbucket Server provider store, which do lots of concurrency and fail when operating with a DBTx
.