2019-05-16 09:13:25 +03:00
|
|
|
module Hasura.RQL.DDL.QueryCollection
|
|
|
|
( runCreateCollection
|
|
|
|
, runDropCollection
|
|
|
|
, addCollectionToCatalog
|
|
|
|
, delCollectionFromCatalog
|
|
|
|
, runAddQueryToCollection
|
|
|
|
, runDropQueryFromCollection
|
|
|
|
, runAddCollectionToAllowlist
|
|
|
|
, runDropCollectionFromAllowlist
|
|
|
|
, addCollectionToAllowlistCatalog
|
|
|
|
, delCollectionFromAllowlistCatalog
|
|
|
|
, fetchAllCollections
|
|
|
|
, fetchAllowlist
|
|
|
|
, module Hasura.RQL.Types.QueryCollection
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.RQL.Types.QueryCollection
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
2020-05-27 18:02:58 +03:00
|
|
|
import Data.List.Extended (duplicates)
|
|
|
|
|
2019-05-16 09:13:25 +03:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Data.Text.Extended as T
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
|
|
|
|
addCollectionP2
|
|
|
|
:: (QErrM m)
|
|
|
|
=> CollectionDef -> m ()
|
|
|
|
addCollectionP2 (CollectionDef queryList) =
|
|
|
|
withPathK "queries" $
|
|
|
|
unless (null duplicateNames) $ throw400 NotSupported $
|
|
|
|
"found duplicate query names "
|
2020-05-27 18:02:58 +03:00
|
|
|
<> T.intercalate ", " (map (T.dquote . unNonEmptyText . unQueryName) $ toList duplicateNames)
|
2019-05-16 09:13:25 +03:00
|
|
|
where
|
|
|
|
duplicateNames = duplicates $ map _lqName queryList
|
|
|
|
|
|
|
|
runCreateCollection
|
2019-11-26 15:14:21 +03:00
|
|
|
:: (QErrM m, MonadTx m, HasSystemDefined m)
|
2019-05-16 09:13:25 +03:00
|
|
|
=> CreateCollection -> m EncJSON
|
|
|
|
runCreateCollection cc = do
|
|
|
|
collDetM <- getCollectionDefM collName
|
|
|
|
withPathK "name" $
|
|
|
|
onJust collDetM $ const $ throw400 AlreadyExists $
|
|
|
|
"query collection with name " <> collName <<> " already exists"
|
|
|
|
withPathK "definition" $ addCollectionP2 def
|
2019-10-21 19:01:05 +03:00
|
|
|
systemDefined <- askSystemDefined
|
|
|
|
liftTx $ addCollectionToCatalog cc systemDefined
|
2019-05-16 09:13:25 +03:00
|
|
|
return successMsg
|
|
|
|
where
|
|
|
|
CreateCollection collName def _ = cc
|
|
|
|
|
|
|
|
runAddQueryToCollection
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (CacheRWM m, MonadTx m)
|
2019-05-16 09:13:25 +03:00
|
|
|
=> AddQueryToCollection -> m EncJSON
|
|
|
|
runAddQueryToCollection (AddQueryToCollection collName queryName query) = do
|
|
|
|
CollectionDef qList <- getCollectionDef collName
|
|
|
|
let queryExists = flip any qList $ \q -> _lqName q == queryName
|
|
|
|
|
|
|
|
when queryExists $ throw400 AlreadyExists $ "query with name "
|
|
|
|
<> queryName <<> " already exists in collection " <>> collName
|
|
|
|
|
|
|
|
let collDef = CollectionDef $ qList <> pure listQ
|
2019-11-20 21:21:30 +03:00
|
|
|
liftTx $ updateCollectionDefCatalog collName collDef
|
|
|
|
withNewInconsistentObjsCheck buildSchemaCache
|
2019-05-16 09:13:25 +03:00
|
|
|
return successMsg
|
|
|
|
where
|
|
|
|
listQ = ListedQuery queryName query
|
|
|
|
|
|
|
|
runDropCollection
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (MonadTx m, CacheRWM m)
|
2019-05-16 09:13:25 +03:00
|
|
|
=> DropCollection -> m EncJSON
|
|
|
|
runDropCollection (DropCollection collName cascade) = do
|
|
|
|
withPathK "collection" $ do
|
|
|
|
-- check for query collection
|
|
|
|
void $ getCollectionDef collName
|
|
|
|
|
|
|
|
allowlist <- liftTx fetchAllowlist
|
|
|
|
when (collName `elem` allowlist) $
|
|
|
|
if cascade then do
|
|
|
|
-- drop collection in allowlist
|
|
|
|
liftTx $ delCollectionFromAllowlistCatalog collName
|
2019-11-20 21:21:30 +03:00
|
|
|
withNewInconsistentObjsCheck buildSchemaCache
|
2019-05-16 09:13:25 +03:00
|
|
|
else throw400 DependencyError $ "query collection with name "
|
|
|
|
<> collName <<> " is present in allowlist; cannot proceed to drop"
|
|
|
|
liftTx $ delCollectionFromCatalog collName
|
|
|
|
return successMsg
|
|
|
|
|
|
|
|
runDropQueryFromCollection
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (CacheRWM m, MonadTx m)
|
2019-05-16 09:13:25 +03:00
|
|
|
=> DropQueryFromCollection -> m EncJSON
|
|
|
|
runDropQueryFromCollection (DropQueryFromCollection collName queryName) = do
|
|
|
|
CollectionDef qList <- getCollectionDef collName
|
|
|
|
let queryExists = flip any qList $ \q -> _lqName q == queryName
|
|
|
|
when (not queryExists) $ throw400 NotFound $ "query with name "
|
|
|
|
<> queryName <<> " not found in collection " <>> collName
|
|
|
|
let collDef = CollectionDef $ flip filter qList $
|
|
|
|
\q -> _lqName q /= queryName
|
2019-11-20 21:21:30 +03:00
|
|
|
liftTx $ updateCollectionDefCatalog collName collDef
|
|
|
|
withNewInconsistentObjsCheck buildSchemaCache
|
2019-05-16 09:13:25 +03:00
|
|
|
return successMsg
|
|
|
|
|
|
|
|
runAddCollectionToAllowlist
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (MonadTx m, CacheRWM m)
|
2019-05-16 09:13:25 +03:00
|
|
|
=> CollectionReq -> m EncJSON
|
|
|
|
runAddCollectionToAllowlist (CollectionReq collName) = do
|
|
|
|
void $ withPathK "collection" $ getCollectionDef collName
|
|
|
|
liftTx $ addCollectionToAllowlistCatalog collName
|
2019-11-20 21:21:30 +03:00
|
|
|
withNewInconsistentObjsCheck buildSchemaCache
|
2019-05-16 09:13:25 +03:00
|
|
|
return successMsg
|
|
|
|
|
|
|
|
runDropCollectionFromAllowlist
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (UserInfoM m, MonadTx m, CacheRWM m)
|
2019-05-16 09:13:25 +03:00
|
|
|
=> CollectionReq -> m EncJSON
|
|
|
|
runDropCollectionFromAllowlist (CollectionReq collName) = do
|
|
|
|
void $ withPathK "collection" $ getCollectionDef collName
|
|
|
|
liftTx $ delCollectionFromAllowlistCatalog collName
|
2019-11-20 21:21:30 +03:00
|
|
|
withNewInconsistentObjsCheck buildSchemaCache
|
2019-05-16 09:13:25 +03:00
|
|
|
return successMsg
|
|
|
|
|
|
|
|
getCollectionDef
|
|
|
|
:: (QErrM m, MonadTx m)
|
|
|
|
=> CollectionName -> m CollectionDef
|
|
|
|
getCollectionDef collName = do
|
|
|
|
detM <- getCollectionDefM collName
|
|
|
|
onNothing detM $ throw400 NotExists $
|
|
|
|
"query collection with name " <> collName <<> " does not exists"
|
|
|
|
|
|
|
|
getCollectionDefM
|
|
|
|
:: (QErrM m, MonadTx m)
|
|
|
|
=> CollectionName -> m (Maybe CollectionDef)
|
|
|
|
getCollectionDefM collName = do
|
|
|
|
allCollections <- liftTx fetchAllCollections
|
|
|
|
let filteredList = flip filter allCollections $ \c -> collName == _ccName c
|
|
|
|
case filteredList of
|
|
|
|
[] -> return Nothing
|
|
|
|
[a] -> return $ Just $ _ccDefinition a
|
|
|
|
_ -> throw500 "more than one row returned for query collections"
|
|
|
|
|
|
|
|
-- Database functions
|
|
|
|
fetchAllCollections :: Q.TxE QErr [CreateCollection]
|
|
|
|
fetchAllCollections = do
|
|
|
|
r <- Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
SELECT collection_name, collection_defn::json, comment
|
|
|
|
FROM hdb_catalog.hdb_query_collection
|
|
|
|
|] () False
|
|
|
|
return $ flip map r $ \(name, Q.AltJ defn, mComment)
|
|
|
|
-> CreateCollection name defn mComment
|
|
|
|
|
|
|
|
fetchAllowlist :: Q.TxE QErr [CollectionName]
|
|
|
|
fetchAllowlist = map runIdentity <$>
|
|
|
|
Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
SELECT collection_name
|
|
|
|
FROM hdb_catalog.hdb_allowlist
|
|
|
|
|] () True
|
|
|
|
|
2019-10-21 19:01:05 +03:00
|
|
|
addCollectionToCatalog :: CreateCollection -> SystemDefined -> Q.TxE QErr ()
|
|
|
|
addCollectionToCatalog (CreateCollection name defn mComment) systemDefined =
|
2019-05-16 09:13:25 +03:00
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
INSERT INTO hdb_catalog.hdb_query_collection
|
2019-10-21 19:01:05 +03:00
|
|
|
(collection_name, collection_defn, comment, is_system_defined)
|
|
|
|
VALUES ($1, $2, $3, $4)
|
|
|
|
|] (name, Q.AltJ defn, mComment, systemDefined) True
|
2019-05-16 09:13:25 +03:00
|
|
|
|
|
|
|
delCollectionFromCatalog :: CollectionName -> Q.TxE QErr ()
|
|
|
|
delCollectionFromCatalog name =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
DELETE FROM hdb_catalog.hdb_query_collection
|
|
|
|
WHERE collection_name = $1
|
|
|
|
|] (Identity name) True
|
|
|
|
|
|
|
|
updateCollectionDefCatalog
|
2019-11-20 21:21:30 +03:00
|
|
|
:: CollectionName -> CollectionDef -> Q.TxE QErr ()
|
2019-05-20 13:29:32 +03:00
|
|
|
updateCollectionDefCatalog collName def = do
|
2019-05-16 09:13:25 +03:00
|
|
|
-- Update definition
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.hdb_query_collection
|
|
|
|
SET collection_defn = $1
|
|
|
|
WHERE collection_name = $2
|
|
|
|
|] (Q.AltJ def, collName) True
|
|
|
|
|
|
|
|
addCollectionToAllowlistCatalog :: CollectionName -> Q.TxE QErr ()
|
|
|
|
addCollectionToAllowlistCatalog collName =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
INSERT INTO hdb_catalog.hdb_allowlist
|
|
|
|
(collection_name)
|
|
|
|
VALUES ($1)
|
|
|
|
|] (Identity collName) True
|
|
|
|
|
|
|
|
delCollectionFromAllowlistCatalog :: CollectionName -> Q.TxE QErr ()
|
|
|
|
delCollectionFromAllowlistCatalog collName =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
DELETE FROM hdb_catalog.hdb_allowlist
|
|
|
|
WHERE collection_name = $1
|
|
|
|
|] (Identity collName) True
|