2019-03-01 12:17:22 +03:00
|
|
|
module Hasura.RQL.DDL.Relationship.Rename
|
|
|
|
(runRenameRel)
|
|
|
|
where
|
|
|
|
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
|
2020-10-21 19:35:06 +03:00
|
|
|
import Data.Text.Extended
|
2021-05-11 18:18:31 +03:00
|
|
|
|
|
|
|
import Hasura.Base.Error
|
2019-03-18 19:22:21 +03:00
|
|
|
import Hasura.EncJSON
|
2021-02-23 20:37:27 +03:00
|
|
|
import Hasura.RQL.DDL.Schema (renameRelationshipInMetadata)
|
2019-03-01 12:17:22 +03:00
|
|
|
import Hasura.RQL.Types
|
|
|
|
|
|
|
|
|
|
|
|
renameRelP2
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall b m
|
|
|
|
. (QErrM m, CacheRM m, BackendMetadata b)
|
2021-02-23 20:37:27 +03:00
|
|
|
=> SourceName -> TableName b -> RelName -> RelInfo b -> m MetadataModifier
|
2020-12-28 15:56:00 +03:00
|
|
|
renameRelP2 source qt newRN relInfo = withNewInconsistentObjsCheck $ do
|
2021-04-22 00:44:37 +03:00
|
|
|
tabInfo <- askTableCoreInfo @b source qt
|
2019-03-01 12:17:22 +03:00
|
|
|
-- check for conflicts in fieldInfoMap
|
2020-02-04 18:34:17 +03:00
|
|
|
case Map.lookup (fromRel newRN) $ _tciFieldInfoMap tabInfo of
|
2019-03-01 12:17:22 +03:00
|
|
|
Nothing -> return ()
|
|
|
|
Just _ ->
|
|
|
|
throw400 AlreadyExists $ "cannot rename relationship " <> oldRN
|
|
|
|
<<> " to " <> newRN <<> " in table " <> qt <<>
|
|
|
|
" as a column/relationship with the name already exists"
|
2020-12-08 17:22:31 +03:00
|
|
|
-- update metadata
|
2021-04-22 00:44:37 +03:00
|
|
|
execWriterT $ renameRelationshipInMetadata @b source qt oldRN (riType relInfo) newRN
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
oldRN = riName relInfo
|
|
|
|
|
|
|
|
runRenameRel
|
2021-04-22 00:44:37 +03:00
|
|
|
:: forall b m
|
|
|
|
. (MonadError QErr m, CacheRWM m, MetadataM m, BackendMetadata b)
|
2021-02-23 20:37:27 +03:00
|
|
|
=> RenameRel b -> m EncJSON
|
2020-12-28 15:56:00 +03:00
|
|
|
runRenameRel (RenameRel source qt rn newRN) = do
|
2021-04-22 00:44:37 +03:00
|
|
|
tabInfo <- askTableCoreInfo @b source qt
|
2019-11-20 21:21:30 +03:00
|
|
|
ri <- askRelType (_tciFieldInfoMap tabInfo) rn ""
|
2020-12-08 17:22:31 +03:00
|
|
|
withNewInconsistentObjsCheck $
|
2020-12-28 15:56:00 +03:00
|
|
|
renameRelP2 source qt newRN ri >>= buildSchemaCache
|
2019-11-20 21:21:30 +03:00
|
|
|
pure successMsg
|