2019-11-20 09:47:06 +03:00
|
|
|
{-# LANGUAGE TypeApplications #-}
|
2018-06-27 16:11:32 +03:00
|
|
|
module Hasura.RQL.DDL.Metadata
|
2019-12-14 09:47:38 +03:00
|
|
|
( runReplaceMetadata
|
2018-12-13 10:26:15 +03:00
|
|
|
, runExportMetadata
|
2018-06-27 16:11:32 +03:00
|
|
|
, fetchMetadata
|
2018-12-13 10:26:15 +03:00
|
|
|
, runClearMetadata
|
|
|
|
, runReloadMetadata
|
|
|
|
, runDumpInternalState
|
2019-04-17 19:29:39 +03:00
|
|
|
, runGetInconsistentMetadata
|
|
|
|
, runDropInconsistentMetadata
|
2019-12-14 09:47:38 +03:00
|
|
|
|
|
|
|
, module Hasura.RQL.DDL.Metadata.Types
|
2018-06-27 16:11:32 +03:00
|
|
|
) where
|
|
|
|
|
2019-04-17 19:29:39 +03:00
|
|
|
import Control.Lens hiding ((.=))
|
2018-06-27 16:11:32 +03:00
|
|
|
import Data.Aeson
|
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
import qualified Data.Aeson.Ordered as AO
|
2019-11-06 05:11:03 +03:00
|
|
|
import qualified Data.HashMap.Strict.InsOrd as HMIns
|
2019-04-17 19:29:39 +03:00
|
|
|
import qualified Data.HashSet as HS
|
|
|
|
import qualified Data.List as L
|
|
|
|
import qualified Data.Text as T
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
2019-11-06 05:11:03 +03:00
|
|
|
import Hasura.RQL.DDL.ComputedField (dropComputedFieldFromCatalog)
|
|
|
|
import Hasura.RQL.DDL.EventTrigger (delEventTriggerFromCatalog,
|
|
|
|
subTableP2)
|
2019-12-14 09:47:38 +03:00
|
|
|
import Hasura.RQL.DDL.Metadata.Types
|
2019-11-06 05:11:03 +03:00
|
|
|
import Hasura.RQL.DDL.Permission.Internal (dropPermFromCatalog)
|
|
|
|
import Hasura.RQL.DDL.RemoteSchema (addRemoteSchemaP2,
|
|
|
|
removeRemoteSchemaFromCatalog)
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
2019-04-17 19:29:39 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
2019-11-07 17:39:48 +03:00
|
|
|
import qualified Hasura.RQL.DDL.ComputedField as ComputedField
|
2019-11-06 05:11:03 +03:00
|
|
|
import qualified Hasura.RQL.DDL.Permission as Permission
|
|
|
|
import qualified Hasura.RQL.DDL.QueryCollection as Collection
|
|
|
|
import qualified Hasura.RQL.DDL.Relationship as Relationship
|
|
|
|
import qualified Hasura.RQL.DDL.Schema as Schema
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
clearMetadata :: Q.TxE QErr ()
|
|
|
|
clearMetadata = Q.catchE defaultTxErrorHandler $ do
|
2019-01-25 06:31:54 +03:00
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.hdb_function WHERE is_system_defined <> 'true'" () False
|
2018-06-27 16:11:32 +03:00
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.hdb_permission WHERE is_system_defined <> 'true'" () False
|
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.hdb_relationship WHERE is_system_defined <> 'true'" () False
|
2018-12-19 09:34:27 +03:00
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.event_triggers" () False
|
2019-11-07 17:39:48 +03:00
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.hdb_computed_field" () False
|
2019-03-01 16:59:24 +03:00
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.hdb_table WHERE is_system_defined <> 'true'" () False
|
2018-11-23 16:02:46 +03:00
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.remote_schemas" () False
|
2019-05-16 09:13:25 +03:00
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.hdb_allowlist" () False
|
|
|
|
Q.unitQ "DELETE FROM hdb_catalog.hdb_query_collection WHERE is_system_defined <> 'true'" () False
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runClearMetadata
|
2019-11-20 21:21:30 +03:00
|
|
|
:: (MonadTx m, CacheRWM m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> ClearMetadata -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
runClearMetadata _ = do
|
|
|
|
liftTx clearMetadata
|
2019-11-20 21:21:30 +03:00
|
|
|
buildSchemaCacheStrict
|
2018-12-13 10:26:15 +03:00
|
|
|
return successMsg
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
applyQP1
|
2019-11-26 15:14:21 +03:00
|
|
|
:: (QErrM m)
|
2018-12-13 10:26:15 +03:00
|
|
|
=> ReplaceMetadata -> m ()
|
2019-12-14 09:47:38 +03:00
|
|
|
applyQP1 (ReplaceMetadata _ tables functionsMeta schemas collections allowlist) = do
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
withPathK "tables" $ do
|
|
|
|
|
|
|
|
checkMultipleDecls "tables" $ map _tmTable tables
|
|
|
|
|
|
|
|
-- process each table
|
|
|
|
void $ indexedForM tables $ \table -> withTableName (table ^. tmTable) $ do
|
2019-11-06 05:11:03 +03:00
|
|
|
let allRels = map Relationship.rdName (table ^. tmObjectRelationships) <>
|
|
|
|
map Relationship.rdName (table ^. tmArrayRelationships)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-11-06 05:11:03 +03:00
|
|
|
insPerms = map Permission.pdRole $ table ^. tmInsertPermissions
|
|
|
|
selPerms = map Permission.pdRole $ table ^. tmSelectPermissions
|
|
|
|
updPerms = map Permission.pdRole $ table ^. tmUpdatePermissions
|
|
|
|
delPerms = map Permission.pdRole $ table ^. tmDeletePermissions
|
|
|
|
eventTriggers = map etcName $ table ^. tmEventTriggers
|
2019-11-07 17:39:48 +03:00
|
|
|
computedFields = map _cfmName $ table ^. tmComputedFields
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
checkMultipleDecls "relationships" allRels
|
|
|
|
checkMultipleDecls "insert permissions" insPerms
|
|
|
|
checkMultipleDecls "select permissions" selPerms
|
|
|
|
checkMultipleDecls "update permissions" updPerms
|
|
|
|
checkMultipleDecls "delete permissions" delPerms
|
2018-09-05 14:26:46 +03:00
|
|
|
checkMultipleDecls "event triggers" eventTriggers
|
2019-11-07 17:39:48 +03:00
|
|
|
checkMultipleDecls "computed fields" computedFields
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
withPathK "functions" $
|
2019-12-14 09:47:38 +03:00
|
|
|
case functionsMeta of
|
|
|
|
FMVersion1 qualifiedFunctions ->
|
2019-11-20 09:47:06 +03:00
|
|
|
checkMultipleDecls "functions" qualifiedFunctions
|
2019-12-14 09:47:38 +03:00
|
|
|
FMVersion2 functionsV2 ->
|
2019-11-20 09:47:06 +03:00
|
|
|
checkMultipleDecls "functions" $ map Schema._tfv2Function functionsV2
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
withPathK "remote_schemas" $
|
|
|
|
checkMultipleDecls "remote schemas" $ map _arsqName schemas
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
withPathK "query_collections" $
|
|
|
|
checkMultipleDecls "query collections" $ map Collection._ccName collections
|
2019-05-16 09:13:25 +03:00
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
withPathK "allowlist" $
|
|
|
|
checkMultipleDecls "allow list" $ map Collection._crCollection allowlist
|
2019-05-16 09:13:25 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
where
|
2019-01-25 06:31:54 +03:00
|
|
|
withTableName qt = withPathK (qualObjectToText qt)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
checkMultipleDecls t l = do
|
|
|
|
let dups = getDups l
|
|
|
|
unless (null dups) $
|
|
|
|
throw400 AlreadyExists $ "multiple declarations exist for the following " <> t <> " : "
|
|
|
|
<> T.pack (show dups)
|
|
|
|
|
|
|
|
getDups l =
|
|
|
|
l L.\\ HS.toList (HS.fromList l)
|
|
|
|
|
2018-11-23 16:02:46 +03:00
|
|
|
applyQP2
|
2019-11-20 21:21:30 +03:00
|
|
|
:: ( MonadIO m
|
2018-11-23 16:02:46 +03:00
|
|
|
, MonadTx m
|
2019-11-20 21:21:30 +03:00
|
|
|
, CacheRWM m
|
2019-10-11 08:13:57 +03:00
|
|
|
, HasSystemDefined m
|
2019-11-20 21:21:30 +03:00
|
|
|
, HasHttpManager m
|
2018-11-23 16:02:46 +03:00
|
|
|
)
|
|
|
|
=> ReplaceMetadata
|
2019-03-18 19:22:21 +03:00
|
|
|
-> m EncJSON
|
2019-12-14 09:47:38 +03:00
|
|
|
applyQP2 (ReplaceMetadata _ tables functionsMeta schemas collections allowlist) = do
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
liftTx clearMetadata
|
2019-11-20 21:21:30 +03:00
|
|
|
buildSchemaCacheStrict
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-10-21 19:01:05 +03:00
|
|
|
systemDefined <- askSystemDefined
|
2018-06-27 16:11:32 +03:00
|
|
|
withPathK "tables" $ do
|
|
|
|
-- tables and views
|
2019-07-22 15:47:13 +03:00
|
|
|
indexedForM_ tables $ \tableMeta -> do
|
2019-09-19 07:47:36 +03:00
|
|
|
let tableName = tableMeta ^. tmTable
|
|
|
|
isEnum = tableMeta ^. tmIsEnum
|
|
|
|
config = tableMeta ^. tmConfiguration
|
2019-11-06 05:11:03 +03:00
|
|
|
void $ Schema.trackExistingTableOrViewP2 tableName systemDefined isEnum config
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
indexedForM_ tables $ \table -> do
|
2019-11-07 17:39:48 +03:00
|
|
|
-- Relationships
|
2018-06-27 16:11:32 +03:00
|
|
|
withPathK "object_relationships" $
|
|
|
|
indexedForM_ (table ^. tmObjectRelationships) $ \objRel ->
|
2019-11-20 21:21:30 +03:00
|
|
|
Relationship.insertRelationshipToCatalog (table ^. tmTable) ObjRel objRel
|
2018-06-27 16:11:32 +03:00
|
|
|
withPathK "array_relationships" $
|
|
|
|
indexedForM_ (table ^. tmArrayRelationships) $ \arrRel ->
|
2019-11-20 21:21:30 +03:00
|
|
|
Relationship.insertRelationshipToCatalog (table ^. tmTable) ArrRel arrRel
|
2019-11-07 17:39:48 +03:00
|
|
|
-- Computed Fields
|
|
|
|
withPathK "computed_fields" $
|
|
|
|
indexedForM_ (table ^. tmComputedFields) $
|
|
|
|
\(ComputedFieldMeta name definition comment) ->
|
2019-11-20 21:21:30 +03:00
|
|
|
ComputedField.addComputedFieldToCatalog $
|
2019-11-07 17:39:48 +03:00
|
|
|
ComputedField.AddComputedField (table ^. tmTable) name definition comment
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Permissions
|
|
|
|
indexedForM_ tables $ \table -> do
|
|
|
|
let tableName = table ^. tmTable
|
2019-11-20 21:21:30 +03:00
|
|
|
tabInfo <- modifyErrAndSet500 ("apply " <> ) $ askTableCoreInfo tableName
|
2018-06-27 16:11:32 +03:00
|
|
|
withPathK "insert_permissions" $ processPerms tabInfo $
|
|
|
|
table ^. tmInsertPermissions
|
|
|
|
withPathK "select_permissions" $ processPerms tabInfo $
|
|
|
|
table ^. tmSelectPermissions
|
|
|
|
withPathK "update_permissions" $ processPerms tabInfo $
|
|
|
|
table ^. tmUpdatePermissions
|
|
|
|
withPathK "delete_permissions" $ processPerms tabInfo $
|
|
|
|
table ^. tmDeletePermissions
|
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
indexedForM_ tables $ \table ->
|
2018-09-05 14:26:46 +03:00
|
|
|
withPathK "event_triggers" $
|
2018-11-14 10:13:01 +03:00
|
|
|
indexedForM_ (table ^. tmEventTriggers) $ \etc ->
|
2019-11-06 05:11:03 +03:00
|
|
|
subTableP2 (table ^. tmTable) False etc
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
-- sql functions
|
2019-12-14 09:47:38 +03:00
|
|
|
withPathK "functions" $ case functionsMeta of
|
2019-11-20 09:47:06 +03:00
|
|
|
FMVersion1 qualifiedFunctions -> indexedForM_ qualifiedFunctions $
|
|
|
|
\qf -> void $ Schema.trackFunctionP2 qf Schema.emptyFunctionConfig
|
|
|
|
FMVersion2 functionsV2 -> indexedForM_ functionsV2 $
|
|
|
|
\(Schema.TrackFunctionV2 function config) -> void $ Schema.trackFunctionP2 function config
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2019-05-16 09:13:25 +03:00
|
|
|
-- query collections
|
|
|
|
withPathK "query_collections" $
|
2019-11-20 09:47:06 +03:00
|
|
|
indexedForM_ collections $ \c -> liftTx $ Collection.addCollectionToCatalog c systemDefined
|
2019-05-16 09:13:25 +03:00
|
|
|
|
|
|
|
-- allow list
|
|
|
|
withPathK "allowlist" $ do
|
2019-11-20 21:21:30 +03:00
|
|
|
indexedForM_ allowlist $ \(Collection.CollectionReq name) ->
|
|
|
|
liftTx $ Collection.addCollectionToAllowlistCatalog name
|
2019-05-16 09:13:25 +03:00
|
|
|
|
2018-11-27 14:26:10 +03:00
|
|
|
-- remote schemas
|
2019-12-14 09:47:38 +03:00
|
|
|
withPathK "remote_schemas" $
|
|
|
|
indexedMapM_ (void . addRemoteSchemaP2) schemas
|
2019-04-17 19:29:39 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
buildSchemaCacheStrict
|
2018-06-27 16:11:32 +03:00
|
|
|
return successMsg
|
|
|
|
|
|
|
|
where
|
2019-11-20 21:21:30 +03:00
|
|
|
processPerms tabInfo perms = indexedForM_ perms $ Permission.addPermP2 (_tciName tabInfo)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runReplaceMetadata
|
2019-11-20 21:21:30 +03:00
|
|
|
:: ( MonadIO m
|
|
|
|
, MonadTx m
|
|
|
|
, CacheRWM m
|
2019-10-11 08:13:57 +03:00
|
|
|
, HasSystemDefined m
|
2019-11-20 21:21:30 +03:00
|
|
|
, HasHttpManager m
|
2019-03-01 14:45:04 +03:00
|
|
|
)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> ReplaceMetadata -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
runReplaceMetadata q = do
|
|
|
|
applyQP1 q
|
|
|
|
applyQP2 q
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
fetchMetadata :: Q.TxE QErr ReplaceMetadata
|
|
|
|
fetchMetadata = do
|
|
|
|
tables <- Q.catchE defaultTxErrorHandler fetchTables
|
2019-11-06 05:11:03 +03:00
|
|
|
let tableMetaMap = HMIns.fromList . flip map tables $
|
2019-09-19 07:47:36 +03:00
|
|
|
\(schema, name, isEnum, maybeConfig) ->
|
|
|
|
let qualifiedName = QualifiedObject schema name
|
|
|
|
configuration = maybe emptyTableConfig Q.getAltJ maybeConfig
|
|
|
|
in (qualifiedName, mkTableMeta qualifiedName isEnum configuration)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
-- Fetch all the relationships
|
|
|
|
relationships <- Q.catchE defaultTxErrorHandler fetchRelationships
|
|
|
|
|
|
|
|
objRelDefs <- mkRelDefs ObjRel relationships
|
|
|
|
arrRelDefs <- mkRelDefs ArrRel relationships
|
|
|
|
|
|
|
|
-- Fetch all the permissions
|
|
|
|
permissions <- Q.catchE defaultTxErrorHandler fetchPermissions
|
|
|
|
|
|
|
|
-- Parse all the permissions
|
|
|
|
insPermDefs <- mkPermDefs PTInsert permissions
|
|
|
|
selPermDefs <- mkPermDefs PTSelect permissions
|
|
|
|
updPermDefs <- mkPermDefs PTUpdate permissions
|
|
|
|
delPermDefs <- mkPermDefs PTDelete permissions
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
-- Fetch all event triggers
|
|
|
|
eventTriggers <- Q.catchE defaultTxErrorHandler fetchEventTriggers
|
|
|
|
triggerMetaDefs <- mkTriggerMetaDefs eventTriggers
|
|
|
|
|
2019-11-07 17:39:48 +03:00
|
|
|
-- Fetch all computed fields
|
|
|
|
computedFields <- fetchComputedFields
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
let (_, postRelMap) = flip runState tableMetaMap $ do
|
|
|
|
modMetaMap tmObjectRelationships objRelDefs
|
|
|
|
modMetaMap tmArrayRelationships arrRelDefs
|
|
|
|
modMetaMap tmInsertPermissions insPermDefs
|
|
|
|
modMetaMap tmSelectPermissions selPermDefs
|
|
|
|
modMetaMap tmUpdatePermissions updPermDefs
|
|
|
|
modMetaMap tmDeletePermissions delPermDefs
|
2018-09-05 14:26:46 +03:00
|
|
|
modMetaMap tmEventTriggers triggerMetaDefs
|
2019-11-07 17:39:48 +03:00
|
|
|
modMetaMap tmComputedFields computedFields
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
-- fetch all functions
|
2019-11-20 09:47:06 +03:00
|
|
|
functions <- FMVersion2 <$> Q.catchE defaultTxErrorHandler fetchFunctions
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2018-11-23 16:02:46 +03:00
|
|
|
-- fetch all custom resolvers
|
2019-11-06 05:11:03 +03:00
|
|
|
remoteSchemas <- fetchRemoteSchemas
|
2018-11-23 16:02:46 +03:00
|
|
|
|
2019-05-16 09:13:25 +03:00
|
|
|
-- fetch all collections
|
2019-10-21 19:01:05 +03:00
|
|
|
collections <- fetchCollections
|
2019-05-16 09:13:25 +03:00
|
|
|
|
|
|
|
-- fetch allow list
|
2019-11-06 05:11:03 +03:00
|
|
|
allowlist <- map Collection.CollectionReq <$> fetchAllowlists
|
2019-05-16 09:13:25 +03:00
|
|
|
|
2019-12-14 09:47:38 +03:00
|
|
|
return $ ReplaceMetadata currentMetadataVersion (HMIns.elems postRelMap) functions
|
|
|
|
remoteSchemas collections allowlist
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
where
|
|
|
|
|
|
|
|
modMetaMap l xs = do
|
|
|
|
st <- get
|
|
|
|
put $ foldr (\(qt, dfn) b -> b & at qt._Just.l %~ (:) dfn) st xs
|
|
|
|
|
|
|
|
mkPermDefs pt = mapM permRowToDef . filter (\pr -> pr ^. _4 == pt)
|
|
|
|
|
|
|
|
permRowToDef (sn, tn, rn, _, Q.AltJ pDef, mComment) = do
|
|
|
|
perm <- decodeValue pDef
|
2019-11-06 05:11:03 +03:00
|
|
|
return (QualifiedObject sn tn, Permission.PermDef rn perm mComment)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
mkRelDefs rt = mapM relRowToDef . filter (\rr -> rr ^. _4 == rt)
|
|
|
|
|
|
|
|
relRowToDef (sn, tn, rn, _, Q.AltJ rDef, mComment) = do
|
|
|
|
using <- decodeValue rDef
|
2019-11-06 05:11:03 +03:00
|
|
|
return (QualifiedObject sn tn, Relationship.RelDef rn using mComment)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
mkTriggerMetaDefs = mapM trigRowToDef
|
|
|
|
|
2018-11-14 10:13:01 +03:00
|
|
|
trigRowToDef (sn, tn, Q.AltJ configuration) = do
|
|
|
|
conf <- decodeValue configuration
|
2019-01-25 06:31:54 +03:00
|
|
|
return (QualifiedObject sn tn, conf::EventTriggerConf)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
fetchTables =
|
|
|
|
Q.listQ [Q.sql|
|
2019-09-19 07:47:36 +03:00
|
|
|
SELECT table_schema, table_name, is_enum, configuration::json
|
|
|
|
FROM hdb_catalog.hdb_table
|
2018-06-27 16:11:32 +03:00
|
|
|
WHERE is_system_defined = 'false'
|
2019-11-06 05:11:03 +03:00
|
|
|
ORDER BY table_schema ASC, table_name ASC
|
2018-06-27 16:11:32 +03:00
|
|
|
|] () False
|
|
|
|
|
|
|
|
fetchRelationships =
|
|
|
|
Q.listQ [Q.sql|
|
|
|
|
SELECT table_schema, table_name, rel_name, rel_type, rel_def::json, comment
|
|
|
|
FROM hdb_catalog.hdb_relationship
|
|
|
|
WHERE is_system_defined = 'false'
|
2019-11-06 05:11:03 +03:00
|
|
|
ORDER BY table_schema ASC, table_name ASC, rel_name ASC
|
2018-06-27 16:11:32 +03:00
|
|
|
|] () False
|
|
|
|
|
|
|
|
fetchPermissions =
|
|
|
|
Q.listQ [Q.sql|
|
|
|
|
SELECT table_schema, table_name, role_name, perm_type, perm_def::json, comment
|
|
|
|
FROM hdb_catalog.hdb_permission
|
|
|
|
WHERE is_system_defined = 'false'
|
2019-11-06 05:11:03 +03:00
|
|
|
ORDER BY table_schema ASC, table_name ASC, role_name ASC, perm_type ASC
|
2018-06-27 16:11:32 +03:00
|
|
|
|] () False
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
fetchEventTriggers =
|
|
|
|
Q.listQ [Q.sql|
|
2018-11-14 10:13:01 +03:00
|
|
|
SELECT e.schema_name, e.table_name, e.configuration::json
|
2018-09-05 14:26:46 +03:00
|
|
|
FROM hdb_catalog.event_triggers e
|
2019-11-06 05:11:03 +03:00
|
|
|
ORDER BY e.schema_name ASC, e.table_name ASC, e.name ASC
|
2018-09-05 14:26:46 +03:00
|
|
|
|] () False
|
2019-10-21 19:01:05 +03:00
|
|
|
|
2019-11-20 09:47:06 +03:00
|
|
|
fetchFunctions = do
|
|
|
|
l <- Q.listQ [Q.sql|
|
|
|
|
SELECT function_schema, function_name, configuration::json
|
2019-01-25 06:31:54 +03:00
|
|
|
FROM hdb_catalog.hdb_function
|
|
|
|
WHERE is_system_defined = 'false'
|
2019-11-06 05:11:03 +03:00
|
|
|
ORDER BY function_schema ASC, function_name ASC
|
2019-01-25 06:31:54 +03:00
|
|
|
|] () False
|
2019-11-20 09:47:06 +03:00
|
|
|
pure $ flip map l $ \(sn, fn, Q.AltJ config) ->
|
|
|
|
Schema.TrackFunctionV2 (QualifiedObject sn fn) config
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2019-11-06 05:11:03 +03:00
|
|
|
fetchCollections =
|
|
|
|
map fromRow <$> Q.listQE defaultTxErrorHandler [Q.sql|
|
2019-10-21 19:01:05 +03:00
|
|
|
SELECT collection_name, collection_defn::json, comment
|
|
|
|
FROM hdb_catalog.hdb_query_collection
|
|
|
|
WHERE is_system_defined = 'false'
|
2019-11-06 05:11:03 +03:00
|
|
|
ORDER BY collection_name ASC
|
2019-10-21 19:01:05 +03:00
|
|
|
|] () False
|
2019-11-06 05:11:03 +03:00
|
|
|
where
|
|
|
|
fromRow (name, Q.AltJ defn, mComment) =
|
|
|
|
Collection.CreateCollection name defn mComment
|
|
|
|
|
|
|
|
fetchAllowlists = map runIdentity <$>
|
|
|
|
Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
SELECT collection_name
|
|
|
|
FROM hdb_catalog.hdb_allowlist
|
|
|
|
ORDER BY collection_name ASC
|
|
|
|
|] () False
|
|
|
|
|
|
|
|
fetchRemoteSchemas =
|
|
|
|
map fromRow <$> Q.listQE defaultTxErrorHandler
|
|
|
|
[Q.sql|
|
|
|
|
SELECT name, definition, comment
|
|
|
|
FROM hdb_catalog.remote_schemas
|
|
|
|
ORDER BY name ASC
|
|
|
|
|] () True
|
|
|
|
where
|
|
|
|
fromRow (name, Q.AltJ def, comment) =
|
|
|
|
AddRemoteSchemaQuery name def comment
|
2019-10-21 19:01:05 +03:00
|
|
|
|
2019-11-07 17:39:48 +03:00
|
|
|
fetchComputedFields = do
|
|
|
|
r <- Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
SELECT table_schema, table_name, computed_field_name,
|
|
|
|
definition::json, comment
|
|
|
|
FROM hdb_catalog.hdb_computed_field
|
|
|
|
|] () False
|
|
|
|
pure $ flip map r $ \(schema, table, name, Q.AltJ definition, comment) ->
|
|
|
|
( QualifiedObject schema table
|
|
|
|
, ComputedFieldMeta name definition comment
|
|
|
|
)
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runExportMetadata
|
2019-11-26 15:14:21 +03:00
|
|
|
:: (QErrM m, MonadTx m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> ExportMetadata -> m EncJSON
|
2019-11-26 15:14:21 +03:00
|
|
|
runExportMetadata _ =
|
2019-12-14 09:47:38 +03:00
|
|
|
(AO.toEncJSON . replaceMetadataToOrdJSON) <$> liftTx fetchMetadata
|
2018-09-05 18:25:30 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
runReloadMetadata :: (QErrM m, CacheRWM m) => ReloadMetadata -> m EncJSON
|
|
|
|
runReloadMetadata ReloadMetadata = do
|
|
|
|
buildSchemaCache
|
2018-12-13 10:26:15 +03:00
|
|
|
return successMsg
|
2018-09-05 18:25:30 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runDumpInternalState
|
2019-11-26 15:14:21 +03:00
|
|
|
:: (QErrM m, CacheRM m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> DumpInternalState -> m EncJSON
|
2019-11-26 15:14:21 +03:00
|
|
|
runDumpInternalState _ =
|
2019-03-18 19:22:21 +03:00
|
|
|
encJFromJValue <$> askSchemaCache
|
2019-04-17 19:29:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
runGetInconsistentMetadata
|
2019-11-26 15:14:21 +03:00
|
|
|
:: (QErrM m, CacheRM m)
|
2019-04-17 19:29:39 +03:00
|
|
|
=> GetInconsistentMetadata -> m EncJSON
|
|
|
|
runGetInconsistentMetadata _ = do
|
|
|
|
inconsObjs <- scInconsistentObjs <$> askSchemaCache
|
|
|
|
return $ encJFromJValue $ object
|
|
|
|
[ "is_consistent" .= null inconsObjs
|
|
|
|
, "inconsistent_objects" .= inconsObjs
|
|
|
|
]
|
|
|
|
|
|
|
|
runDropInconsistentMetadata
|
2019-11-26 15:14:21 +03:00
|
|
|
:: (QErrM m, CacheRWM m, MonadTx m)
|
2019-04-17 19:29:39 +03:00
|
|
|
=> DropInconsistentMetadata -> m EncJSON
|
|
|
|
runDropInconsistentMetadata _ = do
|
|
|
|
sc <- askSchemaCache
|
2019-11-20 21:21:30 +03:00
|
|
|
let inconsSchObjs = map (_moId . _imoObject) $ scInconsistentObjs sc
|
|
|
|
-- Note: when building the schema cache, we try to put dependents after their dependencies in the
|
|
|
|
-- list of inconsistent objects, so reverse the list to start with dependents first. This is not
|
|
|
|
-- perfect — a completely accurate solution would require performing a topological sort — but it
|
|
|
|
-- seems to work well enough for now.
|
|
|
|
mapM_ purgeMetadataObj (reverse inconsSchObjs)
|
|
|
|
buildSchemaCacheStrict
|
2019-04-17 19:29:39 +03:00
|
|
|
return successMsg
|
|
|
|
|
|
|
|
purgeMetadataObj :: MonadTx m => MetadataObjId -> m ()
|
|
|
|
purgeMetadataObj = liftTx . \case
|
2019-11-20 21:21:30 +03:00
|
|
|
MOTable qt -> Schema.deleteTableFromCatalog qt
|
|
|
|
MOFunction qf -> Schema.delFunctionFromCatalog qf
|
|
|
|
MORemoteSchema rsn -> removeRemoteSchemaFromCatalog rsn
|
|
|
|
MOTableObj qt (MTORel rn _) -> Relationship.delRelFromCatalog qt rn
|
|
|
|
MOTableObj qt (MTOPerm rn pt) -> dropPermFromCatalog qt rn pt
|
|
|
|
MOTableObj _ (MTOTrigger trn) -> delEventTriggerFromCatalog trn
|
|
|
|
MOTableObj qt (MTOComputedField ccn) -> dropComputedFieldFromCatalog qt ccn
|