2019-07-22 15:47:13 +03:00
|
|
|
-- | Functions for mutating the catalog (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 catalog is updated.
|
2019-03-01 12:17:22 +03:00
|
|
|
module Hasura.RQL.DDL.Schema.Rename
|
|
|
|
( renameTableInCatalog
|
|
|
|
, renameColInCatalog
|
|
|
|
, renameRelInCatalog
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2019-09-05 10:34:53 +03:00
|
|
|
import Control.Arrow ((***))
|
|
|
|
import Control.Lens.Combinators
|
|
|
|
import Control.Lens.Operators
|
2019-03-01 12:17:22 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.DDL.Permission
|
|
|
|
import Hasura.RQL.DDL.Permission.Internal
|
|
|
|
import Hasura.RQL.DDL.Relationship.Types
|
2019-10-03 10:45:52 +03:00
|
|
|
import Hasura.RQL.DDL.Schema.Catalog
|
2019-03-01 12:17:22 +03:00
|
|
|
import Hasura.RQL.Types
|
2020-04-24 12:10:53 +03:00
|
|
|
import Hasura.Session
|
2019-03-01 12:17:22 +03:00
|
|
|
import Hasura.SQL.Types
|
|
|
|
|
2020-04-24 12:10:53 +03:00
|
|
|
import qualified Hasura.RQL.DDL.EventTrigger as DS
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
import qualified Data.HashMap.Strict as M
|
|
|
|
import qualified Database.PG.Query as Q
|
|
|
|
|
|
|
|
import Data.Aeson
|
|
|
|
|
|
|
|
data RenameItem a
|
|
|
|
= RenameItem
|
|
|
|
{ _riTable :: !QualifiedTable
|
|
|
|
, _riOld :: !a
|
|
|
|
, _riNew :: !a
|
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
type RenameCol = RenameItem PGCol
|
|
|
|
data RenameField
|
|
|
|
= RFCol !RenameCol
|
|
|
|
| RFRel !(RenameItem RelName)
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
|
|
|
type RenameTable = (QualifiedTable, QualifiedTable)
|
|
|
|
|
2019-09-05 10:34:53 +03:00
|
|
|
data Rename
|
|
|
|
= RTable !RenameTable
|
|
|
|
| RField !RenameField
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
otherDeps :: QErrM m => Text -> SchemaObjId -> m ()
|
2019-07-23 22:11:34 +03:00
|
|
|
otherDeps errMsg d =
|
|
|
|
throw500 $ "unexpected dependancy "
|
|
|
|
<> reportSchemaObj d <> "; " <> errMsg
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
renameTableInCatalog
|
|
|
|
:: (MonadTx m, CacheRM m)
|
|
|
|
=> QualifiedTable -> QualifiedTable -> m ()
|
|
|
|
renameTableInCatalog newQT oldQT = do
|
|
|
|
sc <- askSchemaCache
|
|
|
|
let allDeps = getDependentObjs sc $ SOTable oldQT
|
|
|
|
-- update all dependant schema objects
|
|
|
|
forM_ allDeps $ \case
|
|
|
|
SOTableObj refQT (TORel rn) ->
|
|
|
|
updateRelDefs refQT rn (oldQT, newQT)
|
2019-09-05 10:34:53 +03:00
|
|
|
SOTableObj refQT (TOPerm rn pt) ->
|
|
|
|
updatePermFlds refQT rn pt $ RTable (oldQT, newQT)
|
2019-03-01 16:59:24 +03:00
|
|
|
-- A trigger's definition is not dependent on the table directly
|
|
|
|
SOTableObj _ (TOTrigger _) -> return ()
|
2019-03-01 12:17:22 +03:00
|
|
|
d -> otherDeps errMsg d
|
|
|
|
-- -- Update table name in hdb_catalog
|
|
|
|
liftTx $ Q.catchE defaultTxErrorHandler updateTableInCatalog
|
|
|
|
where
|
|
|
|
QualifiedObject nsn ntn = newQT
|
|
|
|
QualifiedObject osn otn = oldQT
|
|
|
|
errMsg = "cannot rename table " <> oldQT <<> " to " <>> newQT
|
|
|
|
updateTableInCatalog =
|
|
|
|
Q.unitQ [Q.sql|
|
|
|
|
UPDATE "hdb_catalog"."hdb_table"
|
|
|
|
SET table_schema = $1, table_name = $2
|
|
|
|
WHERE table_schema = $3 AND table_name = $4
|
|
|
|
|] (nsn, ntn, osn, otn) False
|
|
|
|
|
|
|
|
renameColInCatalog
|
|
|
|
:: (MonadTx m, CacheRM m)
|
2019-11-20 21:21:30 +03:00
|
|
|
=> PGCol -> PGCol -> QualifiedTable -> FieldInfoMap FieldInfo -> m ()
|
|
|
|
renameColInCatalog oCol nCol qt fieldInfo = do
|
2019-03-01 12:17:22 +03:00
|
|
|
sc <- askSchemaCache
|
|
|
|
-- Check if any relation exists with new column name
|
|
|
|
assertFldNotExists
|
|
|
|
-- Fetch dependent objects
|
|
|
|
let depObjs = getDependentObjs sc $ SOTableObj qt $ TOCol oCol
|
|
|
|
renameFld = RFCol $ RenameItem qt oCol nCol
|
|
|
|
-- Update dependent objects
|
|
|
|
forM_ depObjs $ \case
|
|
|
|
SOTableObj refQT (TOPerm role pt) ->
|
2019-09-05 10:34:53 +03:00
|
|
|
updatePermFlds refQT role pt $ RField renameFld
|
2019-03-01 12:17:22 +03:00
|
|
|
SOTableObj refQT (TORel rn) ->
|
|
|
|
updateColInRel refQT rn $ RenameItem qt oCol nCol
|
2019-03-01 16:59:24 +03:00
|
|
|
SOTableObj _ (TOTrigger triggerName) ->
|
|
|
|
updateColInEventTriggerDef triggerName $ RenameItem qt oCol nCol
|
2019-03-01 12:17:22 +03:00
|
|
|
d -> otherDeps errMsg d
|
2019-10-03 10:45:52 +03:00
|
|
|
-- Update custom column names
|
|
|
|
possiblyUpdateCustomColumnNames qt oCol nCol
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
errMsg = "cannot rename column " <> oCol <<> " to " <>> nCol
|
|
|
|
assertFldNotExists =
|
2019-11-20 21:21:30 +03:00
|
|
|
case M.lookup (fromPGCol oCol) fieldInfo of
|
2019-03-01 12:17:22 +03:00
|
|
|
Just (FIRelationship _) ->
|
|
|
|
throw400 AlreadyExists $ "cannot rename column " <> oCol
|
|
|
|
<<> " to " <> nCol <<> " in table " <> qt <<>
|
|
|
|
" as a relationship with the name already exists"
|
|
|
|
_ -> return ()
|
|
|
|
|
|
|
|
renameRelInCatalog
|
|
|
|
:: (MonadTx m, CacheRM m)
|
|
|
|
=> QualifiedTable -> RelName -> RelName -> m ()
|
|
|
|
renameRelInCatalog qt oldRN newRN = do
|
|
|
|
sc <- askSchemaCache
|
|
|
|
let depObjs = getDependentObjs sc $ SOTableObj qt $ TORel oldRN
|
|
|
|
renameFld = RFRel $ RenameItem qt oldRN newRN
|
|
|
|
|
|
|
|
forM_ depObjs $ \case
|
|
|
|
SOTableObj refQT (TOPerm role pt) ->
|
2019-09-05 10:34:53 +03:00
|
|
|
updatePermFlds refQT role pt $ RField renameFld
|
2019-03-01 12:17:22 +03:00
|
|
|
d -> otherDeps errMsg d
|
|
|
|
liftTx updateRelName
|
|
|
|
where
|
|
|
|
errMsg = "cannot rename relationship " <> oldRN <<> " to " <>> newRN
|
|
|
|
QualifiedObject sn tn = qt
|
|
|
|
updateRelName =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.hdb_relationship
|
|
|
|
SET rel_name = $1
|
|
|
|
WHERE table_schema = $2
|
|
|
|
AND table_name = $3
|
|
|
|
AND rel_name = $4
|
|
|
|
|] (newRN, sn, tn, oldRN) True
|
|
|
|
|
|
|
|
|
|
|
|
-- update table names in relationship definition
|
|
|
|
updateRelDefs
|
|
|
|
:: (MonadTx m, CacheRM m)
|
|
|
|
=> QualifiedTable -> RelName -> RenameTable -> m ()
|
|
|
|
updateRelDefs qt rn renameTable = do
|
|
|
|
fim <- askFieldInfoMap qt
|
|
|
|
ri <- askRelType fim rn ""
|
|
|
|
case riType ri of
|
|
|
|
ObjRel -> updateObjRelDef qt rn renameTable
|
|
|
|
ArrRel -> updateArrRelDef qt rn renameTable
|
|
|
|
|
|
|
|
|
|
|
|
updateObjRelDef
|
|
|
|
:: (MonadTx m)
|
|
|
|
=> QualifiedTable -> RelName -> RenameTable -> m ()
|
|
|
|
updateObjRelDef qt rn (oldQT, newQT) = do
|
|
|
|
oldDefV <- liftTx $ getRelDef qt rn
|
|
|
|
oldDef :: ObjRelUsing <- decodeValue oldDefV
|
|
|
|
let newDef = case oldDef of
|
|
|
|
RUFKeyOn _ -> oldDef
|
2020-01-14 10:09:10 +03:00
|
|
|
RUManual (RelManualConfig dbQT rmCols) ->
|
2019-03-01 12:17:22 +03:00
|
|
|
let updQT = bool oldQT newQT $ oldQT == dbQT
|
2020-01-14 10:09:10 +03:00
|
|
|
in RUManual $ RelManualConfig updQT rmCols
|
2019-03-01 12:17:22 +03:00
|
|
|
liftTx $ updateRel qt rn $ toJSON newDef
|
|
|
|
|
|
|
|
updateArrRelDef
|
|
|
|
:: (MonadTx m)
|
|
|
|
=> QualifiedTable -> RelName -> RenameTable -> m ()
|
|
|
|
updateArrRelDef qt rn (oldQT, newQT) = do
|
|
|
|
oldDefV <- liftTx $ getRelDef qt rn
|
|
|
|
oldDef <- decodeValue oldDefV
|
|
|
|
let newDef = case oldDef of
|
|
|
|
RUFKeyOn (ArrRelUsingFKeyOn dbQT c) ->
|
|
|
|
let updQT = getUpdQT dbQT
|
|
|
|
in RUFKeyOn $ ArrRelUsingFKeyOn updQT c
|
2020-01-14 10:09:10 +03:00
|
|
|
RUManual (RelManualConfig dbQT rmCols) ->
|
2019-03-01 12:17:22 +03:00
|
|
|
let updQT = getUpdQT dbQT
|
2020-01-14 10:09:10 +03:00
|
|
|
in RUManual $ RelManualConfig updQT rmCols
|
2019-03-01 12:17:22 +03:00
|
|
|
liftTx $ updateRel qt rn $ toJSON newDef
|
|
|
|
where
|
|
|
|
getUpdQT dbQT = bool oldQT newQT $ oldQT == dbQT
|
|
|
|
|
|
|
|
-- | update fields in premissions
|
|
|
|
updatePermFlds :: (MonadTx m, CacheRM m)
|
2019-09-05 10:34:53 +03:00
|
|
|
=> QualifiedTable -> RoleName -> PermType -> Rename -> m ()
|
|
|
|
updatePermFlds refQT rn pt rename = do
|
2019-08-17 00:35:22 +03:00
|
|
|
pDef <- fmap fst $ liftTx $ fetchPermDef refQT rn pt
|
2019-03-01 12:17:22 +03:00
|
|
|
case pt of
|
|
|
|
PTInsert -> do
|
|
|
|
perm <- decodeValue pDef
|
2019-09-05 10:34:53 +03:00
|
|
|
updateInsPermFlds refQT rename rn perm
|
2019-03-01 12:17:22 +03:00
|
|
|
PTSelect -> do
|
|
|
|
perm <- decodeValue pDef
|
2019-09-05 10:34:53 +03:00
|
|
|
updateSelPermFlds refQT rename rn perm
|
2019-03-01 12:17:22 +03:00
|
|
|
PTUpdate -> do
|
|
|
|
perm <- decodeValue pDef
|
2019-09-05 10:34:53 +03:00
|
|
|
updateUpdPermFlds refQT rename rn perm
|
2019-03-01 12:17:22 +03:00
|
|
|
PTDelete -> do
|
|
|
|
perm <- decodeValue pDef
|
2019-09-05 10:34:53 +03:00
|
|
|
updateDelPermFlds refQT rename rn perm
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateInsPermFlds
|
|
|
|
:: (MonadTx m, CacheRM m)
|
2019-09-05 10:34:53 +03:00
|
|
|
=> QualifiedTable -> Rename -> RoleName -> InsPerm -> m ()
|
2020-04-24 12:10:53 +03:00
|
|
|
updateInsPermFlds refQT rename rn (InsPerm chk preset cols mBackendOnly) = do
|
2019-09-05 10:34:53 +03:00
|
|
|
updatedPerm <- case rename of
|
|
|
|
RTable rt -> do
|
|
|
|
let updChk = updateTableInBoolExp rt chk
|
2020-04-24 12:10:53 +03:00
|
|
|
return $ InsPerm updChk preset cols mBackendOnly
|
2019-09-05 10:34:53 +03:00
|
|
|
RField rf -> do
|
|
|
|
updChk <- updateFieldInBoolExp refQT rf chk
|
|
|
|
let updPresetM = updatePreset refQT rf <$> preset
|
|
|
|
updColsM = updateCols refQT rf <$> cols
|
2020-04-24 12:10:53 +03:00
|
|
|
return $ InsPerm updChk updPresetM updColsM mBackendOnly
|
2019-09-05 10:34:53 +03:00
|
|
|
liftTx $ updatePermDefInCatalog PTInsert refQT rn updatedPerm
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateSelPermFlds
|
|
|
|
:: (MonadTx m, CacheRM m)
|
2019-09-05 10:34:53 +03:00
|
|
|
=> QualifiedTable -> Rename -> RoleName -> SelPerm -> m ()
|
2019-11-07 17:39:48 +03:00
|
|
|
updateSelPermFlds refQT rename rn (SelPerm cols fltr limit aggAllwd computedFields) = do
|
2019-09-05 10:34:53 +03:00
|
|
|
updatedPerm <- case rename of
|
|
|
|
RTable rt -> do
|
|
|
|
let updFltr = updateTableInBoolExp rt fltr
|
2019-11-07 17:39:48 +03:00
|
|
|
return $ SelPerm cols updFltr limit aggAllwd computedFields
|
2019-09-05 10:34:53 +03:00
|
|
|
RField rf -> do
|
|
|
|
updFltr <- updateFieldInBoolExp refQT rf fltr
|
|
|
|
let updCols = updateCols refQT rf cols
|
2019-11-07 17:39:48 +03:00
|
|
|
return $ SelPerm updCols updFltr limit aggAllwd computedFields
|
2019-09-05 10:34:53 +03:00
|
|
|
liftTx $ updatePermDefInCatalog PTSelect refQT rn updatedPerm
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateUpdPermFlds
|
|
|
|
:: (MonadTx m, CacheRM m)
|
2019-09-05 10:34:53 +03:00
|
|
|
=> QualifiedTable -> Rename -> RoleName -> UpdPerm -> m ()
|
2020-02-13 10:38:49 +03:00
|
|
|
updateUpdPermFlds refQT rename rn (UpdPerm cols preset fltr check) = do
|
2019-09-05 10:34:53 +03:00
|
|
|
updatedPerm <- case rename of
|
|
|
|
RTable rt -> do
|
|
|
|
let updFltr = updateTableInBoolExp rt fltr
|
2020-02-13 10:38:49 +03:00
|
|
|
updCheck = fmap (updateTableInBoolExp rt) check
|
|
|
|
return $ UpdPerm cols preset updFltr updCheck
|
2019-09-05 10:34:53 +03:00
|
|
|
RField rf -> do
|
|
|
|
updFltr <- updateFieldInBoolExp refQT rf fltr
|
2020-02-13 10:38:49 +03:00
|
|
|
updCheck <- traverse (updateFieldInBoolExp refQT rf) check
|
2019-09-05 10:34:53 +03:00
|
|
|
let updCols = updateCols refQT rf cols
|
|
|
|
updPresetM = updatePreset refQT rf <$> preset
|
2020-02-13 10:38:49 +03:00
|
|
|
return $ UpdPerm updCols updPresetM updFltr updCheck
|
2019-09-05 10:34:53 +03:00
|
|
|
liftTx $ updatePermDefInCatalog PTUpdate refQT rn updatedPerm
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateDelPermFlds
|
|
|
|
:: (MonadTx m, CacheRM m)
|
2019-09-05 10:34:53 +03:00
|
|
|
=> QualifiedTable -> Rename -> RoleName -> DelPerm -> m ()
|
|
|
|
updateDelPermFlds refQT rename rn (DelPerm fltr) = do
|
|
|
|
updFltr <- case rename of
|
|
|
|
RTable rt -> return $ updateTableInBoolExp rt fltr
|
|
|
|
RField rf -> updateFieldInBoolExp refQT rf fltr
|
|
|
|
liftTx $ updatePermDefInCatalog PTDelete refQT rn $ DelPerm updFltr
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updatePreset
|
2019-12-11 02:20:55 +03:00
|
|
|
:: QualifiedTable -> RenameField -> (ColumnValues Value) -> (ColumnValues Value)
|
2019-03-01 12:17:22 +03:00
|
|
|
updatePreset qt rf obj =
|
|
|
|
case rf of
|
|
|
|
RFCol (RenameItem opQT oCol nCol) ->
|
|
|
|
if qt == opQT then updatePreset' oCol nCol
|
|
|
|
else obj
|
|
|
|
_ -> obj
|
|
|
|
|
|
|
|
where
|
|
|
|
updatePreset' oCol nCol =
|
|
|
|
M.fromList updItems
|
|
|
|
where
|
|
|
|
updItems= map procObjItem $ M.toList obj
|
|
|
|
procObjItem (pgCol, v) =
|
|
|
|
let isUpdated = pgCol == oCol
|
|
|
|
updCol = bool pgCol nCol isUpdated
|
|
|
|
in (updCol, v)
|
|
|
|
|
|
|
|
updateCols
|
|
|
|
:: QualifiedTable -> RenameField -> PermColSpec -> PermColSpec
|
|
|
|
updateCols qt rf permSpec =
|
|
|
|
case rf of
|
|
|
|
RFCol (RenameItem opQT oCol nCol) ->
|
|
|
|
if qt == opQT then updateCols' oCol nCol permSpec
|
|
|
|
else permSpec
|
|
|
|
_ -> permSpec
|
|
|
|
where
|
|
|
|
updateCols' oCol nCol cols = case cols of
|
|
|
|
PCStar -> cols
|
|
|
|
PCCols c -> PCCols $ flip map c $
|
|
|
|
\col -> if col == oCol then nCol else col
|
|
|
|
|
2019-09-05 10:34:53 +03:00
|
|
|
updateTableInBoolExp :: RenameTable -> BoolExp -> BoolExp
|
|
|
|
updateTableInBoolExp (oldQT, newQT) =
|
|
|
|
over _Wrapped . transform $ (_BoolExists . geTable) %~ \rqfQT ->
|
|
|
|
if rqfQT == oldQT then newQT else rqfQT
|
|
|
|
|
|
|
|
updateFieldInBoolExp
|
2019-03-01 12:17:22 +03:00
|
|
|
:: (QErrM m, CacheRM m)
|
|
|
|
=> QualifiedTable -> RenameField -> BoolExp -> m BoolExp
|
2019-09-05 10:34:53 +03:00
|
|
|
updateFieldInBoolExp qt rf be = BoolExp <$>
|
|
|
|
case unBoolExp be of
|
|
|
|
BoolAnd exps -> BoolAnd <$> procExps exps
|
|
|
|
BoolOr exps -> BoolOr <$> procExps exps
|
|
|
|
BoolNot e -> BoolNot <$> updateBoolExp' e
|
|
|
|
BoolExists (GExists refqt wh) ->
|
|
|
|
(BoolExists . GExists refqt . unBoolExp)
|
|
|
|
<$> updateFieldInBoolExp refqt rf (BoolExp wh)
|
|
|
|
BoolFld fld -> BoolFld <$> updateColExp qt rf fld
|
|
|
|
where
|
|
|
|
procExps = mapM updateBoolExp'
|
|
|
|
updateBoolExp' =
|
|
|
|
fmap unBoolExp . updateFieldInBoolExp qt rf . BoolExp
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateColExp
|
|
|
|
:: (QErrM m, CacheRM m)
|
|
|
|
=> QualifiedTable -> RenameField -> ColExp-> m ColExp
|
|
|
|
updateColExp qt rf (ColExp fld val) =
|
|
|
|
ColExp updatedFld <$> updatedVal
|
|
|
|
where
|
|
|
|
updatedFld = bool fld nFld $ opQT == qt && oFld == fld
|
|
|
|
updatedVal = do
|
|
|
|
fim <- askFieldInfoMap qt
|
|
|
|
fi <- askFieldInfo fim fld
|
|
|
|
case fi of
|
2019-10-18 11:29:47 +03:00
|
|
|
FIColumn _ -> return val
|
|
|
|
FIComputedField _ -> return val
|
|
|
|
FIRelationship ri -> do
|
2019-03-01 12:17:22 +03:00
|
|
|
let remTable = riRTable ri
|
|
|
|
be <- decodeValue val
|
2019-09-05 10:34:53 +03:00
|
|
|
ube <- updateFieldInBoolExp remTable rf be
|
2019-03-01 12:17:22 +03:00
|
|
|
return $ toJSON ube
|
|
|
|
|
|
|
|
(oFld, nFld, opQT) = case rf of
|
|
|
|
RFCol (RenameItem tn oCol nCol) -> (fromPGCol oCol, fromPGCol nCol, tn)
|
|
|
|
RFRel (RenameItem tn oRel nRel) -> (fromRel oRel, fromRel nRel, tn)
|
|
|
|
|
|
|
|
-- rename columns in relationship definitions
|
|
|
|
updateColInRel
|
|
|
|
:: (MonadTx m, CacheRM m)
|
|
|
|
=> QualifiedTable -> RelName -> RenameCol -> m ()
|
|
|
|
updateColInRel fromQT rn rnCol = do
|
|
|
|
fim <- askFieldInfoMap fromQT
|
|
|
|
ri <- askRelType fim rn ""
|
|
|
|
let toQT = riRTable ri
|
|
|
|
oldDefV <- liftTx $ getRelDef fromQT rn
|
|
|
|
newDefV <- case riType ri of
|
|
|
|
ObjRel -> fmap toJSON $
|
|
|
|
updateColInObjRel fromQT toQT rnCol <$> decodeValue oldDefV
|
|
|
|
ArrRel -> fmap toJSON $
|
|
|
|
updateColInArrRel fromQT toQT rnCol <$> decodeValue oldDefV
|
|
|
|
liftTx $ updateRel fromQT rn newDefV
|
|
|
|
|
2019-03-01 16:59:24 +03:00
|
|
|
-- rename columns in relationship definitions
|
|
|
|
updateColInEventTriggerDef
|
|
|
|
:: (MonadTx m)
|
|
|
|
=> TriggerName -> RenameCol -> m ()
|
|
|
|
updateColInEventTriggerDef trigName rnCol = do
|
|
|
|
(trigTab, trigDef) <- liftTx $ DS.getEventTriggerDef trigName
|
2020-02-05 15:54:26 +03:00
|
|
|
void $ liftTx $ DS.updateEventTriggerInCatalog $
|
2019-03-01 16:59:24 +03:00
|
|
|
rewriteEventTriggerConf trigTab trigDef
|
|
|
|
where
|
|
|
|
rewriteSubsCols trigTab = \case
|
|
|
|
SubCStar -> SubCStar
|
|
|
|
SubCArray cols -> SubCArray $
|
|
|
|
map (getNewCol rnCol trigTab) cols
|
|
|
|
rewriteOpSpec trigTab (SubscribeOpSpec cols payload) =
|
|
|
|
SubscribeOpSpec
|
|
|
|
(rewriteSubsCols trigTab cols)
|
|
|
|
(rewriteSubsCols trigTab <$> payload)
|
2019-05-13 12:41:07 +03:00
|
|
|
rewriteTrigOpsDef trigTab (TriggerOpsDef ins upd del man) =
|
2019-03-01 16:59:24 +03:00
|
|
|
TriggerOpsDef
|
|
|
|
(rewriteOpSpec trigTab <$> ins)
|
|
|
|
(rewriteOpSpec trigTab <$> upd)
|
|
|
|
(rewriteOpSpec trigTab <$> del)
|
2019-05-13 12:41:07 +03:00
|
|
|
man
|
2019-03-01 16:59:24 +03:00
|
|
|
rewriteEventTriggerConf trigTab etc =
|
|
|
|
etc { etcDefinition =
|
|
|
|
rewriteTrigOpsDef trigTab $ etcDefinition etc
|
|
|
|
}
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
updateColInObjRel
|
|
|
|
:: QualifiedTable -> QualifiedTable
|
|
|
|
-> RenameCol -> ObjRelUsing -> ObjRelUsing
|
|
|
|
updateColInObjRel fromQT toQT rnCol = \case
|
2019-03-01 16:59:24 +03:00
|
|
|
RUFKeyOn col -> RUFKeyOn $ getNewCol rnCol fromQT col
|
2020-01-14 10:09:10 +03:00
|
|
|
RUManual manConfig -> RUManual $ updateRelManualConfig fromQT toQT rnCol manConfig
|
2019-03-01 12:17:22 +03:00
|
|
|
|
|
|
|
updateColInArrRel
|
|
|
|
:: QualifiedTable -> QualifiedTable
|
|
|
|
-> RenameCol -> ArrRelUsing -> ArrRelUsing
|
|
|
|
updateColInArrRel fromQT toQT rnCol = \case
|
|
|
|
RUFKeyOn (ArrRelUsingFKeyOn t c) ->
|
2019-03-01 16:59:24 +03:00
|
|
|
let updCol = getNewCol rnCol toQT c
|
2019-03-01 12:17:22 +03:00
|
|
|
in RUFKeyOn $ ArrRelUsingFKeyOn t updCol
|
2020-01-14 10:09:10 +03:00
|
|
|
RUManual manConfig -> RUManual $ updateRelManualConfig fromQT toQT rnCol manConfig
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2019-12-13 00:46:33 +03:00
|
|
|
type ColMap = HashMap PGCol PGCol
|
2019-03-01 12:17:22 +03:00
|
|
|
|
2019-03-01 16:59:24 +03:00
|
|
|
getNewCol
|
|
|
|
:: RenameCol -> QualifiedTable -> PGCol -> PGCol
|
|
|
|
getNewCol rnCol qt col =
|
2019-03-01 12:17:22 +03:00
|
|
|
if opQT == qt && col == oCol then nCol else col
|
|
|
|
where
|
|
|
|
RenameItem opQT oCol nCol = rnCol
|
|
|
|
|
|
|
|
updateRelManualConfig
|
|
|
|
:: QualifiedTable -> QualifiedTable
|
|
|
|
-> RenameCol -> RelManualConfig -> RelManualConfig
|
|
|
|
updateRelManualConfig fromQT toQT rnCol manConfig =
|
|
|
|
RelManualConfig tn $ updateColMap fromQT toQT rnCol colMap
|
|
|
|
where
|
|
|
|
RelManualConfig tn colMap = manConfig
|
|
|
|
|
|
|
|
updateColMap
|
|
|
|
:: QualifiedTable -> QualifiedTable
|
|
|
|
-> RenameCol -> ColMap -> ColMap
|
2019-12-13 00:46:33 +03:00
|
|
|
updateColMap fromQT toQT rnCol =
|
|
|
|
M.fromList . map (modCol fromQT *** modCol toQT) . M.toList
|
2019-03-01 12:17:22 +03:00
|
|
|
where
|
|
|
|
RenameItem qt oCol nCol = rnCol
|
|
|
|
modCol colQt col = if colQt == qt && col == oCol then nCol else col
|
|
|
|
|
2019-10-03 10:45:52 +03:00
|
|
|
possiblyUpdateCustomColumnNames
|
|
|
|
:: MonadTx m => QualifiedTable -> PGCol -> PGCol -> m ()
|
|
|
|
possiblyUpdateCustomColumnNames qt oCol nCol = do
|
|
|
|
TableConfig customRootFields customColumns <- getTableConfig qt
|
|
|
|
let updatedCustomColumns =
|
|
|
|
M.fromList $ flip map (M.toList customColumns) $
|
|
|
|
\(dbCol, val) -> (, val) $ if dbCol == oCol then nCol else dbCol
|
|
|
|
when (updatedCustomColumns /= customColumns) $
|
|
|
|
updateTableConfig qt $ TableConfig customRootFields updatedCustomColumns
|
|
|
|
|
2019-03-01 12:17:22 +03:00
|
|
|
-- database functions for relationships
|
|
|
|
getRelDef :: QualifiedTable -> RelName -> Q.TxE QErr Value
|
|
|
|
getRelDef (QualifiedObject sn tn) rn =
|
|
|
|
Q.getAltJ . runIdentity . Q.getRow <$> Q.withQE defaultTxErrorHandler
|
|
|
|
[Q.sql|
|
|
|
|
SELECT rel_def::json FROM hdb_catalog.hdb_relationship
|
|
|
|
WHERE table_schema = $1 AND table_name = $2
|
|
|
|
AND rel_name = $3
|
|
|
|
|] (sn, tn, rn) True
|
|
|
|
|
|
|
|
updateRel :: QualifiedTable -> RelName -> Value -> Q.TxE QErr ()
|
|
|
|
updateRel (QualifiedObject sn tn) rn relDef =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.hdb_relationship
|
|
|
|
SET rel_def = $1 :: jsonb
|
|
|
|
WHERE table_schema = $2
|
|
|
|
AND table_name = $3
|
|
|
|
AND rel_name = $4
|
|
|
|
|] (Q.AltJ relDef, sn , tn, rn) True
|