2019-12-09 01:17:39 +03:00
|
|
|
module Hasura.RQL.DDL.Schema.Cache.Dependencies
|
|
|
|
( resolveDependencies
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict.Extended as M
|
|
|
|
import qualified Data.HashSet as HS
|
|
|
|
|
|
|
|
import Control.Arrow.Extended
|
|
|
|
import Control.Lens hiding ((.=))
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.List (nub)
|
2019-12-15 16:28:23 +03:00
|
|
|
import Data.Monoid (First)
|
2020-10-21 19:35:06 +03:00
|
|
|
import Data.Text.Extended
|
2021-03-15 16:02:58 +03:00
|
|
|
|
|
|
|
import qualified Hasura.SQL.AnyBackend as AB
|
2021-02-03 19:17:20 +03:00
|
|
|
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-08-24 10:36:32 +03:00
|
|
|
import Hasura.RQL.DDL.Network
|
2019-12-09 01:17:39 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Cache.Common
|
|
|
|
import Hasura.RQL.Types
|
|
|
|
|
2021-04-22 00:44:37 +03:00
|
|
|
|
2019-12-09 01:17:39 +03:00
|
|
|
-- | Processes collected 'CIDependency' values into a 'DepMap', performing integrity checking to
|
|
|
|
-- ensure the dependencies actually exist. If a dependency is missing, its transitive dependents are
|
|
|
|
-- removed from the cache, and 'InconsistentMetadata's are returned.
|
|
|
|
resolveDependencies
|
|
|
|
:: (ArrowKleisli m arr, QErrM m)
|
2021-02-14 09:07:52 +03:00
|
|
|
=> ( BuildOutputs
|
2019-12-09 01:17:39 +03:00
|
|
|
, [(MetadataObject, SchemaObjId, SchemaDependency)]
|
2021-02-14 09:07:52 +03:00
|
|
|
) `arr` (BuildOutputs, [InconsistentMetadata], DepMap)
|
2019-12-09 01:17:39 +03:00
|
|
|
resolveDependencies = arrM \(cache, dependencies) -> do
|
|
|
|
let dependencyMap = dependencies
|
|
|
|
& M.groupOn (view _2)
|
|
|
|
& fmap (map \(metadataObject, _, schemaDependency) -> (metadataObject, schemaDependency))
|
|
|
|
performIteration 0 cache [] dependencyMap
|
|
|
|
|
|
|
|
-- Processes dependencies using an iterative process that alternates between two steps:
|
|
|
|
--
|
|
|
|
-- 1. First, pruneDanglingDependents searches for any dependencies that do not exist in the
|
|
|
|
-- current cache and removes their dependents from the dependency map, returning an
|
|
|
|
-- InconsistentMetadata for each dependent that was removed. This step does not change
|
|
|
|
-- the schema cache in any way.
|
|
|
|
--
|
|
|
|
-- 2. Second, deleteMetadataObject drops the pruned dependent objects from the cache. It does
|
|
|
|
-- not alter (or consult) the dependency map, so transitive dependents are /not/ removed.
|
|
|
|
--
|
|
|
|
-- By iterating the above process until pruneDanglingDependents does not discover any new
|
|
|
|
-- inconsistencies, all missing dependencies will eventually be removed, and since dependency
|
|
|
|
-- graphs between schema objects are unlikely to be very deep, it will usually terminate in just
|
|
|
|
-- a few iterations.
|
|
|
|
performIteration
|
|
|
|
:: (QErrM m)
|
|
|
|
=> Int
|
2021-02-14 09:07:52 +03:00
|
|
|
-> BuildOutputs
|
2019-12-09 01:17:39 +03:00
|
|
|
-> [InconsistentMetadata]
|
|
|
|
-> HashMap SchemaObjId [(MetadataObject, SchemaDependency)]
|
2021-02-14 09:07:52 +03:00
|
|
|
-> m (BuildOutputs, [InconsistentMetadata], DepMap)
|
2019-12-09 01:17:39 +03:00
|
|
|
performIteration iterationNumber cache inconsistencies dependencies = do
|
|
|
|
let (newInconsistencies, prunedDependencies) = pruneDanglingDependents cache dependencies
|
|
|
|
case newInconsistencies of
|
|
|
|
[] -> pure (cache, inconsistencies, HS.fromList . map snd <$> prunedDependencies)
|
|
|
|
_ | iterationNumber < 100 -> do
|
|
|
|
let inconsistentIds = nub $ concatMap imObjectIds newInconsistencies
|
|
|
|
prunedCache = foldl' (flip deleteMetadataObject) cache inconsistentIds
|
|
|
|
allInconsistencies = inconsistencies <> newInconsistencies
|
|
|
|
performIteration (iterationNumber + 1) prunedCache allInconsistencies prunedDependencies
|
|
|
|
| otherwise ->
|
|
|
|
-- Running for 100 iterations without terminating is (hopefully) enormously unlikely
|
|
|
|
-- unless we did something very wrong, so halt the process and abort with some
|
|
|
|
-- debugging information.
|
|
|
|
throwError (err500 Unexpected "schema dependency resolution failed to terminate")
|
|
|
|
{ qeInternal = Just $ object
|
|
|
|
[ "inconsistent_objects" .= object
|
|
|
|
[ "old" .= inconsistencies
|
|
|
|
, "new" .= newInconsistencies ]
|
|
|
|
, "pruned_dependencies" .= (map snd <$> prunedDependencies) ] }
|
|
|
|
|
|
|
|
pruneDanglingDependents
|
2021-02-14 09:07:52 +03:00
|
|
|
:: BuildOutputs
|
2019-12-09 01:17:39 +03:00
|
|
|
-> HashMap SchemaObjId [(MetadataObject, SchemaDependency)]
|
|
|
|
-> ([InconsistentMetadata], HashMap SchemaObjId [(MetadataObject, SchemaDependency)])
|
|
|
|
pruneDanglingDependents cache = fmap (M.filter (not . null)) . traverse do
|
|
|
|
partitionEithers . map \(metadataObject, dependency) -> case resolveDependency dependency of
|
|
|
|
Right () -> Right (metadataObject, dependency)
|
2021-04-14 20:51:02 +03:00
|
|
|
Left errorMessage -> Left (InconsistentObject errorMessage Nothing metadataObject)
|
2019-12-09 01:17:39 +03:00
|
|
|
where
|
|
|
|
resolveDependency :: SchemaDependency -> Either Text ()
|
|
|
|
resolveDependency (SchemaDependency objectId _) = case objectId of
|
2020-12-28 15:56:00 +03:00
|
|
|
SOSource source -> void $ M.lookup source (_boSources cache)
|
|
|
|
`onNothing` Left ("no such source exists: " <>> source)
|
2020-05-27 18:02:58 +03:00
|
|
|
SORemoteSchema remoteSchemaName -> unless (remoteSchemaName `M.member` _boRemoteSchemas cache) $
|
|
|
|
Left $ "remote schema " <> remoteSchemaName <<> " is not found"
|
2020-12-21 12:11:37 +03:00
|
|
|
SORemoteSchemaPermission remoteSchemaName roleName -> do
|
|
|
|
remoteSchema <-
|
|
|
|
onNothing (M.lookup remoteSchemaName $ _boRemoteSchemas cache)
|
|
|
|
$ Left $ "remote schema " <> remoteSchemaName <<> " is not found"
|
|
|
|
unless (roleName `M.member` _rscPermissions (fst remoteSchema)) $
|
|
|
|
Left $ "no permission defined on remote schema " <> remoteSchemaName
|
|
|
|
<<> " for role " <>> roleName
|
2021-03-15 16:02:58 +03:00
|
|
|
SOSourceObj source exists -> do
|
|
|
|
AB.dispatchAnyBackend @BackendMetadata exists $ \sourceObjId -> do
|
|
|
|
sourceInfo <- castSourceInfo source sourceObjId
|
|
|
|
case sourceObjId of
|
|
|
|
SOITable tableName -> do
|
|
|
|
void $ resolveTable sourceInfo tableName
|
|
|
|
SOIFunction functionName -> void $
|
|
|
|
M.lookup functionName (_siFunctions sourceInfo)
|
|
|
|
`onNothing` Left ("function " <> functionName <<> " is not tracked")
|
|
|
|
SOITableObj tableName tableObjectId -> do
|
|
|
|
tableInfo <- resolveTable sourceInfo tableName
|
|
|
|
case tableObjectId of
|
|
|
|
TOCol columnName ->
|
|
|
|
void $ resolveField tableInfo (columnToFieldName tableInfo columnName) _FIColumn "column"
|
|
|
|
TORel relName ->
|
|
|
|
void $ resolveField tableInfo (fromRel relName) _FIRelationship "relationship"
|
|
|
|
TOComputedField fieldName ->
|
|
|
|
void $ resolveField tableInfo (fromComputedField fieldName) _FIComputedField "computed field"
|
|
|
|
TORemoteRel fieldName ->
|
|
|
|
void $ resolveField tableInfo (fromRemoteRelationship fieldName) _FIRemoteRelationship "remote relationship"
|
|
|
|
TOForeignKey constraintName -> do
|
|
|
|
let foreignKeys = _tciForeignKeys $ _tiCoreInfo tableInfo
|
|
|
|
unless (isJust $ find ((== constraintName) . _cName . _fkConstraint) foreignKeys) $
|
|
|
|
Left $ "no foreign key constraint named " <> constraintName <<> " is "
|
|
|
|
<> "defined for table " <>> tableName
|
|
|
|
TOPerm roleName permType -> withPermType permType \accessor -> do
|
|
|
|
let permLens = permAccToLens accessor
|
|
|
|
unless (has (tiRolePermInfoMap.ix roleName.permLens._Just) tableInfo) $
|
|
|
|
Left $ "no " <> permTypeToCode permType <> " permission defined on table "
|
|
|
|
<> tableName <<> " for role " <>> roleName
|
|
|
|
TOTrigger triggerName ->
|
|
|
|
unless (M.member triggerName (_tiEventTriggerInfoMap tableInfo)) $ Left $
|
|
|
|
"no event trigger named " <> triggerName <<> " is defined for table " <>> tableName
|
2021-07-17 00:18:58 +03:00
|
|
|
SORole roleName -> void $ (M.lookup roleName (_boRoles cache))
|
|
|
|
`onNothing` Left ("parent role " <> roleName <<> " does not exist")
|
2019-12-09 01:17:39 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
castSourceInfo
|
|
|
|
:: (Backend b) => SourceName -> SourceObjId b -> Either Text (SourceInfo b)
|
|
|
|
castSourceInfo sourceName _ =
|
|
|
|
-- TODO: if the cast returns Nothing, we should be throwing an internal error
|
|
|
|
-- the type of the dependency in sources is not as recorded
|
2021-03-15 16:02:58 +03:00
|
|
|
(M.lookup sourceName (_boSources cache) >>= unsafeSourceInfo)
|
2021-02-14 09:07:52 +03:00
|
|
|
`onNothing` Left ("no such source found " <>> sourceName)
|
|
|
|
|
|
|
|
resolveTable sourceInfo tableName =
|
|
|
|
M.lookup tableName (_siTables sourceInfo)
|
2020-12-28 15:56:00 +03:00
|
|
|
`onNothing` Left ("table " <> tableName <<> " is not tracked")
|
2019-12-09 01:17:39 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
columnToFieldName :: forall b. (Backend b) => TableInfo b -> Column b -> FieldName
|
|
|
|
columnToFieldName _ = fromCol @b
|
|
|
|
|
|
|
|
|
|
|
|
resolveField
|
|
|
|
:: Backend b
|
|
|
|
=> TableInfo b -> FieldName -> Getting (First a) (FieldInfo b) a -> Text -> Either Text a
|
2019-12-09 01:17:39 +03:00
|
|
|
resolveField tableInfo fieldName fieldType fieldTypeName = do
|
|
|
|
let coreInfo = _tiCoreInfo tableInfo
|
2021-05-18 16:06:42 +03:00
|
|
|
tableName = tableInfoName tableInfo
|
2019-12-09 01:17:39 +03:00
|
|
|
fieldInfo <- M.lookup fieldName (_tciFieldInfoMap coreInfo) `onNothing` Left
|
|
|
|
("table " <> tableName <<> " has no field named " <>> fieldName)
|
|
|
|
(fieldInfo ^? fieldType) `onNothing` Left
|
|
|
|
("field " <> fieldName <<> "of table " <> tableName <<> " is not a " <> fieldTypeName)
|
|
|
|
|
2020-12-28 15:56:00 +03:00
|
|
|
deleteMetadataObject
|
2021-02-14 09:07:52 +03:00
|
|
|
:: MetadataObjId -> BuildOutputs -> BuildOutputs
|
2021-01-20 03:31:53 +03:00
|
|
|
deleteMetadataObject = \case
|
|
|
|
MOSource name -> boSources %~ M.delete name
|
2021-03-15 16:02:58 +03:00
|
|
|
MOSourceObjId source exists -> AB.dispatchAnyBackend @Backend exists (\sourceObjId -> boSources %~ M.adjust (deleteObjId sourceObjId) source)
|
2021-01-20 03:31:53 +03:00
|
|
|
MORemoteSchema name -> boRemoteSchemas %~ M.delete name
|
2020-12-21 12:11:37 +03:00
|
|
|
MORemoteSchemaPermissions name role -> boRemoteSchemas.ix name._1.rscPermissions %~ M.delete role
|
2021-01-20 03:31:53 +03:00
|
|
|
MOCronTrigger name -> boCronTriggers %~ M.delete name
|
|
|
|
MOCustomTypes -> boCustomTypes %~ const emptyAnnotatedCustomTypes
|
|
|
|
MOAction name -> boActions %~ M.delete name
|
2021-01-29 04:02:34 +03:00
|
|
|
MOEndpoint name -> boEndpoints %~ M.delete name
|
2021-01-20 03:31:53 +03:00
|
|
|
MOActionPermission name role -> boActions.ix name.aiPermissions %~ M.delete role
|
2021-07-17 00:18:58 +03:00
|
|
|
MOInheritedRole name -> boRoles %~ M.delete name
|
2021-08-24 10:36:32 +03:00
|
|
|
MOHostTlsAllowlist host -> removeHostFromAllowList host
|
2021-01-20 03:31:53 +03:00
|
|
|
where
|
2021-08-24 10:36:32 +03:00
|
|
|
removeHostFromAllowList hst bo = bo {
|
|
|
|
_boTlsAllowlist = filter (not . checkForHostnameInAllowlistObject hst) (_boTlsAllowlist bo)
|
|
|
|
}
|
|
|
|
|
2021-03-15 16:02:58 +03:00
|
|
|
deleteObjId :: forall b. (Backend b) => SourceMetadataObjId b -> BackendSourceInfo -> BackendSourceInfo
|
|
|
|
deleteObjId sourceObjId sourceInfo =
|
|
|
|
maybe
|
|
|
|
sourceInfo
|
|
|
|
(AB.mkAnyBackend . deleteObjFn sourceObjId)
|
|
|
|
$ unsafeSourceInfo sourceInfo
|
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
deleteObjFn :: (Backend b) => SourceMetadataObjId b -> SourceInfo b -> SourceInfo b
|
2021-01-20 03:31:53 +03:00
|
|
|
deleteObjFn = \case
|
|
|
|
SMOTable name -> siTables %~ M.delete name
|
|
|
|
SMOFunction name -> siFunctions %~ M.delete name
|
2021-01-29 08:48:17 +03:00
|
|
|
SMOFunctionPermission functionName role ->
|
2021-08-09 13:20:04 +03:00
|
|
|
siFunctions.ix functionName.fiPermissions %~ M.delete role
|
2021-01-20 03:31:53 +03:00
|
|
|
SMOTableObj tableName tableObjectId -> siTables.ix tableName %~ case tableObjectId of
|
|
|
|
MTORel name _ -> tiCoreInfo.tciFieldInfoMap %~ M.delete (fromRel name)
|
|
|
|
MTOComputedField name -> tiCoreInfo.tciFieldInfoMap %~ M.delete (fromComputedField name)
|
|
|
|
MTORemoteRelationship name -> tiCoreInfo.tciFieldInfoMap %~ M.delete (fromRemoteRelationship name)
|
|
|
|
MTOTrigger name -> tiEventTriggerInfoMap %~ M.delete name
|
|
|
|
MTOPerm roleName permType -> withPermType permType \accessor ->
|
|
|
|
tiRolePermInfoMap.ix roleName.permAccToLens accessor .~ Nothing
|