Skip to content

Commit

Permalink
Adds inWithTransaction helper to get transaction status
Browse files Browse the repository at this point in the history
I added a function that will return `True` when called within the action
passed to `withTransaction`.
  • Loading branch information
jlavelle committed Nov 19, 2024
1 parent e0ef4af commit 27008f3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions orville-postgresql/src/Orville/PostgreSQL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module Orville.PostgreSQL

-- * Opening transactions and savepoints
, Transaction.withTransaction
, Transaction.inWithTransaction

-- * Types for incorporating Orville into other Monads
, MonadOrville.MonadOrville
Expand Down
19 changes: 19 additions & 0 deletions orville-postgresql/src/Orville/PostgreSQL/Execution/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ to ensure some Haskell action occurs within a database transaction.
-}
module Orville.PostgreSQL.Execution.Transaction
( withTransaction
, inWithTransaction
)
where

Expand Down Expand Up @@ -125,3 +126,21 @@ savepointName savepoint =
n = OrvilleState.savepointNestingLevel savepoint
in
Expr.savepointName ("orville_savepoint_level_" <> show n)

{- |
Returns 'True' when called inside of the action passed to 'Orville.PostgreSQL.withTransaction',
and 'False' otherwise.
@since 1.1.0.0
-}
inWithTransaction :: MonadOrville.MonadOrville m => m Bool
inWithTransaction =
fmap
( \state -> case OrvilleState.orvilleConnectionState state of
OrvilleState.Connected connectedState -> case OrvilleState.connectedTransaction connectedState of
Just _ -> True
Nothing -> False
OrvilleState.NotConnected ->
False
)
Monad.askOrvilleState
13 changes: 13 additions & 0 deletions orville-postgresql/test/Test/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ transactionTests pool =
, prop_callbacksMadeForTransactionCommit pool
, prop_callbacksMadeForTransactionRollback pool
, prop_usesCustomBeginTransactionSql pool
, prop_inWithTransaction pool
]

prop_transactionsWithoutExceptionsCommit :: Property.NamedDBProperty
Expand Down Expand Up @@ -162,6 +163,18 @@ prop_usesCustomBeginTransactionSql =
, (Orville.OtherQuery, RawSql.toExampleBytes customExpr)
]

prop_inWithTransaction :: Property.NamedDBProperty
prop_inWithTransaction =
Property.singletonNamedDBProperty "inWithTransaction returns true inside of withTransaction" $ \pool -> do
(inside, outsideBefore, outsideAfter) <- HH.evalIO . Orville.runOrville pool $ do
outsideBefore <- Orville.inWithTransaction
inside <- Orville.withTransaction Orville.inWithTransaction
outsideAfter <- Orville.inWithTransaction
pure (inside, outsideBefore, outsideAfter)
inside === True
outsideBefore === False
outsideAfter === False

captureTransactionCallbackEvents ::
Orville.ConnectionPool ->
Orville.Orville () ->
Expand Down

0 comments on commit 27008f3

Please sign in to comment.