Use transactions for only patch and put

This commit is contained in:
Joe Nelson
2014-11-06 14:19:19 -08:00
parent f58e3db924
commit 5cbbe12b65
+5 -5
View File
@@ -34,22 +34,22 @@ withDBConnection :: Pool Connection -> (Connection -> Application) -> Applicatio
withDBConnection pool app req respond =
withResource pool (\c -> app c req respond)
safeAction :: Request -> Bool
safeAction = (`notElem` ["PATCH", "PUT"]) . requestMethod
inTransaction :: Environment -> (Connection -> Application) ->
Connection -> Application
inTransaction env app conn req respond =
if env == Production && readOnlyRequest req
if env == Production && safeAction req
then
app conn req respond
else
finally (runRaw conn "begin" >> app conn req respond) (runRaw conn "commit")
readOnlyRequest :: Request -> Bool
readOnlyRequest req = requestMethod req `elem` ["GET", "HEAD", "OPTIONS"]
withSavepoint :: Environment -> (Connection -> Application) ->
Connection -> Application
withSavepoint env app conn req respond =
if env == Production && readOnlyRequest req
if env == Production && safeAction req
then app conn req respond
else do
runRaw conn "savepoint req_sp"