on dev help sign in
remove transactionallySchema
b333355Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Migration.hs+2−2
@@ -251,7 +251,7 @@ instance Migratory (Terminally PQ IO) where
251 migrateUp migration = unsafePQ $
251 migrateUp migration = unsafePQ $
252 define createMigrations
252 define createMigrations
253 & pqBind okResult
253 & pqBind okResult
254− & pqThen (transactionallySchema_ (upMigrations migration))
254+ & pqThen (transactionally_ (upMigrations migration))
255
255
256 where
256 where
257
257
@@ -284,7 +284,7 @@ instance Migratory (Terminally PQ IO) where
284 migrateDown migrations = unsafePQ $
284 migrateDown migrations = unsafePQ $
285 define createMigrations
285 define createMigrations
286 & pqBind okResult
286 & pqBind okResult
287− & pqThen (transactionallySchema_ (downMigrations migrations))
287+ & pqThen (transactionally_ (downMigrations migrations))
288
288
289 where
289 where
290
290
squeal-postgresql/src/Squeal/PostgreSQL/Transaction.hs+14−37
@@ -24,8 +24,6 @@ module Squeal.PostgreSQL.Transaction
24 , begin
24 , begin
25 , commit
25 , commit
26 , rollback
26 , rollback
27− , transactionallySchema
28− , transactionallySchema_
29 -- * Transaction Mode
27 -- * Transaction Mode
30 , TransactionMode (..)
28 , TransactionMode (..)
31 , defaultMode
29 , defaultMode
@@ -36,18 +34,17 @@ module Squeal.PostgreSQL.Transaction
36 ) where
34 ) where
37
35
38import Control.Monad
36import Control.Monad
39−import Generics.SOP
40import UnliftIO
37import UnliftIO
41
38
42−import qualified Database.PostgreSQL.LibPQ as LibPQ
43−
44import Squeal.PostgreSQL.Manipulation
39import Squeal.PostgreSQL.Manipulation
45import Squeal.PostgreSQL.Render
40import Squeal.PostgreSQL.Render
46import Squeal.PostgreSQL.PQ
41import Squeal.PostgreSQL.PQ
47
42
48−{- | Run a computation `transactionally`; first `begin`,
43+{- | Run a computation `transactionally`;
44+first `begin`,
49−then run the computation, then if it raises an exception
45+then run the computation,
46+`onException` `rollback` and rethrow the exception,
50−`rollback`, otherwise `commit` and `return` the result.
47+otherwise `commit` and `return` the result.
51-}
48-}
52transactionally
49transactionally
53 :: (MonadUnliftIO tx, MonadPQ schemas tx)
50 :: (MonadUnliftIO tx, MonadPQ schemas tx)
@@ -60,7 +57,7 @@ transactionally mode tx = mask $ \restore -> do
60 commit
57 commit
61 return result
58 return result
62
59
63−-- | Run a computation `transactionally_` in `defaultMode`.
60+-- | Run a computation `transactionally_`, in `defaultMode`.
64transactionally_
61transactionally_
65 :: (MonadUnliftIO tx, MonadPQ schemas tx)
62 :: (MonadUnliftIO tx, MonadPQ schemas tx)
66 => tx x -- ^ run inside a transaction
63 => tx x -- ^ run inside a transaction
@@ -69,11 +66,12 @@ transactionally_ = transactionally defaultMode
69
66
70{- |
67{- |
71`transactionallyRetry` a computation;
68`transactionallyRetry` a computation;
69+
72−first `begin`,
70+* first `begin`,
73−then `try` the computation,
71+* then `try` the computation,
74−if it raises an serialization failure then `rollback` and restart the transaction,
72+ - if it raises a serialization failure then `rollback` and restart the transaction,
75−if it raises any other exception then `rollback` and rethrow the exception,
73+ - if it raises any other exception then `rollback` and rethrow the exception,
76−otherwise `commit` and `return` the result.
74+ - otherwise `commit` and `return` the result.
77-}
75-}
78transactionallyRetry
76transactionallyRetry
79 :: (MonadUnliftIO tx, MonadPQ schemas tx)
77 :: (MonadUnliftIO tx, MonadPQ schemas tx)
@@ -98,35 +96,14 @@ begin :: MonadPQ schemas tx => TransactionMode -> tx ()
98begin mode = void . manipulate . UnsafeManipulation $
96begin mode = void . manipulate . UnsafeManipulation $
99 "BEGIN" <+> renderSQL mode <> ";"
97 "BEGIN" <+> renderSQL mode <> ";"
100
98
101−-- | @COMMIT@ a schema invariant transaction.
99+-- | @COMMIT@ a transaction.
102commit :: MonadPQ schemas tx => tx ()
100commit :: MonadPQ schemas tx => tx ()
103commit = void . manipulate $ UnsafeManipulation "COMMIT;"
101commit = void . manipulate $ UnsafeManipulation "COMMIT;"
104
102
105−-- | @ROLLBACK@ a schema invariant transaction.
103+-- | @ROLLBACK@ a transaction.
106rollback :: MonadPQ schemas tx => tx ()
104rollback :: MonadPQ schemas tx => tx ()
107rollback = void . manipulate $ UnsafeManipulation "ROLLBACK;"
105rollback = void . manipulate $ UnsafeManipulation "ROLLBACK;"
108
106
109−-- | Run a schema changing computation `transactionallySchema`.
110−transactionallySchema
111− :: MonadUnliftIO io
112− => TransactionMode
113− -> PQ schemas0 schemas1 io x
114− -> PQ schemas0 schemas1 io x
115−transactionallySchema mode u = PQ $ \ conn -> mask $ \ restore -> do
116− _ <- liftIO . LibPQ.exec (unK conn) $
117− "BEGIN" <+> renderSQL mode <> ";"
118− x <- restore (unPQ u conn)
119− `onException` (liftIO (LibPQ.exec (unK conn) "ROLLBACK"))
120− _ <- liftIO $ LibPQ.exec (unK conn) "COMMIT"
121− return x
122−
123−-- | Run a schema changing computation `transactionallySchema_` in `defaultMode`.
124−transactionallySchema_
125− :: MonadUnliftIO io
126− => PQ schemas0 schemas1 io x
127− -> PQ schemas0 schemas1 io x
128−transactionallySchema_ = transactionallySchema defaultMode
129−
130-- | The available transaction characteristics are the transaction `IsolationLevel`,
107-- | The available transaction characteristics are the transaction `IsolationLevel`,
131-- the transaction `AccessMode` (`ReadWrite` or `ReadOnly`), and the `DeferrableMode`.
108-- the transaction `AccessMode` (`ReadWrite` or `ReadOnly`), and the `DeferrableMode`.
132data TransactionMode = TransactionMode
109data TransactionMode = TransactionMode