2022-01-06 12:49:03 +03:00
|
|
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
|
|
|
|
|
|
-- | Responsible for translating and building an MSSQL execution plan for
|
|
|
|
-- update mutations.
|
|
|
|
--
|
|
|
|
-- This module is used by "Hasura.Backends.MSSQL.Instances.Execute".
|
|
|
|
module Hasura.Backends.MSSQL.Execute.Update
|
|
|
|
( executeUpdate,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Database.MSSQL.Transaction qualified as Tx
|
|
|
|
import Hasura.Backends.MSSQL.Connection
|
2022-04-28 22:33:33 +03:00
|
|
|
import Hasura.Backends.MSSQL.Execute.QueryTags
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.Backends.MSSQL.FromIr as TSQL
|
2022-03-10 13:33:55 +03:00
|
|
|
import Hasura.Backends.MSSQL.FromIr.Constants (tempTableNameUpdated)
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.Expression (fromGBoolExp)
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.MutationResponse
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.SelectIntoTempTable qualified as TSQL
|
|
|
|
import Hasura.Backends.MSSQL.FromIr.Update qualified as TSQL
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.Backends.MSSQL.Plan
|
2022-02-07 17:11:49 +03:00
|
|
|
import Hasura.Backends.MSSQL.SQL.Error
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.Backends.MSSQL.ToQuery as TQ
|
|
|
|
import Hasura.Backends.MSSQL.Types.Internal as TSQL
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
import Hasura.GraphQL.Execute.Backend
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.Prelude
|
2022-04-28 22:33:33 +03:00
|
|
|
import Hasura.QueryTags (QueryTagsComment)
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.RQL.IR
|
|
|
|
import Hasura.RQL.IR qualified as IR
|
2023-01-10 04:54:40 +03:00
|
|
|
import Hasura.RQL.IR.Update.Batch qualified as IR
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
2023-04-24 21:35:48 +03:00
|
|
|
import Hasura.RQL.Types.BackendType
|
2023-04-24 18:17:15 +03:00
|
|
|
import Hasura.RQL.Types.Schema.Options qualified as Options
|
2022-01-06 12:49:03 +03:00
|
|
|
import Hasura.Session
|
|
|
|
|
|
|
|
-- | Executes an Update IR AST and return results as JSON.
|
|
|
|
executeUpdate ::
|
2022-04-28 22:33:33 +03:00
|
|
|
(MonadError QErr m, MonadReader QueryTagsComment m) =>
|
2022-01-06 12:49:03 +03:00
|
|
|
UserInfo ->
|
2022-07-14 20:57:28 +03:00
|
|
|
Options.StringifyNumbers ->
|
2022-01-06 12:49:03 +03:00
|
|
|
SourceConfig 'MSSQL ->
|
|
|
|
AnnotatedUpdateG 'MSSQL Void (UnpreparedValue 'MSSQL) ->
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
m (OnBaseMonad (ExceptT QErr) EncJSON)
|
2022-01-06 12:49:03 +03:00
|
|
|
executeUpdate userInfo stringifyNum sourceConfig updateOperation = do
|
2022-04-28 22:33:33 +03:00
|
|
|
queryTags <- ask
|
2022-01-06 12:49:03 +03:00
|
|
|
let mssqlExecCtx = (_mscExecCtx sourceConfig)
|
|
|
|
preparedUpdate <- traverse (prepareValueQuery $ _uiSession userInfo) updateOperation
|
2023-01-10 04:54:40 +03:00
|
|
|
if IR.updateBatchIsEmpty $ _auUpdateVariant updateOperation
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
then pure $ OnBaseMonad $ pure $ IR.buildEmptyMutResp $ _auOutput preparedUpdate
|
|
|
|
else pure $ OnBaseMonad $ (mssqlRunReadWrite mssqlExecCtx) (buildUpdateTx preparedUpdate stringifyNum queryTags)
|
2022-01-06 12:49:03 +03:00
|
|
|
|
|
|
|
-- | Converts an Update IR AST to a transaction of three update sql statements.
|
|
|
|
--
|
|
|
|
-- A GraphQL update mutation does two things:
|
|
|
|
--
|
|
|
|
-- 1. Update rows in a table according to some predicate
|
|
|
|
-- 2. (Potentially) returns the updated rows (including relationships) as JSON
|
|
|
|
--
|
|
|
|
-- In order to complete these 2 things we need 3 SQL statements:
|
|
|
|
--
|
|
|
|
-- 1. @SELECT INTO <temp_table> WHERE <false>@ - creates a temporary table
|
|
|
|
-- with the same schema as the original table in which we'll store the updated rows
|
|
|
|
-- from the table we are deleting
|
|
|
|
-- 2. @UPDATE SET FROM with OUTPUT@ - updates the rows from the table and inserts the
|
|
|
|
-- updated rows to the temporary table from (1)
|
|
|
|
-- 3. @SELECT@ - constructs the @returning@ query from the temporary table, including
|
|
|
|
-- relationships with other tables.
|
|
|
|
buildUpdateTx ::
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
(MonadIO m) =>
|
2022-01-06 12:49:03 +03:00
|
|
|
AnnotatedUpdate 'MSSQL ->
|
2022-07-14 20:57:28 +03:00
|
|
|
Options.StringifyNumbers ->
|
2022-04-28 22:33:33 +03:00
|
|
|
QueryTagsComment ->
|
Allow backend execution to happen on the base app monad.
### Description
Each Backend executes queries against the database in a slightly different stack: Postgres uses its own `TXeT`, MSSQL uses a variant of it, BigQuery is simply in `ExceptT QErr IO`... To accommodate those variations, we had originally introduced an `ExecutionMonad b` type family in `BackendExecute`, allowing each backend to describe its own stack. It was then up to that backend's `BackendTransport` instance to implement running said stack, and converting the result back into our main app monad.
However, this was not without complications: `TraceT` is one of them: as it usually needs to be on the top of the stack, converting from one stack to the other implies the use `interpTraceT`, which is quite monstrous. Furthermore, as part of the Entitlement Services work, we're trying to move to a "Services" architecture in which the entire engine runs in one base monad, that delegates features and dependencies to monad constraints; and as a result we'd like to minimize the number of different monad stacks we have to maintain and translate from and to in the codebase.
To improve things, this PR changes `ExecutionMonad b` from an _absolute_ stack to a _relative_ one: i.e.: what needs to be stacked on top of our base monad for the execution. In `Transport`, we then only need to pop the top of the stack, and voila. This greatly simplifies the implementation of the backends, as there's no longer any need to do any stack transformation: MySQL's implementation becomes a `runIdentityT`! This also removes most mentions of `TraceT` from the execution code since it's no longer required: we can rely on the base monad's existing `MonadTrace` constraint.
To continue encapsulating monadic actions in `DBStepInfo` and avoid threading a bunch of `forall` all over the place, this PR introduces a small local helper: `OnBaseMonad`. One only downside of all this is that this requires adding `MonadBaseControl IO m` constraint all over the place: previously, we would run directly on `IO` and lift, and would therefore not need to bring that constraint all the way.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7789
GitOrigin-RevId: e9b2e431c5c47fa9851abf87545c0415ff6d1a12
2023-02-09 17:38:33 +03:00
|
|
|
Tx.TxET QErr m EncJSON
|
2022-04-28 22:33:33 +03:00
|
|
|
buildUpdateTx updateOperation stringifyNum queryTags = do
|
2022-01-06 12:49:03 +03:00
|
|
|
let withAlias = "with_alias"
|
|
|
|
createInsertedTempTableQuery =
|
2023-05-24 16:51:56 +03:00
|
|
|
toQueryFlat
|
|
|
|
$ TQ.fromSelectIntoTempTable
|
|
|
|
$ TSQL.toSelectIntoTempTable tempTableNameUpdated (_auTable updateOperation) (_auAllCols updateOperation) RemoveConstraints
|
2022-01-06 12:49:03 +03:00
|
|
|
-- Create a temp table
|
2022-04-28 22:33:33 +03:00
|
|
|
Tx.unitQueryE defaultMSSQLTxErrorHandler (createInsertedTempTableQuery `withQueryTags` queryTags)
|
2022-01-06 12:49:03 +03:00
|
|
|
let updateQuery = TQ.fromUpdate <$> TSQL.fromUpdate updateOperation
|
2023-04-26 00:04:29 +03:00
|
|
|
updateQueryValidated <- toQueryFlat . qwdQuery <$> runFromIrErrorOnCTEs updateQuery
|
2023-03-27 19:54:27 +03:00
|
|
|
|
2022-01-06 12:49:03 +03:00
|
|
|
-- Execute UPDATE statement
|
2022-04-28 22:33:33 +03:00
|
|
|
Tx.unitQueryE mutationMSSQLTxErrorHandler (updateQueryValidated `withQueryTags` queryTags)
|
2023-04-26 00:04:29 +03:00
|
|
|
mutationOutputSelect <- qwdQuery <$> runFromIrUseCTEs (mkMutationOutputSelect stringifyNum withAlias $ _auOutput updateOperation)
|
2022-01-06 12:49:03 +03:00
|
|
|
let checkCondition = _auCheck updateOperation
|
2023-03-27 19:54:27 +03:00
|
|
|
|
2022-01-06 12:49:03 +03:00
|
|
|
-- The check constraint is translated to boolean expression
|
2023-04-26 00:04:29 +03:00
|
|
|
checkBoolExp <- qwdQuery <$> runFromIrErrorOnCTEs (runReaderT (fromGBoolExp checkCondition) (EntityAlias withAlias))
|
2022-01-06 12:49:03 +03:00
|
|
|
|
|
|
|
let withSelect =
|
|
|
|
emptySelect
|
|
|
|
{ selectProjections = [StarProjection],
|
|
|
|
selectFrom = Just $ FromTempTable $ Aliased tempTableNameUpdated "updated_alias"
|
|
|
|
}
|
|
|
|
mutationOutputCheckConstraintSelect = selectMutationOutputAndCheckCondition withAlias mutationOutputSelect checkBoolExp
|
2023-04-19 17:52:43 +03:00
|
|
|
finalSelect = mutationOutputCheckConstraintSelect {selectWith = Just $ With $ pure $ Aliased (CTESelect withSelect) withAlias}
|
2022-01-06 12:49:03 +03:00
|
|
|
|
|
|
|
-- Execute SELECT query to fetch mutation response and check constraint result
|
2022-04-28 22:33:33 +03:00
|
|
|
let finalSelectQuery = toQueryFlat $ TQ.fromSelect finalSelect
|
|
|
|
(responseText, checkConditionInt) <- Tx.singleRowQueryE defaultMSSQLTxErrorHandler (finalSelectQuery `withQueryTags` queryTags)
|
2022-01-06 12:49:03 +03:00
|
|
|
-- Drop the temp table
|
2022-04-28 22:33:33 +03:00
|
|
|
Tx.unitQueryE defaultMSSQLTxErrorHandler (toQueryFlat (dropTempTableQuery tempTableNameUpdated) `withQueryTags` queryTags)
|
2022-01-06 12:49:03 +03:00
|
|
|
-- Raise an exception if the check condition is not met
|
2023-05-24 16:51:56 +03:00
|
|
|
unless (checkConditionInt == (0 :: Int))
|
|
|
|
$ throw400 PermissionError "check constraint of an insert/update permission has failed"
|
2022-01-06 12:49:03 +03:00
|
|
|
pure $ encJFromText responseText
|