2018-09-05 14:26:46 +03:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
|
|
|
|
|
|
|
module Hasura.RQL.DDL.Subscribe where
|
|
|
|
|
|
|
|
import Data.Aeson
|
2018-10-09 09:39:20 +03:00
|
|
|
import Data.Int (Int64)
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.Prelude
|
2018-10-09 09:39:20 +03:00
|
|
|
import Hasura.RQL.DML.Internal
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.RQL.Types
|
2018-09-29 08:42:47 +03:00
|
|
|
import Hasura.Server.Utils
|
2018-09-05 14:26:46 +03:00
|
|
|
import Hasura.SQL.Types
|
2018-10-09 09:39:20 +03:00
|
|
|
import System.Environment (lookupEnv)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-10-09 09:39:20 +03:00
|
|
|
import qualified Hasura.SQL.DML as S
|
|
|
|
|
|
|
|
import qualified Data.FileEmbed as FE
|
|
|
|
import qualified Data.HashMap.Strict as HashMap
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import qualified Database.PG.Query as Q
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
|
|
|
|
data OpVar = OLD | NEW deriving (Show)
|
|
|
|
|
2018-09-07 14:51:01 +03:00
|
|
|
defaultNumRetries :: Int
|
2018-09-05 14:26:46 +03:00
|
|
|
defaultNumRetries = 0
|
|
|
|
|
2018-09-07 14:51:01 +03:00
|
|
|
defaultRetryInterval :: Int
|
2018-09-05 14:26:46 +03:00
|
|
|
defaultRetryInterval = 10
|
|
|
|
|
|
|
|
triggerTmplt :: Maybe GingerTmplt
|
|
|
|
triggerTmplt = case parseGingerTmplt $(FE.embedStringFile "src-rsr/trigger.sql.j2") of
|
|
|
|
Left _ -> Nothing
|
|
|
|
Right tmplt -> Just tmplt
|
|
|
|
|
|
|
|
getDropFuncSql :: Ops -> TriggerName -> T.Text
|
|
|
|
getDropFuncSql op trn = "DROP FUNCTION IF EXISTS"
|
|
|
|
<> " hdb_views.notify_hasura_" <> trn <> "_" <> T.pack (show op) <> "()"
|
|
|
|
<> " CASCADE"
|
|
|
|
|
2018-10-09 09:39:20 +03:00
|
|
|
getTriggerSql
|
|
|
|
:: Ops
|
|
|
|
-> TriggerId
|
|
|
|
-> TriggerName
|
|
|
|
-> QualifiedTable
|
|
|
|
-> [PGColInfo]
|
|
|
|
-> Maybe SubscribeOpSpec
|
|
|
|
-> Maybe T.Text
|
|
|
|
getTriggerSql op trid trn qt allCols spec =
|
|
|
|
let globalCtx = HashMap.fromList
|
|
|
|
[ (T.pack "ID", trid)
|
|
|
|
, (T.pack "NAME", trn)
|
|
|
|
, (T.pack "QUALIFIED_TABLE", toSQLTxt qt)
|
|
|
|
]
|
2018-09-05 14:26:46 +03:00
|
|
|
opCtx = maybe HashMap.empty (createOpCtx op) spec
|
|
|
|
context = HashMap.union globalCtx opCtx
|
|
|
|
in
|
2018-10-09 09:39:20 +03:00
|
|
|
spec >> renderGingerTmplt context <$> triggerTmplt
|
2018-09-05 14:26:46 +03:00
|
|
|
where
|
2018-10-25 10:22:51 +03:00
|
|
|
createOpCtx op1 (SubscribeOpSpec columns payload) =
|
2018-10-09 09:39:20 +03:00
|
|
|
HashMap.fromList
|
|
|
|
[ (T.pack "OPERATION", T.pack $ show op1)
|
2018-10-25 10:22:51 +03:00
|
|
|
, (T.pack "OLD_ROW", toSQLTxt $ renderRow OLD columns )
|
|
|
|
, (T.pack "NEW_ROW", toSQLTxt $ renderRow NEW columns )
|
|
|
|
, (T.pack "OLD_PAYLOAD_EXPRESSION", toSQLTxt $ renderOldDataExp op1 $ fromMaybePayload payload )
|
|
|
|
, (T.pack "NEW_PAYLOAD_EXPRESSION", toSQLTxt $ renderNewDataExp op1 $ fromMaybePayload payload )
|
2018-10-09 09:39:20 +03:00
|
|
|
]
|
|
|
|
renderOldDataExp op2 scs =
|
|
|
|
case op2 of
|
|
|
|
INSERT -> S.SEUnsafe "NULL"
|
|
|
|
UPDATE -> getRowExpression OLD scs
|
|
|
|
DELETE -> getRowExpression OLD scs
|
|
|
|
renderNewDataExp op2 scs =
|
|
|
|
case op2 of
|
|
|
|
INSERT -> getRowExpression NEW scs
|
|
|
|
UPDATE -> getRowExpression NEW scs
|
|
|
|
DELETE -> S.SEUnsafe "NULL"
|
|
|
|
getRowExpression opVar scs =
|
|
|
|
case scs of
|
|
|
|
SubCStar -> applyRowToJson $ S.SEUnsafe $ opToTxt opVar
|
|
|
|
SubCArray cols -> applyRowToJson $
|
|
|
|
S.mkRowExp $ map (toExtr . mkQId opVar) $
|
|
|
|
getColInfos cols allCols
|
|
|
|
|
|
|
|
applyRowToJson e = S.SEFnApp "row_to_json" [e] Nothing
|
2018-10-25 10:22:51 +03:00
|
|
|
applyRow e = S.SEFnApp "row" [e] Nothing
|
2018-10-09 09:39:20 +03:00
|
|
|
toExtr = flip S.Extractor Nothing
|
|
|
|
mkQId opVar colInfo = toJSONableExp (pgiType colInfo) $
|
|
|
|
S.SEQIden $ S.QIden (opToQual opVar) $ toIden $ pgiName colInfo
|
|
|
|
|
|
|
|
opToQual = S.QualVar . opToTxt
|
|
|
|
opToTxt = T.pack . show
|
|
|
|
|
2018-10-25 10:22:51 +03:00
|
|
|
renderRow opVar scs =
|
|
|
|
case scs of
|
|
|
|
SubCStar -> applyRow $ S.SEUnsafe $ opToTxt opVar
|
|
|
|
SubCArray cols -> applyRow $
|
|
|
|
S.mkRowExp $ map (toExtr . mkQId opVar) $
|
|
|
|
getColInfos cols allCols
|
|
|
|
|
|
|
|
fromMaybePayload = fromMaybe SubCStar
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
mkTriggerQ
|
|
|
|
:: TriggerId
|
|
|
|
-> TriggerName
|
|
|
|
-> QualifiedTable
|
2018-10-09 09:39:20 +03:00
|
|
|
-> [PGColInfo]
|
2018-09-05 14:26:46 +03:00
|
|
|
-> TriggerOpsDef
|
|
|
|
-> Q.TxE QErr ()
|
2018-10-09 09:39:20 +03:00
|
|
|
mkTriggerQ trid trn qt allCols (TriggerOpsDef insert update delete) = do
|
|
|
|
let msql = getTriggerSql INSERT trid trn qt allCols insert
|
|
|
|
<> getTriggerSql UPDATE trid trn qt allCols update
|
|
|
|
<> getTriggerSql DELETE trid trn qt allCols delete
|
2018-09-05 14:26:46 +03:00
|
|
|
case msql of
|
2018-10-19 05:15:28 +03:00
|
|
|
Just sql -> Q.multiQE defaultTxErrorHandler (Q.fromText sql)
|
2018-10-25 10:22:51 +03:00
|
|
|
Nothing -> throw500 "no trigger sql generated"
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-10-09 09:39:20 +03:00
|
|
|
addEventTriggerToCatalog
|
|
|
|
:: QualifiedTable
|
|
|
|
-> [PGColInfo]
|
|
|
|
-> EventTriggerDef
|
|
|
|
-> Q.TxE QErr TriggerId
|
|
|
|
addEventTriggerToCatalog qt@(QualifiedTable sn tn) allCols (EventTriggerDef name def webhook rconf mheaders) = do
|
2018-09-05 14:26:46 +03:00
|
|
|
ids <- map runIdentity <$> Q.listQE defaultTxErrorHandler [Q.sql|
|
2018-09-24 14:50:11 +03:00
|
|
|
INSERT into hdb_catalog.event_triggers (name, type, schema_name, table_name, definition, webhook, num_retries, retry_interval, headers)
|
|
|
|
VALUES ($1, 'table', $2, $3, $4, $5, $6, $7, $8)
|
2018-09-05 14:26:46 +03:00
|
|
|
RETURNING id
|
2018-09-24 14:50:11 +03:00
|
|
|
|] (name, sn, tn, Q.AltJ $ toJSON def, webhook, toInt64 $ rcNumRetries rconf, toInt64 $ rcIntervalSec rconf, Q.AltJ $ toJSON mheaders) True
|
2018-09-05 14:26:46 +03:00
|
|
|
|
|
|
|
trid <- getTrid ids
|
2018-10-09 09:39:20 +03:00
|
|
|
mkTriggerQ trid name qt allCols def
|
2018-09-05 14:26:46 +03:00
|
|
|
return trid
|
|
|
|
where
|
|
|
|
getTrid [] = throw500 "could not create event-trigger"
|
|
|
|
getTrid (x:_) = return x
|
|
|
|
|
|
|
|
delEventTriggerFromCatalog :: TriggerName -> Q.TxE QErr ()
|
|
|
|
delEventTriggerFromCatalog trn = do
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
DELETE FROM
|
|
|
|
hdb_catalog.event_triggers
|
|
|
|
WHERE name = $1
|
|
|
|
|] (Identity trn) True
|
|
|
|
mapM_ tx [INSERT, UPDATE, DELETE]
|
|
|
|
where
|
|
|
|
tx :: Ops -> Q.TxE QErr ()
|
2018-10-19 05:15:28 +03:00
|
|
|
tx op = Q.multiQE defaultTxErrorHandler (Q.fromText $ getDropFuncSql op trn)
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
updateEventTriggerToCatalog
|
|
|
|
:: QualifiedTable
|
2018-10-09 09:39:20 +03:00
|
|
|
-> [PGColInfo]
|
2018-09-19 15:12:57 +03:00
|
|
|
-> EventTriggerDef
|
|
|
|
-> Q.TxE QErr TriggerId
|
2018-10-09 09:39:20 +03:00
|
|
|
updateEventTriggerToCatalog qt allCols (EventTriggerDef name def webhook rconf mheaders) = do
|
2018-09-19 15:12:57 +03:00
|
|
|
ids <- map runIdentity <$> Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_triggers
|
|
|
|
SET
|
|
|
|
definition = $1,
|
|
|
|
webhook = $2,
|
|
|
|
num_retries = $3,
|
2018-09-24 14:50:11 +03:00
|
|
|
retry_interval = $4,
|
|
|
|
headers = $5
|
|
|
|
WHERE name = $6
|
2018-09-19 15:12:57 +03:00
|
|
|
RETURNING id
|
2018-09-24 14:50:11 +03:00
|
|
|
|] (Q.AltJ $ toJSON def, webhook, toInt64 $ rcNumRetries rconf, toInt64 $ rcIntervalSec rconf, Q.AltJ $ toJSON mheaders, name) True
|
2018-09-19 15:12:57 +03:00
|
|
|
trid <- getTrid ids
|
2018-10-09 09:39:20 +03:00
|
|
|
mkTriggerQ trid name qt allCols def
|
2018-09-19 15:12:57 +03:00
|
|
|
return trid
|
|
|
|
where
|
|
|
|
getTrid [] = throw500 "could not update event-trigger"
|
|
|
|
getTrid (x:_) = return x
|
|
|
|
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
fetchEventTrigger :: TriggerName -> Q.TxE QErr EventTrigger
|
|
|
|
fetchEventTrigger trn = do
|
|
|
|
triggers <- Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
SELECT e.schema_name, e.table_name, e.name, e.definition::json, e.webhook, e.num_retries, e.retry_interval
|
|
|
|
FROM hdb_catalog.event_triggers e
|
|
|
|
WHERE e.name = $1
|
|
|
|
|] (Identity trn) True
|
|
|
|
getTrigger triggers
|
|
|
|
where
|
|
|
|
getTrigger [] = throw400 NotExists ("could not find event trigger '" <> trn <> "'")
|
2018-10-25 10:22:51 +03:00
|
|
|
getTrigger (x:_) = return $ EventTrigger
|
|
|
|
(QualifiedTable sn tn)
|
|
|
|
trn'
|
|
|
|
tDef
|
|
|
|
webhook
|
|
|
|
(RetryConf nr rint)
|
2018-09-05 14:26:46 +03:00
|
|
|
where (sn, tn, trn', Q.AltJ tDef, webhook, nr, rint) = x
|
|
|
|
|
2018-09-07 14:51:01 +03:00
|
|
|
fetchEvent :: EventId -> Q.TxE QErr (EventId, Bool)
|
|
|
|
fetchEvent eid = do
|
|
|
|
events <- Q.listQE defaultTxErrorHandler [Q.sql|
|
|
|
|
SELECT l.id, l.locked
|
|
|
|
FROM hdb_catalog.event_log l
|
|
|
|
JOIN hdb_catalog.event_triggers e
|
|
|
|
ON l.trigger_id = e.id
|
|
|
|
WHERE l.id = $1
|
|
|
|
|] (Identity eid) True
|
|
|
|
event <- getEvent events
|
|
|
|
assertEventUnlocked event
|
|
|
|
return event
|
|
|
|
where
|
|
|
|
getEvent [] = throw400 NotExists "event not found"
|
|
|
|
getEvent (x:_) = return x
|
|
|
|
|
|
|
|
assertEventUnlocked (_, locked) = when locked $
|
|
|
|
throw400 Busy "event is already being processed"
|
|
|
|
|
|
|
|
markForDelivery :: EventId -> Q.TxE QErr ()
|
|
|
|
markForDelivery eid =
|
|
|
|
Q.unitQE defaultTxErrorHandler [Q.sql|
|
|
|
|
UPDATE hdb_catalog.event_log
|
|
|
|
SET
|
|
|
|
delivered = 'f',
|
|
|
|
error = 'f',
|
|
|
|
tries = 0
|
|
|
|
WHERE id = $1
|
|
|
|
|] (Identity eid) True
|
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
subTableP1 :: (P1C m) => CreateEventTriggerQuery -> m (QualifiedTable, Bool, EventTriggerDef)
|
2018-09-24 14:50:11 +03:00
|
|
|
subTableP1 (CreateEventTriggerQuery name qt insert update delete retryConf webhook mheaders replace) = do
|
2018-09-07 14:51:01 +03:00
|
|
|
adminOnly
|
2018-09-05 14:26:46 +03:00
|
|
|
ti <- askTabInfo qt
|
2018-09-19 15:12:57 +03:00
|
|
|
-- can only replace for same table
|
|
|
|
when replace $ do
|
|
|
|
ti' <- askTabInfoFromTrigger name
|
|
|
|
when (tiName ti' /= tiName ti) $ throw400 NotSupported "cannot replace table or schema for trigger"
|
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
assertCols ti insert
|
|
|
|
assertCols ti update
|
|
|
|
assertCols ti delete
|
2018-09-19 15:12:57 +03:00
|
|
|
|
2018-09-05 14:26:46 +03:00
|
|
|
let rconf = fromMaybe (RetryConf defaultNumRetries defaultRetryInterval) retryConf
|
2018-09-24 14:50:11 +03:00
|
|
|
return (qt, replace, EventTriggerDef name (TriggerOpsDef insert update delete) webhook rconf mheaders)
|
2018-09-05 14:26:46 +03:00
|
|
|
where
|
|
|
|
assertCols _ Nothing = return ()
|
|
|
|
assertCols ti (Just sos) = do
|
|
|
|
let cols = sosColumns sos
|
|
|
|
case cols of
|
|
|
|
SubCStar -> return ()
|
|
|
|
SubCArray pgcols -> forM_ pgcols (assertPGCol (tiFieldInfoMap ti) "")
|
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
subTableP2 :: (P2C m) => QualifiedTable -> Bool -> EventTriggerDef -> m ()
|
2018-09-24 14:50:11 +03:00
|
|
|
subTableP2 qt replace q@(EventTriggerDef name def webhook rconf mheaders) = do
|
2018-10-25 10:22:51 +03:00
|
|
|
allCols <- getCols . tiFieldInfoMap <$> askTabInfo qt
|
2018-09-19 15:12:57 +03:00
|
|
|
trid <- if replace
|
|
|
|
then do
|
|
|
|
delEventTriggerFromCache qt name
|
2018-10-09 09:39:20 +03:00
|
|
|
liftTx $ updateEventTriggerToCatalog qt allCols q
|
2018-09-19 15:12:57 +03:00
|
|
|
else
|
2018-10-09 09:39:20 +03:00
|
|
|
liftTx $ addEventTriggerToCatalog qt allCols q
|
2018-09-24 14:50:11 +03:00
|
|
|
let headerConfs = fromMaybe [] mheaders
|
|
|
|
headers <- getHeadersFromConf headerConfs
|
|
|
|
addEventTriggerToCache qt trid name def rconf webhook headers
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
subTableP2shim :: (P2C m) => (QualifiedTable, Bool, EventTriggerDef) -> m RespBody
|
|
|
|
subTableP2shim (qt, replace, etdef) = do
|
|
|
|
subTableP2 qt replace etdef
|
2018-09-05 14:26:46 +03:00
|
|
|
return successMsg
|
|
|
|
|
|
|
|
instance HDBQuery CreateEventTriggerQuery where
|
2018-09-19 15:12:57 +03:00
|
|
|
type Phase1Res CreateEventTriggerQuery = (QualifiedTable, Bool, EventTriggerDef)
|
2018-09-05 14:26:46 +03:00
|
|
|
phaseOne = subTableP1
|
|
|
|
phaseTwo _ = subTableP2shim
|
|
|
|
schemaCachePolicy = SCPReload
|
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
unsubTableP1 :: (P1C m) => DeleteEventTriggerQuery -> m QualifiedTable
|
|
|
|
unsubTableP1 (DeleteEventTriggerQuery name) = do
|
|
|
|
adminOnly
|
|
|
|
ti <- askTabInfoFromTrigger name
|
|
|
|
return $ tiName ti
|
2018-09-05 14:26:46 +03:00
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
unsubTableP2 :: (P2C m) => QualifiedTable -> DeleteEventTriggerQuery -> m RespBody
|
|
|
|
unsubTableP2 qt (DeleteEventTriggerQuery name) = do
|
|
|
|
delEventTriggerFromCache qt name
|
2018-09-05 14:26:46 +03:00
|
|
|
liftTx $ delEventTriggerFromCatalog name
|
|
|
|
return successMsg
|
|
|
|
|
|
|
|
instance HDBQuery DeleteEventTriggerQuery where
|
2018-09-19 15:12:57 +03:00
|
|
|
type Phase1Res DeleteEventTriggerQuery = QualifiedTable
|
2018-09-05 14:26:46 +03:00
|
|
|
phaseOne = unsubTableP1
|
2018-09-19 15:12:57 +03:00
|
|
|
phaseTwo q qt = unsubTableP2 qt q
|
2018-09-05 14:26:46 +03:00
|
|
|
schemaCachePolicy = SCPReload
|
2018-09-07 14:51:01 +03:00
|
|
|
|
|
|
|
deliverEvent :: (P2C m) => DeliverEventQuery -> m RespBody
|
|
|
|
deliverEvent (DeliverEventQuery eventId) = do
|
|
|
|
_ <- liftTx $ fetchEvent eventId
|
|
|
|
liftTx $ markForDelivery eventId
|
|
|
|
return successMsg
|
|
|
|
|
|
|
|
instance HDBQuery DeliverEventQuery where
|
|
|
|
type Phase1Res DeliverEventQuery = ()
|
|
|
|
phaseOne _ = adminOnly
|
|
|
|
phaseTwo q _ = deliverEvent q
|
2018-09-14 15:43:42 +03:00
|
|
|
schemaCachePolicy = SCPNoChange
|
2018-09-19 15:12:57 +03:00
|
|
|
|
2018-09-24 14:50:11 +03:00
|
|
|
getHeadersFromConf :: (P2C m) => [HeaderConf] -> m [(HeaderName, T.Text)]
|
|
|
|
getHeadersFromConf = mapM getHeader
|
|
|
|
where
|
|
|
|
getHeader :: (P2C m) => HeaderConf -> m (HeaderName, T.Text)
|
|
|
|
getHeader hconf = case hconf of
|
|
|
|
(HeaderConf name (HVValue val)) -> return (name, val)
|
|
|
|
(HeaderConf name (HVEnv val)) -> do
|
|
|
|
mEnv <- liftIO $ lookupEnv (T.unpack val)
|
|
|
|
case mEnv of
|
|
|
|
Nothing -> throw400 NotFound $ "environment variable '" <> val <> "' not set"
|
|
|
|
Just val' -> return (name, T.pack val')
|
|
|
|
|
2018-09-19 15:12:57 +03:00
|
|
|
toInt64 :: (Integral a) => a -> Int64
|
|
|
|
toInt64 = fromIntegral
|