-- | 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 HashMap
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
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.IR.BoolExp.Lenses (geTable, _BoolExists)
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Column
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.Metadata.Backend
import Hasura.RQL.Types.Permission
import Hasura.RQL.Types.Relationships.Local
import Hasura.RQL.Types.Relationships.Remote
import Hasura.RQL.Types.Relationships.ToSource
import Hasura.RQL.Types.Roles (RoleName)
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.SchemaCacheTypes
import Hasura.RemoteSchema.Metadata
import Hasura.SQL.AnyBackend qualified as AB
import Hasura.Table.Cache
import Hasura.Table.Metadata
  ( Relationships,
    TableMetadata (..),
    tmArrayRelationships,
    tmConfiguration,
    tmDeletePermissions,
    tmEventTriggers,
    tmInsertPermissions,
    tmObjectRelationships,
    tmRemoteRelationships,
    tmSelectPermissions,
    tmUpdatePermissions,
  )
import Language.GraphQL.Draft.Syntax qualified as G

data RenameItem (b :: BackendType) a = RenameItem
  { forall (b :: BackendType) a. RenameItem b a -> TableName b
_riTable :: TableName b,
    forall (b :: BackendType) a. RenameItem b a -> a
_riOld :: a,
    forall (b :: BackendType) 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 :: forall (m :: * -> *). QErrM m => 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 :: forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
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
$ 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 <- 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) ->
              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) ->
              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 a. a -> m a
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 a. a -> m a
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) ->
      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
            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
. (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) (f :: * -> *).
Functor f =>
(Tables b -> f (Tables b))
-> SourceMetadata b -> f (SourceMetadata 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
InsOrdHashMap.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
InsOrdHashMap.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
InsOrdHashMap.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 :: forall (b :: BackendType) (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
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
$ 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
$ forall (b :: BackendType). Column b -> TableObjId b
TOCol @b Column b
oCol
      renameItem :: RenameItem b (Column b)
renameItem = 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 <- 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) ->
              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) ->
              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
$ 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) (f :: * -> *).
Functor f =>
(EventTriggers b -> f (EventTriggers b))
-> TableMetadata b -> f (TableMetadata 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
%~ 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) ->
      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
            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
  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
HashMap.lookup (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 a. a -> m a
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 :: forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
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
$ 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
$ 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 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)) ->
        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
$ 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) (f :: * -> *).
Functor f =>
(Relationships (ObjRelDef b) -> f (Relationships (ObjRelDef b)))
-> TableMetadata b -> f (TableMetadata 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) (f :: * -> *).
Functor f =>
(Relationships (ArrRelDef b) -> f (Relationships (ArrRelDef b)))
-> TableMetadata b -> f (TableMetadata 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 :: forall a. 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
InsOrdHashMap.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
InsOrdHashMap.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
InsOrdHashMap.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 :: forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
SourceName -> TableName b -> RelName -> RenameTable b -> m ()
updateRelDefs SourceName
source TableName b
qt RelName
rn RenameTable b
renameTable = do
  FieldInfoMap (FieldInfo b)
fim <- 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) (f :: * -> *).
Functor f =>
(Relationships (ObjRelDef b) -> f (Relationships (ObjRelDef b)))
-> TableMetadata b -> f (TableMetadata 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) (f :: * -> *).
Functor f =>
(Relationships (ArrRelDef b) -> f (Relationships (ArrRelDef b)))
-> TableMetadata b -> f (TableMetadata 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) =
      (ObjRelUsing b -> Identity (ObjRelUsing b))
-> ObjRelDef b -> Identity (ObjRelDef b)
forall a1 a2 (f :: * -> *).
Functor f =>
(a1 -> f a2) -> RelDef a1 -> f (RelDef a2)
rdUsing ((ObjRelUsing b -> Identity (ObjRelUsing b))
 -> ObjRelDef b -> Identity (ObjRelDef b))
