2021-09-24 01:56:37 +03:00
|
|
|
-- |
|
|
|
|
-- Description: Add/Drop computed fields in metadata
|
2019-10-18 11:29:47 +03:00
|
|
|
module Hasura.RQL.DDL.ComputedField
|
2021-09-24 01:56:37 +03:00
|
|
|
( AddComputedField (..),
|
|
|
|
runAddComputedField,
|
|
|
|
DropComputedField,
|
|
|
|
runDropComputedField,
|
|
|
|
dropComputedFieldInMetadata,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Permission
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.ComputedField
|
|
|
|
import Hasura.RQL.Types.Metadata
|
|
|
|
import Hasura.RQL.Types.Metadata.Backend
|
|
|
|
import Hasura.RQL.Types.Metadata.Object
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build
|
|
|
|
import Hasura.RQL.Types.SchemaCacheTypes
|
|
|
|
import Hasura.RQL.Types.Table
|
2021-09-24 01:56:37 +03:00
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
|
|
|
|
|
|
|
data AddComputedField b = AddComputedField
|
2022-08-01 12:32:04 +03:00
|
|
|
{ _afcSource :: SourceName,
|
|
|
|
_afcTable :: TableName b,
|
|
|
|
_afcName :: ComputedFieldName,
|
|
|
|
_afcDefinition :: ComputedFieldDefinition b,
|
|
|
|
_afcComment :: Comment
|
2021-09-24 01:56:37 +03:00
|
|
|
}
|
|
|
|
deriving stock (Generic)
|
Clean metadata arguments
## Description
Thanks to #1664, the Metadata API types no longer require a `ToJSON` instance. This PR follows up with a cleanup of the types of the arguments to the metadata API:
- whenever possible, it moves those argument types to where they're used (RQL.DDL.*)
- it removes all unrequired instances (mostly `ToJSON`)
This PR does not attempt to do it for _all_ such argument types. For some of the metadata operations, the type used to describe the argument to the API and used to represent the value in the metadata are one and the same (like for `CreateEndpoint`). Sometimes, the two types are intertwined in complex ways (`RemoteRelationship` and `RemoteRelationshipDef`). In the spirit of only doing uncontroversial cleaning work, this PR only moves types that are not used outside of RQL.DDL.
Furthermore, this is a small step towards separating the different types all jumbled together in RQL.Types.
## Notes
This PR also improves several `FromJSON` instances to make use of `withObject`, and to use a human readable string instead of a type name in error messages whenever possible. For instance:
- before: `expected Object for Object, but encountered X`
after: `expected Object for add computed field, but encountered X`
- before: `Expecting an object for update query`
after: `expected Object for update query, but encountered X`
This PR also renames `CreateFunctionPermission` to `FunctionPermissionArgument`, to remove the quite surprising `type DropFunctionPermission = CreateFunctionPermission`.
This PR also deletes some dead code, mostly in RQL.DML.
This PR also moves a PG-specific source resolving function from DDL.Schema.Source to the only place where it is used: App.hs.
https://github.com/hasura/graphql-engine-mono/pull/1844
GitOrigin-RevId: a594521194bb7fe6a111b02a9e099896f9fed59c
2021-07-27 13:41:42 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
instance (Backend b) => ToJSON (AddComputedField b) where
|
|
|
|
toJSON = genericToJSON hasuraJSON
|
|
|
|
|
|
|
|
instance (Backend b) => FromJSON (AddComputedField b) where
|
2021-09-20 22:49:33 +03:00
|
|
|
parseJSON = withObject "AddComputedField" $ \o ->
|
2020-12-28 15:56:00 +03:00
|
|
|
AddComputedField
|
|
|
|
<$> o .:? "source" .!= defaultSource
|
|
|
|
<*> o .: "table"
|
|
|
|
<*> o .: "name"
|
|
|
|
<*> o .: "definition"
|
2022-02-16 02:16:34 +03:00
|
|
|
<*> o .:? "comment" .!= Automatic
|
2019-10-18 11:29:47 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runAddComputedField ::
|
|
|
|
forall b m.
|
|
|
|
(BackendMetadata b, MonadError QErr m, CacheRWM m, MetadataM m) =>
|
|
|
|
AddComputedField b ->
|
|
|
|
m EncJSON
|
2019-10-18 11:29:47 +03:00
|
|
|
runAddComputedField q = do
|
2022-04-26 18:12:47 +03:00
|
|
|
void $ withPathK "table" $ askTableInfo @b source table
|
2021-09-24 01:56:37 +03:00
|
|
|
let metadataObj =
|
|
|
|
MOSourceObjId source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SMOTableObj @b table $
|
|
|
|
MTOComputedField computedFieldName
|
2020-12-08 17:22:31 +03:00
|
|
|
metadata = ComputedFieldMetadata computedFieldName (_afcDefinition q) (_afcComment q)
|
2021-09-24 01:56:37 +03:00
|
|
|
buildSchemaCacheFor metadataObj $
|
|
|
|
MetadataModifier $
|
2022-05-04 17:52:29 +03:00
|
|
|
tableMetadataSetter @b source table . tmComputedFields
|
2021-09-24 01:56:37 +03:00
|
|
|
%~ OMap.insert computedFieldName metadata
|
2019-11-20 21:21:30 +03:00
|
|
|
pure successMsg
|
2020-12-08 17:22:31 +03:00
|
|
|
where
|
2020-12-28 15:56:00 +03:00
|
|
|
source = _afcSource q
|
2020-12-08 17:22:31 +03:00
|
|
|
table = _afcTable q
|
|
|
|
computedFieldName = _afcName q
|
2019-10-18 11:29:47 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
data DropComputedField b = DropComputedField
|
2022-08-01 12:32:04 +03:00
|
|
|
{ _dccSource :: SourceName,
|
|
|
|
_dccTable :: TableName b,
|
|
|
|
_dccName :: ComputedFieldName,
|
|
|
|
_dccCascade :: Bool
|
Clean metadata arguments
## Description
Thanks to #1664, the Metadata API types no longer require a `ToJSON` instance. This PR follows up with a cleanup of the types of the arguments to the metadata API:
- whenever possible, it moves those argument types to where they're used (RQL.DDL.*)
- it removes all unrequired instances (mostly `ToJSON`)
This PR does not attempt to do it for _all_ such argument types. For some of the metadata operations, the type used to describe the argument to the API and used to represent the value in the metadata are one and the same (like for `CreateEndpoint`). Sometimes, the two types are intertwined in complex ways (`RemoteRelationship` and `RemoteRelationshipDef`). In the spirit of only doing uncontroversial cleaning work, this PR only moves types that are not used outside of RQL.DDL.
Furthermore, this is a small step towards separating the different types all jumbled together in RQL.Types.
## Notes
This PR also improves several `FromJSON` instances to make use of `withObject`, and to use a human readable string instead of a type name in error messages whenever possible. For instance:
- before: `expected Object for Object, but encountered X`
after: `expected Object for add computed field, but encountered X`
- before: `Expecting an object for update query`
after: `expected Object for update query, but encountered X`
This PR also renames `CreateFunctionPermission` to `FunctionPermissionArgument`, to remove the quite surprising `type DropFunctionPermission = CreateFunctionPermission`.
This PR also deletes some dead code, mostly in RQL.DML.
This PR also moves a PG-specific source resolving function from DDL.Schema.Source to the only place where it is used: App.hs.
https://github.com/hasura/graphql-engine-mono/pull/1844
GitOrigin-RevId: a594521194bb7fe6a111b02a9e099896f9fed59c
2021-07-27 13:41:42 +03:00
|
|
|
}
|
2019-10-18 11:29:47 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
instance (Backend b) => FromJSON (DropComputedField b) where
|
2021-09-20 22:49:33 +03:00
|
|
|
parseJSON = withObject "DropComputedField" $ \o ->
|
2019-10-18 11:29:47 +03:00
|
|
|
DropComputedField
|
2020-12-28 15:56:00 +03:00
|
|
|
<$> o .:? "source" .!= defaultSource
|
|
|
|
<*> o .: "table"
|
2019-10-18 11:29:47 +03:00
|
|
|
<*> o .: "name"
|
|
|
|
<*> o .:? "cascade" .!= False
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runDropComputedField ::
|
|
|
|
forall b m.
|
|
|
|
(QErrM m, CacheRWM m, MetadataM m, BackendMetadata b) =>
|
|
|
|
DropComputedField b ->
|
|
|
|
m EncJSON
|
2020-12-28 15:56:00 +03:00
|
|
|
runDropComputedField (DropComputedField source table computedField cascade) = do
|
2019-10-18 11:29:47 +03:00
|
|
|
-- Validation
|
2021-04-22 00:44:37 +03:00
|
|
|
fields <- withPathK "table" $ _tciFieldInfoMap <$> askTableCoreInfo @b source table
|
2019-10-18 11:29:47 +03:00
|
|
|
void $ withPathK "name" $ askComputedFieldInfo fields computedField
|
|
|
|
|
|
|
|
-- Dependencies check
|
|
|
|
sc <- askSchemaCache
|
2021-09-24 01:56:37 +03:00
|
|
|
let deps =
|
|
|
|
getDependentObjs sc $
|
|
|
|
SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
|
|
|
SOITableObj @b table $
|
|
|
|
TOComputedField computedField
|
2022-05-27 16:33:38 +03:00
|
|
|
unless (cascade || null deps) $ reportDependentObjectsExist deps
|
2019-10-18 11:29:47 +03:00
|
|
|
|
2019-11-20 21:21:30 +03:00
|
|
|
withNewInconsistentObjsCheck do
|
2020-12-08 17:22:31 +03:00
|
|
|
metadataModifiers <- mapM purgeComputedFieldDependency deps
|
2021-09-24 01:56:37 +03:00
|
|
|
buildSchemaCache $
|
|
|
|
MetadataModifier $
|
|
|
|
tableMetadataSetter @b source table
|
|
|
|
%~ dropComputedFieldInMetadata computedField . foldl' (.) id metadataModifiers
|
2019-10-18 11:29:47 +03:00
|
|
|
pure successMsg
|
|
|
|
where
|
|
|
|
purgeComputedFieldDependency = \case
|
2021-02-14 09:07:52 +03:00
|
|
|
-- TODO: do a better check of ensuring that the dependency is as expected.
|
|
|
|
-- i.e, the only allowed dependent objects on a computed fields are permissions
|
|
|
|
-- on the same table
|
2021-03-15 16:02:58 +03:00
|
|
|
SOSourceObj _ exists
|
2021-09-24 01:56:37 +03:00
|
|
|
| Just (SOITableObj _ (TOPerm roleName permType)) <-
|
|
|
|
AB.unpackAnyBackend @b exists ->
|
|
|
|
pure $ dropPermissionInMetadata roleName permType
|
|
|
|
d ->
|
|
|
|
throw500 $
|
|
|
|
"unexpected dependency for computed field "
|
|
|
|
<> computedField <<> "; "
|
|
|
|
<> reportSchemaObj d
|