update event triggers on rename operations (#1684)

This commit is contained in:
Vamshi Surabhi 2019-03-01 19:29:24 +05:30 committed by GitHub
parent 377290a058
commit f794653b69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 20 deletions

View File

@ -115,8 +115,8 @@ clearMetadata = Q.catchE defaultTxErrorHandler $ do
Q.unitQ "DELETE FROM hdb_catalog.hdb_function WHERE is_system_defined <> 'true'" () False
Q.unitQ "DELETE FROM hdb_catalog.hdb_permission WHERE is_system_defined <> 'true'" () False
Q.unitQ "DELETE FROM hdb_catalog.hdb_relationship WHERE is_system_defined <> 'true'" () False
Q.unitQ "DELETE FROM hdb_catalog.hdb_table WHERE is_system_defined <> 'true'" () False
Q.unitQ "DELETE FROM hdb_catalog.event_triggers" () False
Q.unitQ "DELETE FROM hdb_catalog.hdb_table WHERE is_system_defined <> 'true'" () False
Q.unitQ "DELETE FROM hdb_catalog.remote_schemas" () False
runClearMetadata

View File

@ -10,6 +10,7 @@ import Hasura.Prelude
import Hasura.RQL.DDL.Permission
import Hasura.RQL.DDL.Permission.Internal
import Hasura.RQL.DDL.Relationship.Types
import qualified Hasura.RQL.DDL.Subscribe as DS
import Hasura.RQL.Types
import Hasura.SQL.Types
@ -56,6 +57,8 @@ renameTableInCatalog newQT oldQT = do
updateRelDefs refQT rn (oldQT, newQT)
-- table names are not specified in permission definitions
SOTableObj _ (TOPerm _ _) -> return ()
-- A trigger's definition is not dependent on the table directly
SOTableObj _ (TOTrigger _) -> return ()
d -> otherDeps errMsg d
-- -- Update table name in hdb_catalog
liftTx $ Q.catchE defaultTxErrorHandler updateTableInCatalog
@ -86,6 +89,8 @@ renameColInCatalog oCol nCol qt ti = do
updatePermFlds refQT role pt renameFld
SOTableObj refQT (TORel rn) ->
updateColInRel refQT rn $ RenameItem qt oCol nCol
SOTableObj _ (TOTrigger triggerName) ->
updateColInEventTriggerDef triggerName $ RenameItem qt oCol nCol
d -> otherDeps errMsg d
where
errMsg = "cannot rename column " <> oCol <<> " to " <>> nCol
@ -313,11 +318,38 @@ updateColInRel fromQT rn rnCol = do
updateColInArrRel fromQT toQT rnCol <$> decodeValue oldDefV
liftTx $ updateRel fromQT rn newDefV
-- rename columns in relationship definitions
updateColInEventTriggerDef
:: (MonadTx m)
=> TriggerName -> RenameCol -> m ()
updateColInEventTriggerDef trigName rnCol = do
(trigTab, trigDef) <- liftTx $ DS.getEventTriggerDef trigName
void $ liftTx $ DS.updateEventTriggerDef trigName $
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)
rewriteTrigOpsDef trigTab (TriggerOpsDef ins upd del) =
TriggerOpsDef
(rewriteOpSpec trigTab <$> ins)
(rewriteOpSpec trigTab <$> upd)
(rewriteOpSpec trigTab <$> del)
rewriteEventTriggerConf trigTab etc =
etc { etcDefinition =
rewriteTrigOpsDef trigTab $ etcDefinition etc
}
updateColInObjRel
:: QualifiedTable -> QualifiedTable
-> RenameCol -> ObjRelUsing -> ObjRelUsing
updateColInObjRel fromQT toQT rnCol = \case
RUFKeyOn col -> RUFKeyOn $ updateColRel fromQT col rnCol
RUFKeyOn col -> RUFKeyOn $ getNewCol rnCol fromQT col
RUManual (ObjRelManualConfig manConfig) ->
RUManual $ ObjRelManualConfig $
updateRelManualConfig fromQT toQT rnCol manConfig
@ -327,7 +359,7 @@ updateColInArrRel
-> RenameCol -> ArrRelUsing -> ArrRelUsing
updateColInArrRel fromQT toQT rnCol = \case
RUFKeyOn (ArrRelUsingFKeyOn t c) ->
let updCol = updateColRel toQT c rnCol
let updCol = getNewCol rnCol toQT c
in RUFKeyOn $ ArrRelUsingFKeyOn t updCol
RUManual (ArrRelManualConfig manConfig) ->
RUManual $ ArrRelManualConfig $
@ -335,9 +367,9 @@ updateColInArrRel fromQT toQT rnCol = \case
type ColMap = Map.Map PGCol PGCol
updateColRel
:: QualifiedTable -> PGCol -> RenameCol -> PGCol
updateColRel qt col rnCol =
getNewCol
:: RenameCol -> QualifiedTable -> PGCol -> PGCol
getNewCol rnCol qt col =
if opQT == qt && col == oCol then nCol else col
where
RenameItem opQT oCol nCol = rnCol
@ -378,4 +410,3 @@ updateRel (QualifiedObject sn tn) rn relDef =
AND table_name = $3
AND rel_name = $4
|] (Q.AltJ relDef, sn , tn, rn) True

