Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/PostgREST/ApiRequest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import Web.Cookie (parseCookies)

import PostgREST.ApiRequest.QueryParams (QueryParams (..))
import PostgREST.Config (AppConfig (..),
DbTxEnd (..),
OpenAPIMode (..))
import PostgREST.Config.Database (TimezoneNames)
import PostgREST.Error (ApiRequestError (..),
Expand Down Expand Up @@ -164,7 +165,13 @@ userApiRequest conf prefs req reqBody = do

-- | Parses the Prefer header
userPreferences :: AppConfig -> Request -> TimezoneNames -> Preferences.Preferences
userPreferences conf req timezones = Preferences.fromHeaders (configDbTxAllowOverride conf) timezones $ requestHeaders req
userPreferences conf req timezones = Preferences.fromHeaders isTxOverrideAllowed timezones $ requestHeaders req
where
isTxOverrideAllowed = case configDbTxEnd conf of
TxCommitAllowOverride -> True
TxRollbackAllowOverride -> True
TxCommit -> False
TxRollback -> False
Comment on lines +170 to +174
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part also overall seems more verbose. Maybe at define it next to the types? Perhaps both these boolean results can be shortened somehow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shortening is already there - in the code that is removed!


getResource :: AppConfig -> [Text] -> Either ApiRequestError Resource
getResource AppConfig{configOpenApiMode, configDbRootSpec} = \case
Expand Down
44 changes: 26 additions & 18 deletions src/PostgREST/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Description : Manages PostgREST configuration type and parser.

module PostgREST.Config
( AppConfig (..)
, DbTxEnd (..)
, Environment
, JSPath
, JSPathExp(..)
Expand Down Expand Up @@ -88,8 +89,7 @@ data AppConfig = AppConfig
, configDbSchemas :: NonEmpty Text
, configDbConfig :: Bool
, configDbPreConfig :: Maybe QualifiedIdentifier
, configDbTxAllowOverride :: Bool
, configDbTxRollbackAll :: Bool
, configDbTxEnd :: DbTxEnd
, configDbUri :: Text
, configFilePath :: Maybe FilePath
, configJWKS :: Maybe JwkSet
Expand Down Expand Up @@ -117,6 +117,13 @@ data AppConfig = AppConfig
, configInternalSCSleep :: Maybe Int32
}

data DbTxEnd
= TxCommit
| TxCommitAllowOverride
| TxRollback
| TxRollbackAllowOverride
deriving (Eq)

data LogLevel = LogCrit | LogError | LogWarn | LogInfo | LogDebug
deriving (Eq, Ord)

Expand Down Expand Up @@ -171,7 +178,7 @@ toText conf =
,("db-schemas", q . T.intercalate "," . toList . configDbSchemas)
,("db-config", T.toLower . show . configDbConfig)
,("db-pre-config", q . maybe mempty dumpQi . configDbPreConfig)
,("db-tx-end", q . showTxEnd)
,("db-tx-end", q . showTxEnd . configDbTxEnd)
,("db-uri", q . configDbUri)
,("jwt-aud", q . fromMaybe mempty . configJwtAudience)
,("jwt-role-claim-key", q . T.intercalate mempty . fmap dumpJSPath . configJwtRoleClaimKey)
Expand Down Expand Up @@ -200,16 +207,19 @@ toText conf =
-- quote strings and replace " with \"
q s = "\"" <> T.replace "\"" "\\\"" s <> "\""

showTxEnd c = case (configDbTxRollbackAll c, configDbTxAllowOverride c) of
( False, False ) -> "commit"
( False, True ) -> "commit-allow-override"
( True , False ) -> "rollback"
( True , True ) -> "rollback-allow-override"
showTxEnd :: DbTxEnd -> Text
showTxEnd = \case
TxCommit -> "commit"
TxCommitAllowOverride -> "commit-allow-override"
TxRollback -> "rollback"
TxRollbackAllowOverride -> "rollback-allow-override"

showJwtSecret c
| configJwtSecretIsBase64 c = B64.encode secret
| otherwise = secret
where
secret = fromMaybe mempty $ configJwtSecret c

showSocketMode c = showOct (configServerUnixSocketMode c) mempty

-- This class is needed for the polymorphism of overrideFromDbOrEnvironment
Expand Down Expand Up @@ -276,8 +286,7 @@ parser optPath env dbSettings roleSettings roleIsolationLvl =
(optString "db-schema"))
<*> (fromMaybe True <$> optBool "db-config")
<*> (fmap toQi <$> optString "db-pre-config")
<*> parseTxEnd "db-tx-end" snd
<*> parseTxEnd "db-tx-end" fst
<*> parseTxEnd "db-tx-end"
<*> (fromMaybe "postgresql://" <$> optString "db-uri")
<*> pure optPath
<*> pure Nothing
Expand Down Expand Up @@ -373,15 +382,14 @@ parser optPath env dbSettings roleSettings roleIsolationLvl =
Just "main-query" -> pure LogQueryMain
Just _ -> fail "Invalid SQL logging value. Check your configuration."

