2020-12-08 17:22:31 +03:00
|
|
|
-- | Functions for updating the metadata (with integrity checking) to incorporate schema changes
|
2019-07-22 15:47:13 +03:00
|
|
|
-- discovered after applying a user-supplied SQL query. None of these functions modify the schema
|
2020-12-08 17:22:31 +03:00
|
|
|
-- cache, so it must be reloaded after the metadata is updated.
|
2019-03-01 12:17:22 +03:00
|
|
|
module Hasura.RQL.DDL.Schema.Rename
|
2020-12-08 17:22:31 +03:00
|
|
|
( renameTableInMetadata,
|
|
|
|
renameColumnInMetadata,
|
|
|
|
renameRelationshipInMetadata,
|
2019-03-01 12:17:22 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2021-03-15 16:02:58 +03:00
|
|
|
import Control.Lens.Combinators
|
2020-12-08 17:22:31 +03:00
|
|
|
import Control.Lens.Operators
|
|
|
|
import Data.Aeson
|
2021-02-14 09:07:52 +03:00
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.HashMap.Strict.InsOrd qualified as OMap
|
|
|
|
import Data.HashSet qualified as Set
|
2021-03-15 16:02:58 +03:00
|
|
|
import Data.Text.Extended
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error
|
2021-03-15 16:02:58 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Permission
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.IR.BoolExp
|
|
|
|
import Hasura.RQL.Types.Backend
|
|
|
|
import Hasura.RQL.Types.Column
|
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.Metadata
|
|
|
|
import Hasura.RQL.Types.Metadata.Backend
|
|
|
|
import Hasura.RQL.Types.Permission
|
|
|
|
import Hasura.RQL.Types.Relationships.Local
|
|
|
|
import Hasura.RQL.Types.Relationships.Remote
|
|
|
|
import Hasura.RQL.Types.Relationships.ToSource
|
|
|
|
import Hasura.RQL.Types.SchemaCache
|
|
|
|
import Hasura.RQL.Types.SchemaCacheTypes
|
|
|
|
import Hasura.RQL.Types.Table
|
scaffolding for remote-schemas module
The main aim of the PR is:
1. To set up a module structure for 'remote-schemas' package.
2. Move parts by the remote schema codebase into the new module structure to validate it.
## Notes to the reviewer
Why a PR with large-ish diff?
1. We've been making progress on the MM project but we don't yet know long it is going to take us to get to the first milestone. To understand this better, we need to figure out the unknowns as soon as possible. Hence I've taken a stab at the first two items in the [end-state](https://gist.github.com/0x777/ca2bdc4284d21c3eec153b51dea255c9) document to figure out the unknowns. Unsurprisingly, there are a bunch of issues that we haven't discussed earlier. These are documented in the 'open questions' section.
1. The diff is large but that is only code moved around and I've added a section that documents how things are moved. In addition, there are fair number of PR comments to help with the review process.
## Changes in the PR
### Module structure
Sets up the module structure as follows:
```
Hasura/
RemoteSchema/
Metadata/
Types.hs
SchemaCache/
Types.hs
Permission.hs
RemoteRelationship.hs
Build.hs
MetadataAPI/
Types.hs
Execute.hs
```
### 1. Types representing metadata are moved
Types that capture metadata information (currently scattered across several RQL modules) are moved into `Hasura.RemoteSchema.Metadata.Types`.
- This new module only depends on very 'core' modules such as
`Hasura.Session` for the notion of roles and `Hasura.Incremental` for `Cacheable` typeclass.
- The requirement on database modules is avoided by generalizing the remote schemas metadata to accept an arbitrary 'r' for a remote relationship
definition.
### 2. SchemaCache related types and build logic have been moved
Types that represent remote schemas information in SchemaCache are moved into `Hasura.RemoteSchema.SchemaCache.Types`.
Similar to `H.RS.Metadata.Types`, this module depends on 'core' modules except for `Hasura.GraphQL.Parser.Variable`. It has something to do with remote relationships but I haven't spent time looking into it. The validation of 'remote relationships to remote schema' is also something that needs to be looked at.
Rips out the logic that builds remote schema's SchemaCache information from the monolithic `buildSchemaCacheRule` and moves it into `Hasura.RemoteSchema.SchemaCache.Build`. Further, the `.SchemaCache.Permission` and `.SchemaCache.RemoteRelationship` have been created from existing modules that capture schema cache building logic for those two components.
This was a fair amount of work. On main, currently remote schema's SchemaCache information is built in two phases - in the first phase, 'permissions' and 'remote relationships' are ignored and in the second phase they are filled in.
While remote relationships can only be resolved after partially resolving sources and other remote schemas, the same isn't true for permissions. Further, most of the work that is done to resolve remote relationships can be moved to the first phase so that the second phase can be a very simple traversal.
This is the approach that was taken - resolve permissions and as much as remote relationships information in the first phase.
### 3. Metadata APIs related types and build logic have been moved
The types that represent remote schema related metadata APIs and the execution logic have been moved to `Hasura.RemoteSchema.MetadataAPI.Types` and `.Execute` modules respectively.
## Open questions:
1. `Hasura.RemoteSchema.Metadata.Types` is so called because I was hoping that all of the metadata related APIs of remote schema can be brought in at `Hasura.RemoteSchema.Metadata.API`. However, as metadata APIs depended on functions from `SchemaCache` module (see [1](https://github.com/hasura/graphql-engine-mono/blob/ceba6d62264603ee5d279814677b29bcc43ecaea/server/src-lib/Hasura/RQL/DDL/RemoteSchema.hs#L55) and [2](https://github.com/hasura/graphql-engine-mono/blob/ceba6d62264603ee5d279814677b29bcc43ecaea/server/src-lib/Hasura/RQL/DDL/RemoteSchema.hs#L91), it made more sense to create a separate top-level module for `MetadataAPI`s.
Maybe we can just have `Hasura.RemoteSchema.Metadata` and get rid of the extra nesting or have `Hasura.RemoteSchema.Metadata.{Core,Permission,RemoteRelationship}` if we want to break them down further.
1. `buildRemoteSchemas` in `H.RS.SchemaCache.Build` has the following type:
```haskell
buildRemoteSchemas ::
( ArrowChoice arr,
Inc.ArrowDistribute arr,
ArrowWriter (Seq CollectedInfo) arr,
Inc.ArrowCache m arr,
MonadIO m,
HasHttpManagerM m,
Inc.Cacheable remoteRelationshipDefinition,
ToJSON remoteRelationshipDefinition,
MonadError QErr m
) =>
Env.Environment ->
( (Inc.Dependency (HashMap RemoteSchemaName Inc.InvalidationKey), OrderedRoles),
[RemoteSchemaMetadataG remoteRelationshipDefinition]
)
`arr` HashMap RemoteSchemaName (PartiallyResolvedRemoteSchemaCtxG remoteRelationshipDefinition, MetadataObject)
```
Note the dependence on `CollectedInfo` which is defined as
```haskell
data CollectedInfo
= CIInconsistency InconsistentMetadata
| CIDependency
MetadataObject
-- ^ for error reporting on missing dependencies
SchemaObjId
SchemaDependency
deriving (Eq)
```
this pretty much means that remote schemas is dependent on types from databases, actions, ....
How do we fix this? Maybe introduce a typeclass such as `ArrowCollectRemoteSchemaDependencies` which is defined in `Hasura.RemoteSchema` and then implemented in graphql-engine?
1. The dependency on `buildSchemaCacheFor` in `.MetadataAPI.Execute` which has the following signature:
```haskell
buildSchemaCacheFor ::
(QErrM m, CacheRWM m, MetadataM m) =>
MetadataObjId ->
MetadataModifier ->
```
This can be easily resolved if we restrict what the metadata APIs are allowed to do. Currently, they operate in an unfettered access to modify SchemaCache (the `CacheRWM` constraint):
```haskell
runAddRemoteSchema ::
( QErrM m,
CacheRWM m,
MonadIO m,
HasHttpManagerM m,
MetadataM m,
Tracing.MonadTrace m
) =>
Env.Environment ->
AddRemoteSchemaQuery ->
m EncJSON
```
This should instead be changed to restrict remote schema APIs to only modify remote schema metadata (but has access to the remote schemas part of the schema cache), this dependency is completely removed.
```haskell
runAddRemoteSchema ::
( QErrM m,
MonadIO m,
HasHttpManagerM m,
MonadReader RemoteSchemasSchemaCache m,
MonadState RemoteSchemaMetadata m,
Tracing.MonadTrace m
) =>
Env.Environment ->
AddRemoteSchemaQuery ->
m RemoteSchemeMetadataObjId
```
The idea is that the core graphql-engine would call these functions and then call
`buildSchemaCacheFor`.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6291
GitOrigin-RevId: 51357148c6404afe70219afa71bd1d59bdf4ffc6
2022-10-21 06:13:07 +03:00
|
|
|
import Hasura.RemoteSchema.Metadata
|
2021-03-15 16:02:58 +03:00
|
|
|
import Hasura.SQL.AnyBackend qualified as AB
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.SQL.Backend
|
2021-03-15 16:02:58 +03:00
|
|
|
import Hasura.Session
|
2021-02-14 09:07:52 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
data RenameItem (b :: BackendType) a = RenameItem
|
2022-08-01 12:32:04 +03:00
|
|
|
{ _riTable :: TableName b,
|
|
|
|
_riOld :: a,
|
|
|
|
_riNew :: a
|
2021-02-14 09:07:52 +03:00
|
|
|
}
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
type RenameCol (b :: BackendType) = RenameItem b (Column b)
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
data RenameField b
|
2022-08-01 12:32:04 +03:00
|
|
|
= RFCol (RenameCol b)
|
|
|
|
| RFRel (RenameItem b RelName)
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
type RenameTable b = (TableName b, TableName b)
|
|
|
|
|
|
|
|
data Rename b
|
2022-08-01 12:32:04 +03:00
|
|
|
= RTable (RenameTable b)
|
|
|
|
| RField (RenameField b)
|
2019-09-05 10:34:53 +03:00
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
otherDeps :: QErrM m => Text -> SchemaObjId -> m ()
|
2019-07-23 22:11:34 +03:00
|
|
|
otherDeps errMsg d =
|
2021-07-23 02:06:10 +03:00
|
|
|
throw500 $
|
|
|
|
"unexpected dependency "
|
2019-07-23 22:11:34 +03:00
|
|
|
<> reportSchemaObj d
|
|
|
|
<> "; "
|
|
|
|
<> errMsg
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2021-07-23 02:06:10 +03:00
|
|
|
-- | Replace all references to a given table name by its new name across the entire metadata.
|
|
|
|
--
|
|
|
|
-- This function will make use of the metadata dependency graph (see 'getDependentObjs') to identify
|
|
|
|
-- all places that refer to the old table name, and replace it accordingly. Most operations will
|
|
|
|
-- occur within the same source, such as table references in relationships and permissions.
|
|
|
|
-- Dependencies across sources can happen in the case of cross-source relationships.
|
|
|
|
--
|
|
|
|
-- This function will fail if it encounters a nonsensical dependency; for instance, if there's a
|
|
|
|
-- dependency from that table to a source.
|
|
|
|
--
|
|
|
|
-- For more information about the dependency graph, see 'SchemaObjId'.
|
2020-12-08 17:22:31 +03:00
|
|
|
renameTableInMetadata ::
|
2021-03-15 16:02:58 +03:00
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
2020-12-08 17:22:31 +03:00
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
2021-02-14 09:07:52 +03:00
|
|
|
BackendMetadata b
|
2020-12-08 17:22:31 +03:00
|
|
|
) =>
|
2021-04-22 00:44:37 +03:00
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
TableName b ->
|
|
|
|
m ()
|
2020-12-28 15:56:00 +03:00
|
|
|
renameTableInMetadata source newQT oldQT = do
|
2019-03-01 12:17:22 +03:00
|
|
|
sc <- askSchemaCache
|
2021-03-15 16:02:58 +03:00
|
|
|
let allDeps =
|
|
|
|
getDependentObjs sc $
|
|
|
|
SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
2021-04-22 00:44:37 +03:00
|
|
|
SOITable @b oldQT
|
2020-06-09 17:29:39 +03:00
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
-- update all dependant schema objects
|
|
|
|
forM_ allDeps $ \case
|
2021-07-23 02:06:10 +03:00
|
|
|
-- the dependend object is a source object in the same source
|
|
|
|
sobj@(SOSourceObj depSourceName exists)
|
|
|
|
| depSourceName == source,
|
|
|
|
Just sourceObjId <- AB.unpackAnyBackend @b exists ->
|
|
|
|
case sourceObjId of
|
|
|
|
SOITableObj refQT (TORel rn) ->
|
|
|
|
updateRelDefs @b source refQT rn (oldQT, newQT)
|
|
|
|
SOITableObj refQT (TOPerm rn pt) ->
|
|
|
|
updatePermFlds @b source refQT rn pt $ RTable (oldQT, newQT)
|
|
|
|
-- A trigger's definition is not dependent on the table directly
|
|
|
|
SOITableObj _ (TOTrigger _) -> pure ()
|
|
|
|
-- A remote relationship's definition is not dependent on the table directly
|
|
|
|
SOITableObj _ (TORemoteRel _) -> pure ()
|
|
|
|
_ -> otherDeps errMsg sobj
|
|
|
|
-- the dependend object is a source object in a different source
|
|
|
|
sobj@(SOSourceObj depSourceName exists) ->
|
2021-10-22 17:49:15 +03:00
|
|
|
AB.dispatchAnyBackend @Backend exists \(sourceObjId :: SourceObjId b') ->
|
2021-07-23 02:06:10 +03:00
|
|
|
case sourceObjId of
|
|
|
|
SOITableObj tableName (TORemoteRel remoteRelationshipName) -> do
|
|
|
|
updateTableInRemoteRelationshipRHS @b' @b depSourceName tableName remoteRelationshipName (oldQT, newQT)
|
|
|
|
-- only remote relationships might create dependencies across sources
|
|
|
|
_ -> otherDeps errMsg sobj
|
|
|
|
-- any other kind of dependent object (erroneous)
|
2019-03-01 12:17:22 +03:00
|
|
|
d -> otherDeps errMsg d
|
2020-12-08 17:22:31 +03:00
|
|
|
-- Update table name in metadata
|
2021-04-22 00:44:37 +03:00
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
|
|
|
metaSources . ix source . (toSourceMetadata @b) . smTables %~ \tables ->
|
2020-12-08 17:22:31 +03:00
|
|
|
flip (maybe tables) (OMap.lookup oldQT tables) $
|
|
|
|
\tableMeta -> OMap.delete oldQT $ OMap.insert newQT tableMeta {_tmTable = newQT} tables
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
errMsg = "cannot rename table " <> oldQT <<> " to " <>> newQT
|
2020-12-08 17:22:31 +03:00
|
|
|
|
2021-07-23 02:06:10 +03:00
|
|
|
-- | Replace all references to a given column name by its new name across the entire metadata.
|
|
|
|
--
|
|
|
|
-- This function will make use of the metadata dependency graph (see 'getDependentObjs') to identify
|
|
|
|
-- all places that refer to the old column name, and replace it accordingly. Most operations will
|
|
|
|
-- occur within the same source, such as column references in relationships and permissions.
|
|
|
|
-- Dependencies across sources can happen in the case of cross-source relationships.
|
|
|
|
--
|
|
|
|
-- This function will fail if it encounters a nonsensical dependency; for instance, if there's a
|
|
|
|
-- dependency from that table to a source.
|
|
|
|
--
|
|
|
|
-- For more information about the dependency graph, see 'SchemaObjId'.
|
2020-12-08 17:22:31 +03:00
|
|
|
renameColumnInMetadata ::
|
2021-02-14 09:07:52 +03:00
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
2020-12-08 17:22:31 +03:00
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
2021-02-14 09:07:52 +03:00
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
Column b ->
|
|
|
|
Column b ->
|
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
FieldInfoMap (FieldInfo b) ->
|
|
|
|
m ()
|
2020-12-28 15:56:00 +03:00
|
|
|
renameColumnInMetadata oCol nCol source qt fieldInfo = do
|
2019-03-01 12:17:22 +03:00
|
|
|
sc <- askSchemaCache
|
|
|
|
-- Check if any relation exists with new column name
|
|
|
|
assertFldNotExists
|
|
|
|
-- Fetch dependent objects
|
2021-03-15 16:02:58 +03:00
|
|
|
let depObjs =
|
|
|
|
getDependentObjs sc $
|
|
|
|
SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
2021-04-22 00:44:37 +03:00
|
|
|
SOITableObj @b qt $
|
|
|
|
TOCol @b oCol
|
2021-07-23 02:06:10 +03:00
|
|
|
renameItem = RenameItem @b qt oCol nCol
|
|
|
|
renameFld = RFCol renameItem
|
2019-03-01 12:17:22 +03:00
|
|
|
-- Update dependent objects
|
|
|
|
forM_ depObjs $ \case
|
2021-07-23 02:06:10 +03:00
|
|
|
-- the dependend object is a source object in the same source
|
|
|
|
sobj@(SOSourceObj depSourceName exists)
|
|
|
|
| depSourceName == source,
|
|
|
|
Just sourceObjId <- AB.unpackAnyBackend @b exists ->
|
|
|
|
case sourceObjId of
|
|
|
|
SOITableObj refQT (TOPerm role pt) ->
|
|
|
|
updatePermFlds @b source refQT role pt $ RField renameFld
|
|
|
|
SOITableObj refQT (TORel rn) ->
|
|
|
|
updateColInRel @b source refQT rn renameItem
|
|
|
|
SOITableObj refQT (TOTrigger triggerName) ->
|
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
|
|
|
tableMetadataSetter @b source refQT . tmEventTriggers . ix triggerName
|
|
|
|
%~ updateColumnInEventTrigger @b refQT oCol nCol qt
|
|
|
|
SOITableObj _ (TORemoteRel remoteRelName) ->
|
|
|
|
updateColInRemoteRelationshipLHS source remoteRelName renameItem
|
|
|
|
_ -> otherDeps errMsg sobj
|
|
|
|
-- the dependend object is a source object in a different source
|
|
|
|
sobj@(SOSourceObj depSourceName exists) ->
|
2021-10-22 17:49:15 +03:00
|
|
|
AB.dispatchAnyBackend @Backend exists \(sourceObjId :: SourceObjId b') ->
|
2021-07-23 02:06:10 +03:00
|
|
|
case sourceObjId of
|
|
|
|
SOITableObj tableName (TORemoteRel remoteRelationshipName) -> do
|
|
|
|
updateColInRemoteRelationshipRHS @b' @b depSourceName tableName remoteRelationshipName renameItem
|
|
|
|
-- only remote relationships might create dependencies across sources
|
|
|
|
_ -> otherDeps errMsg sobj
|
|
|
|
-- any other kind of dependent object (erroneous)
|
2019-03-01 12:17:22 +03:00
|
|
|
d -> otherDeps errMsg d
|
2019-10-03 10:45:52 +03:00
|
|
|
-- Update custom column names
|
2021-04-22 00:44:37 +03:00
|
|
|
possiblyUpdateCustomColumnNames @b source qt oCol nCol
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
errMsg = "cannot rename column " <> oCol <<> " to " <>> nCol
|
|
|
|
assertFldNotExists =
|
2021-02-14 09:07:52 +03:00
|
|
|
case M.lookup (fromCol @b oCol) fieldInfo of
|
2019-03-01 12:17:22 +03:00
|
|
|
Just (FIRelationship _) ->
|
|
|
|
throw400 AlreadyExists $
|
|
|
|
"cannot rename column "
|
|
|
|
<> oCol
|
|
|
|
<<> " to "
|
|
|
|
<> nCol
|
|
|
|
<<> " in table "
|
|
|
|
<> qt
|
|
|
|
<<> " as a relationship with the name already exists"
|
2020-12-08 17:22:31 +03:00
|
|
|
_ -> pure ()
|
|
|
|
|
|
|
|
renameRelationshipInMetadata ::
|
2021-03-15 16:02:58 +03:00
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
2020-12-08 17:22:31 +03:00
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
2021-02-14 09:07:52 +03:00
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
RelName ->
|
|
|
|
RelType ->
|
|
|
|
RelName ->
|
|
|
|
m ()
|
2020-12-28 15:56:00 +03:00
|
|
|
renameRelationshipInMetadata source qt oldRN relType newRN = do
|
2019-03-01 12:17:22 +03:00
|
|
|
sc <- askSchemaCache
|
2021-03-15 16:02:58 +03:00
|
|
|
let depObjs =
|
|
|
|
getDependentObjs sc $
|
|
|
|
SOSourceObj source $
|
|
|
|
AB.mkAnyBackend $
|
2021-04-22 00:44:37 +03:00
|
|
|
SOITableObj @b qt $
|
2021-03-15 16:02:58 +03:00
|
|
|
TORel oldRN
|
2021-04-22 00:44:37 +03:00
|
|
|
renameFld = RFRel $ RenameItem @b qt oldRN newRN
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
forM_ depObjs $ \case
|
2021-04-22 00:44:37 +03:00
|
|
|
sobj@(SOSourceObj _ exists) -> case AB.unpackAnyBackend @b exists of
|
2021-03-15 16:02:58 +03:00
|
|
|
Just (SOITableObj refQT (TOPerm role pt)) ->
|
2021-04-22 00:44:37 +03:00
|
|
|
updatePermFlds @b source refQT role pt $ RField renameFld
|
2021-03-15 16:02:58 +03:00
|
|
|
_ -> otherDeps errMsg sobj
|
2019-03-01 12:17:22 +03:00
|
|
|
d -> otherDeps errMsg d
|
2021-04-22 00:44:37 +03:00
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
|
|
|
tableMetadataSetter @b source qt %~ case relType of
|
2020-12-08 17:22:31 +03:00
|
|
|
ObjRel -> tmObjectRelationships %~ rewriteRelationships
|
|
|
|
ArrRel -> tmArrayRelationships %~ rewriteRelationships
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
errMsg = "cannot rename relationship " <> oldRN <<> " to " <>> newRN
|
2020-12-08 17:22:31 +03:00
|
|
|
rewriteRelationships ::
|
|
|
|
Relationships (RelDef a) -> Relationships (RelDef a)
|
|
|
|
rewriteRelationships relationsMap =
|
|
|
|
flip (maybe relationsMap) (OMap.lookup oldRN relationsMap) $
|
|
|
|
\rd -> OMap.insert newRN rd {_rdName = newRN} $ OMap.delete oldRN relationsMap
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
-- update table names in relationship definition
|
|
|
|
updateRelDefs ::
|
2021-02-14 09:07:52 +03:00
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
2020-12-08 17:22:31 +03:00
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
2021-02-14 09:07:52 +03:00
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
RelName ->
|
|
|
|
RenameTable b ->
|
|
|
|
m ()
|
2020-12-28 15:56:00 +03:00
|
|
|
updateRelDefs source qt rn renameTable = do
|
2022-04-26 18:12:47 +03:00
|
|
|
fim <- askTableFieldInfoMap @b source qt
|
2019-03-01 12:17:22 +03:00
|
|
|
ri <- askRelType fim rn ""
|
2020-12-28 15:56:00 +03:00
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
|
|
|
tableMetadataSetter source qt %~ case riType ri of
|
2020-12-08 17:22:31 +03:00
|
|
|
ObjRel -> tmObjectRelationships . ix rn %~ updateObjRelDef renameTable
|
|
|
|
ArrRel -> tmArrayRelationships . ix rn %~ updateArrRelDef renameTable
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
2021-02-14 09:07:52 +03:00
|
|
|
updateObjRelDef :: RenameTable b -> ObjRelDef b -> ObjRelDef b
|
2020-12-08 17:22:31 +03:00
|
|
|
updateObjRelDef (oldQT, newQT) =
|
|
|
|
rdUsing %~ \case
|
|
|
|
RUFKeyOn fk -> RUFKeyOn fk
|
2021-03-03 16:02:00 +03:00
|
|
|
RUManual (RelManualConfig origQT rmCols rmIO) ->
|
2020-12-08 17:22:31 +03:00
|
|
|
let updQT = bool origQT newQT $ oldQT == origQT
|
2021-03-03 16:02:00 +03:00
|
|
|
in RUManual $ RelManualConfig updQT rmCols rmIO
|
2020-12-08 17:22:31 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
updateArrRelDef :: RenameTable b -> ArrRelDef b -> ArrRelDef b
|
2020-12-08 17:22:31 +03:00
|
|
|
updateArrRelDef (oldQT, newQT) =
|
|
|
|
rdUsing %~ \case
|
|
|
|
RUFKeyOn (ArrRelUsingFKeyOn origQT c) ->
|
|
|
|
let updQT = getUpdQT origQT
|
|
|
|
in RUFKeyOn $ ArrRelUsingFKeyOn updQT c
|
2021-03-03 16:02:00 +03:00
|
|
|
RUManual (RelManualConfig origQT rmCols rmIO) ->
|
2020-12-08 17:22:31 +03:00
|
|
|
let updQT = getUpdQT origQT
|
2021-03-03 16:02:00 +03:00
|
|
|
in RUManual $ RelManualConfig updQT rmCols rmIO
|
2020-12-08 17:22:31 +03:00
|
|
|
where
|
|
|
|
getUpdQT origQT = bool origQT newQT $ oldQT == origQT
|
2019-03-01 12:17:22 +03:00
|
|
|
|
[Preview] Inherited roles for postgres read queries
fixes #3868
docker image - `hasura/graphql-engine:inherited-roles-preview-48b73a2de`
Note:
To be able to use the inherited roles feature, the graphql-engine should be started with the env variable `HASURA_GRAPHQL_EXPERIMENTAL_FEATURES` set to `inherited_roles`.
Introduction
------------
This PR implements the idea of multiple roles as presented in this [paper](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/FGALanguageICDE07.pdf). The multiple roles feature in this PR can be used via inherited roles. An inherited role is a role which can be created by combining multiple singular roles. For example, if there are two roles `author` and `editor` configured in the graphql-engine, then we can create a inherited role with the name of `combined_author_editor` role which will combine the select permissions of the `author` and `editor` roles and then make GraphQL queries using the `combined_author_editor`.
How are select permissions of different roles are combined?
------------------------------------------------------------
A select permission includes 5 things:
1. Columns accessible to the role
2. Row selection filter
3. Limit
4. Allow aggregation
5. Scalar computed fields accessible to the role
Suppose there are two roles, `role1` gives access to the `address` column with row filter `P1` and `role2` gives access to both the `address` and the `phone` column with row filter `P2` and we create a new role `combined_roles` which combines `role1` and `role2`.
Let's say the following GraphQL query is queried with the `combined_roles` role.
```graphql
query {
employees {
address
phone
}
}
```
This will translate to the following SQL query:
```sql
select
(case when (P1 or P2) then address else null end) as address,
(case when P2 then phone else null end) as phone
from employee
where (P1 or P2)
```
The other parameters of the select permission will be combined in the following manner:
1. Limit - Minimum of the limits will be the limit of the inherited role
2. Allow aggregations - If any of the role allows aggregation, then the inherited role will allow aggregation
3. Scalar computed fields - same as table column fields, as in the above example
APIs for inherited roles:
----------------------
1. `add_inherited_role`
`add_inherited_role` is the [metadata API](https://hasura.io/docs/1.0/graphql/core/api-reference/index.html#schema-metadata-api) to create a new inherited role. It accepts two arguments
`role_name`: the name of the inherited role to be added (String)
`role_set`: list of roles that need to be combined (Array of Strings)
Example:
```json
{
"type": "add_inherited_role",
"args": {
"role_name":"combined_user",
"role_set":[
"user",
"user1"
]
}
}
```
After adding the inherited role, the inherited role can be used like single roles like earlier
Note:
An inherited role can only be created with non-inherited/singular roles.
2. `drop_inherited_role`
The `drop_inherited_role` API accepts the name of the inherited role and drops it from the metadata. It accepts a single argument:
`role_name`: name of the inherited role to be dropped
Example:
```json
{
"type": "drop_inherited_role",
"args": {
"role_name":"combined_user"
}
}
```
Metadata
---------
The derived roles metadata will be included under the `experimental_features` key while exporting the metadata.
```json
{
"experimental_features": {
"derived_roles": [
{
"role_name": "manager_is_employee_too",
"role_set": [
"employee",
"manager"
]
}
]
}
}
```
Scope
------
Only postgres queries and subscriptions are supported in this PR.
Important points:
-----------------
1. All columns exposed to an inherited role will be marked as `nullable`, this is done so that cell value nullification can be done.
TODOs
-------
- [ ] Tests
- [ ] Test a GraphQL query running with a inherited role without enabling inherited roles in experimental features
- [] Tests for aggregate queries, limit, computed fields, functions, subscriptions (?)
- [ ] Introspection test with a inherited role (nullability changes in a inherited role)
- [ ] Docs
- [ ] Changelog
Co-authored-by: Vamshi Surabhi <6562944+0x777@users.noreply.github.com>
GitOrigin-RevId: 3b8ee1e11f5ceca80fe294f8c074d42fbccfec63
2021-03-08 14:14:13 +03:00
|
|
|
-- | update fields in permissions
|
2020-12-08 17:22:31 +03:00
|
|
|
updatePermFlds ::
|
2021-02-14 09:07:52 +03:00
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
2020-12-08 17:22:31 +03:00
|
|
|
CacheRM m,
|
|
|
|
MonadWriter MetadataModifier m,
|
2021-02-14 09:07:52 +03:00
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
RoleName ->
|
|
|
|
PermType ->
|
|
|
|
Rename b ->
|
|
|
|
m ()
|
2020-12-28 15:56:00 +03:00
|
|
|
updatePermFlds source refQT rn pt rename = do
|
2022-04-26 18:12:47 +03:00
|
|
|
tables <- fold <$> askTableCache source
|
2021-02-14 09:07:52 +03:00
|
|
|
let withTables :: Reader (TableCache b) a -> a
|
2020-12-08 17:22:31 +03:00
|
|
|
withTables = flip runReader tables
|
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
2020-12-28 15:56:00 +03:00
|
|
|
tableMetadataSetter source refQT %~ case pt of
|
2020-12-08 17:22:31 +03:00
|
|
|
PTInsert ->
|
|
|
|
tmInsertPermissions . ix rn . pdPermission %~ \insPerm ->
|
|
|
|
withTables $ updateInsPermFlds refQT rename insPerm
|
|
|
|
PTSelect ->
|
|
|
|
tmSelectPermissions . ix rn . pdPermission %~ \selPerm ->
|
|
|
|
withTables $ updateSelPermFlds refQT rename selPerm
|
|
|
|
PTUpdate ->
|
|
|
|
tmUpdatePermissions . ix rn . pdPermission %~ \updPerm ->
|
|
|
|
withTables $ updateUpdPermFlds refQT rename updPerm
|
|
|
|
PTDelete ->
|
|
|
|
tmDeletePermissions . ix rn . pdPermission %~ \delPerm ->
|
|
|
|
withTables $ updateDelPermFlds refQT rename delPerm
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
updateInsPermFlds ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(MonadReader (TableCache b) m, Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
Rename b ->
|
2022-04-06 15:47:35 +03:00
|
|
|
PermDefPermission b InsPerm ->
|
|
|
|
m (PermDefPermission b InsPerm)
|
2022-05-31 17:41:09 +03:00
|
|
|
updateInsPermFlds refQT rename (InsPerm' (InsPerm chk preset cols backendOnly)) =
|
2020-12-08 17:22:31 +03:00
|
|
|
case rename of
|
2019-09-05 10:34:53 +03:00
|
|
|
RTable rt -> do
|
|
|
|
let updChk = updateTableInBoolExp rt chk
|
2022-05-31 17:41:09 +03:00
|
|
|
pure $ InsPerm' $ InsPerm updChk preset cols backendOnly
|
2019-09-05 10:34:53 +03:00
|
|
|
RField rf -> do
|
|
|
|
updChk <- updateFieldInBoolExp refQT rf chk
|
|
|
|
let updPresetM = updatePreset refQT rf <$> preset
|
|
|
|
updColsM = updateCols refQT rf <$> cols
|
2022-05-31 17:41:09 +03:00
|
|
|
pure $ InsPerm' $ InsPerm updChk updPresetM updColsM backendOnly
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateSelPermFlds ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(MonadReader (TableCache b) m, Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
Rename b ->
|
2022-04-06 15:47:35 +03:00
|
|
|
PermDefPermission b SelPerm ->
|
|
|
|
m (PermDefPermission b SelPerm)
|
2022-06-07 08:32:08 +03:00
|
|
|
updateSelPermFlds refQT rename (SelPerm' (SelPerm cols fltr limit aggAllwd computedFields allowedQueryRootFieldTypes allowedSubsRootFieldTypes)) = do
|
2020-12-08 17:22:31 +03:00
|
|
|
case rename of
|
2019-09-05 10:34:53 +03:00
|
|
|
RTable rt -> do
|
|
|
|
let updFltr = updateTableInBoolExp rt fltr
|
2022-06-07 08:32:08 +03:00
|
|
|
pure $ SelPerm' $ SelPerm cols updFltr limit aggAllwd computedFields allowedQueryRootFieldTypes allowedSubsRootFieldTypes
|
2019-09-05 10:34:53 +03:00
|
|
|
RField rf -> do
|
|
|
|
updFltr <- updateFieldInBoolExp refQT rf fltr
|
|
|
|
let updCols = updateCols refQT rf cols
|
2022-06-07 08:32:08 +03:00
|
|
|
pure $ SelPerm' $ SelPerm updCols updFltr limit aggAllwd computedFields allowedQueryRootFieldTypes allowedSubsRootFieldTypes
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateUpdPermFlds ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(MonadReader (TableCache b) m, Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
Rename b ->
|
2022-04-06 15:47:35 +03:00
|
|
|
PermDefPermission b UpdPerm ->
|
|
|
|
m (PermDefPermission b UpdPerm)
|
2022-05-31 17:41:09 +03:00
|
|
|
updateUpdPermFlds refQT rename (UpdPerm' (UpdPerm cols preset fltr check backendOnly)) = do
|
2020-12-08 17:22:31 +03:00
|
|
|
case rename of
|
2019-09-05 10:34:53 +03:00
|
|
|
RTable rt -> do
|
|
|
|
let updFltr = updateTableInBoolExp rt fltr
|
2020-02-13 10:38:49 +03:00
|
|
|
updCheck = fmap (updateTableInBoolExp rt) check
|
2022-05-31 17:41:09 +03:00
|
|
|
pure $ UpdPerm' $ UpdPerm cols preset updFltr updCheck backendOnly
|
2019-09-05 10:34:53 +03:00
|
|
|
RField rf -> do
|
|
|
|
updFltr <- updateFieldInBoolExp refQT rf fltr
|
2020-02-13 10:38:49 +03:00
|
|
|
updCheck <- traverse (updateFieldInBoolExp refQT rf) check
|
2019-09-05 10:34:53 +03:00
|
|
|
let updCols = updateCols refQT rf cols
|
|
|
|
updPresetM = updatePreset refQT rf <$> preset
|
2022-05-31 17:41:09 +03:00
|
|
|
pure $ UpdPerm' $ UpdPerm updCols updPresetM updFltr updCheck backendOnly
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateDelPermFlds ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(MonadReader (TableCache b) m, Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
Rename b ->
|
2022-04-06 15:47:35 +03:00
|
|
|
PermDefPermission b DelPerm ->
|
|
|
|
m (PermDefPermission b DelPerm)
|
2022-05-31 17:41:09 +03:00
|
|
|
updateDelPermFlds refQT rename (DelPerm' (DelPerm fltr backendOnly)) = do
|
|
|
|
case rename of
|
|
|
|
RTable rt -> do
|
|
|
|
let updFltr = updateTableInBoolExp rt fltr
|
|
|
|
pure $ DelPerm' $ DelPerm updFltr backendOnly
|
|
|
|
RField rf -> do
|
|
|
|
updFltr <- updateFieldInBoolExp refQT rf fltr
|
|
|
|
pure $ DelPerm' $ DelPerm updFltr backendOnly
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updatePreset ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(Backend b) =>
|
2021-07-23 02:06:10 +03:00
|
|
|
TableName b ->
|
|
|
|
RenameField b ->
|
|
|
|
ColumnValues b Value ->
|
|
|
|
ColumnValues b Value
|
2019-03-01 12:17:22 +03:00
|
|
|
updatePreset qt rf obj =
|
|
|
|
case rf of
|
|
|
|
RFCol (RenameItem opQT oCol nCol) ->
|
|
|
|
if qt == opQT
|
|
|
|
then updatePreset' oCol nCol
|
|
|
|
else obj
|
|
|
|
_ -> obj
|
|
|
|
where
|
|
|
|
updatePreset' oCol nCol =
|
|
|
|
M.fromList updItems
|
|
|
|
where
|
|
|
|
updItems = map procObjItem $ M.toList obj
|
|
|
|
procObjItem (pgCol, v) =
|
|
|
|
let isUpdated = pgCol == oCol
|
|
|
|
updCol = bool pgCol nCol isUpdated
|
|
|
|
in (updCol, v)
|
|
|
|
|
|
|
|
updateCols ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(Backend b) => TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
|
2019-03-01 12:17:22 +03:00
|
|
|
updateCols qt rf permSpec =
|
|
|
|
case rf of
|
|
|
|
RFCol (RenameItem opQT oCol nCol) ->
|
|
|
|
if qt == opQT
|
|
|
|
then updateCols' oCol nCol permSpec
|
|
|
|
else permSpec
|
|
|
|
_ -> permSpec
|
|
|
|
where
|
|
|
|
updateCols' oCol nCol cols = case cols of
|
|
|
|
PCStar -> cols
|
|
|
|
PCCols c -> PCCols $
|
|
|
|
flip map c $
|
|
|
|
\col -> if col == oCol then nCol else col
|
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
updateTableInBoolExp :: (Backend b) => RenameTable b -> BoolExp b -> BoolExp b
|
2019-09-05 10:34:53 +03:00
|
|
|
updateTableInBoolExp (oldQT, newQT) =
|
|
|
|
over _Wrapped . transform $
|
|
|
|
(_BoolExists . geTable) %~ \rqfQT ->
|
|
|
|
if rqfQT == oldQT then newQT else rqfQT
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2019-09-05 10:34:53 +03:00
|
|
|
updateFieldInBoolExp ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(MonadReader (TableCache b) m, Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
RenameField b ->
|
|
|
|
BoolExp b ->
|
|
|
|
m (BoolExp b)
|
2019-09-05 10:34:53 +03:00
|
|
|
updateFieldInBoolExp qt rf be =
|
|
|
|
BoolExp
|
|
|
|
<$> case unBoolExp be of
|
|
|
|
BoolAnd exps -> BoolAnd <$> procExps exps
|
|
|
|
BoolOr exps -> BoolOr <$> procExps exps
|
|
|
|
BoolNot e -> BoolNot <$> updateBoolExp' e
|
|
|
|
BoolExists (GExists refqt wh) ->
|
2021-07-23 02:06:10 +03:00
|
|
|
BoolExists . GExists refqt . unBoolExp
|
2019-09-05 10:34:53 +03:00
|
|
|
<$> updateFieldInBoolExp refqt rf (BoolExp wh)
|
2022-07-12 12:25:22 +03:00
|
|
|
BoolField fld -> BoolField <$> updateColExp qt rf fld
|
2019-09-05 10:34:53 +03:00
|
|
|
where
|
|
|
|
procExps = mapM updateBoolExp'
|
|
|
|
updateBoolExp' =
|
|
|
|
fmap unBoolExp . updateFieldInBoolExp qt rf . BoolExp
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateColExp ::
|
2021-02-14 09:07:52 +03:00
|
|
|
forall b m.
|
|
|
|
(MonadReader (TableCache b) m, Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
RenameField b ->
|
|
|
|
ColExp ->
|
|
|
|
m ColExp
|
2019-03-01 12:17:22 +03:00
|
|
|
updateColExp qt rf (ColExp fld val) =
|
|
|
|
ColExp updatedFld <$> updatedVal
|
|
|
|
where
|
|
|
|
updatedFld = bool fld nFld $ opQT == qt && oFld == fld
|
|
|
|
updatedVal = do
|
2020-12-08 17:22:31 +03:00
|
|
|
tables <- ask
|
|
|
|
let maybeFieldInfo =
|
|
|
|
M.lookup qt tables
|
|
|
|
>>= M.lookup fld . _tciFieldInfoMap . _tiCoreInfo
|
|
|
|
case maybeFieldInfo of
|
|
|
|
Nothing -> pure val
|
|
|
|
Just fi -> case fi of
|
|
|
|
FIColumn _ -> pure val
|
|
|
|
FIComputedField _ -> pure val
|
|
|
|
FIRelationship ri -> do
|
|
|
|
let remTable = riRTable ri
|
|
|
|
case decodeValue val of
|
|
|
|
Left _ -> pure val
|
|
|
|
Right be -> toJSON <$> updateFieldInBoolExp remTable rf be
|
|
|
|
FIRemoteRelationship {} -> pure val
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
(oFld, nFld, opQT) = case rf of
|
2021-02-14 09:07:52 +03:00
|
|
|
RFCol (RenameItem tn oCol nCol) -> (fromCol @b oCol, fromCol @b nCol, tn)
|
2019-03-01 12:17:22 +03:00
|
|
|
RFRel (RenameItem tn oRel nRel) -> (fromRel oRel, fromRel nRel, tn)
|
|
|
|
|
|
|
|
-- rename columns in relationship definitions
|
|
|
|
updateColInRel ::
|
2021-04-22 00:44:37 +03:00
|
|
|
forall b m.
|
|
|
|
(CacheRM m, MonadWriter MetadataModifier m, BackendMetadata b) =>
|
2021-02-14 09:07:52 +03:00
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
RelName ->
|
|
|
|
RenameCol b ->
|
|
|
|
m ()
|
2020-12-28 15:56:00 +03:00
|
|
|
updateColInRel source fromQT rn rnCol = do
|
2022-04-26 18:12:47 +03:00
|
|
|
tables <- fold <$> askTableCache @b source
|
2020-12-08 17:22:31 +03:00
|
|
|
let maybeRelInfo =
|
|
|
|
tables ^? ix fromQT . tiCoreInfo . tciFieldInfoMap . ix (fromRel rn) . _FIRelationship
|
|
|
|
forM_ maybeRelInfo $ \relInfo ->
|
2021-05-18 16:06:42 +03:00
|
|
|
let relTableName = riRTable relInfo
|
|
|
|
in tell $
|
|
|
|
MetadataModifier $
|
|
|
|
tableMetadataSetter source fromQT
|
|
|
|
%~ case riType relInfo of
|
|
|
|
ObjRel ->
|
|
|
|
tmObjectRelationships . ix rn . rdUsing
|
|
|
|
%~ updateColInObjRel fromQT relTableName rnCol
|
|
|
|
ArrRel ->
|
|
|
|
tmArrayRelationships . ix rn . rdUsing
|
|
|
|
%~ updateColInArrRel fromQT relTableName rnCol
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2021-07-23 02:06:10 +03:00
|
|
|
-- | Local helper: update a column's name in the left-hand side of a remote relationship.
|
|
|
|
--
|
|
|
|
-- There are two kinds or remote relationships: remote source relationships, across sources, and
|
|
|
|
-- remote schema relationships, on remote schemas. In both cases, we maintain a mapping from the
|
|
|
|
-- source table's colunns to what they should be joined against in the target; when a column is
|
|
|
|
-- renamed, those references must be renamed as well. This function handles both cases.
|
|
|
|
--
|
|
|
|
-- See 'renameColumnInMetadata'.
|
|
|
|
updateColInRemoteRelationshipLHS ::
|
2021-02-14 09:07:52 +03:00
|
|
|
forall b m.
|
|
|
|
( MonadError QErr m,
|
2020-12-08 17:22:31 +03:00
|
|
|
MonadWriter MetadataModifier m,
|
2021-02-14 09:07:52 +03:00
|
|
|
BackendMetadata b
|
|
|
|
) =>
|
|
|
|
SourceName ->
|
2021-12-01 07:53:34 +03:00
|
|
|
RelName ->
|
2021-02-14 09:07:52 +03:00
|
|
|
RenameCol b ->
|
|
|
|
m ()
|
2021-07-23 02:06:10 +03:00
|
|
|
updateColInRemoteRelationshipLHS source remoteRelationshipName (RenameItem qt oldCol newCol) = do
|
2021-02-14 09:07:52 +03:00
|
|
|
oldColName <- parseGraphQLName $ toTxt oldCol
|
|
|
|
newColName <- parseGraphQLName $ toTxt newCol
|
2021-07-23 02:06:10 +03:00
|
|
|
let oldFieldName = fromCol @b oldCol
|
|
|
|
newFieldName = fromCol @b newCol
|
|
|
|
|
|
|
|
updateSet =
|
|
|
|
Set.insert newFieldName . Set.delete oldFieldName
|
|
|
|
|
|
|
|
updateMapKey =
|
|
|
|
-- mapKeys is not available in 0.2.13.0
|
|
|
|
M.fromList . map (\(key, value) -> (if key == oldFieldName then newFieldName else key, value)) . M.toList
|
|
|
|
|
|
|
|
updateFieldCalls (RemoteFields fields) =
|
|
|
|
RemoteFields $
|
|
|
|
fields <&> \(FieldCall name (RemoteArguments args)) ->
|
|
|
|
FieldCall name $ RemoteArguments $ updateVariableName <$> args
|
|
|
|
|
|
|
|
updateVariableName =
|
|
|
|
fmap \v -> if v == oldColName then newColName else v
|
|
|
|
|
|
|
|
remoteRelationshipLens =
|
2021-12-01 07:53:34 +03:00
|
|
|
tableMetadataSetter @b source qt . tmRemoteRelationships . ix remoteRelationshipName . rrDefinition
|
2021-07-23 02:06:10 +03:00
|
|
|
|
|
|
|
remoteSchemaLHSModifier =
|
2021-12-01 07:53:34 +03:00
|
|
|
remoteRelationshipLens . _RelationshipToSchema . _2
|
|
|
|
%~ (trrdLhsFields %~ updateSet)
|
|
|
|
. (trrdRemoteField %~ updateFieldCalls)
|
2021-07-23 02:06:10 +03:00
|
|
|
|
|
|
|
remoteSourceLHSModifier =
|
2021-12-01 07:53:34 +03:00
|
|
|
remoteRelationshipLens . _RelationshipToSource . tsrdFieldMapping %~ updateMapKey
|
2021-07-23 02:06:10 +03:00
|
|
|
|
|
|
|
tell $ MetadataModifier $ remoteSchemaLHSModifier . remoteSourceLHSModifier
|
|
|
|
where
|
|
|
|
parseGraphQLName txt =
|
|
|
|
G.mkName txt
|
|
|
|
`onNothing` throw400 ParseFailed (txt <> " is not a valid GraphQL name")
|
|
|
|
|
|
|
|
-- | Local helper: update a column's name in the right-hand side of a remote relationship.
|
|
|
|
--
|
|
|
|
-- In the case of remote _source_ relationships, the mapping from column to column needs to be
|
|
|
|
-- updated if one of the rhs columns has been renamed. A dependency is tracked from the rhs source's
|
|
|
|
-- column to the lhs source's relationship: when a rhs source's column has been renamed, this
|
|
|
|
-- function performs the corresponding update in the lhs source's relationship definition.
|
|
|
|
--
|
|
|
|
-- See 'renameColumnInMetadata'.
|
|
|
|
updateColInRemoteRelationshipRHS ::
|
|
|
|
forall source target m.
|
|
|
|
( MonadWriter MetadataModifier m,
|
2021-10-22 17:49:15 +03:00
|
|
|
Backend source,
|
|
|
|
Backend target
|
2021-07-23 02:06:10 +03:00
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
TableName source ->
|
2021-12-01 07:53:34 +03:00
|
|
|
RelName ->
|
2021-07-23 02:06:10 +03:00
|
|
|
RenameCol target ->
|
|
|
|
m ()
|
|
|
|
updateColInRemoteRelationshipRHS source tableName remoteRelationshipName (RenameItem _ oldCol newCol) =
|
2020-12-08 17:22:31 +03:00
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
2021-07-23 02:06:10 +03:00
|
|
|
tableMetadataSetter @source source tableName
|
|
|
|
. tmRemoteRelationships
|
|
|
|
. ix remoteRelationshipName
|
2021-12-01 07:53:34 +03:00
|
|
|
. rrDefinition
|
|
|
|
. _RelationshipToSource
|
|
|
|
. tsrdFieldMapping
|
2021-07-23 02:06:10 +03:00
|
|
|
%~ updateMapValue
|
2020-06-09 17:29:39 +03:00
|
|
|
where
|
2021-07-23 02:06:10 +03:00
|
|
|
oldFieldName = fromCol @target oldCol
|
|
|
|
newFieldName = fromCol @target newCol
|
|
|
|
updateMapValue =
|
|
|
|
fmap \value -> if value == oldFieldName then newFieldName else value
|
|
|
|
|
|
|
|
-- | Local helper: update a table's name in the right-hand side of a remote relationship.
|
|
|
|
--
|
|
|
|
-- In the case of remote _source_ relationships, the relationship definition targets a specific
|
|
|
|
-- table in the rhs source, and that reference needs to be updated if the targeted table has been
|
|
|
|
-- renamed. A dependency is tracked from the rhs source's table to the lhs source's relationship:
|
|
|
|
-- when a rhs table has been renamed, this function performs the corresponding update in the lhs
|
|
|
|
-- source's relationship definition.
|
|
|
|
--
|
|
|
|
-- See 'renameTableInMetadata'.
|
|
|
|
updateTableInRemoteRelationshipRHS ::
|
|
|
|
forall source target m.
|
|
|
|
( MonadWriter MetadataModifier m,
|
2021-10-22 17:49:15 +03:00
|
|
|
Backend source,
|
|
|
|
Backend target
|
2021-07-23 02:06:10 +03:00
|
|
|
) =>
|
|
|
|
SourceName ->
|
|
|
|
TableName source ->
|
2021-12-01 07:53:34 +03:00
|
|
|
RelName ->
|
2021-07-23 02:06:10 +03:00
|
|
|
RenameTable target ->
|
|
|
|
m ()
|
|
|
|
updateTableInRemoteRelationshipRHS source tableName remoteRelationshipName (_, newTableName) =
|
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
|
|
|
tableMetadataSetter @source source tableName
|
|
|
|
. tmRemoteRelationships
|
|
|
|
. ix remoteRelationshipName
|
2021-12-01 07:53:34 +03:00
|
|
|
. rrDefinition
|
|
|
|
. _RelationshipToSource
|
|
|
|
. tsrdTable
|
2021-07-23 02:06:10 +03:00
|
|
|
.~ toJSON newTableName
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
updateColInObjRel ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
TableName b ->
|
|
|
|
RenameCol b ->
|
|
|
|
ObjRelUsing b ->
|
|
|
|
ObjRelUsing b
|
2019-03-01 12:17:22 +03:00
|
|
|
updateColInObjRel fromQT toQT rnCol = \case
|
2021-03-03 16:02:00 +03:00
|
|
|
RUFKeyOn c ->
|
|
|
|
RUFKeyOn $ updateRelChoice fromQT toQT rnCol c
|
|
|
|
RUManual manConfig ->
|
|
|
|
RUManual $ updateRelManualConfig fromQT toQT rnCol manConfig
|
|
|
|
|
|
|
|
updateRelChoice ::
|
|
|
|
Backend b =>
|
|
|
|
TableName b ->
|
|
|
|
TableName b ->
|
|
|
|
RenameCol b ->
|
|
|
|
ObjRelUsingChoice b ->
|
|
|
|
ObjRelUsingChoice b
|
|
|
|
updateRelChoice fromQT toQT rnCol =
|
|
|
|
\case
|
|
|
|
SameTable col -> SameTable $ getNewCol rnCol fromQT col
|
|
|
|
RemoteTable t c -> RemoteTable t (getNewCol rnCol toQT c)
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateColInArrRel ::
|
2021-02-14 09:07:52 +03:00
|
|
|
(Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
TableName b ->
|
|
|
|
RenameCol b ->
|
|
|
|
ArrRelUsing b ->
|
|
|
|
ArrRelUsing b
|
2019-03-01 12:17:22 +03:00
|
|
|
updateColInArrRel fromQT toQT rnCol = \case
|
|
|
|
RUFKeyOn (ArrRelUsingFKeyOn t c) ->
|
2019-03-01 16:59:24 +03:00
|
|
|
let updCol = getNewCol rnCol toQT c
|
2019-03-01 12:17:22 +03:00
|
|
|
in RUFKeyOn $ ArrRelUsingFKeyOn t updCol
|
2020-01-14 10:09:10 +03:00
|
|
|
RUManual manConfig -> RUManual $ updateRelManualConfig fromQT toQT rnCol manConfig
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2021-02-14 09:07:52 +03:00
|
|
|
type ColMap b = HashMap (Column b) (Column b)
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2019-03-01 16:59:24 +03:00
|
|
|
getNewCol ::
|
2021-05-21 05:46:58 +03:00
|
|
|
forall b f.
|
|
|
|
Backend b =>
|
|
|
|
Functor f =>
|
|
|
|
RenameCol b ->
|
|
|
|
TableName b ->
|
|
|
|
f (Column b) ->
|
|
|
|
f (Column b)
|
|
|
|
getNewCol rnCol qt cols =
|
|
|
|
if qt == opQT
|
|
|
|
then go <$> cols
|
|
|
|
else cols
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
RenameItem opQT oCol nCol = rnCol
|
2021-05-21 05:46:58 +03:00
|
|
|
go :: Column b -> Column b
|
|
|
|
go col
|
|
|
|
| col == oCol = nCol
|
|
|
|
| otherwise = col
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
updateRelManualConfig ::
|
2021-04-22 00:44:37 +03:00
|
|
|
forall b.
|
|
|
|
(Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
TableName b ->
|
|
|
|
RenameCol b ->
|
|
|
|
RelManualConfig b ->
|
|
|
|
RelManualConfig b
|
2019-03-01 12:17:22 +03:00
|
|
|
updateRelManualConfig fromQT toQT rnCol manConfig =
|
2021-03-03 16:02:00 +03:00
|
|
|
RelManualConfig tn (updateColMap fromQT toQT rnCol colMap) io
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
2021-03-03 16:02:00 +03:00
|
|
|
RelManualConfig tn colMap io = manConfig
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateColMap ::
|
2021-04-22 00:44:37 +03:00
|
|
|
forall b.
|
|
|
|
(Backend b) =>
|
|
|
|
TableName b ->
|
|
|
|
TableName b ->
|
|
|
|
RenameCol b ->
|
|
|
|
ColMap b ->
|
|
|
|
ColMap b
|
2019-12-13 00:46:33 +03:00
|
|
|
updateColMap fromQT toQT rnCol =
|
|
|
|
M.fromList . map (modCol fromQT *** modCol toQT) . M.toList
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
RenameItem qt oCol nCol = rnCol
|
|
|
|
modCol colQt col = if colQt == qt && col == oCol then nCol else col
|
|
|
|
|
2019-10-03 10:45:52 +03:00
|
|
|
possiblyUpdateCustomColumnNames ::
|
2021-04-22 00:44:37 +03:00
|
|
|
forall b m.
|
|
|
|
(MonadWriter MetadataModifier m, BackendMetadata b) =>
|
|
|
|
SourceName ->
|
|
|
|
TableName b ->
|
|
|
|
Column b ->
|
|
|
|
Column b ->
|
|
|
|
m ()
|
2022-03-09 09:34:47 +03:00
|
|
|
possiblyUpdateCustomColumnNames source tableName oldColumn newColumn = do
|
2020-12-08 17:22:31 +03:00
|
|
|
tell $
|
|
|
|
MetadataModifier $
|
2022-03-09 09:34:47 +03:00
|
|
|
tableMetadataSetter @b source tableName . tmConfiguration . tcColumnConfig %~ swapOldColumnForNewColumn
|
|
|
|
where
|
|
|
|
swapOldColumnForNewColumn :: HashMap (Column b) columnData -> HashMap (Column b) columnData
|
|
|
|
swapOldColumnForNewColumn customColumns =
|
|
|
|
M.fromList $ (\(dbCol, val) -> (,val) $ if dbCol == oldColumn then newColumn else dbCol) <$> M.toList customColumns
|