-> (ObjRelUsing b -> ObjRelUsing 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 -> ObjRelUsing b
forall (b :: BackendType) a. a -> RelUsing b a
RUFKeyOn ObjRelUsingChoice b
fk
        RUManual (RelManualTableConfig TableName b
origQT (RelManualCommon 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 RelManualTableConfig b -> ObjRelUsing b
forall (b :: BackendType) a. RelManualTableConfig b -> RelUsing b a
RUManual (RelManualTableConfig b -> ObjRelUsing b)
-> RelManualTableConfig b -> ObjRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b -> RelManualCommon b -> RelManualTableConfig b
forall (b :: BackendType).
TableName b -> RelManualCommon b -> RelManualTableConfig b
RelManualTableConfig TableName b
updQT (HashMap (Column b) (Column b)
-> Maybe InsertOrder -> RelManualCommon b
forall (b :: BackendType).
HashMap (Column b) (Column b)
-> Maybe InsertOrder -> RelManualCommon b
RelManualCommon 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) =
      (ArrRelUsing b -> Identity (ArrRelUsing b))
-> ArrRelDef b -> Identity (ArrRelDef b)
forall a1 a2 (f :: * -> *).
Functor f =>
(a1 -> f a2) -> RelDef a1 -> f (RelDef a2)
rdUsing ((ArrRelUsing b -> Identity (ArrRelUsing b))
 -> ArrRelDef b -> Identity (ArrRelDef b))
-> (ArrRelUsing b -> ArrRelUsing 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 -> 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
updQT NonEmpty (Column b)
c
        RUManual (RelManualTableConfig TableName b
origQT (RelManualCommon HashMap (Column b) (Column b)
rmCols Maybe InsertOrder
rmIO)) ->
          let updQT :: TableName b
updQT = TableName b -> TableName b
getUpdQT TableName b
origQT
           in RelManualTableConfig b -> ArrRelUsing b
forall (b :: BackendType) a. RelManualTableConfig b -> RelUsing b a
RUManual (RelManualTableConfig b -> ArrRelUsing b)
-> RelManualTableConfig b -> ArrRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b -> RelManualCommon b -> RelManualTableConfig b
forall (b :: BackendType).
TableName b -> RelManualCommon b -> RelManualTableConfig b
RelManualTableConfig TableName b
updQT (HashMap (Column b) (Column b)
-> Maybe InsertOrder -> RelManualCommon b
forall (b :: BackendType).
HashMap (Column b) (Column b)
-> Maybe InsertOrder -> RelManualCommon b
RelManualCommon 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 :: forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, CacheRM m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
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 m. Monoid m => Maybe m -> m
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 :: forall a. Reader (HashMap (TableName b) (TableInfo b)) a -> a
withTables = (ReaderT (HashMap (TableName b) (TableInfo b)) Identity a
 -> HashMap (TableName b) (TableInfo b) -> a)
-> HashMap (TableName b) (TableInfo b)
-> ReaderT (HashMap (TableName b) (TableInfo b)) Identity a
-> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip ReaderT (HashMap (TableName b) (TableInfo b)) Identity 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) (f :: * -> *).
Functor f =>
(Permissions (InsPermDef b) -> f (Permissions (InsPermDef b)))
-> TableMetadata b -> f (TableMetadata 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 -> *) (f :: * -> *).
Functor f =>
(PermDefPermission b1 perm1 -> f (PermDefPermission b2 perm2))
-> PermDef b1 perm1 -> f (PermDef 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) (f :: * -> *).
Functor f =>
(Permissions (SelPermDef b) -> f (Permissions (SelPermDef b)))
-> TableMetadata b -> f (TableMetadata 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 -> *) (f :: * -> *).
Functor f =>
(PermDefPermission b1 perm1 -> f (PermDefPermission b2 perm2))
-> PermDef b1 perm1 -> f (PermDef 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) (f :: * -> *).
Functor f =>
(Permissions (UpdPermDef b) -> f (Permissions (UpdPermDef b)))
-> TableMetadata b -> f (TableMetadata 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 -> *) (f :: * -> *).
Functor f =>
(PermDefPermission b1 perm1 -> f (PermDefPermission b2 perm2))
-> PermDef b1 perm1 -> f (PermDef 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) (f :: * -> *).
Functor f =>
(Permissions (DelPermDef b) -> f (Permissions (DelPermDef b)))
-> TableMetadata b -> f (TableMetadata 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 -> *) (f :: * -> *).
Functor f =>
(PermDefPermission b1 perm1 -> f (PermDefPermission b2 perm2))
-> PermDef b1 perm1 -> f (PermDef 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 :: 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 (InsPerm' (InsPerm BoolExp b
chk Maybe (ColumnValues b Value)
preset Maybe (PermColSpec b)
cols Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput)) =
  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 a. a -> m a
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
-> Maybe (ValidateInput InputWebhook)
-> InsPerm b
forall (b :: BackendType).
BoolExp b
-> Maybe (ColumnValues b Value)
-> Maybe (PermColSpec b)
-> Bool
-> Maybe (ValidateInput InputWebhook)
-> InsPerm b
InsPerm BoolExp b
updChk Maybe (ColumnValues b Value)
preset Maybe (PermColSpec b)
cols Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput
    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 a. a -> m a
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
-> Maybe (ValidateInput InputWebhook)
-> InsPerm b
forall (b :: BackendType).
BoolExp b
-> Maybe (ColumnValues b Value)
-> Maybe (PermColSpec b)
-> Bool
-> Maybe (ValidateInput InputWebhook)
-> InsPerm b
InsPerm BoolExp b
updChk Maybe (ColumnValues b Value)
updPresetM Maybe (PermColSpec b)
updColsM Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput

updateSelPermFlds ::
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  Rename b ->
  PermDefPermission b SelPerm ->
  m (PermDefPermission b SelPerm)
updateSelPermFlds :: 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 (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 a. a -> m a
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 a. a -> m a
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 :: 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 (UpdPerm' (UpdPerm PermColSpec b
cols Maybe (ColumnValues b Value)
preset BoolExp b
fltr Maybe (BoolExp b)
check Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput)) = 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 a b. (a -> b) -> Maybe a -> Maybe 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 a. a -> m a
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
-> Maybe (ValidateInput InputWebhook)
-> UpdPerm b
forall (b :: BackendType).
PermColSpec b
-> Maybe (ColumnValues b Value)
-> BoolExp b
-> Maybe (BoolExp b)
-> Bool
-> Maybe (ValidateInput InputWebhook)
-> UpdPerm b
UpdPerm PermColSpec b
cols Maybe (ColumnValues b Value)
preset BoolExp b
updFltr Maybe (BoolExp b)
updCheck Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput
    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)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe 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 a. a -> m a
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
-> Maybe (ValidateInput InputWebhook)
-> UpdPerm b
forall (b :: BackendType).
PermColSpec b
-> Maybe (ColumnValues b Value)
-> BoolExp b
-> Maybe (BoolExp b)
-> Bool
-> Maybe (ValidateInput InputWebhook)
-> UpdPerm b
UpdPerm PermColSpec b
updCols Maybe (ColumnValues b Value)
updPresetM BoolExp b
updFltr Maybe (BoolExp b)
updCheck Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput

updateDelPermFlds ::
  (MonadReader (TableCache b) m, Backend b) =>
  TableName b ->
  Rename b ->
  PermDefPermission b DelPerm ->
  m (PermDefPermission b DelPerm)
updateDelPermFlds :: 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 (DelPerm' (DelPerm BoolExp b
fltr Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput)) = 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 a. a -> m a
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 -> Maybe (ValidateInput InputWebhook) -> DelPerm b
forall (b :: BackendType).
BoolExp b
-> Bool -> Maybe (ValidateInput InputWebhook) -> DelPerm b
DelPerm BoolExp b
updFltr Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput
    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 a. a -> m a
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 -> Maybe (ValidateInput InputWebhook) -> DelPerm b
forall (b :: BackendType).
BoolExp b
-> Bool -> Maybe (ValidateInput InputWebhook) -> DelPerm b
DelPerm BoolExp b
updFltr Bool
backendOnly Maybe (ValidateInput InputWebhook)
validateInput

updatePreset ::
  (Backend b) =>
  TableName b ->
  RenameField b ->
  ColumnValues b Value ->
  ColumnValues b Value
updatePreset :: forall (b :: BackendType).
Backend b =>
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
HashMap.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)]
HashMap.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 :: forall (b :: BackendType).
Backend b =>
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 :: forall (b :: BackendType).
Backend b =>
RenameTable b -> BoolExp b -> BoolExp b
updateTableInBoolExp (TableName b
oldQT, TableName b
newQT) =
  ASetter
  (BoolExp b)
  (BoolExp b)
  (Unwrapped (BoolExp b))
  (Unwrapped (BoolExp b))
