Skip to content

Commit 0df56f9

Browse files
mkleczeksteve-chavez
authored andcommitted
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.
1 parent 56ad5a9 commit 0df56f9

2 files changed

Lines changed: 4 additions & 9 deletions

File tree

src/PostgREST/App.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ postgrestResponse appState conf@AppConfig{..} maybeSchemaCache jwtTime authResul
196196
(txTime, txResult) <- withTiming conf $ do
197197
case tx of
198198
MainTx.NoDbTx r -> pure r
199-
MainTx.DbTx{..} -> do
200-
dbRes <- lift $ AppState.usePool appState (dqTransaction dqIsoLevel dqTxMode $ runExceptT dqDbHandler)
199+
MainTx.DbTx dbSession -> do
200+
dbRes <- lift $ AppState.usePool appState dbSession
201201
let eitherResp = join $ mapLeft (Error.PgErr . Error.PgError (Just authRole /= configDbAnonRole)) dbRes
202202

203203
-- 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.

src/PostgREST/MainTx.hs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ import Protolude hiding (Handler)
5959
type DbHandler = ExceptT Error SQL.Transaction
6060

6161
data MainTx
62-
= DbTx {
63-
dqIsoLevel :: SQL.IsolationLevel
64-
, dqTxMode :: SQL.Mode
65-
, dqDbHandler :: DbHandler DbResult
66-
, dqTransaction :: SQL.IsolationLevel -> SQL.Mode -> SQL.Transaction (Either Error DbResult) -> SQL.Session (Either Error DbResult)
67-
}
62+
= DbTx (SQL.Session (Either Error DbResult))
6863
| NoDbTx DbResult
6964

7065
data DbResult
@@ -96,7 +91,7 @@ data ResultSet
9691
mainTx :: MainQuery -> AppConfig -> AuthResult -> ApiRequest -> ActionPlan -> SchemaCache -> MainTx
9792
mainTx _ _ _ _ (NoDb x) _ = NoDbTx $ NoDbResult x
9893
mainTx genQ@MainQuery{..} conf@AppConfig{..} AuthResult{..} apiReq (Db plan) sCache =
99-
DbTx isoLvl txMode dbHandler SQL.transactionNoRetry
94+
DbTx $ SQL.transactionNoRetry isoLvl txMode $ runExceptT dbHandler
10095
where
10196
isoLvl = planIsoLvl conf authRole plan
10297
txMode = planTxMode plan

0 commit comments

Comments
 (0)