2018-12-13 10:26:15 +03:00
|
|
|
module Hasura.RQL.DDL.RemoteSchema
|
|
|
|
( runAddRemoteSchema
|
|
|
|
, runRemoveRemoteSchema
|
2019-04-17 19:29:39 +03:00
|
|
|
, removeRemoteSchemaFromCatalog
|
2019-07-08 08:51:41 +03:00
|
|
|
, runReloadRemoteSchema
|
2019-04-17 19:29:39 +03:00
|
|
|
, addRemoteSchemaP1
|
2019-07-08 08:51:41 +03:00
|
|
|
, addRemoteSchemaP2Setup
|
2018-12-13 10:26:15 +03:00
|
|
|
, addRemoteSchemaP2
|
|
|
|
) where
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2018-11-23 16:02:46 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
|
|
|
|
import Hasura.GraphQL.RemoteServer
|
|
|
|
import Hasura.RQL.Types
|
2020-01-23 00:55:55 +03:00
|
|
|
import Hasura.Server.Version (HasVersion)
|
2019-07-08 08:51:41 +03:00
|
|
|
import Hasura.SQL.Types
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runAddRemoteSchema
|
2020-01-23 00:55:55 +03:00
|
|
|
:: ( HasVersion
|
|
|
|
, QErrM m
|
2019-11-26 15:14:21 +03:00
|
|
|
, CacheRWM m
|
|
|
|
, MonadTx m
|
|
|
|
, MonadIO m
|
|
|
|
, HasHttpManager m
|
2018-12-13 10:26:15 +03:00
|
|
|
)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> AddRemoteSchemaQuery -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
runAddRemoteSchema q = do
|
2019-11-20 21:21:30 +03:00
|
|
|
addRemoteSchemaP1 name
|
|
|
|
addRemoteSchemaP2 q
|
|
|
|
buildSchemaCacheFor $ MORemoteSchema name
|
|
|
|
pure successMsg
|
2019-07-08 08:51:41 +03:00
|
|
|
where
|
|
|
|
name = _arsqName q
|
2019-04-17 19:29:39 +03:00
|
|
|
|
|
|
|
addRemoteSchemaP1
|
2019-11-26 15:14:21 +03:00
|
|
|
:: (QErrM m, CacheRM m)
|
2019-07-08 08:51:41 +03:00
|
|
|
=> RemoteSchemaName -> m ()
|
|
|
|
addRemoteSchemaP1 name = do
|
|
|
|
remoteSchemaMap <- scRemoteSchemas <$> askSchemaCache
|
|
|
|
onJust (Map.lookup name remoteSchemaMap) $ const $
|
|
|
|
throw400 AlreadyExists $ "remote schema with name "
|
|
|
|
<> name <<> " already exists"
|
|
|
|
|
|
|
|
addRemoteSchemaP2Setup
|
2020-01-23 00:55:55 +03:00
|
|
|
:: (HasVersion, QErrM m, MonadIO m, HasHttpManager m)
|
2019-07-08 08:51:41 +03:00
|
|
|
=> AddRemoteSchemaQuery -> m RemoteSchemaCtx
|
2019-11-20 21:21:30 +03:00
|
|
|
addRemoteSchemaP2Setup (AddRemoteSchemaQuery name def _) = do
|
2019-04-17 19:29:39 +03:00
|
|
|
httpMgr <- askHttpManager
|
|
|
|
rsi <- validateRemoteSchemaDef def
|
2019-07-08 08:51:41 +03:00
|
|
|
gCtx <- fetchRemoteSchema httpMgr name rsi
|
2019-11-20 21:21:30 +03:00
|
|
|
pure $ RemoteSchemaCtx name gCtx rsi
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2020-01-23 00:55:55 +03:00
|
|
|
addRemoteSchemaP2
|
|
|
|
:: (HasVersion, MonadTx m, MonadIO m, HasHttpManager m) => AddRemoteSchemaQuery -> m ()
|
2019-07-08 08:51:41 +03:00
|
|
|
addRemoteSchemaP2 q = do
|
|
|
|
void $ addRemoteSchemaP2Setup q
|
2018-11-23 16:02:46 +03:00
|
|
|
liftTx $ addRemoteSchemaToCatalog q
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runRemoveRemoteSchema
|
2019-04-17 19:29:39 +03:00
|
|
|
:: (QErrM m, UserInfoM m, CacheRWM m, MonadTx m)
|
2019-07-08 08:51:41 +03:00
|
|
|
=> RemoteSchemaNameQuery -> m EncJSON
|
2019-11-20 21:21:30 +03:00
|
|
|
runRemoveRemoteSchema (RemoteSchemaNameQuery rsn) = do
|
2019-04-17 19:29:39 +03:00
|
|
|
removeRemoteSchemaP1 rsn
|
2019-11-20 21:21:30 +03:00
|
|
|
liftTx $ removeRemoteSchemaFromCatalog rsn
|
|
|
|
withNewInconsistentObjsCheck buildSchemaCache
|
|
|
|
pure successMsg
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
removeRemoteSchemaP1
|
2019-04-17 19:29:39 +03:00
|
|
|
:: (UserInfoM m, QErrM m, CacheRM m)
|
|
|
|
=> RemoteSchemaName -> m ()
|
|
|
|
removeRemoteSchemaP1 rsn = do
|
|
|
|
sc <- askSchemaCache
|
2019-07-08 08:51:41 +03:00
|
|
|
let rmSchemas = scRemoteSchemas sc
|
|
|
|
void $ onNothing (Map.lookup rsn rmSchemas) $
|
|
|
|
throw400 NotExists "no such remote schema"
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-07-08 08:51:41 +03:00
|
|
|
runReloadRemoteSchema
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (QErrM m, CacheRWM m)
|
2019-07-08 08:51:41 +03:00
|
|
|
=> RemoteSchemaNameQuery -> m EncJSON
|
|
|
|
runReloadRemoteSchema (RemoteSchemaNameQuery name) = do
|
|
|
|
rmSchemas <- scRemoteSchemas <$> askSchemaCache
|
2019-11-20 21:21:30 +03:00
|
|
|
void $ onNothing (Map.lookup name rmSchemas) $
|
|
|
|
throw400 NotExists $ "remote schema with name " <> name <<> " does not exist"
|
2019-07-08 08:51:41 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
invalidateCachedRemoteSchema name
|
|
|
|
withNewInconsistentObjsCheck buildSchemaCache
|
|
|
|
pure successMsg
|
2018-11-23 16:02:46 +03:00
|
|
|
|
|
|
|
addRemoteSchemaToCatalog
|
|
|
|
:: AddRemoteSchemaQuery
|
|
|
|
-> Q.TxE QErr ()
|
|
|
|
addRemoteSchemaToCatalog (AddRemoteSchemaQuery name def comment) =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
INSERT into hdb_catalog.remote_schemas
|
|
|
|
(name, definition, comment)
|
|
|
|
VALUES ($1, $2, $3)
|
|
|
|
|] (name, Q.AltJ $ J.toJSON def, comment) True
|
|
|
|
|
2019-04-17 19:29:39 +03:00
|
|
|
removeRemoteSchemaFromCatalog :: RemoteSchemaName -> Q.TxE QErr ()
|
2018-11-23 16:02:46 +03:00
|
|
|
removeRemoteSchemaFromCatalog name =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
DELETE FROM hdb_catalog.remote_schemas
|
|
|
|
WHERE name = $1
|
|
|
|
|] (Identity name) True
|