-> (Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
-> BoolExp b
-> BoolExp b
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
over ASetter
  (BoolExp b)
  (BoolExp b)
  (Unwrapped (BoolExp b))
  (Unwrapped (BoolExp b))
forall s t. Rewrapping s t => Iso s t (Unwrapped s) (Unwrapped t)
Iso
  (BoolExp b)
  (BoolExp b)
  (Unwrapped (BoolExp b))
  (Unwrapped (BoolExp b))
_Wrapped
    ((Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
 -> BoolExp b -> BoolExp b)
-> ((Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
    -> Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
-> (Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
-> BoolExp b
-> BoolExp b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
-> Unwrapped (BoolExp b) -> Unwrapped (BoolExp b)
forall a. Plated a => (a -> a) -> a -> a
transform
    ((Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
 -> BoolExp b -> BoolExp b)
-> (Unwrapped (BoolExp b) -> Unwrapped (BoolExp b))
-> BoolExp b
-> BoolExp b
forall a b. (a -> b) -> a -> b
$ ((GExists b ColExp -> Identity (GExists b ColExp))
-> Unwrapped (BoolExp b) -> Identity (Unwrapped (BoolExp b))
(GExists b ColExp -> Identity (GExists b ColExp))
-> GBoolExp b ColExp -> Identity (GBoolExp b ColExp)
forall (backend :: BackendType) field (p :: * -> * -> *)
       (f :: * -> *).
(Choice p, Applicative f) =>
p (GExists backend field) (f (GExists backend field))
-> p (GBoolExp backend field) (f (GBoolExp backend field))
_BoolExists ((GExists b ColExp -> Identity (GExists b ColExp))
 -> Unwrapped (BoolExp b) -> Identity (Unwrapped (BoolExp b)))
-> ((TableName b -> Identity (TableName b))
    -> GExists b ColExp -> Identity (GExists b ColExp))
-> (TableName b -> Identity (TableName b))
-> Unwrapped (BoolExp b)
-> Identity (Unwrapped (BoolExp b))
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 (f :: * -> *).
Functor f =>
(TableName backend -> f (TableName backend))
-> GExists backend field -> f (GExists backend field)
geTable)
    ((TableName b -> Identity (TableName b))
 -> Unwrapped (BoolExp b) -> Identity (Unwrapped (BoolExp b)))
-> (TableName b -> TableName b)
-> Unwrapped (BoolExp b)
-> Unwrapped (BoolExp b)
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 :: 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
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)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [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 a b. (a -> b) -> m a -> m b
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 :: 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 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
      TableCache b
tables <- m (TableCache b)
forall r (m :: * -> *). MonadReader r m => m r
ask
      let maybeFieldInfo :: Maybe (FieldInfo b)
maybeFieldInfo =
            TableName b -> TableCache b -> Maybe (TableInfo b)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup TableName b
qt TableCache b
tables
              Maybe (TableInfo b)
-> (TableInfo b -> Maybe (FieldInfo b)) -> Maybe (FieldInfo b)
forall a b. Maybe a -> (a -> Maybe b) -> Maybe 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
HashMap.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) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> FieldInfoMap field
_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 a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
        Just FieldInfo b
fi -> case FieldInfo b
fi of
          FIColumn StructuredColumnInfo b
_ -> Value -> m Value
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
          FIComputedField ComputedFieldInfo b
_ -> Value -> m Value
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
val
          FIRelationship RelInfo b
ri -> do
            case RelInfo b -> RelTarget b
forall (b :: BackendType). RelInfo b -> RelTarget b
riTarget RelInfo b
ri of
              RelTargetNativeQuery NativeQueryName
_ -> [Char] -> m Value
forall a. HasCallStack => [Char] -> a
error [Char]
"updateColExp RelTargetNativeQuery"
              RelTargetTable TableName b
remTable ->
                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 a. a -> m a
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 a. a -> m a
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) -> (forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @b Column b
oCol, 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 :: forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadWriter MetadataModifier m, BackendMetadata b) =>
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 m. Monoid m => Maybe m -> m
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
<$> 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) (f :: * -> *).
Functor f =>
(TableCoreInfo b -> f (TableCoreInfo b))
-> TableInfo b -> f (TableInfo 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
       (f :: * -> *).
Functor f =>
(FieldInfoMap field1 -> f (FieldInfoMap field2))
-> TableCoreInfoG b field1 primaryKeyColumn
-> f (TableCoreInfoG b field2 primaryKeyColumn)
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) (p :: * -> * -> *) (f :: * -> *).
(Choice p, Applicative f) =>
p (RelInfo b) (f (RelInfo b)) -> p (FieldInfo b) (f (FieldInfo 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 ->
    case RelInfo b -> RelTarget b
forall (b :: BackendType). RelInfo b -> RelTarget b
riTarget RelInfo b
relInfo of
      RelTargetNativeQuery NativeQueryName
_ -> [Char] -> m ()
forall a. HasCallStack => [Char] -> a
error [Char]
"updateColInRel RelTargetNativeQuery"
      RelTargetTable TableName b
relTableName ->
        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) (f :: * -> *).
Functor f =>
(Relationships (ObjRelDef b) -> f (Relationships (ObjRelDef b)))
-> TableMetadata b -> f (TableMetadata b)
tmObjectRelationships ((Relationships (ObjRelDef b)
  -> Identity (Relationships (ObjRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((ObjRelUsing b -> Identity (ObjRelUsing b))
    -> Relationships (ObjRelDef b)
    -> Identity (Relationships (ObjRelDef b)))
-> (ObjRelUsing b -> Identity (ObjRelUsing 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)))
-> ((ObjRelUsing b -> Identity (ObjRelUsing b))
    -> ObjRelDef b -> Identity (ObjRelDef b))
-> (ObjRelUsing b -> Identity (ObjRelUsing b))
-> Relationships (ObjRelDef b)
-> Identity (Relationships (ObjRelDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ObjRelUsing b -> Identity (ObjRelUsing b))
-> ObjRelDef b -> Identity (ObjRelDef b)
forall a1 a2 (f :: * -> *).
Functor f =>
(a1 -> f a2) -> RelDef a1 -> f (RelDef a2)
rdUsing
                ((ObjRelUsing b -> Identity (ObjRelUsing b))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (ObjRelUsing b -> ObjRelUsing 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 -> ObjRelUsing b -> ObjRelUsing 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) (f :: * -> *).
Functor f =>
(Relationships (ArrRelDef b) -> f (Relationships (ArrRelDef b)))
-> TableMetadata b -> f (TableMetadata b)
tmArrayRelationships ((Relationships (ArrRelDef b)
  -> Identity (Relationships (ArrRelDef b)))
 -> TableMetadata b -> Identity (TableMetadata b))
-> ((ArrRelUsing b -> Identity (ArrRelUsing b))
    -> Relationships (ArrRelDef b)
    -> Identity (Relationships (ArrRelDef b)))
-> (ArrRelUsing b -> Identity (ArrRelUsing 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)))
-> ((ArrRelUsing b -> Identity (ArrRelUsing b))
    -> ArrRelDef b -> Identity (ArrRelDef b))
-> (ArrRelUsing b -> Identity (ArrRelUsing b))
-> Relationships (ArrRelDef b)
-> Identity (Relationships (ArrRelDef b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ArrRelUsing b -> Identity (ArrRelUsing b))
-> ArrRelDef b -> Identity (ArrRelDef b)
forall a1 a2 (f :: * -> *).
Functor f =>
(a1 -> f a2) -> RelDef a1 -> f (RelDef a2)
rdUsing
                ((ArrRelUsing b -> Identity (ArrRelUsing b))
 -> TableMetadata b -> Identity (TableMetadata b))
-> (ArrRelUsing b -> ArrRelUsing 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 -> ArrRelUsing b -> ArrRelUsing 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 :: forall (b :: BackendType) (m :: * -> *).
(MonadError QErr m, MonadWriter MetadataModifier m,
 BackendMetadata b) =>
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 = forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @b Column b
oldCol
      newFieldName :: FieldName
newFieldName = 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
HashMap.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)]
HashMap.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 a b. (a -> b) -> Value a -> Value b
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 =
        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) (f :: * -> *).
Functor f =>
(RemoteRelationships -> f RemoteRelationships)
-> TableMetadata b -> f (TableMetadata b)
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 ((RemoteRelationshipG RemoteRelationshipDefinition
  -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
 -> RemoteRelationships -> Identity RemoteRelationships)
-> ((RemoteRelationshipDefinition
     -> Identity RemoteRelationshipDefinition)
    -> RemoteRelationshipG RemoteRelationshipDefinition
    -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
-> (RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> RemoteRelationships
-> Identity RemoteRelationships
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> RemoteRelationshipG RemoteRelationshipDefinition
-> Identity (RemoteRelationshipG RemoteRelationshipDefinition)
forall def def' (f :: * -> *).
Functor f =>
(def -> f def')
-> RemoteRelationshipG def -> f (RemoteRelationshipG def')
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
Lens
  (RRFormat, ToSchemaRelationshipDef)
  (RRFormat, ToSchemaRelationshipDef)
  ToSchemaRelationshipDef
  ToSchemaRelationshipDef
_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 :: forall (source :: BackendType) (target :: BackendType)
       (m :: * -> *).
(MonadWriter MetadataModifier m, Backend source, Backend target) =>
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
$ 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) (f :: * -> *).
Functor f =>
(RemoteRelationships -> f RemoteRelationships)
-> TableMetadata b -> f (TableMetadata b)
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
    ((RemoteRelationshipG RemoteRelationshipDefinition
  -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
 -> RemoteRelationships -> Identity RemoteRelationships)
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> RemoteRelationshipG RemoteRelationshipDefinition
    -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> RemoteRelationships
-> Identity RemoteRelationships
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> RemoteRelationshipG RemoteRelationshipDefinition
-> Identity (RemoteRelationshipG RemoteRelationshipDefinition)
forall def def' (f :: * -> *).
Functor f =>
(def -> f def')
-> RemoteRelationshipG def -> f (RemoteRelationshipG def')
rrDefinition
    ((RemoteRelationshipDefinition
  -> Identity RemoteRelationshipDefinition)
 -> RemoteRelationshipG RemoteRelationshipDefinition
 -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
-> ((HashMap FieldName FieldName
     -> Identity (HashMap FieldName FieldName))
    -> RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> (HashMap FieldName FieldName
    -> Identity (HashMap FieldName FieldName))
-> RemoteRelationshipG RemoteRelationshipDefinition
-> Identity (RemoteRelationshipG RemoteRelationshipDefinition)
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 = forall (b :: BackendType). Backend b => Column b -> FieldName
fromCol @target Column target
oldCol
    newFieldName :: FieldName
newFieldName = 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 a b. (a -> b) -> HashMap FieldName a -> HashMap FieldName b
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 :: forall (source :: BackendType) (target :: BackendType)
       (m :: * -> *).
(MonadWriter MetadataModifier m, Backend source, Backend target) =>
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
$ 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) (f :: * -> *).
Functor f =>
(RemoteRelationships -> f RemoteRelationships)
-> TableMetadata b -> f (TableMetadata b)
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
    ((RemoteRelationshipG RemoteRelationshipDefinition
  -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
 -> RemoteRelationships -> Identity RemoteRelationships)
-> ((Value -> Identity Value)
    -> RemoteRelationshipG RemoteRelationshipDefinition
    -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
-> (Value -> Identity Value)
-> RemoteRelationships
-> Identity RemoteRelationships
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RemoteRelationshipDefinition
 -> Identity RemoteRelationshipDefinition)
-> RemoteRelationshipG RemoteRelationshipDefinition
-> Identity (RemoteRelationshipG RemoteRelationshipDefinition)
forall def def' (f :: * -> *).
Functor f =>
(def -> f def')
-> RemoteRelationshipG def -> f (RemoteRelationshipG def')
rrDefinition
    ((RemoteRelationshipDefinition
  -> Identity RemoteRelationshipDefinition)
 -> RemoteRelationshipG RemoteRelationshipDefinition
 -> Identity (RemoteRelationshipG RemoteRelationshipDefinition))
-> ((Value -> Identity Value)
    -> RemoteRelationshipDefinition
    -> Identity RemoteRelationshipDefinition)
-> (Value -> Identity Value)
-> RemoteRelationshipG RemoteRelationshipDefinition
-> Identity (RemoteRelationshipG RemoteRelationshipDefinition)
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 :: forall (b :: BackendType).
Backend b =>
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 RelManualTableConfig b
manConfig ->
    RelManualTableConfig b -> ObjRelUsing b
forall (b :: BackendType) a. RelManualTableConfig b -> RelUsing b a
RUManual (RelManualTableConfig b -> ObjRelUsing b)
-> RelManualTableConfig b -> ObjRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b
-> TableName b
-> RenameCol b
-> RelManualTableConfig b
-> RelManualTableConfig b
forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b
-> RenameCol b
-> RelManualTableConfig b
-> RelManualTableConfig b
updateRelManualConfig TableName b
fromQT TableName b
toQT RenameCol b
rnCol RelManualTableConfig b
manConfig

updateRelChoice ::
  (Backend b) =>
  TableName b ->
  TableName b ->
  RenameCol b ->
  ObjRelUsingChoice b ->
  ObjRelUsingChoice b
updateRelChoice :: 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 =
  \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 :: forall (b :: BackendType).
Backend b =>
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 RelManualTableConfig b
manConfig -> RelManualTableConfig b -> ArrRelUsing b
forall (b :: BackendType) a. RelManualTableConfig b -> RelUsing b a
RUManual (RelManualTableConfig b -> ArrRelUsing b)
-> RelManualTableConfig b -> ArrRelUsing b
forall a b. (a -> b) -> a -> b
$ TableName b
-> TableName b
-> RenameCol b
-> RelManualTableConfig b
-> RelManualTableConfig b
forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b
-> RenameCol b
-> RelManualTableConfig b
-> RelManualTableConfig b
updateRelManualConfig TableName b
fromQT TableName b
toQT RenameCol b
rnCol RelManualTableConfig 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 :: forall (b :: BackendType) (f :: * -> *).
(Backend b, Functor f) =>
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 ->
  RelManualTableConfig b ->
  RelManualTableConfig b
updateRelManualConfig :: forall (b :: BackendType).
Backend b =>
TableName b
-> TableName b
-> RenameCol b
-> RelManualTableConfig b
-> RelManualTableConfig b
updateRelManualConfig TableName b
fromQT TableName b
toQT RenameCol b
rnCol (RelManualTableConfig TableName b
tn (RelManualCommon HashMap (Column b) (Column b)
colMap Maybe InsertOrder
io)) =
  TableName b -> RelManualCommon b -> RelManualTableConfig b
forall (b :: BackendType).
TableName b -> RelManualCommon b -> RelManualTableConfig b
RelManualTableConfig TableName b
tn (HashMap (Column b) (Column b)
-> Maybe InsertOrder -> RelManualCommon b
forall (b :: BackendType).
HashMap (Column b) (Column b)
-> Maybe InsertOrder -> RelManualCommon b
RelManualCommon (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)

updateColMap ::
  forall b.
  (Backend b) =>
  TableName b ->
  TableName b ->
  RenameCol b ->
  ColMap b ->
  ColMap b
updateColMap :: 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 =
  [(Column b, Column b)] -> HashMap (Column b) (Column b)
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
HashMap.fromList ([(Column b, Column b)] -> HashMap (Column b) (Column b))
-> (HashMap (Column b) (Column b) -> [(Column b, Column b)])
-> HashMap (Column b) (Column b)
-> HashMap (Column b) (Column 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 b c b' c'. (b -> c) -> (b' -> c') -> (b, b') -> (c, c')
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)])
-> (HashMap (Column b) (Column b) -> [(Column b, Column b)])
-> HashMap (Column b) (Column b)
-> [(Column b, Column b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HashMap (Column b) (Column b) -> [(Column b, Column b)]
forall k v. HashMap k v -> [(k, v)]
HashMap.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 :: forall (b :: BackendType) (m :: * -> *).
(MonadWriter MetadataModifier m, BackendMetadata b) =>
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
$ 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) (f :: * -> *).
Functor f =>
(TableConfig b -> f (TableConfig b))
-> TableMetadata b -> f (TableMetadata 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) (f :: * -> *).
Functor f =>
(HashMap (Column b1) ColumnConfig
 -> f (HashMap (Column b2) ColumnConfig))
-> TableConfig b1 -> f (TableConfig b2)
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 :: forall columnData.
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
HashMap.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)]
HashMap.toList HashMap (Column b) columnData
customColumns