parseTxEnd :: C.Key -> ((Bool, Bool) -> Bool) -> C.Parser C.Config Bool
parseTxEnd k f =
parseTxEnd :: C.Key -> C.Parser C.Config DbTxEnd
parseTxEnd k =
optString k >>= \case
-- RollbackAll AllowOverride
Nothing -> pure $ f (False, False)
Just "commit" -> pure $ f (False, False)
Just "commit-allow-override" -> pure $ f (False, True)
Just "rollback" -> pure $ f (True, False)
Just "rollback-allow-override" -> pure $ f (True, True)
Nothing -> pure TxCommit -- default
Just "commit" -> pure TxCommit
Just "commit-allow-override" -> pure TxCommitAllowOverride
Just "rollback" -> pure TxRollback
Just "rollback-allow-override" -> pure TxRollbackAllowOverride
Comment on lines -379 to +392
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, see how this change is much cleaner and less confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, yeah. I always found these booleans a bit confusing.

Just _ -> fail "Invalid transaction termination. Check your configuration."

parseRoleClaimKey :: C.Key -> C.Key -> C.Parser C.Config JSPath
Expand Down
8 changes: 7 additions & 1 deletion src/PostgREST/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import PostgREST.ApiRequest.Preferences (PreferCount (..),
shouldCount)
import PostgREST.Auth.Types (AuthResult (..))
import PostgREST.Config (AppConfig (..),
DbTxEnd (..),
OpenAPIMode (..))
import PostgREST.Config.PgVersion (PgVersion (..))
import PostgREST.Error (Error)
Expand Down Expand Up @@ -255,14 +256,19 @@ failExceedsMaxAffectedPref (Just (PreferMaxAffected n), handling) RSStandard{rsQ
-- | Set a transaction to roll back if requested
optionalRollback :: AppConfig -> ApiRequest -> DbHandler ()
optionalRollback AppConfig{..} ApiRequest{iPreferences=Preferences{..}} = do
lift $ when (shouldRollback || (configDbTxRollbackAll && not shouldCommit)) $ do
lift $ when (shouldRollback || (isTxRollback && not shouldCommit)) $ do
SQL.sql "SET CONSTRAINTS ALL IMMEDIATE"
SQL.condemn
where
shouldCommit =
preferTransaction == Just Commit
shouldRollback =
preferTransaction == Just Rollback
isTxRollback = case configDbTxEnd of
TxRollback -> True
TxRollbackAllowOverride -> True
TxCommit -> False
TxCommitAllowOverride -> False
Comment on lines +267 to +271
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this part is not looking like a net gain. Is there any other way to do it?


-- | Set transaction scoped settings
setPgLocals :: DbActionPlan -> AppConfig -> KM.KeyMap JSON.Value -> BS.ByteString -> ApiRequest -> DbHandler ()
Expand Down
8 changes: 4 additions & 4 deletions test/spec/SpecHelper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Text.Heredoc

import Data.String (String)
import PostgREST.Config (AppConfig (..),
DbTxEnd (..),
JSPathExp (..),
LogLevel (..),
LogQuery (..),
Expand Down Expand Up @@ -152,8 +153,7 @@ baseCfg = let secret = encodeUtf8 "reallyreallyreallyreallyverysafe" in
, configServerTraceHeader = Nothing
, configServerUnixSocket = Nothing
, configServerUnixSocketMode = 432
, configDbTxAllowOverride = True
, configDbTxRollbackAll = True
, configDbTxEnd = TxRollbackAllowOverride
, configAdminServerHost = "localhost"
, configAdminServerPort = Nothing
, configRoleSettings = mempty
Expand All @@ -166,10 +166,10 @@ testCfg :: AppConfig
testCfg = baseCfg

testCfgDisallowRollback :: AppConfig
testCfgDisallowRollback = baseCfg { configDbTxAllowOverride = False, configDbTxRollbackAll = False }
testCfgDisallowRollback = baseCfg { configDbTxEnd = TxCommit }

testCfgForceRollback :: AppConfig
testCfgForceRollback = baseCfg { configDbTxAllowOverride = False, configDbTxRollbackAll = True }
testCfgForceRollback = baseCfg { configDbTxEnd = TxRollback }

testCfgNoAnon :: AppConfig
testCfgNoAnon = baseCfg { configDbAnonRole = Nothing }
Expand Down