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
|
|
|
|
, runClearMetadata
|
|
|
|
, runReloadMetadata
|
|
|
|
, runDumpInternalState
|
2019-04-17 19:29:39 +03:00
|
|
|
, runGetInconsistentMetadata
|
|
|
|
, runDropInconsistentMetadata
|
2021-01-07 12:04:22 +03:00
|
|
|
, runGetCatalogState
|
|
|
|
, runSetCatalogState
|
2019-12-14 09:47:38 +03:00
|
|
|
|
|
|
|
, module Hasura.RQL.DDL.Metadata.Types
|
2018-06-27 16:11:32 +03:00
|
|
|
) where
|
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
import Hasura.Prelude
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
import qualified Data.Aeson.Ordered as AO
|
2021-01-07 12:04:22 +03:00
|
|
|
import qualified Data.HashMap.Strict as HM
|
2020-12-08 17:22:31 +03:00
|
|
|
import qualified Data.HashMap.Strict.InsOrd as OMap
|
|
|
|
import qualified Data.HashSet as HS
|
|
|
|
import qualified Data.List as L
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2021-02-11 12:43:39 +03:00
|
|
|
import Control.Lens ((^?))
|
2020-10-27 16:53:49 +03:00
|
|
|
import Data.Aeson
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2021-01-07 12:04:22 +03:00
|
|
|
import Hasura.Metadata.Class
|
2020-12-08 17:22:31 +03:00
|
|
|
import Hasura.RQL.DDL.Action
|
|
|
|
import Hasura.RQL.DDL.ComputedField
|
|
|
|
import Hasura.RQL.DDL.CustomTypes
|
2021-01-29 04:02:34 +03:00
|
|
|
import Hasura.RQL.DDL.Endpoint
|
2020-12-08 17:22:31 +03:00
|
|
|
import Hasura.RQL.DDL.EventTrigger
|
|
|
|
import Hasura.RQL.DDL.Permission
|
|
|
|
import Hasura.RQL.DDL.Relationship
|
|
|
|
import Hasura.RQL.DDL.RemoteRelationship
|
|
|
|
import Hasura.RQL.DDL.RemoteSchema
|
|
|
|
import Hasura.RQL.DDL.ScheduledTrigger
|
|
|
|
import Hasura.RQL.DDL.Schema
|
2020-10-27 16:53:49 +03:00
|
|
|
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2020-01-23 00:55:55 +03:00
|
|
|
import Hasura.RQL.DDL.Metadata.Types
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runClearMetadata
|
2021-01-07 12:04:22 +03:00
|
|
|
:: (MonadIO m, CacheRWM m, MetadataM m, MonadError QErr m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> ClearMetadata -> m EncJSON
|
2018-12-13 10:26:15 +03:00
|
|
|
runClearMetadata _ = do
|
2020-12-28 15:56:00 +03:00
|
|
|
metadata <- getMetadata
|
|
|
|
-- We can infer whether the server is started with `--database-url` option
|
|
|
|
-- (or corresponding env variable) by checking the existence of @'defaultSource'
|
|
|
|
-- in current metadata.
|
|
|
|
let maybeDefaultSourceMetadata = metadata ^? metaSources.ix defaultSource
|
|
|
|
emptyMetadata' = case maybeDefaultSourceMetadata of
|
|
|
|
Nothing -> emptyMetadata
|
|
|
|
Just defaultSourceMetadata ->
|
|
|
|
-- If default postgres source is defined, we need to set metadata
|
|
|
|
-- which contains only default source without any tables and functions.
|
|
|
|
let emptyDefaultSource = SourceMetadata defaultSource mempty mempty
|
|
|
|
$ _smConfiguration defaultSourceMetadata
|
|
|
|
in emptyMetadata
|
|
|
|
& metaSources %~ OMap.insert defaultSource emptyDefaultSource
|
|
|
|
runReplaceMetadata $ RMWithSources emptyMetadata'
|
2020-12-08 17:22:31 +03:00
|
|
|
|
|
|
|
{- Note [Clear postgres schema for dropped triggers]
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
There was an issue (https://github.com/hasura/graphql-engine/issues/5461)
|
|
|
|
fixed (via https://github.com/hasura/graphql-engine/pull/6137) related to
|
|
|
|
event triggers while replacing metadata in the catalog prior to metadata
|
|
|
|
separation. The metadata separation solves the issue naturally, since the
|
|
|
|
'hdb_catalog.event_triggers' table is no more in use and new/updated event
|
|
|
|
triggers are processed in building schema cache. But we need to drop the
|
|
|
|
pg trigger and archive events for dropped event triggers. This is handled
|
|
|
|
explicitly in @'runReplaceMetadata' function.
|
|
|
|
-}
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runReplaceMetadata
|
2020-12-08 17:22:31 +03:00
|
|
|
:: ( QErrM m
|
2019-11-20 21:21:30 +03:00
|
|
|
, CacheRWM m
|
2020-12-08 17:22:31 +03:00
|
|
|
, MetadataM m
|
2020-12-28 15:56:00 +03:00
|
|
|
, MonadIO m
|
2019-03-01 14:45:04 +03:00
|
|
|
)
|
2020-12-28 15:56:00 +03:00
|
|
|
=> ReplaceMetadata -> m EncJSON
|
|
|
|
runReplaceMetadata replaceMetadata = do
|
2020-12-08 17:22:31 +03:00
|
|
|
oldMetadata <- getMetadata
|
2020-12-28 15:56:00 +03:00
|
|
|
metadata <- case replaceMetadata of
|
|
|
|
RMWithSources m -> pure m
|
|
|
|
RMWithoutSources MetadataNoSources{..} -> do
|
|
|
|
defaultSourceMetadata <- onNothing (OMap.lookup defaultSource $ _metaSources oldMetadata) $
|
|
|
|
throw400 NotSupported $ "cannot import metadata without sources since no default source is defined"
|
|
|
|
let newDefaultSourceMetadata = defaultSourceMetadata
|
|
|
|
{ _smTables = _mnsTables
|
|
|
|
, _smFunctions = _mnsFunctions
|
|
|
|
}
|
2021-01-07 12:04:22 +03:00
|
|
|
pure $ Metadata (OMap.singleton defaultSource newDefaultSourceMetadata)
|
|
|
|
_mnsRemoteSchemas _mnsQueryCollections _mnsAllowlist
|
2021-01-29 04:02:34 +03:00
|
|
|
_mnsCustomTypes _mnsActions _mnsCronTriggers (_metaRestEndpoints oldMetadata)
|
2020-12-08 17:22:31 +03:00
|
|
|
putMetadata metadata
|
2020-11-12 12:25:48 +03:00
|
|
|
buildSchemaCacheStrict
|
2020-12-08 17:22:31 +03:00
|
|
|
-- See Note [Clear postgres schema for dropped triggers]
|
2020-12-28 15:56:00 +03:00
|
|
|
for_ (OMap.toList $ _metaSources metadata) $ \(source, newSourceCache) ->
|
|
|
|
onJust (OMap.lookup source $ _metaSources oldMetadata) $ \oldSourceCache -> do
|
|
|
|
let getTriggersMap = OMap.unions . map _tmEventTriggers . OMap.elems . _smTables
|
|
|
|
oldTriggersMap = getTriggersMap oldSourceCache
|
|
|
|
newTriggersMap = getTriggersMap newSourceCache
|
|
|
|
droppedTriggers = OMap.keys $ oldTriggersMap `OMap.difference` newTriggersMap
|
2021-01-20 03:31:53 +03:00
|
|
|
sourceConfig <- askSourceConfig source
|
2020-12-28 15:56:00 +03:00
|
|
|
for_ droppedTriggers $
|
2021-01-07 12:04:22 +03:00
|
|
|
\name -> liftIO $ runPgSourceWriteTx sourceConfig $ delTriggerQ name >> archiveEvents name
|
2019-01-25 06:31:54 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
pure successMsg
|
2020-05-27 18:02:58 +03:00
|
|
|
|
2021-01-29 08:48:17 +03:00
|
|
|
|
2018-12-13 10:26:15 +03:00
|
|
|
runExportMetadata
|
2021-02-11 12:43:39 +03:00
|
|
|
:: forall m . ( QErrM m, MetadataM m)
|
2019-03-18 19:22:21 +03:00
|
|
|
=> ExportMetadata -> m EncJSON
|
2021-01-29 08:48:17 +03:00
|
|
|
runExportMetadata _ = do
|
|
|
|
metadata <- getMetadata
|
2021-02-11 12:43:39 +03:00
|
|
|
pure $ AO.toEncJSON . metadataToOrdJSON $ metadata
|
2018-09-05 18:25:30 +03:00
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
runReloadMetadata :: (QErrM m, CacheRWM m, MetadataM m) => ReloadMetadata -> m EncJSON
|
2021-01-07 12:04:22 +03:00
|
|
|
runReloadMetadata (ReloadMetadata reloadRemoteSchemas reloadSources) = do
|
2020-03-26 14:52:20 +03:00
|
|
|
sc <- askSchemaCache
|
2021-01-07 12:04:22 +03:00
|
|
|
let remoteSchemaInvalidations = case reloadRemoteSchemas of
|
|
|
|
RSReloadAll -> HS.fromList $ getAllRemoteSchemas sc
|
|
|
|
RSReloadList l -> l
|
|
|
|
pgSourcesInvalidations = case reloadSources of
|
|
|
|
RSReloadAll -> HS.fromList $ HM.keys $ scPostgres sc
|
|
|
|
RSReloadList l -> l
|
2020-12-08 17:22:31 +03:00
|
|
|
cacheInvalidations = CacheInvalidations
|
|
|
|
{ ciMetadata = True
|
|
|
|
, ciRemoteSchemas = remoteSchemaInvalidations
|
2021-01-07 12:04:22 +03:00
|
|
|
, ciSources = pgSourcesInvalidations
|
2020-12-08 17:22:31 +03:00
|
|
|
}
|
|
|
|
metadata <- getMetadata
|
|
|
|
buildSchemaCacheWithOptions CatalogUpdate cacheInvalidations metadata
|
2020-03-26 14:52:20 +03:00
|
|
|
pure 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
|
2020-12-08 17:22:31 +03:00
|
|
|
:: (QErrM m, CacheRWM m, MetadataM m)
|
2019-04-17 19:29:39 +03:00
|
|
|
=> DropInconsistentMetadata -> m EncJSON
|
|
|
|
runDropInconsistentMetadata _ = do
|
|
|
|
sc <- askSchemaCache
|
2019-11-27 01:49:42 +03:00
|
|
|
let inconsSchObjs = L.nub . concatMap imObjectIds $ scInconsistentObjs sc
|
2019-11-20 21:21:30 +03:00
|
|
|
-- 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.
|
2020-12-08 17:22:31 +03:00
|
|
|
metadataModifier <- execWriterT $ mapM_ (tell . purgeMetadataObj) (reverse inconsSchObjs)
|
|
|
|
metadata <- getMetadata
|
|
|
|
putMetadata $ unMetadataModifier metadataModifier $ metadata
|
2019-11-20 21:21:30 +03:00
|
|
|
buildSchemaCacheStrict
|
2019-04-17 19:29:39 +03:00
|
|
|
return successMsg
|
|
|
|
|
2020-12-08 17:22:31 +03:00
|
|
|
purgeMetadataObj :: MetadataObjId -> MetadataModifier
|
|
|
|
purgeMetadataObj = \case
|
2020-12-28 15:56:00 +03:00
|
|
|
MOSource source -> MetadataModifier $ metaSources %~ OMap.delete source
|
|
|
|
MOSourceObjId source sourceObjId -> case sourceObjId of
|
|
|
|
SMOTable qt -> dropTableInMetadata source qt
|
|
|
|
SMOTableObj qt tableObj -> MetadataModifier $
|
|
|
|
tableMetadataSetter source qt %~ case tableObj of
|
2020-12-08 17:22:31 +03:00
|
|
|
MTORel rn _ -> dropRelationshipInMetadata rn
|
|
|
|
MTOPerm rn pt -> dropPermissionInMetadata rn pt
|
|
|
|
MTOTrigger trn -> dropEventTriggerInMetadata trn
|
|
|
|
MTOComputedField ccn -> dropComputedFieldInMetadata ccn
|
|
|
|
MTORemoteRelationship rn -> dropRemoteRelationshipInMetadata rn
|
2021-01-29 08:48:17 +03:00
|
|
|
SMOFunction qf -> dropFunctionInMetadata source qf
|
|
|
|
SMOFunctionPermission qf rn -> dropFunctionPermissionInMetadata source qf rn
|
2020-12-08 17:22:31 +03:00
|
|
|
MORemoteSchema rsn -> dropRemoteSchemaInMetadata rsn
|
2020-12-21 12:11:37 +03:00
|
|
|
MORemoteSchemaPermissions rsName role -> dropRemoteSchemaPermissionInMetadata rsName role
|
2020-12-08 17:22:31 +03:00
|
|
|
MOCustomTypes -> clearCustomTypesInMetadata
|
2020-12-28 15:56:00 +03:00
|
|
|
MOAction action -> dropActionInMetadata action -- Nothing
|
2020-12-08 17:22:31 +03:00
|
|
|
MOActionPermission action role -> dropActionPermissionInMetadata action role
|
|
|
|
MOCronTrigger ctName -> dropCronTriggerInMetadata ctName
|
2021-01-29 04:02:34 +03:00
|
|
|
MOEndpoint epName -> dropEndpointInMetadata epName
|
2021-01-07 12:04:22 +03:00
|
|
|
|
|
|
|
runGetCatalogState
|
|
|
|
:: (MonadMetadataStorageQueryAPI m) => GetCatalogState -> m EncJSON
|
|
|
|
runGetCatalogState _ =
|
|
|
|
encJFromJValue <$> fetchCatalogState
|
|
|
|
|
|
|
|
runSetCatalogState
|
|
|
|
:: (MonadMetadataStorageQueryAPI m) => SetCatalogState -> m EncJSON
|
|
|
|
runSetCatalogState SetCatalogState{..} = do
|
|
|
|
updateCatalogState _scsType _scsState
|
|
|
|
pure successMsg
|