@@ -28,7 +28,8 @@ module Squeal.PostgreSQL.Transaction
2828 , begin
2929 , commit
3030 , rollback
31− , rollbackOrCommit
31+ , transactionallySchema
32+ , transactionallySchema_
3233 -- * Transaction Mode
3334 , TransactionMode (..)
3435 , defaultMode
@@ -45,11 +46,13 @@ import Control.Exception.Lifted
4546import Control.Monad.Base
4647import Control.Monad.Trans.Control
4748import Data.ByteString
49+import Data.Function
4850import Data.Monoid
4951import Generics.SOP
5052
5153import qualified Database.PostgreSQL.LibPQ as LibPQ
5254
55+import Squeal.PostgreSQL.Definition
5356import Squeal.PostgreSQL.Manipulation
5457import Squeal.PostgreSQL.Prettyprint
5558import Squeal.PostgreSQL.PQ
@@ -87,17 +90,26 @@ commit = manipulate $ UnsafeManipulation "COMMIT;"
8790rollback :: MonadPQ schema tx => tx (K Result NilRelation)
8891rollback = manipulate $ UnsafeManipulation "ROLLBACK;"
8992
90−-- | @ROLLBACK@ a transaction upon `Exception`, otherwise @COMMIT@.
91−rollbackOrCommit
92− :: MonadBaseControl IO io
93− => PQ schema0 schema1 io x
93+-- | Run a schema changing computation `transactionallySchema`.
94+transactionallySchema
95+ :: MonadBaseControl IO io
96+ => TransactionMode
97+ -> PQ schema0 schema1 io x
9498 -> PQ schema0 schema1 io x
95−rollbackOrCommit u = PQ $ \ conn -> mask $ \ restore -> do
99+transactionallySchema mode u = PQ $ \ conn -> mask $ \ restore -> do
100+ liftBase (LibPQ.exec (unK conn) ("BEGIN" <+> renderTransactionMode mode <> ";"))
96101 x <- restore (unPQ u conn)
97102 `onException` (liftBase (LibPQ.exec (unK conn) "ROLLBACK"))
98103 _ <- liftBase (LibPQ.exec (unK conn) "COMMIT")
99104 return x
100105
106+-- | Run a schema changing computation `transactionallySchema_` in `DefaultMode`.
107+transactionallySchema_
108+ :: MonadBaseControl IO io
109+ => PQ schema0 schema1 io x
110+ -> PQ schema0 schema1 io x
111+transactionallySchema_ = transactionallySchema defaultMode
112+
101113-- | The available transaction characteristics are the transaction `IsolationLevel`,
102114-- the transaction access mode (`ReadWrite` or `ReadOnly`), and the `DeferrableMode`.
103115data TransactionMode = TransactionMode