-- | Functions for updating the metadata (with integrity checking) to incorporate schema changes
-- discovered after applying a user-supplied SQL query. None of these functions modify the schema
-- cache, so it must be reloaded after the metadata is updated.
module Hasura.RQL.DDL.Schema.Rename
  ( renameTableInMetadata,
    renameColumnInMetadata,
    renameRelationshipInMetadata,
  )
where

import Control.Lens.Combinators
import Control.Lens.Operators
import Data.Aeson
import Data.HashMap.Strict qualified as M
import Data.HashMap.Strict.InsOrd qualified as OMap
import Data.HashSet qualified as Set
import Data.Text.Extended
import Hasura.Base.Error
import Hasura.Prelude
import Hasura.RQL.DDL.Permission
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.ToSchema
import Hasura.RQL.Types.Relationships.ToSource
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.SchemaCacheTypes
import Hasura.RQL.Types.Table
import Hasura.SQL.AnyBackend qualified as AB
import Hasura.SQL.Backend
import Hasura.Session
import Language.GraphQL.Draft.Syntax qualified as G

data RenameItem (b :: BackendType) a = RenameItem
  { RenameItem b a -> TableName b
_riTable :: TableName b,
    RenameItem b a -> a
_riOld :: a,
    RenameItem b a -> a
_riNew :: a
  }

type RenameCol (b :: BackendType) = RenameItem b (Column b)

data RenameField b
  = RFCol (RenameCol b)
  | RFRel (RenameItem b RelName)

type RenameTable b = (TableName b, TableName b)

data Rename b
  = RTable (RenameTable b)
  | RField (RenameField b)

otherDeps :: QErrM m => Text -> SchemaObjId -> m ()
otherDeps :: Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
d =
  Text -> m ()
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$
    Text
"unexpected dependency "
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> SchemaObjId -> Text
reportSchemaObj SchemaObjId
d
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"; "
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
errMsg

-- | 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'.
renameTableInMetadata ::
  forall b m.
  ( MonadError QErr m,
    CacheRM m,
    MonadWriter MetadataModifier m,
    BackendMetadata b
  ) =>
  SourceName ->
  TableName b ->
  TableName b ->
  m ()
renameTableInMetadata :: SourceName -> TableName b -> TableName b -> m ()
renameTableInMetadata SourceName
source TableName b
newQT TableName b
oldQT = do
  SchemaCache
sc <- m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  let allDeps :: [SchemaObjId]
allDeps =
        SchemaCache -> SchemaObjId -> [SchemaObjId]
getDependentObjs SchemaCache
sc (SchemaObjId -> [SchemaObjId]) -> SchemaObjId -> [SchemaObjId]
forall a b. (a -> b) -> a -> b
$
          SourceName -> AnyBackend SourceObjId -> SchemaObjId
SOSourceObj SourceName
source (AnyBackend SourceObjId -> SchemaObjId)
-> AnyBackend SourceObjId -> SchemaObjId
forall a b. (a -> b) -> a -> b
$
            SourceObjId b -> AnyBackend SourceObjId
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend (SourceObjId b -> AnyBackend SourceObjId)
-> SourceObjId b -> AnyBackend SourceObjId
forall a b. (a -> b) -> a -> b
$
              TableName b -> SourceObjId b
forall (b :: BackendType). TableName b -> SourceObjId b
SOITable @b TableName b
oldQT

  -- update all dependant schema objects
  [SchemaObjId] -> (SchemaObjId -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SchemaObjId]