View File

@ -11,6 +11,8 @@ module Hasura.RQL.DDL.Subscribe
, subTableP2
, subTableP2Setup
, mkTriggerQ
, getEventTriggerDef
, updateEventTriggerDef
) where
import Data.Aeson
@ -172,22 +174,12 @@ updateEventTriggerToCatalog
-> EventTriggerConf
-> Q.TxE QErr TriggerId
updateEventTriggerToCatalog qt allCols strfyNum etc = do
ids <- map runIdentity <$> Q.listQE defaultTxErrorHandler
[Q.sql|
UPDATE hdb_catalog.event_triggers
SET
configuration = $1
WHERE name = $2
RETURNING id
|] (Q.AltJ $ toJSON etc, name) True
trid <- getTrid ids
trid <- updateEventTriggerDef name etc
delTriggerQ name
mkTriggerQ trid name qt allCols strfyNum opsdef
return trid
where
EventTriggerConf name opsdef _ _ _ _ = etc
getTrid [] = throw500 "could not update event-trigger"
getTrid (x:_) = return x
fetchEvent :: EventId -> Q.TxE QErr (EventId, Bool)
fetchEvent eid = do
@ -364,3 +356,26 @@ getEnv env = do
case mEnv of
Nothing -> throw400 NotFound $ "environment variable '" <> env <> "' not set"
Just envVal -> return (T.pack envVal)
getEventTriggerDef
:: TriggerName
-> Q.TxE QErr (QualifiedTable, EventTriggerConf)
getEventTriggerDef triggerName = do
(sn, tn, Q.AltJ etc) <- Q.getRow <$> Q.withQE defaultTxErrorHandler
[Q.sql|
SELECT e.schema_name, e.table_name, e.configuration::json
FROM hdb_catalog.event_triggers e where e.name = $1
|] (Identity triggerName) False
return (QualifiedObject sn tn, etc)
updateEventTriggerDef
:: TriggerName -> EventTriggerConf -> Q.TxE QErr TriggerId
updateEventTriggerDef trigName trigConf =
runIdentity . Q.getRow <$> Q.withQE defaultTxErrorHandler
[Q.sql|
UPDATE hdb_catalog.event_triggers
SET
configuration = $1
WHERE name = $2
RETURNING id
|] (Q.AltJ $ toJSON trigConf, trigName) True

View File

@ -284,7 +284,9 @@ CREATE TABLE hdb_catalog.event_triggers
schema_name TEXT NOT NULL,
table_name TEXT NOT NULL,
configuration JSON,
comment TEXT
comment TEXT,
FOREIGN KEY (schema_name, table_name)
REFERENCES hdb_catalog.hdb_table(table_schema, table_name) ON UPDATE CASCADE
);
CREATE TABLE hdb_catalog.event_log

View File

@ -2,7 +2,9 @@ ALTER TABLE hdb_catalog.hdb_relationship
DROP CONSTRAINT hdb_relationship_table_schema_fkey,
ADD CONSTRAINT hdb_relationship_table_schema_fkey FOREIGN KEY (table_schema, table_name) REFERENCES hdb_catalog.hdb_table(table_schema, table_name) ON UPDATE CASCADE;
ALTER TABLE hdb_catalog.hdb_permission
DROP CONSTRAINT hdb_permission_table_schema_fkey,
ADD CONSTRAINT hdb_permission_table_schema_fkey FOREIGN KEY (table_schema, table_name) REFERENCES hdb_catalog.hdb_table(table_schema, table_name) ON UPDATE CASCADE;
ALTER TABLE hdb_catalog.event_triggers
ADD CONSTRAINT event_triggers_table_schema_fkey FOREIGN KEY (schema_name, table_name) REFERENCES hdb_catalog.hdb_table(table_schema, table_name) ON UPDATE CASCADE;