Skip to content

Commit d6669ae

Browse files
committed
refactor: merge transaction end configs to a single config
1 parent 9d5968a commit d6669ae

File tree

4 files changed

+45
-24
lines changed

4 files changed

+45
-24
lines changed

src/PostgREST/ApiRequest.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import Web.Cookie (parseCookies)
4545

4646
import PostgREST.ApiRequest.QueryParams (QueryParams (..))
4747
import PostgREST.Config (AppConfig (..),
48+
DbTxEnd (..),
4849
OpenAPIMode (..))
4950
import PostgREST.Config.Database (TimezoneNames)
5051
import PostgREST.Error (ApiRequestError (..),
@@ -164,7 +165,13 @@ userApiRequest conf prefs req reqBody = do
164165

165166
-- | Parses the Prefer header
166167
userPreferences :: AppConfig -> Request -> TimezoneNames -> Preferences.Preferences
167-
userPreferences conf req timezones = Preferences.fromHeaders (configDbTxAllowOverride conf) timezones $ requestHeaders req
168+
userPreferences conf req timezones = Preferences.fromHeaders isTxOverrideAllowed timezones $ requestHeaders req
169+
where
170+
isTxOverrideAllowed = case configDbTxEnd conf of
171+
TxCommitAllowOverride -> True
172+
TxRollbackAllowOverride -> True
173+
TxCommit -> False
174+
TxRollback -> False
168175

169176
getResource :: AppConfig -> [Text] -> Either ApiRequestError Resource
170177
getResource AppConfig{configOpenApiMode, configDbRootSpec} = \case

src/PostgREST/Config.hs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Description : Manages PostgREST configuration type and parser.
1212

1313
module PostgREST.Config
1414
( AppConfig (..)
15+
, DbTxEnd (..)
1516
, Environment
1617
, JSPath
1718
, JSPathExp(..)
@@ -88,8 +89,7 @@ data AppConfig = AppConfig
8889
, configDbSchemas :: NonEmpty Text
8990
, configDbConfig :: Bool
9091
, configDbPreConfig :: Maybe QualifiedIdentifier
91-
, configDbTxAllowOverride :: Bool
92-
, configDbTxRollbackAll :: Bool
92+
, configDbTxEnd :: DbTxEnd
9393
, configDbUri :: Text
9494
, configFilePath :: Maybe FilePath
9595
, configJWKS :: Maybe JwkSet
@@ -117,6 +117,13 @@ data AppConfig = AppConfig
117117
, configInternalSCSleep :: Maybe Int32
118118
}
119119

120+
data DbTxEnd
121+
= TxCommit
122+
| TxCommitAllowOverride
123+
| TxRollback
124+
| TxRollbackAllowOverride
125+
deriving (Eq)
126+
120127
data LogLevel = LogCrit | LogError | LogWarn | LogInfo | LogDebug
121128
deriving (Eq, Ord)
122129

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

203-
showTxEnd c = case (configDbTxRollbackAll c, configDbTxAllowOverride c) of
204-
( False, False ) -> "commit"
205-
( False, True ) -> "commit-allow-override"
206-
( True , False ) -> "rollback"
207-
( True , True ) -> "rollback-allow-override"
210+
showTxEnd :: DbTxEnd -> Text
211+
showTxEnd = \case
212+
TxCommit -> "commit"
213+
TxCommitAllowOverride -> "commit-allow-override"
214+
TxRollback -> "rollback"
215+
TxRollbackAllowOverride -> "rollback-allow-override"
216+
208217
showJwtSecret c
209218
| configJwtSecretIsBase64 c = B64.encode secret
210219
| otherwise = secret
211220
where
212221
secret = fromMaybe mempty $ configJwtSecret c
222+
213223
showSocketMode c = showOct (configServerUnixSocketMode c) mempty
214224

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

376-
parseTxEnd :: C.Key -> ((Bool, Bool) -> Bool) -> C.Parser C.Config Bool
377-
parseTxEnd k f =
385+
parseTxEnd :: C.Key -> C.Parser C.Config DbTxEnd
386+
parseTxEnd k =
378387
optString k >>= \case
379-
-- RollbackAll AllowOverride
380-
Nothing -> pure $ f (False, False)
381-
Just "commit" -> pure $ f (False, False)
382-
Just "commit-allow-override" -> pure $ f (False, True)
383-
Just "rollback" -> pure $ f (True, False)
384-
Just "rollback-allow-override" -> pure $ f (True, True)
388+
Nothing -> pure TxCommit -- default
389+
Just "commit" -> pure TxCommit
390+
Just "commit-allow-override" -> pure TxCommitAllowOverride
391+
Just "rollback" -> pure TxRollback
392+
Just "rollback-allow-override" -> pure TxRollbackAllowOverride
385393
Just _ -> fail "Invalid transaction termination. Check your configuration."
386394

387395
parseRoleClaimKey :: C.Key -> C.Key -> C.Parser C.Config JSPath

src/PostgREST/Query.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import PostgREST.ApiRequest.Preferences (PreferCount (..),
3838
shouldCount)
3939
import PostgREST.Auth.Types (AuthResult (..))
4040
import PostgREST.Config (AppConfig (..),
41+
DbTxEnd (..),
4142
OpenAPIMode (..))
4243
import PostgREST.Config.PgVersion (PgVersion (..))
4344
import PostgREST.Error (Error)
@@ -255,14 +256,19 @@ failExceedsMaxAffectedPref (Just (PreferMaxAffected n), handling) RSStandard{rsQ
255256
-- | Set a transaction to roll back if requested
256257
optionalRollback :: AppConfig -> ApiRequest -> DbHandler ()
257258
optionalRollback AppConfig{..} ApiRequest{iPreferences=Preferences{..}} = do
258-
lift $ when (shouldRollback || (configDbTxRollbackAll && not shouldCommit)) $ do
259+
lift $ when (shouldRollback || (isTxRollback && not shouldCommit)) $ do
259260
SQL.sql "SET CONSTRAINTS ALL IMMEDIATE"
260261
SQL.condemn
261262
where
262263
shouldCommit =
263264
preferTransaction == Just Commit
264265
shouldRollback =
265266
preferTransaction == Just Rollback
267+
isTxRollback = case configDbTxEnd of
268+
TxRollback -> True
269+
TxRollbackAllowOverride -> True
270+
TxCommit -> False
271+
TxCommitAllowOverride -> False
266272

267273
-- | Set transaction scoped settings
268274
setPgLocals :: DbActionPlan -> AppConfig -> KM.KeyMap JSON.Value -> BS.ByteString -> ApiRequest -> DbHandler ()

test/spec/SpecHelper.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Text.Heredoc
3131

3232
import Data.String (String)
3333
import PostgREST.Config (AppConfig (..),
34+
DbTxEnd (..),
3435
JSPathExp (..),
3536
LogLevel (..),
3637
LogQuery (..),
@@ -152,8 +153,7 @@ baseCfg = let secret = encodeUtf8 "reallyreallyreallyreallyverysafe" in
152153
, configServerTraceHeader = Nothing
153154
, configServerUnixSocket = Nothing
154155
, configServerUnixSocketMode = 432
155-
, configDbTxAllowOverride = True
156-
, configDbTxRollbackAll = True
156+
, configDbTxEnd = TxRollbackAllowOverride
157157
, configAdminServerHost = "localhost"
158158
, configAdminServerPort = Nothing
159159
, configRoleSettings = mempty
@@ -166,10 +166,10 @@ testCfg :: AppConfig
166166
testCfg = baseCfg
167167

168168
testCfgDisallowRollback :: AppConfig
169-
testCfgDisallowRollback = baseCfg { configDbTxAllowOverride = False, configDbTxRollbackAll = False }
169+
testCfgDisallowRollback = baseCfg { configDbTxEnd = TxCommit }
170170

171171
testCfgForceRollback :: AppConfig
172-
testCfgForceRollback = baseCfg { configDbTxAllowOverride = False, configDbTxRollbackAll = True }
172+
testCfgForceRollback = baseCfg { configDbTxEnd = TxRollback }
173173

174174
testCfgNoAnon :: AppConfig
175175
testCfgNoAnon = baseCfg { configDbAnonRole = Nothing }

0 commit comments

Comments
 (0)