From 0df56f9ea81a1e6ffaa0f7be398ff40e20bbc092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20K=C5=82eczek?= Date: Sun, 31 May 2026 08:15:27 +0200 Subject: [PATCH] refactor: assemble main transaction session in MainTx This change makes the API surface between MainTx and App smaller. Currently, App reconstructs a database transaction by unpacking the isolation level, transaction mode, DbHandler, and transaction runner returned by MainTx. That exposes MainTx internals at the call site even though MainTx already owns query setup, execution, decoding, and rollback behavior. The goal is to keep transaction assembly in MainTx while App remains responsible for pool execution, database error mapping, and response orchestration. DbTx now carries the assembled SQL session, and App passes that session directly to the connection pool. --- src/PostgREST/App.hs | 4 ++-- src/PostgREST/MainTx.hs | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 1d9b69b4e..ac1be60ea 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -196,8 +196,8 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache jwtTime authResul (txTime, txResult) <- withTiming conf $ do case tx of MainTx.NoDbTx r -> pure r - MainTx.DbTx{..} -> do - dbRes <- lift $ AppState.usePool appState (dqTransaction dqIsoLevel dqTxMode $ runExceptT dqDbHandler) + MainTx.DbTx dbSession -> do + dbRes <- lift $ AppState.usePool appState dbSession let eitherResp = join $ mapLeft (Error.PgErr . Error.PgError (Just authRole /= configDbAnonRole)) dbRes -- TODO: we use obsQuery twice, one here and one below because in case of an error with the usePool above, the request will finish here and return an error message. diff --git a/src/PostgREST/MainTx.hs b/src/PostgREST/MainTx.hs index e5b314671..4a6d753ae 100644 --- a/src/PostgREST/MainTx.hs +++ b/src/PostgREST/MainTx.hs @@ -59,12 +59,7 @@ import Protolude hiding (Handler) type DbHandler = ExceptT Error SQL.Transaction data MainTx - = DbTx { - dqIsoLevel :: SQL.IsolationLevel - , dqTxMode :: SQL.Mode - , dqDbHandler :: DbHandler DbResult - , dqTransaction :: SQL.IsolationLevel -> SQL.Mode -> SQL.Transaction (Either Error DbResult) -> SQL.Session (Either Error DbResult) - } + = DbTx (SQL.Session (Either Error DbResult)) | NoDbTx DbResult data DbResult @@ -96,7 +91,7 @@ data ResultSet mainTx :: MainQuery -> AppConfig -> AuthResult -> ApiRequest -> ActionPlan -> SchemaCache -> MainTx mainTx _ _ _ _ (NoDb x) _ = NoDbTx $ NoDbResult x mainTx genQ@MainQuery{..} conf@AppConfig{..} AuthResult{..} apiReq (Db plan) sCache = - DbTx isoLvl txMode dbHandler SQL.transactionNoRetry + DbTx $ SQL.transactionNoRetry isoLvl txMode $ runExceptT dbHandler where isoLvl = planIsoLvl conf authRole plan txMode = planTxMode plan