2020-10-29 19:58:13 +03:00
|
|
|
module Hasura.Backends.Postgres.Execute.Mutation
|
|
|
|
( MutationRemoteJoinCtx
|
|
|
|
--
|
|
|
|
, execDeleteQuery
|
|
|
|
, execInsertQuery
|
|
|
|
, execUpdateQuery
|
|
|
|
--
|
2020-05-27 18:02:58 +03:00
|
|
|
, executeMutationOutputQuery
|
2019-03-07 13:24:07 +03:00
|
|
|
, mutateAndFetchCols
|
2020-10-29 19:58:13 +03:00
|
|
|
) where
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2019-09-14 09:01:06 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
import qualified Data.Environment as Env
|
|
|
|
import qualified Data.Sequence as DS
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
import qualified Network.HTTP.Client as HTTP
|
|
|
|
import qualified Network.HTTP.Types as N
|
|
|
|
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
import Instances.TH.Lift ()
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
import qualified Hasura.Backends.Postgres.SQL.DML as S
|
|
|
|
import qualified Hasura.Tracing as Tracing
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Backends.Postgres.Connection
|
|
|
|
import Hasura.Backends.Postgres.Execute.RemoteJoin
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
|
|
|
import Hasura.Backends.Postgres.SQL.Value
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Backends.Postgres.Translate.Delete
|
|
|
|
import Hasura.Backends.Postgres.Translate.Insert
|
|
|
|
import Hasura.Backends.Postgres.Translate.Mutation
|
|
|
|
import Hasura.Backends.Postgres.Translate.Returning
|
|
|
|
import Hasura.Backends.Postgres.Translate.Select
|
|
|
|
import Hasura.Backends.Postgres.Translate.Update
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2019-03-07 13:24:07 +03:00
|
|
|
import Hasura.RQL.DML.Internal
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.RQL.IR.Delete
|
|
|
|
import Hasura.RQL.IR.Insert
|
|
|
|
import Hasura.RQL.IR.Returning
|
|
|
|
import Hasura.RQL.IR.Select
|
|
|
|
import Hasura.RQL.IR.Update
|
2020-11-10 10:50:26 +03:00
|
|
|
import Hasura.RQL.Instances ()
|
2019-03-07 13:24:07 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-11-10 10:50:26 +03:00
|
|
|
import Hasura.SQL.Types
|
2020-10-29 19:58:13 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2020-10-21 19:35:06 +03:00
|
|
|
import Hasura.Session
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
type MutationRemoteJoinCtx = (HTTP.Manager, [N.Header], UserInfo)
|
|
|
|
|
2020-11-12 12:25:48 +03:00
|
|
|
data Mutation (b :: BackendType)
|
2019-03-07 13:24:07 +03:00
|
|
|
= Mutation
|
2020-05-27 18:02:58 +03:00
|
|
|
{ _mTable :: !QualifiedTable
|
2020-11-12 12:25:48 +03:00
|
|
|
, _mQuery :: !(MutationCTE, DS.Seq Q.PrepArg)
|
2020-10-22 23:42:27 +03:00
|
|
|
, _mOutput :: !(MutationOutput b)
|
|
|
|
, _mCols :: ![ColumnInfo b]
|
|
|
|
, _mRemoteJoins :: !(Maybe (RemoteJoins b, MutationRemoteJoinCtx))
|
2020-05-27 18:02:58 +03:00
|
|
|
, _mStrfyNum :: !Bool
|
|
|
|
}
|
|
|
|
|
|
|
|
mkMutation
|
|
|
|
:: Maybe MutationRemoteJoinCtx
|
|
|
|
-> QualifiedTable
|
2020-11-12 12:25:48 +03:00
|
|
|
-> (MutationCTE, DS.Seq Q.PrepArg)
|
2020-10-22 23:42:27 +03:00
|
|
|
-> MutationOutput 'Postgres
|
|
|
|
-> [ColumnInfo 'Postgres]
|
2020-05-27 18:02:58 +03:00
|
|
|
-> Bool
|
2020-10-22 23:42:27 +03:00
|
|
|
-> Mutation 'Postgres
|
2020-05-27 18:02:58 +03:00
|
|
|
mkMutation ctx table query output' allCols strfyNum =
|
|
|
|
let (output, remoteJoins) = getRemoteJoinsMutationOutput output'
|
|
|
|
remoteJoinsCtx = (,) <$> remoteJoins <*> ctx
|
|
|
|
in Mutation table query output allCols remoteJoinsCtx strfyNum
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
runMutation
|
2020-07-14 22:00:58 +03:00
|
|
|
::
|
|
|
|
( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.MonadTrace m
|
2020-07-14 22:00:58 +03:00
|
|
|
)
|
|
|
|
=> Env.Environment
|
2020-10-22 23:42:27 +03:00
|
|
|
-> Mutation 'Postgres
|
2020-07-14 22:00:58 +03:00
|
|
|
-> m EncJSON
|
|
|
|
runMutation env mut =
|
|
|
|
bool (mutateAndReturn env mut) (mutateAndSel env mut) $
|
2020-02-13 20:38:23 +03:00
|
|
|
hasNestedFld $ _mOutput mut
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
mutateAndReturn
|
2020-07-14 22:00:58 +03:00
|
|
|
::
|
|
|
|
( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.MonadTrace m
|
2020-07-14 22:00:58 +03:00
|
|
|
)
|
|
|
|
=> Env.Environment
|
2020-10-22 23:42:27 +03:00
|
|
|
-> Mutation 'Postgres
|
2020-07-14 22:00:58 +03:00
|
|
|
-> m EncJSON
|
|
|
|
mutateAndReturn env (Mutation qt (cte, p) mutationOutput allCols remoteJoins strfyNum) =
|
2020-11-12 12:25:48 +03:00
|
|
|
executeMutationOutputQuery env qt allCols Nothing cte mutationOutput strfyNum (toList p) remoteJoins
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-10-29 19:58:13 +03:00
|
|
|
|
|
|
|
execUpdateQuery
|
|
|
|
::
|
|
|
|
( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
|
|
|
, Tracing.MonadTrace m
|
|
|
|
)
|
|
|
|
=> Env.Environment
|
|
|
|
-> Bool
|
|
|
|
-> Maybe MutationRemoteJoinCtx
|
|
|
|
-> (AnnUpd 'Postgres, DS.Seq Q.PrepArg)
|
|
|
|
-> m EncJSON
|
|
|
|
execUpdateQuery env strfyNum remoteJoinCtx (u, p) =
|
2020-11-12 12:25:48 +03:00
|
|
|
runMutation env $ mkMutation remoteJoinCtx (uqp1Table u) (MCCheckConstraint updateCTE, p)
|
2020-10-29 19:58:13 +03:00
|
|
|
(uqp1Output u) (uqp1AllCols u) strfyNum
|
|
|
|
where
|
|
|
|
updateCTE = mkUpdateCTE u
|
|
|
|
|
|
|
|
execDeleteQuery
|
|
|
|
::
|
|
|
|
( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
|
|
|
, Tracing.MonadTrace m
|
|
|
|
)
|
|
|
|
=> Env.Environment
|
|
|
|
-> Bool
|
|
|
|
-> Maybe MutationRemoteJoinCtx
|
|
|
|
-> (AnnDel 'Postgres, DS.Seq Q.PrepArg)
|
|
|
|
-> m EncJSON
|
|
|
|
execDeleteQuery env strfyNum remoteJoinCtx (u, p) =
|
2020-11-12 12:25:48 +03:00
|
|
|
runMutation env $ mkMutation remoteJoinCtx (dqp1Table u) (MCDelete delete, p)
|
2020-10-29 19:58:13 +03:00
|
|
|
(dqp1Output u) (dqp1AllCols u) strfyNum
|
|
|
|
where
|
2020-11-12 12:25:48 +03:00
|
|
|
delete = mkDelete u
|
2020-10-29 19:58:13 +03:00
|
|
|
|
|
|
|
execInsertQuery
|
|
|
|
:: ( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
|
|
|
, Tracing.MonadTrace m
|
|
|
|
)
|
|
|
|
=> Env.Environment
|
|
|
|
-> Bool
|
|
|
|
-> Maybe MutationRemoteJoinCtx
|
|
|
|
-> (InsertQueryP1 'Postgres, DS.Seq Q.PrepArg)
|
|
|
|
-> m EncJSON
|
|
|
|
execInsertQuery env strfyNum remoteJoinCtx (u, p) =
|
|
|
|
runMutation env
|
2020-11-12 12:25:48 +03:00
|
|
|
$ mkMutation remoteJoinCtx (iqp1Table u) (MCCheckConstraint insertCTE, p)
|
2020-10-29 19:58:13 +03:00
|
|
|
(iqp1Output u) (iqp1AllCols u) strfyNum
|
|
|
|
where
|
|
|
|
insertCTE = mkInsertCTE u
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-01 15:14:19 +03:00
|
|
|
{- Note: [Prepared statements in Mutations]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
The SQL statements we generate for mutations seem to include the actual values
|
|
|
|
in the statements in some cases which pretty much makes them unfit for reuse
|
|
|
|
(Handling relationships in the returning clause is the source of this
|
|
|
|
complexity). Further, `PGConn` has an internal cache which maps a statement to
|
|
|
|
a 'prepared statement id' on Postgres. As we prepare more and more single-use
|
|
|
|
SQL statements we end up leaking memory both on graphql-engine and Postgres
|
|
|
|
till the connection is closed. So a simpler but very crude fix is to not use
|
|
|
|
prepared statements for mutations. The performance of insert mutations
|
|
|
|
shouldn't be affected but updates and delete mutations with complex boolean
|
|
|
|
conditions **might** see some degradation.
|
|
|
|
-}
|
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
mutateAndSel
|
2020-07-14 22:00:58 +03:00
|
|
|
::
|
|
|
|
( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.MonadTrace m
|
2020-07-14 22:00:58 +03:00
|
|
|
)
|
|
|
|
=> Env.Environment
|
2020-10-22 23:42:27 +03:00
|
|
|
-> Mutation 'Postgres
|
2020-07-14 22:00:58 +03:00
|
|
|
-> m EncJSON
|
|
|
|
mutateAndSel env (Mutation qt q mutationOutput allCols remoteJoins strfyNum) = do
|
2019-03-07 13:24:07 +03:00
|
|
|
-- Perform mutation and fetch unique columns
|
2020-05-27 18:02:58 +03:00
|
|
|
MutateResp _ columnVals <- liftTx $ mutateAndFetchCols qt allCols q strfyNum
|
2020-11-12 12:25:48 +03:00
|
|
|
select <- mkSelectExpFromColumnValues qt allCols columnVals
|
2019-03-07 13:24:07 +03:00
|
|
|
-- Perform select query and fetch returning fields
|
2020-11-12 12:25:48 +03:00
|
|
|
executeMutationOutputQuery env qt allCols Nothing
|
|
|
|
(MCSelectValues select) mutationOutput strfyNum [] remoteJoins
|
|
|
|
|
|
|
|
withCheckPermission :: (MonadError QErr m) => m (a, Bool) -> m a
|
|
|
|
withCheckPermission sqlTx = do
|
|
|
|
(rawResponse, checkConstraint) <- sqlTx
|
|
|
|
unless checkConstraint $ throw400 PermissionError $
|
|
|
|
"check constraint of an insert/update permission has failed"
|
|
|
|
pure rawResponse
|
2020-05-27 18:02:58 +03:00
|
|
|
|
|
|
|
executeMutationOutputQuery
|
2020-11-12 12:25:48 +03:00
|
|
|
:: forall m.
|
2020-07-14 22:00:58 +03:00
|
|
|
( HasVersion
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
2020-07-15 13:40:48 +03:00
|
|
|
, Tracing.MonadTrace m
|
2020-07-14 22:00:58 +03:00
|
|
|
)
|
|
|
|
=> Env.Environment
|
2020-11-12 12:25:48 +03:00
|
|
|
-> QualifiedTable
|
|
|
|
-> [ColumnInfo 'Postgres]
|
|
|
|
-> Maybe Int
|
|
|
|
-> MutationCTE
|
|
|
|
-> MutationOutput 'Postgres
|
|
|
|
-> Bool
|
2020-05-27 18:02:58 +03:00
|
|
|
-> [Q.PrepArg] -- ^ Prepared params
|
2020-10-22 23:42:27 +03:00
|
|
|
-> Maybe (RemoteJoins 'Postgres, MutationRemoteJoinCtx) -- ^ Remote joins context
|
2020-05-27 18:02:58 +03:00
|
|
|
-> m EncJSON
|
2020-11-12 12:25:48 +03:00
|
|
|
executeMutationOutputQuery env qt allCols preCalAffRows cte mutOutput strfyNum prepArgs maybeRJ = do
|
|
|
|
let queryTx :: Q.FromRes a => m a
|
|
|
|
queryTx = do
|
|
|
|
let selectWith = mkMutationOutputExp qt allCols preCalAffRows cte mutOutput strfyNum
|
|
|
|
query = Q.fromBuilder $ toSQL selectWith
|
|
|
|
-- See Note [Prepared statements in Mutations]
|
|
|
|
liftTx (Q.rawQE dmlTxErrorHandler query prepArgs False)
|
|
|
|
|
|
|
|
rawResponse <-
|
|
|
|
if checkPermissionRequired cte
|
|
|
|
then withCheckPermission $ Q.getRow <$> queryTx
|
|
|
|
else (runIdentity . Q.getRow) <$> queryTx
|
|
|
|
case maybeRJ of
|
|
|
|
Nothing -> pure $ encJFromLBS rawResponse
|
|
|
|
Just (remoteJoins, (httpManager, reqHeaders, userInfo)) ->
|
|
|
|
processRemoteJoins env httpManager reqHeaders userInfo rawResponse remoteJoins
|
2019-03-07 13:24:07 +03:00
|
|
|
|
|
|
|
mutateAndFetchCols
|
2020-02-04 18:34:17 +03:00
|
|
|
:: QualifiedTable
|
2020-10-22 23:42:27 +03:00
|
|
|
-> [ColumnInfo 'Postgres]
|
2020-11-12 12:25:48 +03:00
|
|
|
-> (MutationCTE, DS.Seq Q.PrepArg)
|
2019-03-07 13:24:07 +03:00
|
|
|
-> Bool
|
2020-02-04 18:34:17 +03:00
|
|
|
-> Q.TxE QErr (MutateResp TxtEncodedPGVal)
|
2020-11-12 12:25:48 +03:00
|
|
|
mutateAndFetchCols qt cols (cte, p) strfyNum = do
|
|
|
|
let mutationTx :: Q.FromRes a => Q.TxE QErr a
|
|
|
|
mutationTx =
|
|
|
|
-- See Note [Prepared statements in Mutations]
|
|
|
|
Q.rawQE dmlTxErrorHandler sqlText (toList p) False
|
|
|
|
|
|
|
|
if checkPermissionRequired cte
|
|
|
|
then withCheckPermission $ (first Q.getAltJ . Q.getRow) <$> mutationTx
|
|
|
|
else (Q.getAltJ . runIdentity . Q.getRow) <$> mutationTx
|
2019-03-07 13:24:07 +03:00
|
|
|
where
|
2020-10-27 13:34:31 +03:00
|
|
|
aliasIdentifier = Identifier $ qualifiedObjectToText qt <> "__mutation_result"
|
|
|
|
tabFrom = FromIdentifier aliasIdentifier
|
2019-03-07 13:24:07 +03:00
|
|
|
tabPerm = TablePerm annBoolExpTrue Nothing
|
|
|
|
selFlds = flip map cols $
|
2020-06-08 15:13:01 +03:00
|
|
|
\ci -> (fromPGCol $ pgiColumn ci, mkAnnColumnFieldAsText ci)
|
2019-03-07 13:24:07 +03:00
|
|
|
|
2020-11-12 12:25:48 +03:00
|
|
|
sqlText = Q.fromBuilder $ toSQL selectWith
|
|
|
|
selectWith = S.SelectWith [(S.Alias aliasIdentifier, getMutationCTE cte)] select
|
|
|
|
select = S.mkSelect { S.selExtr = S.Extractor extrExp Nothing
|
|
|
|
: bool [] [S.Extractor checkErrExp Nothing] (checkPermissionRequired cte)
|
|
|
|
}
|
|
|
|
checkErrExp = mkCheckErrorExp aliasIdentifier
|
2019-03-07 13:24:07 +03:00
|
|
|
extrExp = S.applyJsonBuildObj
|
|
|
|
[ S.SELit "affected_rows", affRowsSel
|
|
|
|
, S.SELit "returning_columns", colSel
|
|
|
|
]
|
|
|
|
|
|
|
|
affRowsSel = S.SESelect $
|
|
|
|
S.mkSelect
|
|
|
|
{ S.selExtr = [S.Extractor S.countStar Nothing]
|
2020-10-27 13:34:31 +03:00
|
|
|
, S.selFrom = Just $ S.FromExp [S.FIIdentifier aliasIdentifier]
|
2019-03-07 13:24:07 +03:00
|
|
|
}
|
2020-02-13 20:38:23 +03:00
|
|
|
colSel = S.SESelect $ mkSQLSelect JASMultipleRows $
|
2020-06-08 15:13:01 +03:00
|
|
|
AnnSelectG selFlds tabFrom tabPerm noSelectArgs strfyNum
|