allDeps ((SchemaObjId -> m ()) -> m ()) -> (SchemaObjId -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \case
    -- the dependend object is a source object in the same source
    sobj :: SchemaObjId
sobj@(SOSourceObj SourceName
depSourceName AnyBackend SourceObjId
exists)
      | SourceName
depSourceName SourceName -> SourceName -> Bool
forall a. Eq a => a -> a -> Bool
== SourceName
source,
        Just SourceObjId b
sourceObjId <- AnyBackend SourceObjId -> Maybe (SourceObjId b)
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
AnyBackend i -> Maybe (i b)
AB.unpackAnyBackend @b AnyBackend SourceObjId
exists ->
        case SourceObjId b
sourceObjId of
          SOITableObj TableName b
refQT (TORel RelName
rn) ->
            SourceName -> TableName b -> RelName -> RenameTable b -> m ()
forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
SourceName -> TableName b -> RelName -> RenameTable b -> m ()
updateRelDefs @b SourceName
source TableName b
refQT RelName
rn (TableName b
oldQT, TableName b
newQT)
          SOITableObj TableName b
refQT (TOPerm RoleName
rn PermType
pt) ->
            SourceName
-> TableName b -> RoleName -> PermType -> Rename b -> m ()
forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
SourceName
-> TableName b -> RoleName -> PermType -> Rename b -> m ()
updatePermFlds @b SourceName
source TableName b
refQT RoleName
rn PermType
pt (Rename b -> m ()) -> Rename b -> m ()
forall a b. (a -> b) -> a -> b
$ RenameTable b -> Rename b
forall (b :: BackendType). RenameTable b -> Rename b
RTable (TableName b
oldQT, TableName b
newQT)
          -- A trigger's definition is not dependent on the table directly
          SOITableObj TableName b
_ (TOTrigger TriggerName
_) -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
          -- A remote relationship's definition is not dependent on the table directly
          SOITableObj TableName b
_ (TORemoteRel RelName
_) -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
          SourceObjId b
_ -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
sobj
    -- the dependend object is a source object in a different source
    sobj :: SchemaObjId
sobj@(SOSourceObj SourceName
depSourceName AnyBackend SourceObjId
exists) ->
      AnyBackend SourceObjId
-> (forall (b :: BackendType). Backend b => SourceObjId b -> m ())
-> m ()
forall (c :: BackendType -> Constraint) (i :: BackendType -> *) r.
AllBackendsSatisfy c =>
AnyBackend i -> (forall (b :: BackendType). c b => i b -> r) -> r
AB.dispatchAnyBackend @Backend AnyBackend SourceObjId
exists \(SourceObjId b
sourceObjId :: SourceObjId b') ->
        case SourceObjId b
sourceObjId of
          SOITableObj TableName b
tableName (TORemoteRel RelName
remoteRelationshipName) -> do
            SourceName -> TableName b -> RelName -> RenameTable b -> m ()
forall (source :: BackendType) (target :: BackendType)
       (m :: * -> *).
(MonadWriter MetadataModifier m, Backend source, Backend target) =>
SourceName
-> TableName source -> RelName -> RenameTable target -> m ()
updateTableInRemoteRelationshipRHS @b' @b SourceName
depSourceName TableName b
tableName RelName
remoteRelationshipName (TableName b
oldQT, TableName b
newQT)
          -- only remote relationships might create dependencies across sources
          SourceObjId b
_ -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
sobj
    -- any other kind of dependent object (erroneous)
    SchemaObjId
d -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
d
  -- Update table name in metadata
  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      (Sources -> Identity Sources) -> Metadata -> Identity Metadata
Lens' Metadata Sources
metaSources ((Sources -> Identity Sources) -> Metadata -> Identity Metadata)
-> ((InsOrdHashMap (TableName b) (TableMetadata b)
     -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
    -> Sources -> Identity Sources)
-> (InsOrdHashMap (TableName b) (TableMetadata b)
    -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index Sources -> Traversal' Sources (IxValue Sources)
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index Sources
SourceName
source ((BackendSourceMetadata -> Identity BackendSourceMetadata)
 -> Sources -> Identity Sources)
-> ((InsOrdHashMap (TableName b) (TableMetadata b)
     -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
    -> BackendSourceMetadata -> Identity BackendSourceMetadata)
-> (InsOrdHashMap (TableName b) (TableMetadata b)
    -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
-> Sources
-> Identity Sources
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Backend b => Prism' BackendSourceMetadata (SourceMetadata b)
forall (b :: BackendType).
Backend b =>
Prism' BackendSourceMetadata (SourceMetadata b)
toSourceMetadata @b) ((SourceMetadata b -> Identity (SourceMetadata b))
 -> BackendSourceMetadata -> Identity BackendSourceMetadata)
-> ((InsOrdHashMap (TableName b) (TableMetadata b)
     -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
    -> SourceMetadata b -> Identity (SourceMetadata b))
-> (InsOrdHashMap (TableName b) (TableMetadata b)
    -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
-> BackendSourceMetadata
-> Identity BackendSourceMetadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (InsOrdHashMap (TableName b) (TableMetadata b)
 -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
-> SourceMetadata b -> Identity (SourceMetadata b)
forall (b :: BackendType). Lens' (SourceMetadata b) (Tables b)
smTables ((InsOrdHashMap (TableName b) (TableMetadata b)
  -> Identity (InsOrdHashMap (TableName b) (TableMetadata b)))
 -> Metadata -> Identity Metadata)
-> (InsOrdHashMap (TableName b) (TableMetadata b)
    -> InsOrdHashMap (TableName b) (TableMetadata b))
-> Metadata
-> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \InsOrdHashMap (TableName b) (TableMetadata b)
tables ->
        ((TableMetadata b -> InsOrdHashMap (TableName b) (TableMetadata b))
 -> Maybe (TableMetadata b)
 -> InsOrdHashMap (TableName b) (TableMetadata b))
-> Maybe (TableMetadata b)
-> (TableMetadata b
    -> InsOrdHashMap (TableName b) (TableMetadata b))
-> InsOrdHashMap (TableName b) (TableMetadata b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (InsOrdHashMap (TableName b) (TableMetadata b)
-> (TableMetadata b
    -> InsOrdHashMap (TableName b) (TableMetadata b))
-> Maybe (TableMetadata b)
-> InsOrdHashMap (TableName b) (TableMetadata b)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe InsOrdHashMap (TableName b) (TableMetadata b)
tables) (TableName b
-> InsOrdHashMap (TableName b) (TableMetadata b)
-> Maybe (TableMetadata b)
forall k v. (Eq k, Hashable k) => k -> InsOrdHashMap k v -> Maybe v
OMap.lookup TableName b
oldQT InsOrdHashMap (TableName b) (TableMetadata b)
tables) ((TableMetadata b -> InsOrdHashMap (TableName b) (TableMetadata b))
 -> InsOrdHashMap (TableName b) (TableMetadata b))
-> (TableMetadata b
    -> InsOrdHashMap (TableName b) (TableMetadata b))
-> InsOrdHashMap (TableName b) (TableMetadata b)
forall a b. (a -> b) -> a -> b
$
          \TableMetadata b
tableMeta -> TableName b
-> InsOrdHashMap (TableName b) (TableMetadata b)
-> InsOrdHashMap (TableName b) (TableMetadata b)
forall k v.
(Eq k, Hashable k) =>
k -> InsOrdHashMap k v -> InsOrdHashMap k v
OMap.delete TableName b
oldQT (InsOrdHashMap (TableName b) (TableMetadata b)
 -> InsOrdHashMap (TableName b) (TableMetadata b))
-> InsOrdHashMap (TableName b) (TableMetadata b)
-> InsOrdHashMap (TableName b) (TableMetadata b)
forall a b. (a -> b) -> a -> b
$ TableName b
-> TableMetadata b
-> InsOrdHashMap (TableName b) (TableMetadata b)
-> InsOrdHashMap (TableName b) (TableMetadata b)
forall k v.
(Eq k, Hashable k) =>
k -> v -> InsOrdHashMap k v -> InsOrdHashMap k v
OMap.insert TableName b
newQT TableMetadata b
tableMeta {_tmTable :: TableName b
_tmTable = TableName b
newQT} InsOrdHashMap (TableName b) (TableMetadata b)
tables
  where
    errMsg :: Text
errMsg = Text
"cannot rename table " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TableName b
oldQT TableName b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" to " Text -> TableName b -> Text
forall t. ToTxt t => Text -> t -> Text
<>> TableName b
newQT

-- | 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'.
renameColumnInMetadata ::
  forall b m.
  ( MonadError QErr m,
    CacheRM m,
    MonadWriter MetadataModifier m,
    BackendMetadata b
  ) =>
  Column b ->
  Column b ->
  SourceName ->
  TableName b ->
  FieldInfoMap (FieldInfo b) ->
  m ()
renameColumnInMetadata :: Column b
-> Column b
-> SourceName
-> TableName b
-> FieldInfoMap (FieldInfo b)
-> m ()
renameColumnInMetadata Column b
oCol Column b
nCol SourceName
source TableName b
qt FieldInfoMap (FieldInfo b)
fieldInfo = do
  SchemaCache
sc <- m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  -- Check if any relation exists with new column name
  m ()
assertFldNotExists
  -- Fetch dependent objects
  let depObjs :: [SchemaObjId]
depObjs =
        SchemaCache -> SchemaObjId -> [SchemaObjId]
getDependentObjs SchemaCache
sc (SchemaObjId -> [SchemaObjId]) -> SchemaObjId -> [SchemaObjId]
forall a b. (a -> b) -> a -> b
$
          SourceName -> AnyBackend SourceObjId -> SchemaObjId
SOSourceObj SourceName
source (AnyBackend SourceObjId -> SchemaObjId)
-> AnyBackend SourceObjId -> SchemaObjId
forall a b. (a -> b) -> a -> b
$
            SourceObjId b -> AnyBackend SourceObjId
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend (SourceObjId b -> AnyBackend SourceObjId)
-> SourceObjId b -> AnyBackend SourceObjId
forall a b. (a -> b) -> a -> b
$
              TableName b -> TableObjId b -> SourceObjId b
forall (b :: BackendType).
TableName b -> TableObjId b -> SourceObjId b
SOITableObj @b TableName b
qt (TableObjId b -> SourceObjId b) -> TableObjId b -> SourceObjId b
forall a b. (a -> b) -> a -> b
$
                Column b -> TableObjId b
forall (b :: BackendType). Column b -> TableObjId b
TOCol @b Column b
oCol
      renameItem :: RenameItem b (Column b)
renameItem = TableName b -> Column b -> Column b -> RenameItem b (Column b)
forall (b :: BackendType) a.
TableName b -> a -> a -> RenameItem b a
RenameItem @b TableName b
qt Column b
oCol Column b
nCol
      renameFld :: RenameField b
renameFld = RenameItem b (Column b) -> RenameField b
forall (b :: BackendType). RenameCol b -> RenameField b
RFCol RenameItem b (Column b)
renameItem
  -- Update dependent objects
  [SchemaObjId] -> (SchemaObjId -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SchemaObjId]
depObjs ((SchemaObjId -> m ()) -> m ()) -> (SchemaObjId -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \case
    -- the dependend object is a source object in the same source
    sobj :: SchemaObjId
sobj@(SOSourceObj SourceName
depSourceName AnyBackend SourceObjId
exists)
      | SourceName
depSourceName SourceName -> SourceName -> Bool
forall a. Eq a => a -> a -> Bool
== SourceName
source,
        Just SourceObjId b
sourceObjId <- AnyBackend SourceObjId -> Maybe (SourceObjId b)
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
AnyBackend i -> Maybe (i b)
AB.unpackAnyBackend @b AnyBackend SourceObjId
exists ->
        case SourceObjId b
sourceObjId of
          SOITableObj TableName b
refQT (TOPerm RoleName
role PermType
pt) ->
            SourceName
-> TableName b -> RoleName -> PermType -> Rename b -> m ()
forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
SourceName
-> TableName b -> RoleName -> PermType -> Rename b -> m ()
updatePermFlds @b SourceName
source TableName b
refQT RoleName
role PermType
pt (Rename b -> m ()) -> Rename b -> m ()
forall a b. (a -> b) -> a -> b
$ RenameField b -> Rename b
forall (b :: BackendType). RenameField b -> Rename b
RField RenameField b
renameFld
          SOITableObj TableName b
refQT (TORel RelName
rn) ->
            SourceName
-> TableName b -> RelName -> RenameItem b (Column b) -> m ()
forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadWriter MetadataModifier m, BackendMetadata b) =>
SourceName -> TableName b -> RelName -> RenameCol b -> m ()
updateColInRel @b SourceName
source TableName b
refQT RelName
rn RenameItem b (Column b)
renameItem
          SOITableObj TableName b
refQT (TOTrigger TriggerName
triggerName) ->
            MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
              (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
                SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter @b SourceName
source TableName b
refQT ASetter' Metadata (TableMetadata b)
-> ((EventTriggerConf b -> Identity (EventTriggerConf b))
    -> TableMetadata b -> Identity (TableMetadata b))
-> (EventTriggerConf b -> Identity (EventTriggerConf b))
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (EventTriggers b -> Identity (EventTriggers b))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (EventTriggers b)
tmEventTriggers ((EventTriggers b -> Identity (EventTriggers b))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((EventTriggerConf b -> Identity (EventTriggerConf b))
    -> EventTriggers b -> Identity (EventTriggers b))
-> (EventTriggerConf b -> Identity (EventTriggerConf b))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (EventTriggers b)
-> Traversal' (EventTriggers b) (IxValue (EventTriggers b))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (EventTriggers b)
TriggerName
triggerName
                  ((EventTriggerConf b -> Identity (EventTriggerConf b))
 -> Metadata -> Identity Metadata)
-> (EventTriggerConf b -> EventTriggerConf b)
-> Metadata
-> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ TableName b
-> Column b
-> Column b
-> TableName b
-> EventTriggerConf b
-> EventTriggerConf b
forall (b :: BackendType).
BackendMetadata b =>
TableName b
-> Column b
-> Column b
-> TableName b
-> EventTriggerConf b
-> EventTriggerConf b
updateColumnInEventTrigger @b TableName b
refQT Column b
oCol Column b
nCol TableName b
qt
          SOITableObj TableName b
_ (TORemoteRel RelName
remoteRelName) ->
            SourceName -> RelName -> RenameItem b (Column b) -> m ()
forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
SourceName -> RelName -> RenameCol b -> m ()
updateColInRemoteRelationshipLHS SourceName
source RelName
remoteRelName RenameItem b (Column b)
renameItem
          SourceObjId b
_ -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
sobj
    -- the dependend object is a source object in a different source
    sobj :: SchemaObjId
sobj@(SOSourceObj SourceName
depSourceName AnyBackend SourceObjId
exists) ->
      AnyBackend SourceObjId
-> (forall (b :: BackendType). Backend b => SourceObjId b -> m ())
-> m ()
forall (c :: BackendType -> Constraint) (i :: BackendType -> *) r.
AllBackendsSatisfy c =>
AnyBackend i -> (forall (b :: BackendType). c b => i b -> r) -> r
AB.dispatchAnyBackend @Backend AnyBackend SourceObjId
exists \(SourceObjId b
sourceObjId :: SourceObjId b') ->
        case SourceObjId b
sourceObjId of
          SOITableObj TableName b
tableName (TORemoteRel RelName
remoteRelationshipName) -> do
            SourceName
-> TableName b -> RelName -> RenameItem b (Column b) -> m ()
forall (source :: BackendType) (target :: BackendType)
       (m :: * -> *).
(MonadWriter MetadataModifier m, Backend source, Backend target) =>
SourceName
-> TableName source -> RelName -> RenameCol target -> m ()
updateColInRemoteRelationshipRHS @b' @b SourceName
depSourceName TableName b
tableName RelName
remoteRelationshipName RenameItem b (Column b)
renameItem
          -- only remote relationships might create dependencies across sources
          SourceObjId b
_ -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
sobj
    -- any other kind of dependent object (erroneous)
    SchemaObjId
d -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
d
  -- Update custom column names
  SourceName -> TableName b -> Column b -> Column b -> m ()
forall (b :: BackendType) (m :: * -> *).
(MonadWriter MetadataModifier m, BackendMetadata b) =>
SourceName -> TableName b -> Column b -> Column b -> m ()
possiblyUpdateCustomColumnNames @b SourceName
source TableName b
qt Column b
oCol Column b
nCol
  where
    errMsg :: Text
errMsg = Text
"cannot rename column " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Column b
oCol Column b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" to " Text -> Column b -> Text
forall t. ToTxt t => Text -> t -> Text
<>> Column b
nCol
    assertFldNotExists :: m ()
assertFldNotExists =
      case FieldName -> FieldInfoMap (FieldInfo b) -> Maybe (FieldInfo b)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup (Column b -> FieldName
forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @b Column b
oCol) FieldInfoMap (FieldInfo b)
fieldInfo of
        Just (FIRelationship RelInfo b
_) ->
          Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
AlreadyExists (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$
            Text
"cannot rename column " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Column b
oCol
              Column b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" to " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Column b
nCol
              Column b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" in table " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TableName b
qt
              TableName b -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" as a relationship with the name already exists"
        Maybe (FieldInfo b)
_ -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

renameRelationshipInMetadata ::
  forall b m.
  ( MonadError QErr m,
    CacheRM m,
    MonadWriter MetadataModifier m,
    BackendMetadata b
  ) =>
  SourceName ->
  TableName b ->
  RelName ->
  RelType ->
  RelName ->
  m ()
renameRelationshipInMetadata :: SourceName -> TableName b -> RelName -> RelType -> RelName -> m ()
renameRelationshipInMetadata SourceName
source TableName b
qt RelName
oldRN RelType
relType RelName
newRN = do
  SchemaCache
sc <- m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  let depObjs :: [SchemaObjId]
depObjs =
        SchemaCache -> SchemaObjId -> [SchemaObjId]
getDependentObjs SchemaCache
sc (SchemaObjId -> [SchemaObjId]) -> SchemaObjId -> [SchemaObjId]
forall a b. (a -> b) -> a -> b
$
          SourceName -> AnyBackend SourceObjId -> SchemaObjId
SOSourceObj SourceName
source (AnyBackend SourceObjId -> SchemaObjId)
-> AnyBackend SourceObjId -> SchemaObjId
forall a b. (a -> b) -> a -> b
$
            SourceObjId b -> AnyBackend SourceObjId
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend (SourceObjId b -> AnyBackend SourceObjId)
-> SourceObjId b -> AnyBackend SourceObjId
forall a b. (a -> b) -> a -> b
$
              TableName b -> TableObjId b -> SourceObjId b
forall (b :: BackendType).
TableName b -> TableObjId b -> SourceObjId b
SOITableObj @b TableName b
qt (TableObjId b -> SourceObjId b) -> TableObjId b -> SourceObjId b
forall a b. (a -> b) -> a -> b
$
                RelName -> TableObjId b
forall (b :: BackendType). RelName -> TableObjId b
TORel RelName
oldRN
      renameFld :: RenameField b
renameFld = RenameItem b RelName -> RenameField b
forall (b :: BackendType). RenameItem b RelName -> RenameField b
RFRel (RenameItem b RelName -> RenameField b)
-> RenameItem b RelName -> RenameField b
forall a b. (a -> b) -> a -> b
$ TableName b -> RelName -> RelName -> RenameItem b RelName
forall (b :: BackendType) a.
TableName b -> a -> a -> RenameItem b a
RenameItem @b TableName b
qt RelName
oldRN RelName
newRN

  [SchemaObjId] -> (SchemaObjId -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [SchemaObjId]
depObjs ((SchemaObjId -> m ()) -> m ()) -> (SchemaObjId -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \case
    sobj :: SchemaObjId
sobj@(SOSourceObj SourceName
_ AnyBackend SourceObjId
exists) -> case AnyBackend SourceObjId -> Maybe (SourceObjId b)
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
AnyBackend i -> Maybe (i b)
AB.unpackAnyBackend @b AnyBackend SourceObjId
exists of
      Just (SOITableObj TableName b
refQT (TOPerm RoleName
role PermType
pt)) ->
        SourceName
-> TableName b -> RoleName -> PermType -> Rename b -> m ()
forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
SourceName
-> TableName b -> RoleName -> PermType -> Rename b -> m ()
updatePermFlds @b SourceName
source TableName b
refQT RoleName
role PermType
pt (Rename b -> m ()) -> Rename b -> m ()
forall a b. (a -> b) -> a -> b
$ RenameField b -> Rename b
forall (b :: BackendType). RenameField b -> Rename b
RField RenameField b
renameFld
      Maybe (SourceObjId b)
_ -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
sobj
    SchemaObjId
d -> Text -> SchemaObjId -> m ()
forall (m :: * -> *). QErrM m => Text -> SchemaObjId -> m ()
otherDeps Text
errMsg SchemaObjId
d
  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter @b SourceName
source TableName b
qt ASetter' Metadata (TableMetadata b)
-> (TableMetadata b -> TableMetadata b) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ case RelType
relType of
        RelType
ObjRel -> (Relationships (ObjRelDef b)
 -> Identity (Relationships (ObjRelDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Relationships (ObjRelDef b))
tmObjectRelationships ((Relationships (ObjRelDef b)
  -> Identity (Relationships (ObjRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (Relationships (ObjRelDef b) -> Relationships (ObjRelDef b))
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Relationships (ObjRelDef b) -> Relationships (ObjRelDef b)
forall a. Relationships (RelDef a) -> Relationships (RelDef a)
rewriteRelationships
        RelType
ArrRel -> (Relationships (ArrRelDef b)
 -> Identity (Relationships (ArrRelDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Relationships (ArrRelDef b))
tmArrayRelationships ((Relationships (ArrRelDef b)
  -> Identity (Relationships (ArrRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (Relationships (ArrRelDef b) -> Relationships (ArrRelDef b))
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Relationships (ArrRelDef b) -> Relationships (ArrRelDef b)
forall a. Relationships (RelDef a) -> Relationships (RelDef a)
rewriteRelationships
  where
    errMsg :: Text
errMsg = Text
"cannot rename relationship " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> RelName
oldRN RelName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" to " Text -> RelName -> Text
forall t. ToTxt t => Text -> t -> Text
<>> RelName
newRN
    rewriteRelationships ::
      Relationships (RelDef a) -> Relationships (RelDef a)
    rewriteRelationships :: Relationships (RelDef a) -> Relationships (RelDef a)
rewriteRelationships Relationships (RelDef a)
relationsMap =
      ((RelDef a -> Relationships (RelDef a))
 -> Maybe (RelDef a) -> Relationships (RelDef a))
-> Maybe (RelDef a)
-> (RelDef a -> Relationships (RelDef a))
-> Relationships (RelDef a)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Relationships (RelDef a)
-> (RelDef a -> Relationships (RelDef a))
-> Maybe (RelDef a)
-> Relationships (RelDef a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Relationships (RelDef a)
relationsMap) (RelName -> Relationships (RelDef a) -> Maybe (RelDef a)
forall k v. (Eq k, Hashable k) => k -> InsOrdHashMap k v -> Maybe v
OMap.lookup RelName
oldRN Relationships (RelDef a)
relationsMap) ((RelDef a -> Relationships (RelDef a))
 -> Relationships (RelDef a))
-> (RelDef a -> Relationships (RelDef a))
-> Relationships (RelDef a)
forall a b. (a -> b) -> a -> b
$
        \RelDef a
rd -> RelName
-> RelDef a -> Relationships (RelDef a) -> Relationships (RelDef a)
forall k v.
(Eq k, Hashable k) =>
k -> v -> InsOrdHashMap k v -> InsOrdHashMap k v
OMap.insert RelName
newRN RelDef a
rd {_rdName :: RelName
_rdName = RelName
newRN} (Relationships (RelDef a) -> Relationships (RelDef a))
-> Relationships (RelDef a) -> Relationships (RelDef a)
forall a b. (a -> b) -> a -> b
$ RelName -> Relationships (RelDef a) -> Relationships (RelDef a)
forall k v.
(Eq k, Hashable k) =>
k -> InsOrdHashMap k v -> InsOrdHashMap k v
OMap.delete RelName
oldRN Relationships (RelDef a)
relationsMap

-- update table names in relationship definition
updateRelDefs ::
  forall b m.
  ( MonadError QErr m,
    CacheRM m,
    MonadWriter MetadataModifier m,
    BackendMetadata b
  ) =>
  SourceName ->
  TableName b ->
  RelName ->
  RenameTable b ->
  m ()
updateRelDefs :: SourceName -> TableName b -> RelName -> RenameTable b -> m ()
updateRelDefs SourceName
source TableName b
qt RelName
rn RenameTable b
renameTable = do
  FieldInfoMap (FieldInfo b)
fim <- SourceName -> TableName b -> m (FieldInfoMap (FieldInfo b))
forall (b :: BackendType) (m :: * -> *).
(QErrM m, CacheRM m, Backend b) =>
SourceName -> TableName b -> m (FieldInfoMap (FieldInfo b))
askTableFieldInfoMap @b SourceName
source TableName b
qt
  RelInfo b
ri <- FieldInfoMap (FieldInfo b) -> RelName -> Text -> m (RelInfo b)
forall (m :: * -> *) (backend :: BackendType).
MonadError QErr m =>
FieldInfoMap (FieldInfo backend)
-> RelName -> Text -> m (RelInfo backend)
askRelType FieldInfoMap (FieldInfo b)
fim RelName
rn Text
""
  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter SourceName
source TableName b
qt ASetter' Metadata (TableMetadata b)
-> (TableMetadata b -> TableMetadata b) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ case RelInfo b -> RelType
forall (b :: BackendType). RelInfo b -> RelType
riType RelInfo b
ri of
        RelType
ObjRel -> (Relationships (ObjRelDef b)
 -> Identity (Relationships (ObjRelDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Relationships (ObjRelDef b))
tmObjectRelationships ((Relationships (ObjRelDef b)
  -> Identity (Relationships (ObjRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((ObjRelDef b -> Identity (ObjRelDef b))
    -> Relationships (ObjRelDef b)
    -> Identity (Relationships (ObjRelDef b)))
-> (ObjRelDef b -> Identity (ObjRelDef b))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Relationships (ObjRelDef b))
-> Traversal'
     (Relationships (ObjRelDef b))
     (IxValue (Relationships (ObjRelDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Relationships (ObjRelDef b))
RelName
rn ((ObjRelDef b -> Identity (ObjRelDef b))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (ObjRelDef b -> ObjRelDef b)
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ RenameTable b -> ObjRelDef b -> ObjRelDef b
updateObjRelDef RenameTable b
renameTable
        RelType
ArrRel -> (Relationships (ArrRelDef b)
 -> Identity (Relationships (ArrRelDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Relationships (ArrRelDef b))
tmArrayRelationships ((Relationships (ArrRelDef b)
  -> Identity (Relationships (ArrRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((ArrRelDef b -> Identity (ArrRelDef b))
    -> Relationships (ArrRelDef b)
    -> Identity (Relationships (ArrRelDef b)))
-> (ArrRelDef b -> Identity (ArrRelDef b))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Relationships (ArrRelDef b))
-> Traversal'
     (Relationships (ArrRelDef b))
     (IxValue (Relationships (ArrRelDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Relationships (ArrRelDef b))
RelName
rn ((ArrRelDef b -> Identity (ArrRelDef b))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (ArrRelDef b -> ArrRelDef b)
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ RenameTable b -> ArrRelDef b -> ArrRelDef b
updateArrRelDef RenameTable b
renameTable
  where
    updateObjRelDef :: RenameTable b -> ObjRelDef b -> ObjRelDef b
    updateObjRelDef :: RenameTable b -> ObjRelDef b -> ObjRelDef b
updateObjRelDef (TableName b
oldQT, TableName b
newQT) =
      (RelUsing b (ObjRelUsingChoice b)
 -> Identity (RelUsing b (ObjRelUsingChoice b)))
-> ObjRelDef b -> Identity (ObjRelDef b)
forall a1 a2. Lens (RelDef a1) (RelDef a2) a1 a2
rdUsing ((RelUsing b (ObjRelUsingChoice b)
  -> Identity (RelUsing b (ObjRelUsingChoice b)))
 -> ObjRelDef b -> Identity (ObjRelDef b))
-> (RelUsing b (ObjRelUsingChoice b)
    -> RelUsing b (ObjRelUsingChoice b))
-> ObjRelDef b
-> ObjRelDef b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \case
        RUFKeyOn ObjRelUsingChoice b
fk -> ObjRelUsingChoice b -> RelUsing b (ObjRelUsingChoice b)
forall (b :: BackendType) a. a -> RelUsing b a
RUFKeyOn ObjRelUsingChoice b
fk
        RUManual (RelManualConfig TableName b
origQT HashMap (Column b) (Column b)
rmCols Maybe InsertOrder
rmIO) ->
          let updQT :: TableName b
updQT = TableName b -> TableName b -> Bool -> TableName b
forall a. a -> a -> Bool -> a
bool TableName b
origQT TableName b
newQT (Bool -> TableName b) -> Bool -> TableName b
forall a b. (a -> b) -> a -> b
$ TableName b
oldQT TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
origQT
           in RelManualConfig b -> RelUsing b (ObjRelUsingChoice b)
forall (b :: BackendType) a. RelManualConfig b -> RelUsing b a
RUManual (RelManualConfig b -> RelUsing b (ObjRelUsingChoice b))
-> RelManualConfig b -> RelUsing b (ObjRelUsingChoice b)
forall a b. (a -> b) -> a -> b
$ TableName b
-> HashMap (Column b) (Column b)
-> Maybe InsertOrder
-> RelManualConfig b
forall (b :: BackendType).
TableName b
-> HashMap (Column b) (Column b)
-> Maybe InsertOrder
-> RelManualConfig b
RelManualConfig TableName b
updQT HashMap (Column b) (Column b)
rmCols Maybe InsertOrder
rmIO

    updateArrRelDef :: RenameTable b -> ArrRelDef b -> ArrRelDef b
    updateArrRelDef :: RenameTable b -> ArrRelDef b -> ArrRelDef b
updateArrRelDef (TableName b
oldQT, TableName b
newQT) =
      (RelUsing b (ArrRelUsingFKeyOn b)
 -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
-> ArrRelDef b -> Identity (ArrRelDef b)
forall a1 a2. Lens (RelDef a1) (RelDef a2) a1 a2
rdUsing ((RelUsing b (ArrRelUsingFKeyOn b)
  -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
 -> ArrRelDef b -> Identity (ArrRelDef b))
-> (RelUsing b (ArrRelUsingFKeyOn b)
    -> RelUsing b (ArrRelUsingFKeyOn b))
-> ArrRelDef b
-> ArrRelDef b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \case
        RUFKeyOn (ArrRelUsingFKeyOn TableName b
origQT NonEmpty (Column b)
c) ->
          let updQT :: TableName b
updQT = TableName b -> TableName b
getUpdQT TableName b
origQT
           in ArrRelUsingFKeyOn b -> RelUsing b (ArrRelUsingFKeyOn b)
forall (b :: BackendType) a. a -> RelUsing b a
RUFKeyOn (ArrRelUsingFKeyOn b -> RelUsing b (ArrRelUsingFKeyOn b))
-> ArrRelUsingFKeyOn b -> RelUsing b (ArrRelUsingFKeyOn b)
forall a b. (a -> b) -> a -> b
$ TableName b -> NonEmpty (Column b) -> ArrRelUsingFKeyOn b
forall (b :: BackendType).
TableName b -> NonEmpty (Column b) -> ArrRelUsingFKeyOn b
ArrRelUsingFKeyOn TableName b
updQT NonEmpty (Column b)
c
        RUManual (RelManualConfig TableName b
origQT HashMap (Column b) (Column b)
rmCols Maybe InsertOrder
rmIO) ->
          let updQT :: TableName b
updQT = TableName b -> TableName b
getUpdQT TableName b
origQT
           in RelManualConfig b -> RelUsing b (ArrRelUsingFKeyOn b)
forall (b :: BackendType) a. RelManualConfig b -> RelUsing b a
RUManual (RelManualConfig b -> RelUsing b (ArrRelUsingFKeyOn b))
-> RelManualConfig b -> RelUsing b (ArrRelUsingFKeyOn b)
forall a b. (a -> b) -> a -> b
$ TableName b
-> HashMap (Column b) (Column b)
-> Maybe InsertOrder
-> RelManualConfig b
forall (b :: BackendType).
TableName b
-> HashMap (Column b) (Column b)
-> Maybe InsertOrder
-> RelManualConfig b
RelManualConfig TableName b
updQT HashMap (Column b) (Column b)
rmCols Maybe InsertOrder
rmIO
      where
        getUpdQT :: TableName b -> TableName b
getUpdQT TableName b
origQT = TableName b -> TableName b -> Bool -> TableName b
forall a. a -> a -> Bool -> a
bool TableName b
origQT TableName b
newQT (Bool -> TableName b) -> Bool -> TableName b
forall a b. (a -> b) -> a -> b
$ TableName b
oldQT TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
origQT

-- | update fields in permissions
updatePermFlds ::
  forall b m.
  ( MonadError QErr m,
    CacheRM m,
    MonadWriter MetadataModifier m,
    BackendMetadata b
  ) =>
  SourceName ->
  TableName b ->
  RoleName ->
  PermType ->
  Rename b ->
  m ()
updatePermFlds :: SourceName
-> TableName b -> RoleName -> PermType -> Rename b -> m ()
updatePermFlds SourceName
source TableName b
refQT RoleName
rn PermType
pt Rename b
rename = do
  HashMap (TableName b) (TableInfo b)
tables <- Maybe (HashMap (TableName b) (TableInfo b))
-> HashMap (TableName b) (TableInfo b)
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Maybe (HashMap (TableName b) (TableInfo b))
 -> HashMap (TableName b) (TableInfo b))
-> m (Maybe (HashMap (TableName b) (TableInfo b)))
-> m (HashMap (TableName b) (TableInfo b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SourceName -> m (Maybe (HashMap (TableName b) (TableInfo b)))
forall (b :: BackendType) (m :: * -> *).
(Backend b, CacheRM m) =>
SourceName -> m (Maybe (TableCache b))
askTableCache SourceName
source
  let withTables :: Reader (TableCache b) a -> a
      withTables :: Reader (HashMap (TableName b) (TableInfo b)) a -> a
withTables = (Reader (HashMap (TableName b) (TableInfo b)) a
 -> HashMap (TableName b) (TableInfo b) -> a)
-> HashMap (TableName b) (TableInfo b)
-> Reader (HashMap (TableName b) (TableInfo b)) a
-> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Reader (HashMap (TableName b) (TableInfo b)) a
-> HashMap (TableName b) (TableInfo b) -> a
forall r a. Reader r a -> r -> a
runReader HashMap (TableName b) (TableInfo b)
tables
  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter SourceName
source TableName b
refQT ASetter' Metadata (TableMetadata b)
-> (TableMetadata b -> TableMetadata b) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ case PermType
pt of
        PermType
PTInsert ->
          (Permissions (InsPermDef b)
 -> Identity (Permissions (InsPermDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Permissions (InsPermDef b))
tmInsertPermissions ((Permissions (InsPermDef b)
  -> Identity (Permissions (InsPermDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((PermDefPermission b InsPerm
     -> Identity (PermDefPermission b InsPerm))
    -> Permissions (InsPermDef b)
    -> Identity (Permissions (InsPermDef b)))
-> (PermDefPermission b InsPerm
    -> Identity (PermDefPermission b InsPerm))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Permissions (InsPermDef b))
-> Traversal'
     (Permissions (InsPermDef b)) (IxValue (Permissions (InsPermDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Permissions (InsPermDef b))
RoleName
rn ((InsPermDef b -> Identity (InsPermDef b))
 -> Permissions (InsPermDef b)
 -> Identity (Permissions (InsPermDef b)))
-> ((PermDefPermission b InsPerm
     -> Identity (PermDefPermission b InsPerm))
    -> InsPermDef b -> Identity (InsPermDef b))
-> (PermDefPermission b InsPerm
    -> Identity (PermDefPermission b InsPerm))
-> Permissions (InsPermDef b)
-> Identity (Permissions (InsPermDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PermDefPermission b InsPerm
 -> Identity (PermDefPermission b InsPerm))
-> InsPermDef b -> Identity (InsPermDef b)
forall (b1 :: BackendType) (perm1 :: BackendType -> *)
       (b2 :: BackendType) (perm2 :: BackendType -> *).
Lens
  (PermDef b1 perm1)
  (PermDef b2 perm2)
  (PermDefPermission b1 perm1)
  (PermDefPermission b2 perm2)
pdPermission ((PermDefPermission b InsPerm
  -> Identity (PermDefPermission b InsPerm))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (PermDefPermission b InsPerm -> PermDefPermission b InsPerm)
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \PermDefPermission b InsPerm
insPerm ->
            Reader
  (HashMap (TableName b) (TableInfo b)) (PermDefPermission b InsPerm)
-> PermDefPermission b InsPerm
forall a. Reader (HashMap (TableName b) (TableInfo b)) a -> a
withTables (Reader
   (HashMap (TableName b) (TableInfo b)) (PermDefPermission b InsPerm)
 -> PermDefPermission b InsPerm)
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b InsPerm)
-> PermDefPermission b InsPerm
forall a b. (a -> b) -> a -> b
$ TableName b
-> Rename b
-> PermDefPermission b InsPerm
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b InsPerm)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b
-> Rename b
-> PermDefPermission b InsPerm
-> m (PermDefPermission b InsPerm)
updateInsPermFlds TableName b
refQT Rename b
rename PermDefPermission b InsPerm
insPerm
        PermType
PTSelect ->
          (Permissions (SelPermDef b)
 -> Identity (Permissions (SelPermDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Permissions (SelPermDef b))
tmSelectPermissions ((Permissions (SelPermDef b)
  -> Identity (Permissions (SelPermDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((PermDefPermission b SelPerm
     -> Identity (PermDefPermission b SelPerm))
    -> Permissions (SelPermDef b)
    -> Identity (Permissions (SelPermDef b)))
-> (PermDefPermission b SelPerm
    -> Identity (PermDefPermission b SelPerm))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Permissions (SelPermDef b))
-> Traversal'
     (Permissions (SelPermDef b)) (IxValue (Permissions (SelPermDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Permissions (SelPermDef b))
RoleName
rn ((SelPermDef b -> Identity (SelPermDef b))
 -> Permissions (SelPermDef b)
 -> Identity (Permissions (SelPermDef b)))
-> ((PermDefPermission b SelPerm
     -> Identity (PermDefPermission b SelPerm))
    -> SelPermDef b -> Identity (SelPermDef b))
-> (PermDefPermission b SelPerm
    -> Identity (PermDefPermission b SelPerm))
-> Permissions (SelPermDef b)
-> Identity (Permissions (SelPermDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PermDefPermission b SelPerm
 -> Identity (PermDefPermission b SelPerm))
-> SelPermDef b -> Identity (SelPermDef b)
forall (b1 :: BackendType) (perm1 :: BackendType -> *)
       (b2 :: BackendType) (perm2 :: BackendType -> *).
Lens
  (PermDef b1 perm1)
  (PermDef b2 perm2)
  (PermDefPermission b1 perm1)
  (PermDefPermission b2 perm2)
pdPermission ((PermDefPermission b SelPerm
  -> Identity (PermDefPermission b SelPerm))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (PermDefPermission b SelPerm -> PermDefPermission b SelPerm)
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \PermDefPermission b SelPerm
selPerm ->
            Reader
  (HashMap (TableName b) (TableInfo b)) (PermDefPermission b SelPerm)
-> PermDefPermission b SelPerm
forall a. Reader (HashMap (TableName b) (TableInfo b)) a -> a
withTables (Reader
   (HashMap (TableName b) (TableInfo b)) (PermDefPermission b SelPerm)
 -> PermDefPermission b SelPerm)
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b SelPerm)
-> PermDefPermission b SelPerm
forall a b. (a -> b) -> a -> b
$ TableName b
-> Rename b
-> PermDefPermission b SelPerm
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b SelPerm)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b
-> Rename b
-> PermDefPermission b SelPerm
-> m (PermDefPermission b SelPerm)
updateSelPermFlds TableName b
refQT Rename b
rename PermDefPermission b SelPerm
selPerm
        PermType
PTUpdate ->
          (Permissions (UpdPermDef b)
 -> Identity (Permissions (UpdPermDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Permissions (UpdPermDef b))
tmUpdatePermissions ((Permissions (UpdPermDef b)
  -> Identity (Permissions (UpdPermDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((PermDefPermission b UpdPerm
     -> Identity (PermDefPermission b UpdPerm))
    -> Permissions (UpdPermDef b)
    -> Identity (Permissions (UpdPermDef b)))
-> (PermDefPermission b UpdPerm
    -> Identity (PermDefPermission b UpdPerm))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Permissions (UpdPermDef b))
-> Traversal'
     (Permissions (UpdPermDef b)) (IxValue (Permissions (UpdPermDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Permissions (UpdPermDef b))
RoleName
rn ((UpdPermDef b -> Identity (UpdPermDef b))
 -> Permissions (UpdPermDef b)
 -> Identity (Permissions (UpdPermDef b)))
-> ((PermDefPermission b UpdPerm
     -> Identity (PermDefPermission b UpdPerm))
    -> UpdPermDef b -> Identity (UpdPermDef b))
-> (PermDefPermission b UpdPerm
    -> Identity (PermDefPermission b UpdPerm))
-> Permissions (UpdPermDef b)
-> Identity (Permissions (UpdPermDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PermDefPermission b UpdPerm
 -> Identity (PermDefPermission b UpdPerm))
-> UpdPermDef b -> Identity (UpdPermDef b)
forall (b1 :: BackendType) (perm1 :: BackendType -> *)
       (b2 :: BackendType) (perm2 :: BackendType -> *).
Lens
  (PermDef b1 perm1)
  (PermDef b2 perm2)
  (PermDefPermission b1 perm1)
  (PermDefPermission b2 perm2)
pdPermission ((PermDefPermission b UpdPerm
  -> Identity (PermDefPermission b UpdPerm))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (PermDefPermission b UpdPerm -> PermDefPermission b UpdPerm)
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \PermDefPermission b UpdPerm
updPerm ->
            Reader
  (HashMap (TableName b) (TableInfo b)) (PermDefPermission b UpdPerm)
-> PermDefPermission b UpdPerm
forall a. Reader (HashMap (TableName b) (TableInfo b)) a -> a
withTables (Reader
   (HashMap (TableName b) (TableInfo b)) (PermDefPermission b UpdPerm)
 -> PermDefPermission b UpdPerm)
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b UpdPerm)
-> PermDefPermission b UpdPerm
forall a b. (a -> b) -> a -> b
$ TableName b
-> Rename b
-> PermDefPermission b UpdPerm
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b UpdPerm)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b
-> Rename b
-> PermDefPermission b UpdPerm
-> m (PermDefPermission b UpdPerm)
updateUpdPermFlds TableName b
refQT Rename b
rename PermDefPermission b UpdPerm
updPerm
        PermType
PTDelete ->
          (Permissions (DelPermDef b)
 -> Identity (Permissions (DelPermDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Permissions (DelPermDef b))
tmDeletePermissions ((Permissions (DelPermDef b)
  -> Identity (Permissions (DelPermDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((PermDefPermission b DelPerm
     -> Identity (PermDefPermission b DelPerm))
    -> Permissions (DelPermDef b)
    -> Identity (Permissions (DelPermDef b)))
-> (PermDefPermission b DelPerm
    -> Identity (PermDefPermission b DelPerm))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Permissions (DelPermDef b))
-> Traversal'
     (Permissions (DelPermDef b)) (IxValue (Permissions (DelPermDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Permissions (DelPermDef b))
RoleName
rn ((DelPermDef b -> Identity (DelPermDef b))
 -> Permissions (DelPermDef b)
 -> Identity (Permissions (DelPermDef b)))
-> ((PermDefPermission b DelPerm
     -> Identity (PermDefPermission b DelPerm))
    -> DelPermDef b -> Identity (DelPermDef b))
-> (PermDefPermission b DelPerm
    -> Identity (PermDefPermission b DelPerm))
-> Permissions (DelPermDef b)
-> Identity (Permissions (DelPermDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PermDefPermission b DelPerm
 -> Identity (PermDefPermission b DelPerm))
-> DelPermDef b -> Identity (DelPermDef b)
forall (b1 :: BackendType) (perm1 :: BackendType -> *)
       (b2 :: BackendType) (perm2 :: BackendType -> *).
Lens
  (PermDef b1 perm1)
  (PermDef b2 perm2)
  (PermDefPermission b1 perm1)
  (PermDefPermission b2 perm2)
pdPermission ((PermDefPermission b DelPerm
  -> Identity (PermDefPermission b DelPerm))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (PermDefPermission b DelPerm -> PermDefPermission b DelPerm)
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \PermDefPermission b DelPerm
delPerm ->
            Reader
  (HashMap (TableName b) (TableInfo b)) (PermDefPermission b DelPerm)
-> PermDefPermission b DelPerm
forall a. Reader (HashMap (TableName b) (TableInfo b)) a -> a
withTables (Reader
   (HashMap (TableName b) (TableInfo b)) (PermDefPermission b DelPerm)
 -> PermDefPermission b DelPerm)
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b DelPerm)
-> PermDefPermission b DelPerm
forall a b. (a -> b) -> a -> b
$ TableName b
-> Rename b
-> PermDefPermission b DelPerm
-> Reader
     (HashMap (TableName b) (TableInfo b)) (PermDefPermission b DelPerm)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b
-> Rename b
-> PermDefPermission b DelPerm
-> m (PermDefPermission b DelPerm)
updateDelPermFlds TableName b
refQT Rename b
rename PermDefPermission b DelPerm
delPerm

updateInsPermFlds ::
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  Rename b ->
  PermDefPermission b InsPerm ->
  m (PermDefPermission b InsPerm)
updateInsPermFlds :: TableName b
-> Rename b
-> PermDefPermission b InsPerm
-> m (PermDefPermission b InsPerm)
updateInsPermFlds TableName b
refQT Rename b
rename (InsPerm' (InsPerm BoolExp b
chk Maybe (ColumnValues b Value)
preset Maybe (PermColSpec b)
cols Bool
backendOnly)) =
  case Rename b
rename of
    RTable RenameTable b
rt -> do
      let updChk :: BoolExp b
updChk = RenameTable b -> BoolExp b -> BoolExp b
forall (b :: BackendType).
Backend b =>
RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp RenameTable b
rt BoolExp b
chk
      PermDefPermission b InsPerm -> m (PermDefPermission b InsPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b InsPerm -> m (PermDefPermission b InsPerm))
-> PermDefPermission b InsPerm -> m (PermDefPermission b InsPerm)
forall a b. (a -> b) -> a -> b
$ InsPerm b -> PermDefPermission b InsPerm
forall (b :: BackendType). InsPerm b -> PermDefPermission b InsPerm
InsPerm' (InsPerm b -> PermDefPermission b InsPerm)
-> InsPerm b -> PermDefPermission b InsPerm
forall a b. (a -> b) -> a -> b
$ BoolExp b
-> Maybe (ColumnValues b Value)
-> Maybe (PermColSpec b)
-> Bool
-> InsPerm b
forall (b :: BackendType).
BoolExp b
-> Maybe (ColumnValues b Value)
-> Maybe (PermColSpec b)
-> Bool
-> InsPerm b
InsPerm BoolExp b
updChk Maybe (ColumnValues b Value)
preset Maybe (PermColSpec b)
cols Bool
backendOnly
    RField RenameField b
rf -> do
      BoolExp b
updChk <- TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
refQT RenameField b
rf BoolExp b
chk
      let updPresetM :: Maybe (ColumnValues b Value)
updPresetM = TableName b
-> RenameField b -> ColumnValues b Value -> ColumnValues b Value
forall (b :: BackendType).
Backend b =>
TableName b
-> RenameField b -> ColumnValues b Value -> ColumnValues b Value
updatePreset TableName b
refQT RenameField b
rf (ColumnValues b Value -> ColumnValues b Value)
-> Maybe (ColumnValues b Value) -> Maybe (ColumnValues b Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (ColumnValues b Value)
preset
          updColsM :: Maybe (PermColSpec b)
updColsM = TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
forall (b :: BackendType).
Backend b =>
TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
updateCols TableName b
refQT RenameField b
rf (PermColSpec b -> PermColSpec b)
-> Maybe (PermColSpec b) -> Maybe (PermColSpec b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (PermColSpec b)
cols
      PermDefPermission b InsPerm -> m (PermDefPermission b InsPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b InsPerm -> m (PermDefPermission b InsPerm))
-> PermDefPermission b InsPerm -> m (PermDefPermission b InsPerm)
forall a b. (a -> b) -> a -> b
$ InsPerm b -> PermDefPermission b InsPerm
forall (b :: BackendType). InsPerm b -> PermDefPermission b InsPerm
InsPerm' (InsPerm b -> PermDefPermission b InsPerm)
-> InsPerm b -> PermDefPermission b InsPerm
forall a b. (a -> b) -> a -> b
$ BoolExp b
-> Maybe (ColumnValues b Value)
-> Maybe (PermColSpec b)
-> Bool
-> InsPerm b
forall (b :: BackendType).
BoolExp b
-> Maybe (ColumnValues b Value)
-> Maybe (PermColSpec b)
-> Bool
-> InsPerm b
InsPerm BoolExp b
updChk Maybe (ColumnValues b Value)
updPresetM Maybe (PermColSpec b)
updColsM Bool
backendOnly

updateSelPermFlds ::
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  Rename b ->
  PermDefPermission b SelPerm ->
  m (PermDefPermission b SelPerm)
updateSelPermFlds :: TableName b
-> Rename b
-> PermDefPermission b SelPerm
-> m (PermDefPermission b SelPerm)
updateSelPermFlds TableName b
refQT Rename b
rename (SelPerm' (SelPerm PermColSpec b
cols BoolExp b
fltr Maybe Int
limit Bool
aggAllwd [ComputedFieldName]
computedFields AllowedRootFields QueryRootFieldType
allowedQueryRootFieldTypes AllowedRootFields SubscriptionRootFieldType
allowedSubsRootFieldTypes)) = do
  case Rename b
rename of
    RTable RenameTable b
rt -> do
      let updFltr :: BoolExp b
updFltr = RenameTable b -> BoolExp b -> BoolExp b
forall (b :: BackendType).
Backend b =>
RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp RenameTable b
rt BoolExp b
fltr
      PermDefPermission b SelPerm -> m (PermDefPermission b SelPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b SelPerm -> m (PermDefPermission b SelPerm))
-> PermDefPermission b SelPerm -> m (PermDefPermission b SelPerm)
forall a b. (a -> b) -> a -> b
$ SelPerm b -> PermDefPermission b SelPerm
forall (b :: BackendType). SelPerm b -> PermDefPermission b SelPerm
SelPerm' (SelPerm b -> PermDefPermission b SelPerm)
-> SelPerm b -> PermDefPermission b SelPerm
forall a b. (a -> b) -> a -> b
$ PermColSpec b
-> BoolExp b
-> Maybe Int
-> Bool
-> [ComputedFieldName]
-> AllowedRootFields QueryRootFieldType
-> AllowedRootFields SubscriptionRootFieldType
-> SelPerm b
forall (b :: BackendType).
PermColSpec b
-> BoolExp b
-> Maybe Int
-> Bool
-> [ComputedFieldName]
-> AllowedRootFields QueryRootFieldType
-> AllowedRootFields SubscriptionRootFieldType
-> SelPerm b
SelPerm PermColSpec b
cols BoolExp b
updFltr Maybe Int
limit Bool
aggAllwd [ComputedFieldName]
computedFields AllowedRootFields QueryRootFieldType
allowedQueryRootFieldTypes AllowedRootFields SubscriptionRootFieldType
allowedSubsRootFieldTypes
    RField RenameField b
rf -> do
      BoolExp b
updFltr <- TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
refQT RenameField b
rf BoolExp b
fltr
      let updCols :: PermColSpec b
updCols = TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
forall (b :: BackendType).
Backend b =>
TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
updateCols TableName b
refQT RenameField b
rf PermColSpec b
cols
      PermDefPermission b SelPerm -> m (PermDefPermission b SelPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b SelPerm -> m (PermDefPermission b SelPerm))
-> PermDefPermission b SelPerm -> m (PermDefPermission b SelPerm)
forall a b. (a -> b) -> a -> b
$ SelPerm b -> PermDefPermission b SelPerm
forall (b :: BackendType). SelPerm b -> PermDefPermission b SelPerm
SelPerm' (SelPerm b -> PermDefPermission b SelPerm)
-> SelPerm b -> PermDefPermission b SelPerm
forall a b. (a -> b) -> a -> b
$ PermColSpec b
-> BoolExp b
-> Maybe Int
-> Bool
-> [ComputedFieldName]
-> AllowedRootFields QueryRootFieldType
-> AllowedRootFields SubscriptionRootFieldType
-> SelPerm b
forall (b :: BackendType).
PermColSpec b
-> BoolExp b
-> Maybe Int
-> Bool
-> [ComputedFieldName]
-> AllowedRootFields QueryRootFieldType
-> AllowedRootFields SubscriptionRootFieldType
-> SelPerm b
SelPerm PermColSpec b
updCols BoolExp b
updFltr Maybe Int
limit Bool
aggAllwd [ComputedFieldName]
computedFields AllowedRootFields QueryRootFieldType
allowedQueryRootFieldTypes AllowedRootFields SubscriptionRootFieldType
allowedSubsRootFieldTypes

updateUpdPermFlds ::
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  Rename b ->
  PermDefPermission b UpdPerm ->
  m (PermDefPermission b UpdPerm)
updateUpdPermFlds :: TableName b
-> Rename b
-> PermDefPermission b UpdPerm
-> m (PermDefPermission b UpdPerm)
updateUpdPermFlds TableName b
refQT Rename b
rename (UpdPerm' (UpdPerm PermColSpec b
cols Maybe (ColumnValues b Value)
preset BoolExp b
fltr Maybe (BoolExp b)
check Bool
backendOnly)) = do
  case Rename b
rename of
    RTable RenameTable b
rt -> do
      let updFltr :: BoolExp b
updFltr = RenameTable b -> BoolExp b -> BoolExp b
forall (b :: BackendType).
Backend b =>
RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp RenameTable b
rt BoolExp b
fltr
          updCheck :: Maybe (BoolExp b)
updCheck = (BoolExp b -> BoolExp b) -> Maybe (BoolExp b) -> Maybe (BoolExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (RenameTable b -> BoolExp b -> BoolExp b
forall (b :: BackendType).
Backend b =>
RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp RenameTable b
rt) Maybe (BoolExp b)
check
      PermDefPermission b UpdPerm -> m (PermDefPermission b UpdPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b UpdPerm -> m (PermDefPermission b UpdPerm))
-> PermDefPermission b UpdPerm -> m (PermDefPermission b UpdPerm)
forall a b. (a -> b) -> a -> b
$ UpdPerm b -> PermDefPermission b UpdPerm
forall (b :: BackendType). UpdPerm b -> PermDefPermission b UpdPerm
UpdPerm' (UpdPerm b -> PermDefPermission b UpdPerm)
-> UpdPerm b -> PermDefPermission b UpdPerm
forall a b. (a -> b) -> a -> b
$ PermColSpec b
-> Maybe (ColumnValues b Value)
-> BoolExp b
-> Maybe (BoolExp b)
-> Bool
-> UpdPerm b
forall (b :: BackendType).
PermColSpec b
-> Maybe (ColumnValues b Value)
-> BoolExp b
-> Maybe (BoolExp b)
-> Bool
-> UpdPerm b
UpdPerm PermColSpec b
cols Maybe (ColumnValues b Value)
preset BoolExp b
updFltr Maybe (BoolExp b)
updCheck Bool
backendOnly
    RField RenameField b
rf -> do
      BoolExp b
updFltr <- TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
refQT RenameField b
rf BoolExp b
fltr
      Maybe (BoolExp b)
updCheck <- (BoolExp b -> m (BoolExp b))
-> Maybe (BoolExp b) -> m (Maybe (BoolExp b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse (TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
refQT RenameField b
rf) Maybe (BoolExp b)
check
      let updCols :: PermColSpec b
updCols = TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
forall (b :: BackendType).
Backend b =>
TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
updateCols TableName b
refQT RenameField b
rf PermColSpec b
cols
          updPresetM :: Maybe (ColumnValues b Value)
updPresetM = TableName b
-> RenameField b -> ColumnValues b Value -> ColumnValues b Value
forall (b :: BackendType).
Backend b =>
TableName b
-> RenameField b -> ColumnValues b Value -> ColumnValues b Value
updatePreset TableName b
refQT RenameField b
rf (ColumnValues b Value -> ColumnValues b Value)
-> Maybe (ColumnValues b Value) -> Maybe (ColumnValues b Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (ColumnValues b Value)
preset
      PermDefPermission b UpdPerm -> m (PermDefPermission b UpdPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b UpdPerm -> m (PermDefPermission b UpdPerm))
-> PermDefPermission b UpdPerm -> m (PermDefPermission b UpdPerm)
forall a b. (a -> b) -> a -> b
$ UpdPerm b -> PermDefPermission b UpdPerm
forall (b :: BackendType). UpdPerm b -> PermDefPermission b UpdPerm
UpdPerm' (UpdPerm b -> PermDefPermission b UpdPerm)
-> UpdPerm b -> PermDefPermission b UpdPerm
forall a b. (a -> b) -> a -> b
$ PermColSpec b
-> Maybe (ColumnValues b Value)
-> BoolExp b
-> Maybe (BoolExp b)
-> Bool
-> UpdPerm b
forall (b :: BackendType).
PermColSpec b
-> Maybe (ColumnValues b Value)
-> BoolExp b
-> Maybe (BoolExp b)
-> Bool
-> UpdPerm b
UpdPerm PermColSpec b
updCols Maybe (ColumnValues b Value)
updPresetM BoolExp b
updFltr Maybe (BoolExp b)
updCheck Bool
backendOnly

updateDelPermFlds ::
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  Rename b ->
  PermDefPermission b DelPerm ->
  m (PermDefPermission b DelPerm)
updateDelPermFlds :: TableName b
-> Rename b
-> PermDefPermission b DelPerm
-> m (PermDefPermission b DelPerm)
updateDelPermFlds TableName b
refQT Rename b
rename (DelPerm' (DelPerm BoolExp b
fltr Bool
backendOnly)) = do
  case Rename b
rename of
    RTable RenameTable b
rt -> do
      let updFltr :: BoolExp b
updFltr = RenameTable b -> BoolExp b -> BoolExp b
forall (b :: BackendType).
Backend b =>
RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp RenameTable b
rt BoolExp b
fltr
      PermDefPermission b DelPerm -> m (PermDefPermission b DelPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b DelPerm -> m (PermDefPermission b DelPerm))
-> PermDefPermission b DelPerm -> m (PermDefPermission b DelPerm)
forall a b. (a -> b) -> a -> b
$ DelPerm b -> PermDefPermission b DelPerm
forall (b :: BackendType). DelPerm b -> PermDefPermission b DelPerm
DelPerm' (DelPerm b -> PermDefPermission b DelPerm)
-> DelPerm b -> PermDefPermission b DelPerm
forall a b. (a -> b) -> a -> b
$ BoolExp b -> Bool -> DelPerm b
forall (b :: BackendType). BoolExp b -> Bool -> DelPerm b
DelPerm BoolExp b
updFltr Bool
backendOnly
    RField RenameField b
rf -> do
      BoolExp b
updFltr <- TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
refQT RenameField b
rf BoolExp b
fltr
      PermDefPermission b DelPerm -> m (PermDefPermission b DelPerm)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PermDefPermission b DelPerm -> m (PermDefPermission b DelPerm))
-> PermDefPermission b DelPerm -> m (PermDefPermission b DelPerm)
forall a b. (a -> b) -> a -> b
$ DelPerm b -> PermDefPermission b DelPerm
forall (b :: BackendType). DelPerm b -> PermDefPermission b DelPerm
DelPerm' (DelPerm b -> PermDefPermission b DelPerm)
-> DelPerm b -> PermDefPermission b DelPerm
forall a b. (a -> b) -> a -> b
$ BoolExp b -> Bool -> DelPerm b
forall (b :: BackendType). BoolExp b -> Bool -> DelPerm b
DelPerm BoolExp b
updFltr Bool
backendOnly

updatePreset ::
  (Backend b) =>
  TableName b ->
  RenameField b ->
  ColumnValues b Value ->
  ColumnValues b Value
updatePreset :: TableName b
-> RenameField b -> ColumnValues b Value -> ColumnValues b Value
updatePreset TableName b
qt RenameField b
rf ColumnValues b Value
obj =
  case RenameField b
rf of
    RFCol (RenameItem TableName b
opQT Column b
oCol Column b
nCol) ->
      if TableName b
qt TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
opQT
        then Column b -> Column b -> ColumnValues b Value
updatePreset' Column b
oCol Column b
nCol
        else ColumnValues b Value
obj
    RenameField b
_ -> ColumnValues b Value
obj
  where
    updatePreset' :: Column b -> Column b -> ColumnValues b Value
updatePreset' Column b
oCol Column b
nCol =
      [(Column b, Value)] -> ColumnValues b Value
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList [(Column b, Value)]
updItems
      where
        updItems :: [(Column b, Value)]
updItems = ((Column b, Value) -> (Column b, Value))
-> [(Column b, Value)] -> [(Column b, Value)]
forall a b. (a -> b) -> [a] -> [b]
map (Column b, Value) -> (Column b, Value)
procObjItem ([(Column b, Value)] -> [(Column b, Value)])
-> [(Column b, Value)] -> [(Column b, Value)]
forall a b. (a -> b) -> a -> b
$ ColumnValues b Value -> [(Column b, Value)]
forall k v. HashMap k v -> [(k, v)]
M.toList ColumnValues b Value
obj
        procObjItem :: (Column b, Value) -> (Column b, Value)
procObjItem (Column b
pgCol, Value
v) =
          let isUpdated :: Bool
isUpdated = Column b
pgCol Column b -> Column b -> Bool
forall a. Eq a => a -> a -> Bool
== Column b
oCol
              updCol :: Column b
updCol = Column b -> Column b -> Bool -> Column b
forall a. a -> a -> Bool -> a
bool Column b
pgCol Column b
nCol Bool
isUpdated
           in (Column b
updCol, Value
v)

updateCols ::
  (Backend b) => TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
updateCols :: TableName b -> RenameField b -> PermColSpec b -> PermColSpec b
updateCols TableName b
qt RenameField b
rf PermColSpec b
permSpec =
  case RenameField b
rf of
    RFCol (RenameItem TableName b
opQT Column b
oCol Column b
nCol) ->
      if TableName b
qt TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
opQT
        then Column b -> Column b -> PermColSpec b -> PermColSpec b
forall (b :: BackendType).
Eq (Column b) =>
Column b -> Column b -> PermColSpec b -> PermColSpec b
updateCols' Column b
oCol Column b
nCol PermColSpec b
permSpec
        else PermColSpec b
permSpec
    RenameField b
_ -> PermColSpec b
permSpec
  where
    updateCols' :: Column b -> Column b -> PermColSpec b -> PermColSpec b
updateCols' Column b
oCol Column b
nCol PermColSpec b
cols = case PermColSpec b
cols of
      PermColSpec b
PCStar -> PermColSpec b
cols
      PCCols [Column b]
c -> [Column b] -> PermColSpec b
forall (b :: BackendType). [Column b] -> PermColSpec b
PCCols ([Column b] -> PermColSpec b) -> [Column b] -> PermColSpec b
forall a b. (a -> b) -> a -> b
$
        ((Column b -> Column b) -> [Column b] -> [Column b])
-> [Column b] -> (Column b -> Column b) -> [Column b]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Column b -> Column b) -> [Column b] -> [Column b]
forall a b. (a -> b) -> [a] -> [b]
map [Column b]
c ((Column b -> Column b) -> [Column b])
-> (Column b -> Column b) -> [Column b]
forall a b. (a -> b) -> a -> b
$
          \Column b
col -> if Column b
col Column b -> Column b -> Bool
forall a. Eq a => a -> a -> Bool
== Column b
oCol then Column b
nCol else Column b
col

updateTableInBoolExp :: (Backend b) => RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp :: RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp (TableName b
oldQT, TableName b
newQT) =
  ASetter
  (BoolExp b) (BoolExp b) (GBoolExp b ColExp) (GBoolExp b ColExp)
-> (GBoolExp b ColExp -> GBoolExp b ColExp)
-> BoolExp b
-> BoolExp b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (BoolExp b) (BoolExp b) (GBoolExp b ColExp) (GBoolExp b ColExp)
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
_Wrapped ((GBoolExp b ColExp -> GBoolExp b ColExp)
 -> BoolExp b -> BoolExp b)
-> ((GBoolExp b ColExp -> GBoolExp b ColExp)
    -> GBoolExp b ColExp -> GBoolExp b ColExp)
-> (GBoolExp b ColExp -> GBoolExp b ColExp)
-> BoolExp b
-> BoolExp b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GBoolExp b ColExp -> GBoolExp b ColExp)
-> GBoolExp b ColExp -> GBoolExp b ColExp
forall a. Plated a => (a -> a) -> a -> a
transform ((GBoolExp b ColExp -> GBoolExp b ColExp)
 -> BoolExp b -> BoolExp b)
-> (GBoolExp b ColExp -> GBoolExp b ColExp)
-> BoolExp b
-> BoolExp b
forall a b. (a -> b) -> a -> b
$
    ((GExists b ColExp -> Identity (GExists b ColExp))
-> GBoolExp b ColExp -> Identity (GBoolExp b ColExp)
forall (backend :: BackendType) field.
Prism' (GBoolExp backend field) (GExists backend field)
_BoolExists ((GExists b ColExp -> Identity (GExists b ColExp))
 -> GBoolExp b ColExp -> Identity (GBoolExp b ColExp))
-> ((TableName b -> Identity (TableName b))
    -> GExists b ColExp -> Identity (GExists b ColExp))
-> (TableName b -> Identity (TableName b))
-> GBoolExp b ColExp
-> Identity (GBoolExp b ColExp)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TableName b -> Identity (TableName b))
-> GExists b ColExp -> Identity (GExists b ColExp)
forall (backend :: BackendType) field.
Lens' (GExists backend field) (TableName backend)
geTable) ((TableName b -> Identity (TableName b))
 -> GBoolExp b ColExp -> Identity (GBoolExp b ColExp))
-> (TableName b -> TableName b)
-> GBoolExp b ColExp
-> GBoolExp b ColExp
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ \TableName b
rqfQT ->
      if TableName b
rqfQT TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
oldQT then TableName b
newQT else TableName b
rqfQT

updateFieldInBoolExp ::
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  RenameField b ->
  BoolExp b ->
  m (BoolExp b)
updateFieldInBoolExp :: TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
qt RenameField b
rf BoolExp b
be =
  GBoolExp b ColExp -> BoolExp b
forall (b :: BackendType). GBoolExp b ColExp -> BoolExp b
BoolExp
    (GBoolExp b ColExp -> BoolExp b)
-> m (GBoolExp b ColExp) -> m (BoolExp b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case BoolExp b -> GBoolExp b ColExp
forall (b :: BackendType). BoolExp b -> GBoolExp b ColExp
unBoolExp BoolExp b
be of
      BoolAnd [GBoolExp b ColExp]
exps -> [GBoolExp b ColExp] -> GBoolExp b ColExp
forall (backend :: BackendType) field.
[GBoolExp backend field] -> GBoolExp backend field
BoolAnd ([GBoolExp b ColExp] -> GBoolExp b ColExp)
-> m [GBoolExp b ColExp] -> m (GBoolExp b ColExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [GBoolExp b ColExp] -> m [GBoolExp b ColExp]
procExps [GBoolExp b ColExp]
exps
      BoolOr [GBoolExp b ColExp]
exps -> [GBoolExp b ColExp] -> GBoolExp b ColExp
forall (backend :: BackendType) field.
[GBoolExp backend field] -> GBoolExp backend field
BoolOr ([GBoolExp b ColExp] -> GBoolExp b ColExp)
-> m [GBoolExp b ColExp] -> m (GBoolExp b ColExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [GBoolExp b ColExp] -> m [GBoolExp b ColExp]
procExps [GBoolExp b ColExp]
exps
      BoolNot GBoolExp b ColExp
e -> GBoolExp b ColExp -> GBoolExp b ColExp
forall (backend :: BackendType) field.
GBoolExp backend field -> GBoolExp backend field
BoolNot (GBoolExp b ColExp -> GBoolExp b ColExp)
-> m (GBoolExp b ColExp) -> m (GBoolExp b ColExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GBoolExp b ColExp -> m (GBoolExp b ColExp)
updateBoolExp' GBoolExp b ColExp
e
      BoolExists (GExists TableName b
refqt GBoolExp b ColExp
wh) ->
        GExists b ColExp -> GBoolExp b ColExp
forall (backend :: BackendType) field.
GExists backend field -> GBoolExp backend field
BoolExists (GExists b ColExp -> GBoolExp b ColExp)
-> (BoolExp b -> GExists b ColExp)
-> BoolExp b
-> GBoolExp b ColExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableName b -> GBoolExp b ColExp -> GExists b ColExp
forall (backend :: BackendType) field.
TableName backend
-> GBoolExp backend field -> GExists backend field
GExists TableName b
refqt (GBoolExp b ColExp -> GExists b ColExp)
-> (BoolExp b -> GBoolExp b ColExp)
-> BoolExp b
-> GExists b ColExp
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BoolExp b -> GBoolExp b ColExp
forall (b :: BackendType). BoolExp b -> GBoolExp b ColExp
unBoolExp
          (BoolExp b -> GBoolExp b ColExp)
-> m (BoolExp b) -> m (GBoolExp b ColExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
refqt RenameField b
rf (GBoolExp b ColExp -> BoolExp b
forall (b :: BackendType). GBoolExp b ColExp -> BoolExp b
BoolExp GBoolExp b ColExp
wh)
      BoolField ColExp
fld -> ColExp -> GBoolExp b ColExp
forall (backend :: BackendType) field.
field -> GBoolExp backend field
BoolField (ColExp -> GBoolExp b ColExp) -> m ColExp -> m (GBoolExp b ColExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TableName b -> RenameField b -> ColExp -> m ColExp
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> ColExp -> m ColExp
updateColExp TableName b
qt RenameField b
rf ColExp
fld
  where
    procExps :: [GBoolExp b ColExp] -> m [GBoolExp b ColExp]
procExps = (GBoolExp b ColExp -> m (GBoolExp b ColExp))
-> [GBoolExp b ColExp] -> m [GBoolExp b ColExp]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM GBoolExp b ColExp -> m (GBoolExp b ColExp)
updateBoolExp'
    updateBoolExp' :: GBoolExp b ColExp -> m (GBoolExp b ColExp)
updateBoolExp' =
      (BoolExp b -> GBoolExp b ColExp)
-> m (BoolExp b) -> m (GBoolExp b ColExp)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BoolExp b -> GBoolExp b ColExp
forall (b :: BackendType). BoolExp b -> GBoolExp b ColExp
unBoolExp (m (BoolExp b) -> m (GBoolExp b ColExp))
-> (GBoolExp b ColExp -> m (BoolExp b))
-> GBoolExp b ColExp
-> m (GBoolExp b ColExp)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
qt RenameField b
rf (BoolExp b -> m (BoolExp b))
-> (GBoolExp b ColExp -> BoolExp b)
-> GBoolExp b ColExp
-> m (BoolExp b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GBoolExp b ColExp -> BoolExp b
forall (b :: BackendType). GBoolExp b ColExp -> BoolExp b
BoolExp

updateColExp ::
  forall b m.
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  RenameField b ->
  ColExp ->
  m ColExp
updateColExp :: TableName b -> RenameField b -> ColExp -> m ColExp
updateColExp TableName b
qt RenameField b
rf (ColExp FieldName
fld Value
val) =
  FieldName -> Value -> ColExp
ColExp FieldName
updatedFld (Value -> ColExp) -> m Value -> m ColExp
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Value
updatedVal
  where
    updatedFld :: FieldName
updatedFld = FieldName -> FieldName -> Bool -> FieldName
forall a. a -> a -> Bool -> a
bool FieldName
fld FieldName
nFld (Bool -> FieldName) -> Bool -> FieldName
forall a b. (a -> b) -> a -> b
$ TableName b
opQT TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
qt Bool -> Bool -> Bool
&& FieldName
oFld FieldName -> FieldName -> Bool
forall a. Eq a => a -> a -> Bool
== FieldName
fld
    updatedVal :: m Value
updatedVal = do
      HashMap (TableName b) (TableInfo b)
tables <- m (HashMap (TableName b) (TableInfo b))
forall r (m :: * -> *). MonadReader r m => m r
ask
      let maybeFieldInfo :: Maybe (FieldInfo b)
maybeFieldInfo =
            TableName b
-> HashMap (TableName b) (TableInfo b) -> Maybe (TableInfo b)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup TableName b
qt HashMap (TableName b) (TableInfo b)
tables
              Maybe (TableInfo b)
-> (TableInfo b -> Maybe (FieldInfo b)) -> Maybe (FieldInfo b)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= FieldName -> HashMap FieldName (FieldInfo b) -> Maybe (FieldInfo b)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
M.lookup FieldName
fld (HashMap FieldName (FieldInfo b) -> Maybe (FieldInfo b))
-> (TableInfo b -> HashMap FieldName (FieldInfo b))
-> TableInfo b
-> Maybe (FieldInfo b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableCoreInfoG b (FieldInfo b) (ColumnInfo b)
-> HashMap FieldName (FieldInfo b)
forall (b :: BackendType) field1 primaryKeyColumn.
TableCoreInfoG b field1 primaryKeyColumn -> FieldInfoMap field1
_tciFieldInfoMap (TableCoreInfoG b (FieldInfo b) (ColumnInfo b)
 -> HashMap FieldName (FieldInfo b))
-> (TableInfo b -> TableCoreInfoG b (FieldInfo b) (ColumnInfo b))
-> TableInfo b
-> HashMap FieldName (FieldInfo b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableInfo b -> TableCoreInfoG b (FieldInfo b) (ColumnInfo b)
forall (b :: BackendType). TableInfo b -> TableCoreInfo b
_tiCoreInfo
      case Maybe (FieldInfo b)
maybeFieldInfo of
        Maybe (FieldInfo b)
Nothing -> Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
        Just FieldInfo b
fi -> case FieldInfo b
fi of
          FIColumn ColumnInfo b
_ -> Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
          FIComputedField ComputedFieldInfo b
_ -> Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
          FIRelationship RelInfo b
ri -> do
            let remTable :: TableName b
remTable = RelInfo b -> TableName b
forall (b :: BackendType). RelInfo b -> TableName b
riRTable RelInfo b
ri
            case Value -> Either QErr (BoolExp b)
forall a (m :: * -> *). (FromJSON a, QErrM m) => Value -> m a
decodeValue Value
val of
              Left QErr
_ -> Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
              Right BoolExp b
be -> BoolExp b -> Value
forall a. ToJSON a => a -> Value
toJSON (BoolExp b -> Value) -> m (BoolExp b) -> m Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
forall (b :: BackendType) (m :: * -> *).
(MonadReader (TableCache b) m, Backend b) =>
TableName b -> RenameField b -> BoolExp b -> m (BoolExp b)
updateFieldInBoolExp TableName b
remTable RenameField b
rf BoolExp b
be
          FIRemoteRelationship {} -> Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val

    (FieldName
oFld, FieldName
nFld, TableName b
opQT) = case RenameField b
rf of
      RFCol (RenameItem TableName b
tn Column b
oCol Column b
nCol) -> (Column b -> FieldName
forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @b Column b
oCol, Column b -> FieldName
forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @b Column b
nCol, TableName b
tn)
      RFRel (RenameItem TableName b
tn RelName
oRel RelName
nRel) -> (RelName -> FieldName
fromRel RelName
oRel, RelName -> FieldName
fromRel RelName
nRel, TableName b
tn)

-- rename columns in relationship definitions
updateColInRel ::
  forall b m.
  (CacheRM m, MonadWriter MetadataModifier m, BackendMetadata b) =>
  SourceName ->
  TableName b ->
  RelName ->
  RenameCol b ->
  m ()
updateColInRel :: SourceName -> TableName b -> RelName -> RenameCol b -> m ()
updateColInRel SourceName
source TableName b
fromQT RelName
rn RenameCol b
rnCol = do
  HashMap (TableName b) (TableInfo b)
tables <- Maybe (HashMap (TableName b) (TableInfo b))
-> HashMap (TableName b) (TableInfo b)
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Maybe (HashMap (TableName b) (TableInfo b))
 -> HashMap (TableName b) (TableInfo b))
-> m (Maybe (HashMap (TableName b) (TableInfo b)))
-> m (HashMap (TableName b) (TableInfo b))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SourceName -> m (Maybe (HashMap (TableName b) (TableInfo b)))
forall (b :: BackendType) (m :: * -> *).
(Backend b, CacheRM m) =>
SourceName -> m (Maybe (TableCache b))
askTableCache @b SourceName
source
  let maybeRelInfo :: Maybe (RelInfo b)
maybeRelInfo =
        HashMap (TableName b) (TableInfo b)
tables HashMap (TableName b) (TableInfo b)
-> Getting
     (First (RelInfo b))
     (HashMap (TableName b) (TableInfo b))
     (RelInfo b)
-> Maybe (RelInfo b)
forall s a. s -> Getting (First a) s a -> Maybe a
^? Index (HashMap (TableName b) (TableInfo b))
-> Traversal'
     (HashMap (TableName b) (TableInfo b))
     (IxValue (HashMap (TableName b) (TableInfo b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (HashMap (TableName b) (TableInfo b))
TableName b
fromQT ((TableInfo b -> Const (First (RelInfo b)) (TableInfo b))
 -> HashMap (TableName b) (TableInfo b)
 -> Const (First (RelInfo b)) (HashMap (TableName b) (TableInfo b)))
-> ((RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
    -> TableInfo b -> Const (First (RelInfo b)) (TableInfo b))
-> Getting
     (First (RelInfo b))
     (HashMap (TableName b) (TableInfo b))
     (RelInfo b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TableCoreInfo b -> Const (First (RelInfo b)) (TableCoreInfo b))
-> TableInfo b -> Const (First (RelInfo b)) (TableInfo b)
forall (b :: BackendType). Lens' (TableInfo b) (TableCoreInfo b)
tiCoreInfo ((TableCoreInfo b -> Const (First (RelInfo b)) (TableCoreInfo b))
 -> TableInfo b -> Const (First (RelInfo b)) (TableInfo b))
-> ((RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
    -> TableCoreInfo b -> Const (First (RelInfo b)) (TableCoreInfo b))
-> (RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
-> TableInfo b
-> Const (First (RelInfo b)) (TableInfo b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FieldInfoMap (FieldInfo b)
 -> Const (First (RelInfo b)) (FieldInfoMap (FieldInfo b)))
-> TableCoreInfo b -> Const (First (RelInfo b)) (TableCoreInfo b)
forall (b :: BackendType) field1 primaryKeyColumn field2.
Lens
  (TableCoreInfoG b field1 primaryKeyColumn)
  (TableCoreInfoG b field2 primaryKeyColumn)
  (FieldInfoMap field1)
  (FieldInfoMap field2)
tciFieldInfoMap ((FieldInfoMap (FieldInfo b)
  -> Const (First (RelInfo b)) (FieldInfoMap (FieldInfo b)))
 -> TableCoreInfo b -> Const (First (RelInfo b)) (TableCoreInfo b))
-> ((RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
    -> FieldInfoMap (FieldInfo b)
    -> Const (First (RelInfo b)) (FieldInfoMap (FieldInfo b)))
-> (RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
-> TableCoreInfo b
-> Const (First (RelInfo b)) (TableCoreInfo b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (FieldInfoMap (FieldInfo b))
-> Traversal'
     (FieldInfoMap (FieldInfo b)) (IxValue (FieldInfoMap (FieldInfo b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix (RelName -> FieldName
fromRel RelName
rn) ((FieldInfo b -> Const (First (RelInfo b)) (FieldInfo b))
 -> FieldInfoMap (FieldInfo b)
 -> Const (First (RelInfo b)) (FieldInfoMap (FieldInfo b)))
-> ((RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
    -> FieldInfo b -> Const (First (RelInfo b)) (FieldInfo b))
-> (RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
-> FieldInfoMap (FieldInfo b)
-> Const (First (RelInfo b)) (FieldInfoMap (FieldInfo b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RelInfo b -> Const (First (RelInfo b)) (RelInfo b))
-> FieldInfo b -> Const (First (RelInfo b)) (FieldInfo b)
forall (b :: BackendType). Prism' (FieldInfo b) (RelInfo b)
_FIRelationship
  Maybe (RelInfo b) -> (RelInfo b -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe (RelInfo b)
maybeRelInfo ((RelInfo b -> m ()) -> m ()) -> (RelInfo b -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \RelInfo b
relInfo ->
    let relTableName :: TableName b
relTableName = RelInfo b -> TableName b
forall (b :: BackendType). RelInfo b -> TableName b
riRTable RelInfo b
relInfo
     in MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
          (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
            SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter SourceName
source TableName b
fromQT
              ASetter' Metadata (TableMetadata b)
-> (TableMetadata b -> TableMetadata b) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ case RelInfo b -> RelType
forall (b :: BackendType). RelInfo b -> RelType
riType RelInfo b
relInfo of
                RelType
ObjRel ->
                  (Relationships (ObjRelDef b)
 -> Identity (Relationships (ObjRelDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Relationships (ObjRelDef b))
tmObjectRelationships ((Relationships (ObjRelDef b)
  -> Identity (Relationships (ObjRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((RelUsing b (ObjRelUsingChoice b)
     -> Identity (RelUsing b (ObjRelUsingChoice b)))
    -> Relationships (ObjRelDef b)
    -> Identity (Relationships (ObjRelDef b)))
-> (RelUsing b (ObjRelUsingChoice b)
    -> Identity (RelUsing b (ObjRelUsingChoice b)))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Relationships (ObjRelDef b))
-> Traversal'
     (Relationships (ObjRelDef b))
     (IxValue (Relationships (ObjRelDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Relationships (ObjRelDef b))
RelName
rn ((ObjRelDef b -> Identity (ObjRelDef b))
 -> Relationships (ObjRelDef b)
 -> Identity (Relationships (ObjRelDef b)))
-> ((RelUsing b (ObjRelUsingChoice b)
     -> Identity (RelUsing b (ObjRelUsingChoice b)))
    -> ObjRelDef b -> Identity (ObjRelDef b))
-> (RelUsing b (ObjRelUsingChoice b)
    -> Identity (RelUsing b (ObjRelUsingChoice b)))
-> Relationships (ObjRelDef b)
-> Identity (Relationships (ObjRelDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RelUsing b (ObjRelUsingChoice b)
 -> Identity (RelUsing b (ObjRelUsingChoice b)))
-> ObjRelDef b -> Identity (ObjRelDef b)
forall a1 a2. Lens (RelDef a1) (RelDef a2) a1 a2
rdUsing
                    ((RelUsing b (ObjRelUsingChoice b)
  -> Identity (RelUsing b (ObjRelUsingChoice b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (RelUsing b (ObjRelUsingChoice b)
    -> RelUsing b (ObjRelUsingChoice b))
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ TableName b
-> TableName b
-> RenameCol b
-> RelUsing b (ObjRelUsingChoice b)
-> RelUsing b (ObjRelUsingChoice b)
forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b -> RenameCol b -> ObjRelUsing b -> ObjRelUsing b
updateColInObjRel TableName b
fromQT TableName b
relTableName RenameCol b
rnCol
                RelType
ArrRel ->
                  (Relationships (ArrRelDef b)
 -> Identity (Relationships (ArrRelDef b)))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) (Relationships (ArrRelDef b))
tmArrayRelationships ((Relationships (ArrRelDef b)
  -> Identity (Relationships (ArrRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((RelUsing b (ArrRelUsingFKeyOn b)
     -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
    -> Relationships (ArrRelDef b)
    -> Identity (Relationships (ArrRelDef b)))
-> (RelUsing b (ArrRelUsingFKeyOn b)
    -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (Relationships (ArrRelDef b))
-> Traversal'
     (Relationships (ArrRelDef b))
     (IxValue (Relationships (ArrRelDef b)))
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index (Relationships (ArrRelDef b))
RelName
rn ((ArrRelDef b -> Identity (ArrRelDef b))
 -> Relationships (ArrRelDef b)
 -> Identity (Relationships (ArrRelDef b)))
-> ((RelUsing b (ArrRelUsingFKeyOn b)
     -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
    -> ArrRelDef b -> Identity (ArrRelDef b))
-> (RelUsing b (ArrRelUsingFKeyOn b)
    -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
-> Relationships (ArrRelDef b)
-> Identity (Relationships (ArrRelDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RelUsing b (ArrRelUsingFKeyOn b)
 -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
-> ArrRelDef b -> Identity (ArrRelDef b)
forall a1 a2. Lens (RelDef a1) (RelDef a2) a1 a2
rdUsing
                    ((RelUsing b (ArrRelUsingFKeyOn b)
  -> Identity (RelUsing b (ArrRelUsingFKeyOn b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (RelUsing b (ArrRelUsingFKeyOn b)
    -> RelUsing b (ArrRelUsingFKeyOn b))
-> TableMetadata b
-> TableMetadata b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ TableName b
-> TableName b
-> RenameCol b
-> RelUsing b (ArrRelUsingFKeyOn b)
-> RelUsing b (ArrRelUsingFKeyOn b)
forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b -> RenameCol b -> ArrRelUsing b -> ArrRelUsing b
updateColInArrRel TableName b
fromQT TableName b
relTableName RenameCol b
rnCol

-- | 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 ::
  forall b m.
  ( MonadError QErr m,
    MonadWriter MetadataModifier m,
    BackendMetadata b
  ) =>
  SourceName ->
  RelName ->
  RenameCol b ->
  m ()
updateColInRemoteRelationshipLHS :: SourceName -> RelName -> RenameCol b -> m ()
updateColInRemoteRelationshipLHS SourceName
source RelName
remoteRelationshipName (RenameItem TableName b
qt Column b
oldCol Column b
newCol) = do
  Name
oldColName <- Text -> m Name
forall (m :: * -> *). MonadError QErr m => Text -> m Name
parseGraphQLName (Text -> m Name) -> Text -> m Name
forall a b. (a -> b) -> a -> b
$ Column b -> Text
forall a. ToTxt a => a -> Text
toTxt Column b
oldCol
  Name
newColName <- Text -> m Name
forall (m :: * -> *). MonadError QErr m => Text -> m Name
parseGraphQLName (Text -> m Name) -> Text -> m Name
forall a b. (a -> b) -> a -> b
$ Column b -> Text
forall a. ToTxt a => a -> Text
toTxt Column b
newCol
  let oldFieldName :: FieldName
oldFieldName = Column b -> FieldName
forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @b Column b
oldCol
      newFieldName :: FieldName
newFieldName = Column b -> FieldName
forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @b Column b
newCol

      updateSet :: HashSet FieldName -> HashSet FieldName
updateSet =
        FieldName -> HashSet FieldName -> HashSet FieldName
forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
Set.insert FieldName
newFieldName (HashSet FieldName -> HashSet FieldName)
-> (HashSet FieldName -> HashSet FieldName)
-> HashSet FieldName
-> HashSet FieldName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldName -> HashSet FieldName -> HashSet FieldName
forall a. (Eq a, Hashable a) => a -> HashSet a -> HashSet a
Set.delete FieldName
oldFieldName

      updateMapKey :: HashMap FieldName FieldName -> HashMap FieldName FieldName
updateMapKey =
        -- mapKeys is not available in 0.2.13.0
        [(FieldName, FieldName)] -> HashMap FieldName FieldName
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(FieldName, FieldName)] -> HashMap FieldName FieldName)
-> (HashMap FieldName FieldName -> [(FieldName, FieldName)])
-> HashMap FieldName FieldName
-> HashMap FieldName FieldName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((FieldName, FieldName) -> (FieldName, FieldName))
-> [(FieldName, FieldName)] -> [(FieldName, FieldName)]
forall a b. (a -> b) -> [a] -> [b]
map (\(FieldName
key, FieldName
value) -> (if FieldName
key FieldName -> FieldName -> Bool
forall a. Eq a => a -> a -> Bool
== FieldName
oldFieldName then FieldName
newFieldName else FieldName
key, FieldName
value)) ([(FieldName, FieldName)] -> [(FieldName, FieldName)])
-> (HashMap FieldName FieldName -> [(FieldName, FieldName)])
-> HashMap FieldName FieldName
-> [(FieldName, FieldName)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap FieldName FieldName -> [(FieldName, FieldName)]
forall k v. HashMap k v -> [(k, v)]
M.toList

      updateFieldCalls :: RemoteFields -> RemoteFields
updateFieldCalls (RemoteFields NonEmpty FieldCall
fields) =
        NonEmpty FieldCall -> RemoteFields
RemoteFields (NonEmpty FieldCall -> RemoteFields)
-> NonEmpty FieldCall -> RemoteFields
forall a b. (a -> b) -> a -> b
$
          NonEmpty FieldCall
fields NonEmpty FieldCall
-> (FieldCall -> FieldCall) -> NonEmpty FieldCall
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \(FieldCall Name
name (RemoteArguments HashMap Name (Value Name)
args)) ->
            Name -> RemoteArguments -> FieldCall
FieldCall Name
name (RemoteArguments -> FieldCall) -> RemoteArguments -> FieldCall
forall a b. (a -> b) -> a -> b
$ HashMap Name (Value Name) -> RemoteArguments
RemoteArguments (HashMap Name (Value Name) -> RemoteArguments)
-> HashMap Name (Value Name) -> RemoteArguments
forall a b. (a -> b) -> a -> b
$ Value Name -> Value Name
updateVariableName (Value Name -> Value Name)
-> HashMap Name (Value Name) -> HashMap Name (Value Name)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Name (Value Name)
args

      updateVariableName :: Value Name -> Value Name
updateVariableName =
        (Name -> Name) -> Value Name -> Value Name
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap \Name
v -> if Name
v Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
oldColName then Name
newColName else Name
v

      remoteRelationshipLens :: (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> Metadata -> Identity Metadata
remoteRelationshipLens =
        SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter @b SourceName
source TableName b
qt ASetter' Metadata (TableMetadata b)
-> ((RemoteRelationshipDefinition
     -> Identity RemoteRelationshipDefinition)
    -> TableMetadata b -> Identity (TableMetadata b))
-> (RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationships -> Identity RemoteRelationships)
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType).
Lens' (TableMetadata b) RemoteRelationships
tmRemoteRelationships ((RemoteRelationships -> Identity RemoteRelationships)
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((RemoteRelationshipDefinition
     -> Identity RemoteRelationshipDefinition)
    -> RemoteRelationships -> Identity RemoteRelationships)
-> (RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index RemoteRelationships
-> Traversal' RemoteRelationships (IxValue RemoteRelationships)
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index RemoteRelationships
RelName
remoteRelationshipName ((RemoteRelationship -> Identity RemoteRelationship)
 -> RemoteRelationships -> Identity RemoteRelationships)
-> ((RemoteRelationshipDefinition
     -> Identity RemoteRelationshipDefinition)
    -> RemoteRelationship -> Identity RemoteRelationship)
-> (RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> RemoteRelationships
-> Identity RemoteRelationships
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> RemoteRelationship -> Identity RemoteRelationship
Lens' RemoteRelationship RemoteRelationshipDefinition
rrDefinition

      remoteSchemaLHSModifier :: Metadata -> Metadata
remoteSchemaLHSModifier =
        (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> Metadata -> Identity Metadata
remoteRelationshipLens ((RemoteRelationshipDefinition
  -> Identity RemoteRelationshipDefinition)
 -> Metadata -> Identity Metadata)
-> ((ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
    -> RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> (ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((RRFormat, ToSchemaRelationshipDef)
 -> Identity (RRFormat, ToSchemaRelationshipDef))
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
Prism'
  RemoteRelationshipDefinition (RRFormat, ToSchemaRelationshipDef)
_RelationshipToSchema (((RRFormat, ToSchemaRelationshipDef)
  -> Identity (RRFormat, ToSchemaRelationshipDef))
 -> RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> ((ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
    -> (RRFormat, ToSchemaRelationshipDef)
    -> Identity (RRFormat, ToSchemaRelationshipDef))
-> (ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
-> (RRFormat, ToSchemaRelationshipDef)
-> Identity (RRFormat, ToSchemaRelationshipDef)
forall s t a b. Field2 s t a b => Lens s t a b
_2
          ((ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
 -> Metadata -> Identity Metadata)
-> (ToSchemaRelationshipDef -> ToSchemaRelationshipDef)
-> Metadata
-> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ ((HashSet FieldName -> Identity (HashSet FieldName))
-> ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef
Lens' ToSchemaRelationshipDef (HashSet FieldName)
trrdLhsFields ((HashSet FieldName -> Identity (HashSet FieldName))
 -> ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
-> (HashSet FieldName -> HashSet FieldName)
-> ToSchemaRelationshipDef
-> ToSchemaRelationshipDef
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ HashSet FieldName -> HashSet FieldName
updateSet)
            (ToSchemaRelationshipDef -> ToSchemaRelationshipDef)
-> (ToSchemaRelationshipDef -> ToSchemaRelationshipDef)
-> ToSchemaRelationshipDef
-> ToSchemaRelationshipDef
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((RemoteFields -> Identity RemoteFields)
-> ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef
Lens' ToSchemaRelationshipDef RemoteFields
trrdRemoteField ((RemoteFields -> Identity RemoteFields)
 -> ToSchemaRelationshipDef -> Identity ToSchemaRelationshipDef)
-> (RemoteFields -> RemoteFields)
-> ToSchemaRelationshipDef
-> ToSchemaRelationshipDef
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ RemoteFields -> RemoteFields
updateFieldCalls)

      remoteSourceLHSModifier :: Metadata -> Metadata
remoteSourceLHSModifier =
        (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> Metadata -> Identity Metadata
remoteRelationshipLens ((RemoteRelationshipDefinition
  -> Identity RemoteRelationshipDefinition)
 -> Metadata -> Identity Metadata)
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
Prism' RemoteRelationshipDefinition ToSourceRelationshipDef
_RelationshipToSource ((ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
 -> RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HashMap FieldName FieldName
 -> Identity (HashMap FieldName FieldName))
-> ToSourceRelationshipDef -> Identity ToSourceRelationshipDef
Lens' ToSourceRelationshipDef (HashMap FieldName FieldName)
tsrdFieldMapping ((HashMap FieldName FieldName
  -> Identity (HashMap FieldName FieldName))
 -> Metadata -> Identity Metadata)
-> (HashMap FieldName FieldName -> HashMap FieldName FieldName)
-> Metadata
-> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ HashMap FieldName FieldName -> HashMap FieldName FieldName
updateMapKey

  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$ (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$ Metadata -> Metadata
remoteSchemaLHSModifier (Metadata -> Metadata)
-> (Metadata -> Metadata) -> Metadata -> Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Metadata -> Metadata
remoteSourceLHSModifier
  where
    parseGraphQLName :: Text -> m Name
parseGraphQLName Text
txt =
      Text -> Maybe Name
G.mkName Text
txt
        Maybe Name -> m Name -> m Name
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> m Name
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
ParseFailed (Text
txt Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" 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,
    Backend source,
    Backend target
  ) =>
  SourceName ->
  TableName source ->
  RelName ->
  RenameCol target ->
  m ()
updateColInRemoteRelationshipRHS :: SourceName
-> TableName source -> RelName -> RenameCol target -> m ()
updateColInRemoteRelationshipRHS SourceName
source TableName source
tableName RelName
remoteRelationshipName (RenameItem TableName target
_ Column target
oldCol Column target
newCol) =
  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      SourceName
-> TableName source -> ASetter' Metadata (TableMetadata source)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter @source SourceName
source TableName source
tableName
        ASetter' Metadata (TableMetadata source)
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> TableMetadata source -> Identity (TableMetadata source))
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationships -> Identity RemoteRelationships)
-> TableMetadata source -> Identity (TableMetadata source)
forall (b :: BackendType).
Lens' (TableMetadata b) RemoteRelationships
tmRemoteRelationships
        ((RemoteRelationships -> Identity RemoteRelationships)
 -> TableMetadata source -> Identity (TableMetadata source))
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> RemoteRelationships -> Identity RemoteRelationships)
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> TableMetadata source
-> Identity (TableMetadata source)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index RemoteRelationships
-> Traversal' RemoteRelationships (IxValue RemoteRelationships)
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index RemoteRelationships
RelName
remoteRelationshipName
        ((RemoteRelationship -> Identity RemoteRelationship)
 -> RemoteRelationships -> Identity RemoteRelationships)
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> RemoteRelationship -> Identity RemoteRelationship)
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> RemoteRelationships
-> Identity RemoteRelationships
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> RemoteRelationship -> Identity RemoteRelationship
Lens' RemoteRelationship RemoteRelationshipDefinition
rrDefinition
        ((RemoteRelationshipDefinition
  -> Identity RemoteRelationshipDefinition)
 -> RemoteRelationship -> Identity RemoteRelationship)
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> RemoteRelationship
-> Identity RemoteRelationship
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
Prism' RemoteRelationshipDefinition ToSourceRelationshipDef
_RelationshipToSource
        ((ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
 -> RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HashMap FieldName FieldName
 -> Identity (HashMap FieldName FieldName))
-> ToSourceRelationshipDef -> Identity ToSourceRelationshipDef
Lens' ToSourceRelationshipDef (HashMap FieldName FieldName)
tsrdFieldMapping
        ((HashMap FieldName FieldName
  -> Identity (HashMap FieldName FieldName))
 -> Metadata -> Identity Metadata)
-> (HashMap FieldName FieldName -> HashMap FieldName FieldName)
-> Metadata
-> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ HashMap FieldName FieldName -> HashMap FieldName FieldName
updateMapValue
  where
    oldFieldName :: FieldName
oldFieldName = Column target -> FieldName
forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @target Column target
oldCol
    newFieldName :: FieldName
newFieldName = Column target -> FieldName
forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @target Column target
newCol
    updateMapValue :: HashMap FieldName FieldName -> HashMap FieldName FieldName
updateMapValue =
      (FieldName -> FieldName)
-> HashMap FieldName FieldName -> HashMap FieldName FieldName
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap \FieldName
value -> if FieldName
value FieldName -> FieldName -> Bool
forall a. Eq a => a -> a -> Bool
== FieldName
oldFieldName then FieldName
newFieldName else FieldName
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,
    Backend source,
    Backend target
  ) =>
  SourceName ->
  TableName source ->
  RelName ->
  RenameTable target ->
  m ()
updateTableInRemoteRelationshipRHS :: SourceName
-> TableName source -> RelName -> RenameTable target -> m ()
updateTableInRemoteRelationshipRHS SourceName
source TableName source
tableName RelName
remoteRelationshipName (TableName target
_, TableName target
newTableName) =
  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      SourceName
-> TableName source -> ASetter' Metadata (TableMetadata source)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter @source SourceName
source TableName source
tableName
        ASetter' Metadata (TableMetadata source)
-> ((Value -> Identity Value)
    -> TableMetadata source -> Identity (TableMetadata source))
-> (Value -> Identity Value)
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationships -> Identity RemoteRelationships)
-> TableMetadata source -> Identity (TableMetadata source)
forall (b :: BackendType).
Lens' (TableMetadata b) RemoteRelationships
tmRemoteRelationships
        ((RemoteRelationships -> Identity RemoteRelationships)
 -> TableMetadata source -> Identity (TableMetadata source))
-> ((Value -> Identity Value)
    -> RemoteRelationships -> Identity RemoteRelationships)
-> (Value -> Identity Value)
-> TableMetadata source
-> Identity (TableMetadata source)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index RemoteRelationships
-> Traversal' RemoteRelationships (IxValue RemoteRelationships)
forall m. Ixed m => Index m -> Traversal' m (IxValue m)
ix Index RemoteRelationships
RelName
remoteRelationshipName
        ((RemoteRelationship -> Identity RemoteRelationship)
 -> RemoteRelationships -> Identity RemoteRelationships)
-> ((Value -> Identity Value)
    -> RemoteRelationship -> Identity RemoteRelationship)
-> (Value -> Identity Value)
-> RemoteRelationships
-> Identity RemoteRelationships
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> RemoteRelationship -> Identity RemoteRelationship
Lens' RemoteRelationship RemoteRelationshipDefinition
rrDefinition
        ((RemoteRelationshipDefinition
  -> Identity RemoteRelationshipDefinition)
 -> RemoteRelationship -> Identity RemoteRelationship)
-> ((Value -> Identity Value)
    -> RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> (Value -> Identity Value)
-> RemoteRelationship
-> Identity RemoteRelationship
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
Prism' RemoteRelationshipDefinition ToSourceRelationshipDef
_RelationshipToSource
        ((ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
 -> RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> ((Value -> Identity Value)
    -> ToSourceRelationshipDef -> Identity ToSourceRelationshipDef)
-> (Value -> Identity Value)
-> RemoteRelationshipDefinition
-> Identity RemoteRelationshipDefinition
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Identity Value)
-> ToSourceRelationshipDef -> Identity ToSourceRelationshipDef
Lens' ToSourceRelationshipDef Value
tsrdTable
        ((Value -> Identity Value) -> Metadata -> Identity Metadata)
-> Value -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> b -> s -> t
.~ TableName target -> Value
forall a. ToJSON a => a -> Value
toJSON TableName target
newTableName

updateColInObjRel ::
  (Backend b) =>
  TableName b ->
  TableName b ->
  RenameCol b ->
  ObjRelUsing b ->
  ObjRelUsing b
updateColInObjRel :: TableName b
-> TableName b -> RenameCol b -> ObjRelUsing b -> ObjRelUsing b
updateColInObjRel TableName b
fromQT TableName b
toQT RenameCol b
rnCol = \case
  RUFKeyOn ObjRelUsingChoice b
c ->
    ObjRelUsingChoice b -> ObjRelUsing b
forall (b :: BackendType) a. a -> RelUsing b a
RUFKeyOn (ObjRelUsingChoice b -> ObjRelUsing b)
-> ObjRelUsingChoice b -> ObjRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b
-> TableName b
-> RenameCol b
-> ObjRelUsingChoice b
-> ObjRelUsingChoice b
forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b
-> RenameCol b
-> ObjRelUsingChoice b
-> ObjRelUsingChoice b
updateRelChoice TableName b
fromQT TableName b
toQT RenameCol b
rnCol ObjRelUsingChoice b
c
  RUManual RelManualConfig b
manConfig ->
    RelManualConfig b -> ObjRelUsing b
forall (b :: BackendType) a. RelManualConfig b -> RelUsing b a
RUManual (RelManualConfig b -> ObjRelUsing b)
-> RelManualConfig b -> ObjRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b
-> TableName b
-> RenameCol b
-> RelManualConfig b
-> RelManualConfig b
forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b
-> RenameCol b
-> RelManualConfig b
-> RelManualConfig b
updateRelManualConfig TableName b
fromQT TableName b
toQT RenameCol b
rnCol RelManualConfig b
manConfig

updateRelChoice ::
  Backend b =>
  TableName b ->
  TableName b ->
  RenameCol b ->
  ObjRelUsingChoice b ->
  ObjRelUsingChoice b
updateRelChoice :: TableName b
-> TableName b
-> RenameCol b
-> ObjRelUsingChoice b
-> ObjRelUsingChoice b
updateRelChoice TableName b
fromQT TableName b
toQT RenameCol b
rnCol =
  \case
    SameTable NonEmpty (Column b)
col -> NonEmpty (Column b) -> ObjRelUsingChoice b
forall (b :: BackendType).
NonEmpty (Column b) -> ObjRelUsingChoice b
SameTable (NonEmpty (Column b) -> ObjRelUsingChoice b)
-> NonEmpty (Column b) -> ObjRelUsingChoice b
forall a b. (a -> b) -> a -> b
$ RenameCol b
-> TableName b -> NonEmpty (Column b) -> NonEmpty (Column b)
forall (b :: BackendType) (f :: * -> *).
(Backend b, Functor f) =>
RenameCol b -> TableName b -> f (Column b) -> f (Column b)
getNewCol RenameCol b
rnCol TableName b
fromQT NonEmpty (Column b)
col
    RemoteTable TableName b
t NonEmpty (Column b)
c -> TableName b -> NonEmpty (Column b) -> ObjRelUsingChoice b
forall (b :: BackendType).
TableName b -> NonEmpty (Column b) -> ObjRelUsingChoice b
RemoteTable TableName b
t (RenameCol b
-> TableName b -> NonEmpty (Column b) -> NonEmpty (Column b)
forall (b :: BackendType) (f :: * -> *).
(Backend b, Functor f) =>
RenameCol b -> TableName b -> f (Column b) -> f (Column b)
getNewCol RenameCol b
rnCol TableName b
toQT NonEmpty (Column b)
c)

updateColInArrRel ::
  (Backend b) =>
  TableName b ->
  TableName b ->
  RenameCol b ->
  ArrRelUsing b ->
  ArrRelUsing b
updateColInArrRel :: TableName b
-> TableName b -> RenameCol b -> ArrRelUsing b -> ArrRelUsing b
updateColInArrRel TableName b
fromQT TableName b
toQT RenameCol b
rnCol = \case
  RUFKeyOn (ArrRelUsingFKeyOn TableName b
t NonEmpty (Column b)
c) ->
    let updCol :: NonEmpty (Column b)
updCol = RenameCol b
-> TableName b -> NonEmpty (Column b) -> NonEmpty (Column b)
forall (b :: BackendType) (f :: * -> *).
(Backend b, Functor f) =>
RenameCol b -> TableName b -> f (Column b) -> f (Column b)
getNewCol RenameCol b
rnCol TableName b
toQT NonEmpty (Column b)
c
     in ArrRelUsingFKeyOn b -> ArrRelUsing b
forall (b :: BackendType) a. a -> RelUsing b a
RUFKeyOn (ArrRelUsingFKeyOn b -> ArrRelUsing b)
-> ArrRelUsingFKeyOn b -> ArrRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b -> NonEmpty (Column b) -> ArrRelUsingFKeyOn b
forall (b :: BackendType).
TableName b -> NonEmpty (Column b) -> ArrRelUsingFKeyOn b
ArrRelUsingFKeyOn TableName b
t NonEmpty (Column b)
updCol
  RUManual RelManualConfig b
manConfig -> RelManualConfig b -> ArrRelUsing b
forall (b :: BackendType) a. RelManualConfig b -> RelUsing b a
RUManual (RelManualConfig b -> ArrRelUsing b)
-> RelManualConfig b -> ArrRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b
-> TableName b
-> RenameCol b
-> RelManualConfig b
-> RelManualConfig b
forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b
-> RenameCol b
-> RelManualConfig b
-> RelManualConfig b
updateRelManualConfig TableName b
fromQT TableName b
toQT RenameCol b
rnCol RelManualConfig b
manConfig

type ColMap b = HashMap (Column b) (Column b)

getNewCol ::
  forall b f.
  Backend b =>
  Functor f =>
  RenameCol b ->
  TableName b ->
  f (Column b) ->
  f (Column b)
getNewCol :: RenameCol b -> TableName b -> f (Column b) -> f (Column b)
getNewCol RenameCol b
rnCol TableName b
qt f (Column b)
cols =
  if TableName b
qt TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
opQT
    then Column b -> Column b
go (Column b -> Column b) -> f (Column b) -> f (Column b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f (Column b)
cols
    else f (Column b)
cols
  where
    RenameItem TableName b
opQT Column b
oCol Column b
nCol = RenameCol b
rnCol
    go :: Column b -> Column b
    go :: Column b -> Column b
go Column b
col
      | Column b
col Column b -> Column b -> Bool
forall a. Eq a => a -> a -> Bool
== Column b
oCol = Column b
nCol
      | Bool
otherwise = Column b
col

updateRelManualConfig ::
  forall b.
  (Backend b) =>
  TableName b ->
  TableName b ->
  RenameCol b ->
  RelManualConfig b ->
  RelManualConfig b
updateRelManualConfig :: TableName b
-> TableName b
-> RenameCol b
-> RelManualConfig b
-> RelManualConfig b
updateRelManualConfig TableName b
fromQT TableName b
toQT RenameCol b
rnCol RelManualConfig b
manConfig =
  TableName b
-> HashMap (Column b) (Column b)
-> Maybe InsertOrder
-> RelManualConfig b
forall (b :: BackendType).
TableName b
-> HashMap (Column b) (Column b)
-> Maybe InsertOrder
-> RelManualConfig b
RelManualConfig TableName b
tn (TableName b
-> TableName b
-> RenameCol b
-> HashMap (Column b) (Column b)
-> HashMap (Column b) (Column b)
forall (b :: BackendType).
Backend b =>
TableName b -> TableName b -> RenameCol b -> ColMap b -> ColMap b
updateColMap TableName b
fromQT TableName b
toQT RenameCol b
rnCol HashMap (Column b) (Column b)
colMap) Maybe InsertOrder
io
  where
    RelManualConfig TableName b
tn HashMap (Column b) (Column b)
colMap Maybe InsertOrder
io = RelManualConfig b
manConfig

updateColMap ::
  forall b.
  (Backend b) =>
  TableName b ->
  TableName b ->
  RenameCol b ->
  ColMap b ->
  ColMap b
updateColMap :: TableName b -> TableName b -> RenameCol b -> ColMap b -> ColMap b
updateColMap TableName b
fromQT TableName b
toQT RenameCol b
rnCol =
  [(Column b, Column b)] -> ColMap b
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(Column b, Column b)] -> ColMap b)
-> (ColMap b -> [(Column b, Column b)]) -> ColMap b -> ColMap b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Column b, Column b) -> (Column b, Column b))
-> [(Column b, Column b)] -> [(Column b, Column b)]
forall a b. (a -> b) -> [a] -> [b]
map (TableName b -> Column b -> Column b
modCol TableName b
fromQT (Column b -> Column b)
-> (Column b -> Column b)
-> (Column b, Column b)
-> (Column b, Column b)
forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** TableName b -> Column b -> Column b
modCol TableName b
toQT) ([(Column b, Column b)] -> [(Column b, Column b)])
-> (ColMap b -> [(Column b, Column b)])
-> ColMap b
-> [(Column b, Column b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ColMap b -> [(Column b, Column b)]
forall k v. HashMap k v -> [(k, v)]
M.toList
  where
    RenameItem TableName b
qt Column b
oCol Column b
nCol = RenameCol b
rnCol
    modCol :: TableName b -> Column b -> Column b
modCol TableName b
colQt Column b
col = if TableName b
colQt TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
== TableName b
qt Bool -> Bool -> Bool
&& Column b
col Column b -> Column b -> Bool
forall a. Eq a => a -> a -> Bool
== Column b
oCol then Column b
nCol else Column b
col

possiblyUpdateCustomColumnNames ::
  forall b m.
  (MonadWriter MetadataModifier m, BackendMetadata b) =>
  SourceName ->
  TableName b ->
  Column b ->
  Column b ->
  m ()
possiblyUpdateCustomColumnNames :: SourceName -> TableName b -> Column b -> Column b -> m ()
possiblyUpdateCustomColumnNames SourceName
source TableName b
tableName Column b
oldColumn Column b
newColumn = do
  MetadataModifier -> m ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
forall (b :: BackendType).
Backend b =>
SourceName -> TableName b -> ASetter' Metadata (TableMetadata b)
tableMetadataSetter @b SourceName
source TableName b
tableName ASetter' Metadata (TableMetadata b)
-> ((HashMap (Column b) ColumnConfig
     -> Identity (HashMap (Column b) ColumnConfig))
    -> TableMetadata b -> Identity (TableMetadata b))
-> (HashMap (Column b) ColumnConfig
    -> Identity (HashMap (Column b) ColumnConfig))
-> Metadata
-> Identity Metadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TableConfig b -> Identity (TableConfig b))
-> TableMetadata b -> Identity (TableMetadata b)
forall (b :: BackendType). Lens' (TableMetadata b) (TableConfig b)
tmConfiguration ((TableConfig b -> Identity (TableConfig b))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((HashMap (Column b) ColumnConfig
     -> Identity (HashMap (Column b) ColumnConfig))
    -> TableConfig b -> Identity (TableConfig b))
-> (HashMap (Column b) ColumnConfig
    -> Identity (HashMap (Column b) ColumnConfig))
-> TableMetadata b
-> Identity (TableMetadata b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HashMap (Column b) ColumnConfig
 -> Identity (HashMap (Column b) ColumnConfig))
-> TableConfig b -> Identity (TableConfig b)
forall (b1 :: BackendType) (b2 :: BackendType).
Lens
  (TableConfig b1)
  (TableConfig b2)
  (HashMap (Column b1) ColumnConfig)
  (HashMap (Column b2) ColumnConfig)
tcColumnConfig ((HashMap (Column b) ColumnConfig
  -> Identity (HashMap (Column b) ColumnConfig))
 -> Metadata -> Identity Metadata)
-> (HashMap (Column b) ColumnConfig
    -> HashMap (Column b) ColumnConfig)
-> Metadata
-> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ HashMap (Column b) ColumnConfig -> HashMap (Column b) ColumnConfig
forall columnData.
HashMap (Column b) columnData -> HashMap (Column b) columnData
swapOldColumnForNewColumn
  where
    swapOldColumnForNewColumn :: HashMap (Column b) columnData -> HashMap (Column b) columnData
    swapOldColumnForNewColumn :: HashMap (Column b) columnData -> HashMap (Column b) columnData
swapOldColumnForNewColumn HashMap (Column b) columnData
customColumns =
      [(Column b, columnData)] -> HashMap (Column b) columnData
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
M.fromList ([(Column b, columnData)] -> HashMap (Column b) columnData)
-> [(Column b, columnData)] -> HashMap (Column b) columnData
forall a b. (a -> b) -> a -> b
$ (\(Column b
dbCol, columnData
val) -> (,columnData
val) (Column b -> (Column b, columnData))
-> Column b -> (Column b, columnData)
forall a b. (a -> b) -> a -> b
$ if Column b
dbCol Column b -> Column b -> Bool
forall a. Eq a => a -> a -> Bool
== Column b
oldColumn then Column b
newColumn else Column b
dbCol) ((Column b, columnData) -> (Column b, columnData))
-> [(Column b, columnData)] -> [(Column b, columnData)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap (Column b) columnData -> [(Column b, columnData)]
forall k v. HashMap k v -> [(k, v)]
M.toList HashMap (Column b) columnData
customColumns