Use hasql-transaction
Also use hspec before-wrapper
This commit is contained in:
@@ -36,6 +36,7 @@ executable postgrest
|
|||||||
, errors
|
, errors
|
||||||
, hasql >= 0.19.3.3 && < 0.20
|
, hasql >= 0.19.3.3 && < 0.20
|
||||||
, hasql-pool >= 0.4 && < 0.5
|
, hasql-pool >= 0.4 && < 0.5
|
||||||
|
, hasql-transaction >= 0.4 && < 0.5
|
||||||
, http-types
|
, http-types
|
||||||
, interpolatedstring-perl6
|
, interpolatedstring-perl6
|
||||||
, jwt
|
, jwt
|
||||||
@@ -86,6 +87,7 @@ library
|
|||||||
, contravariant
|
, contravariant
|
||||||
, errors
|
, errors
|
||||||
, hasql
|
, hasql
|
||||||
|
, hasql-transaction
|
||||||
, hasql-pool
|
, hasql-pool
|
||||||
, http-types
|
, http-types
|
||||||
, interpolatedstring-perl6
|
, interpolatedstring-perl6
|
||||||
@@ -162,6 +164,7 @@ Test-Suite spec
|
|||||||
, errors
|
, errors
|
||||||
, hasql
|
, hasql
|
||||||
, hasql-pool
|
, hasql-pool
|
||||||
|
, hasql-transaction
|
||||||
, heredoc
|
, heredoc
|
||||||
, hspec == 2.2.*
|
, hspec == 2.2.*
|
||||||
, hspec-wai
|
, hspec-wai
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import Data.Aeson
|
|||||||
import Data.Aeson.Types (emptyArray)
|
import Data.Aeson.Types (emptyArray)
|
||||||
import Data.Monoid
|
import Data.Monoid
|
||||||
import qualified Data.Vector as V
|
import qualified Data.Vector as V
|
||||||
import qualified Hasql.Session as H
|
import qualified Hasql.Transaction as H
|
||||||
|
|
||||||
import PostgREST.Config (AppConfig (..))
|
import PostgREST.Config (AppConfig (..))
|
||||||
import PostgREST.Parsers
|
import PostgREST.Parsers
|
||||||
@@ -58,7 +58,7 @@ import PostgREST.QueryBuilder ( callProc
|
|||||||
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
app :: DbStructure -> AppConfig -> RequestBody -> Request -> H.Session Response
|
app :: DbStructure -> AppConfig -> RequestBody -> Request -> H.Transaction Response
|
||||||
app dbStructure conf reqBody req =
|
app dbStructure conf reqBody req =
|
||||||
let
|
let
|
||||||
-- TODO: blow up for Left values (there is a middleware that checks the headers)
|
-- TODO: blow up for Left values (there is a middleware that checks the headers)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import PostgREST.DbStructure
|
|||||||
import PostgREST.Error (pgErrResponse)
|
import PostgREST.Error (pgErrResponse)
|
||||||
import PostgREST.Middleware
|
import PostgREST.Middleware
|
||||||
import PostgREST.Types (DbStructure)
|
import PostgREST.Types (DbStructure)
|
||||||
import PostgREST.QueryBuilder (inTransaction, Isolation(..))
|
|
||||||
|
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
import Data.Monoid ((<>))
|
import Data.Monoid ((<>))
|
||||||
@@ -20,6 +19,7 @@ import Data.String.Conversions (cs)
|
|||||||
import Data.Time.Clock.POSIX (getPOSIXTime)
|
import Data.Time.Clock.POSIX (getPOSIXTime)
|
||||||
import qualified Hasql.Query as H
|
import qualified Hasql.Query as H
|
||||||
import qualified Hasql.Session as H
|
import qualified Hasql.Session as H
|
||||||
|
import qualified Hasql.Transaction as HT
|
||||||
import qualified Hasql.Decoders as HD
|
import qualified Hasql.Decoders as HD
|
||||||
import qualified Hasql.Encoders as HE
|
import qualified Hasql.Encoders as HE
|
||||||
import qualified Hasql.Pool as P
|
import qualified Hasql.Pool as P
|
||||||
@@ -92,7 +92,8 @@ postgrest conf dbStructure pool =
|
|||||||
middle $ \ req respond -> do
|
middle $ \ req respond -> do
|
||||||
time <- getPOSIXTime
|
time <- getPOSIXTime
|
||||||
body <- strictRequestBody req
|
body <- strictRequestBody req
|
||||||
let handleReq = inTransaction ReadCommitted $
|
|
||||||
runWithClaims conf time (app dbStructure conf body) req
|
let handleReq = runWithClaims conf time (app dbStructure conf body) req
|
||||||
resp <- either pgErrResponse id <$> P.use pool handleReq
|
resp <- either pgErrResponse id <$> P.use pool
|
||||||
|
(HT.run handleReq HT.ReadCommitted HT.Write)
|
||||||
respond resp
|
respond resp
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import Data.Maybe (fromMaybe)
|
|||||||
import Data.Text
|
import Data.Text
|
||||||
import Data.String.Conversions (cs)
|
import Data.String.Conversions (cs)
|
||||||
import Data.Time.Clock (NominalDiffTime)
|
import Data.Time.Clock (NominalDiffTime)
|
||||||
import qualified Hasql.Session as H
|
import qualified Hasql.Transaction as H
|
||||||
|
|
||||||
import Network.HTTP.Types.Header (hAccept, hAuthorization)
|
import Network.HTTP.Types.Header (hAccept, hAuthorization)
|
||||||
import Network.HTTP.Types.Status (status415, status400)
|
import Network.HTTP.Types.Status (status415, status400)
|
||||||
@@ -27,8 +27,8 @@ import Prelude hiding(concat)
|
|||||||
import qualified Data.Map.Lazy as M
|
import qualified Data.Map.Lazy as M
|
||||||
|
|
||||||
runWithClaims :: AppConfig -> NominalDiffTime ->
|
runWithClaims :: AppConfig -> NominalDiffTime ->
|
||||||
(Request -> H.Session Response) ->
|
(Request -> H.Transaction Response) ->
|
||||||
Request -> H.Session Response
|
Request -> H.Transaction Response
|
||||||
runWithClaims conf time app req = do
|
runWithClaims conf time app req = do
|
||||||
H.sql setAnon
|
H.sql setAnon
|
||||||
case split (== ' ') (cs auth) of
|
case split (== ' ') (cs auth) of
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ module PostgREST.QueryBuilder (
|
|||||||
, callProc
|
, callProc
|
||||||
, createReadStatement
|
, createReadStatement
|
||||||
, createWriteStatement
|
, createWriteStatement
|
||||||
, inTransaction
|
|
||||||
, operators
|
, operators
|
||||||
, pgFmtIdent
|
, pgFmtIdent
|
||||||
, pgFmtLit
|
, pgFmtLit
|
||||||
@@ -27,11 +26,9 @@ module PostgREST.QueryBuilder (
|
|||||||
, sourceCTEName
|
, sourceCTEName
|
||||||
, unquoted
|
, unquoted
|
||||||
, ResultsWithCount
|
, ResultsWithCount
|
||||||
, Isolation(..)
|
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Hasql.Query as H
|
import qualified Hasql.Query as H
|
||||||
import qualified Hasql.Session as H
|
|
||||||
import qualified Hasql.Encoders as HE
|
import qualified Hasql.Encoders as HE
|
||||||
import qualified Hasql.Decoders as HD
|
import qualified Hasql.Decoders as HD
|
||||||
|
|
||||||
@@ -504,20 +501,3 @@ pgFmtAsJsonPath (Just xx) = " AS " <> last xx
|
|||||||
|
|
||||||
trimNullChars :: Text -> Text
|
trimNullChars :: Text -> Text
|
||||||
trimNullChars = T.takeWhile (/= '\x0')
|
trimNullChars = T.takeWhile (/= '\x0')
|
||||||
|
|
||||||
data Isolation = ReadCommitted | RepeatableRead | Serializable
|
|
||||||
|
|
||||||
{- |
|
|
||||||
Wrap a session in a transaction of desired isolation level
|
|
||||||
-}
|
|
||||||
inTransaction :: Isolation -> H.Session a -> H.Session a
|
|
||||||
inTransaction lvl f = do
|
|
||||||
H.sql $ "begin " <> isolate <> ";"
|
|
||||||
r <- f
|
|
||||||
H.sql "commit;"
|
|
||||||
return r
|
|
||||||
where
|
|
||||||
isolate = case lvl of
|
|
||||||
ReadCommitted -> "ISOLATION LEVEL READ COMMITTED"
|
|
||||||
RepeatableRead -> "ISOLATION LEVEL REPEATABLE READ"
|
|
||||||
Serializable -> "ISOLATION LEVEL SERIALIZABLE"
|
|
||||||
|
|||||||
@@ -2,8 +2,11 @@ resolver: lts-5.0
|
|||||||
extra-deps:
|
extra-deps:
|
||||||
- hasql-0.19.3.3
|
- hasql-0.19.3.3
|
||||||
- hasql-pool-0.4
|
- hasql-pool-0.4
|
||||||
|
- hasql-transaction-0.4.2
|
||||||
- Ranged-sets-0.3.0
|
- Ranged-sets-0.3.0
|
||||||
- packdeps-0.4.2.1
|
- packdeps-0.4.2.1
|
||||||
|
- bytestring-tree-builder-0.2.5
|
||||||
|
- postgresql-error-codes-1
|
||||||
ghc-options:
|
ghc-options:
|
||||||
postgrest: -O1 -Werror -Wall -fwarn-monomorphism-restriction -fwarn-missing-exported-sigs -fwarn-identities
|
postgrest: -O1 -Werror -Wall -fwarn-monomorphism-restriction -fwarn-missing-exported-sigs -fwarn-identities
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -27,9 +27,9 @@ main = do
|
|||||||
|
|
||||||
result <- P.use pool $ getDbStructure "test"
|
result <- P.use pool $ getDbStructure "test"
|
||||||
let dbStructure = either (error.show) id result
|
let dbStructure = either (error.show) id result
|
||||||
withApp = ($ postgrest cfgDefault dbStructure pool)
|
withApp = return $ postgrest cfgDefault dbStructure pool
|
||||||
|
|
||||||
hspec . sequence_ . map (around withApp) $ specs
|
hspec . sequence_ . map (before withApp) $ specs
|
||||||
|
|
||||||
where
|
where
|
||||||
specs = map (uncurry describe) [
|
specs = map (uncurry describe) [
|
||||||
|
|||||||
Reference in New Issue
Block a user