{-# LANGUAGE TemplateHaskell #-}

module Hasura.RQL.DDL.EventTrigger
  ( CreateEventTriggerQuery,
    runCreateEventTriggerQuery,
    DeleteEventTriggerQuery,
    runDeleteEventTriggerQuery,
    dropEventTriggerInMetadata,
    RedeliverEventQuery,
    runRedeliverEvent,
    InvokeEventTriggerQuery,
    runInvokeEventTrigger,
    -- TODO(from master): review
    ResolveHeaderError (..),
    getHeaderInfosFromConf,
    getHeaderInfosFromConfEither,
    getWebhookInfoFromConf,
    buildEventTriggerInfo,
    getSourceTableAndTriggers,
    getTriggerNames,
    getTriggersMap,
    getTableNameFromTrigger,
    cetqSource,
    cetqName,
    cetqTable,
    cetqInsert,
    cetqUpdate,
    cetqDelete,
    cetqEnableManual,
    cetqRetryConf,
    cetqWebhook,
    cetqWebhookFromEnv,
    cetqHeaders,
    cetqReplace,
    cetqRequestTransform,
    cetqResponseTrasnform,
    cteqCleanupConfig,
    cteqTriggerOnReplication,
    runCleanupEventTriggerLog,
    runEventTriggerResumeCleanup,
    runEventTriggerPauseCleanup,
    MonadEventLogCleanup (..),
    getAllEventTriggersWithCleanupConfig,
    getAllETWithCleanupConfigInTableMetadata,
    runGetEventLogs,
    runGetEventInvocationLogs,
    runGetEventById,
  )
where

import Control.Lens (ifor_, makeLenses, (.~))
import Data.Aeson
import Data.Either.Combinators
import Data.Environment qualified as Env
import Data.Has (Has)
import Data.HashMap.Strict qualified as HashMap
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.HashSet qualified as Set
import Data.Sequence qualified as Seq
import Data.Text qualified as T
import Data.Text.Extended
import Data.URL.Template (printTemplate, renderTemplate)
import Hasura.Base.Error
import Hasura.EncJSON
import Hasura.Eventing.Backend
import Hasura.Eventing.EventTrigger (logQErr)
import Hasura.Logging qualified as L
import Hasura.Prelude
import Hasura.RQL.DDL.Webhook.Transform (MetadataResponseTransform, RequestTransform)
import Hasura.RQL.Types.Backend
import Hasura.RQL.Types.BackendType
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.EventTrigger
import Hasura.RQL.Types.Eventing
import Hasura.RQL.Types.Headers (HeaderValue (..))
import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.Metadata.Backend
import Hasura.RQL.Types.Metadata.Object
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.SchemaCache.Build
import Hasura.RQL.Types.SchemaCacheTypes
import Hasura.RQL.Types.Source
import Hasura.SQL.AnyBackend qualified as AB
import Hasura.Session
import Hasura.Table.Cache
import Hasura.Table.Metadata (TableMetadata (..), tmEventTriggers)
import Hasura.Tracing (TraceT)
import Hasura.Tracing qualified as Tracing

data CreateEventTriggerQuery (b :: BackendType) = CreateEventTriggerQuery
  { forall (b :: BackendType). CreateEventTriggerQuery b -> SourceName
_cetqSource :: SourceName,
    forall (b :: BackendType). CreateEventTriggerQuery b -> TriggerName
_cetqName :: TriggerName,
    forall (b :: BackendType). CreateEventTriggerQuery b -> TableName b
_cetqTable :: TableName b,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe (SubscribeOpSpec b)
_cetqInsert :: Maybe (SubscribeOpSpec b),
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe (SubscribeOpSpec b)
_cetqUpdate :: Maybe (SubscribeOpSpec b),
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe (SubscribeOpSpec b)
_cetqDelete :: Maybe (SubscribeOpSpec b),
    forall (b :: BackendType). CreateEventTriggerQuery b -> Maybe Bool
_cetqEnableManual :: Maybe Bool,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe RetryConf
_cetqRetryConf :: Maybe RetryConf,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe InputWebhook
_cetqWebhook :: Maybe InputWebhook,
    forall (b :: BackendType). CreateEventTriggerQuery b -> Maybe Text
_cetqWebhookFromEnv :: Maybe Text,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe [HeaderConf]
_cetqHeaders :: Maybe [HeaderConf],
    forall (b :: BackendType). CreateEventTriggerQuery b -> Bool
_cetqReplace :: Bool,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe RequestTransform
_cetqRequestTransform :: Maybe RequestTransform,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe MetadataResponseTransform
_cetqResponseTrasnform :: Maybe MetadataResponseTransform,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe AutoTriggerLogCleanupConfig
_cteqCleanupConfig :: Maybe AutoTriggerLogCleanupConfig,
    forall (b :: BackendType).
CreateEventTriggerQuery b -> TriggerOnReplication
_cteqTriggerOnReplication :: TriggerOnReplication
  }

$(makeLenses ''CreateEventTriggerQuery)

instance (Backend b) => FromJSON (CreateEventTriggerQuery b) where
  parseJSON :: Value -> Parser (CreateEventTriggerQuery b)
parseJSON = String
-> (Object -> Parser (CreateEventTriggerQuery b))
-> Value
-> Parser (CreateEventTriggerQuery b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"CreateEventTriggerQuery" \Object
o -> do
    SourceName
sourceName <- Object
o Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source" Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
    TriggerName
name <- Object
o Object -> Key -> Parser TriggerName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
    TableName b
table <- Object
o Object -> Key -> Parser (TableName b)
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"table"
    Maybe (SubscribeOpSpec b)
insert <- Object
o Object -> Key -> Parser (Maybe (SubscribeOpSpec b))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"insert"
    Maybe (SubscribeOpSpec b)
update <- Object
o Object -> Key -> Parser (Maybe (SubscribeOpSpec b))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"update"
    Maybe (SubscribeOpSpec b)
delete <- Object
o Object -> Key -> Parser (Maybe (SubscribeOpSpec b))
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"delete"
    Bool
enableManual <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"enable_manual" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
    Maybe RetryConf
retryConf <- Object
o Object -> Key -> Parser (Maybe RetryConf)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"retry_conf"
    Maybe InputWebhook
webhook <- Object
o Object -> Key -> Parser (Maybe InputWebhook)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"webhook"
    Maybe Text
webhookFromEnv <- Object
o Object -> Key -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"webhook_from_env"
    Maybe [HeaderConf]
headers <- Object
o Object -> Key -> Parser (Maybe [HeaderConf])
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"headers"
    Bool
replace <- Object
o Object -> Key -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"replace" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
False
    Maybe RequestTransform
requestTransform <- Object
o Object -> Key -> Parser (Maybe RequestTransform)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"request_transform"
    Maybe MetadataResponseTransform
responseTransform <- Object
o Object -> Key -> Parser (Maybe MetadataResponseTransform)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"response_transform"
    Maybe AutoTriggerLogCleanupConfig
cleanupConfig <- Object
o Object -> Key -> Parser (Maybe AutoTriggerLogCleanupConfig)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"cleanup_config"
    Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TriggerName -> Bool
isIllegalTriggerName TriggerName
name)
      (Parser () -> Parser ()) -> Parser () -> Parser ()
forall a b. (a -> b) -> a -> b
$ String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"only alphanumeric and underscore and hyphens allowed for name"
    Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Text -> Int
T.length (TriggerName -> Text
triggerNameToTxt TriggerName
name) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
maxTriggerNameLength)
      (Parser () -> Parser ()) -> Parser () -> Parser ()
forall a b. (a -> b) -> a -> b
$ String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"event trigger name can be at most 42 characters"
    Bool -> Parser () -> Parser ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Maybe (SubscribeOpSpec b) -> Bool)
-> [Maybe (SubscribeOpSpec b)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Maybe (SubscribeOpSpec b) -> Bool
forall a. Maybe a -> Bool
isJust [Maybe (SubscribeOpSpec b)
insert, Maybe (SubscribeOpSpec b)
update, Maybe (SubscribeOpSpec b)
delete] Bool -> Bool -> Bool
|| Bool
enableManual)
      (Parser () -> Parser ()) -> Parser () -> Parser ()
forall a b. (a -> b) -> a -> b
$ String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"atleast one amongst insert/update/delete/enable_manual spec must be provided"
    case (Maybe InputWebhook
webhook, Maybe Text
webhookFromEnv) of
      (Just InputWebhook
_, Maybe Text
Nothing) -> () -> Parser ()
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      (Maybe InputWebhook
Nothing, Just Text
_) -> () -> Parser ()
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      (Just InputWebhook
_, Just Text
_) -> String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"only one of webhook or webhook_from_env should be given"
      (Maybe InputWebhook, Maybe Text)
_ -> String -> Parser ()
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"must provide webhook or webhook_from_env"
    (Maybe (SubscribeOpSpec b) -> Parser ())
-> [Maybe (SubscribeOpSpec b)] -> Parser ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Maybe (SubscribeOpSpec b) -> Parser ()
forall {f :: * -> *} {b :: BackendType}.
MonadFail f =>
Maybe (SubscribeOpSpec b) -> f ()
checkEmptyCols [Maybe (SubscribeOpSpec b)
insert, Maybe (SubscribeOpSpec b)
update, Maybe (SubscribeOpSpec b)
delete]
    TriggerOnReplication
defTOR <- case forall (b :: BackendType).
Backend b =>
Maybe (XEventTriggers b, TriggerOnReplication)
defaultTriggerOnReplication @b of
      Just (XEventTriggers b
_, TriggerOnReplication
dt) -> TriggerOnReplication -> Parser TriggerOnReplication
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure TriggerOnReplication
dt
      Maybe (XEventTriggers b, TriggerOnReplication)
Nothing -> String -> Parser TriggerOnReplication
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"No default setting for trigger_on_replication is defined for backend type."
    TriggerOnReplication
triggerOnReplication <- Object
o Object -> Key -> Parser (Maybe TriggerOnReplication)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"trigger_on_replication" Parser (Maybe TriggerOnReplication)
-> TriggerOnReplication -> Parser TriggerOnReplication
forall a. Parser (Maybe a) -> a -> Parser a
.!= TriggerOnReplication
defTOR
    CreateEventTriggerQuery b -> Parser (CreateEventTriggerQuery b)
forall a. a -> Parser a
forall (m :: * -> *) a. Monad m => a -> m a
return (CreateEventTriggerQuery b -> Parser (CreateEventTriggerQuery b))
-> CreateEventTriggerQuery b -> Parser (CreateEventTriggerQuery b)
forall a b. (a -> b) -> a -> b
$ SourceName
-> TriggerName
-> TableName b
-> Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe Bool
-> Maybe RetryConf
-> Maybe InputWebhook
-> Maybe Text
-> Maybe [HeaderConf]
-> Bool
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> Maybe AutoTriggerLogCleanupConfig
-> TriggerOnReplication
-> CreateEventTriggerQuery b
forall (b :: BackendType).
SourceName
-> TriggerName
-> TableName b
-> Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe Bool
-> Maybe RetryConf
-> Maybe InputWebhook
-> Maybe Text
-> Maybe [HeaderConf]
-> Bool
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> Maybe AutoTriggerLogCleanupConfig
-> TriggerOnReplication
-> CreateEventTriggerQuery b
CreateEventTriggerQuery SourceName
sourceName TriggerName
name TableName b
table Maybe (SubscribeOpSpec b)
insert Maybe (SubscribeOpSpec b)
update Maybe (SubscribeOpSpec b)
delete (Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
enableManual) Maybe RetryConf
retryConf Maybe InputWebhook
webhook Maybe Text
webhookFromEnv Maybe [HeaderConf]
headers Bool
replace Maybe RequestTransform
requestTransform Maybe MetadataResponseTransform
responseTransform Maybe AutoTriggerLogCleanupConfig
cleanupConfig TriggerOnReplication
triggerOnReplication
    where
      checkEmptyCols :: Maybe (SubscribeOpSpec b) -> f ()
checkEmptyCols Maybe (SubscribeOpSpec b)
spec =
        case Maybe (SubscribeOpSpec b)
spec of
          Just (SubscribeOpSpec (SubCArray [Column b]
cols) Maybe (SubscribeColumns b)
_) -> Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Column b] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Column b]
cols) (String -> f ()
forall a. String -> f a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"found empty column specification")
          Just (SubscribeOpSpec SubscribeColumns b
_ (Just (SubCArray [Column b]
cols))) -> Bool -> f () -> f ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when ([Column b] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Column b]
cols) (String -> f ()
forall a. String -> f a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"found empty payload specification")
          Maybe (SubscribeOpSpec b)
_ -> () -> f ()
forall a. a -> f a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

data DeleteEventTriggerQuery (b :: BackendType) = DeleteEventTriggerQuery
  { forall (b :: BackendType). DeleteEventTriggerQuery b -> SourceName
_detqSource :: SourceName,
    forall (b :: BackendType). DeleteEventTriggerQuery b -> TriggerName
_detqName :: TriggerName
  }

instance FromJSON (DeleteEventTriggerQuery b) where
  parseJSON :: Value -> Parser (DeleteEventTriggerQuery b)
parseJSON = String
-> (Object -> Parser (DeleteEventTriggerQuery b))
-> Value
-> Parser (DeleteEventTriggerQuery b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"DeleteEventTriggerQuery" ((Object -> Parser (DeleteEventTriggerQuery b))
 -> Value -> Parser (DeleteEventTriggerQuery b))
-> (Object -> Parser (DeleteEventTriggerQuery b))
-> Value
-> Parser (DeleteEventTriggerQuery b)
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    SourceName -> TriggerName -> DeleteEventTriggerQuery b
forall (b :: BackendType).
SourceName -> TriggerName -> DeleteEventTriggerQuery b
DeleteEventTriggerQuery
      (SourceName -> TriggerName -> DeleteEventTriggerQuery b)
-> Parser SourceName
-> Parser (TriggerName -> DeleteEventTriggerQuery b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o
      Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source"
      Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
      Parser (TriggerName -> DeleteEventTriggerQuery b)
-> Parser TriggerName -> Parser (DeleteEventTriggerQuery b)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser TriggerName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"

data RedeliverEventQuery (b :: BackendType) = RedeliverEventQuery
  { forall (b :: BackendType). RedeliverEventQuery b -> EventId
_rdeqEventId :: EventId,
    forall (b :: BackendType). RedeliverEventQuery b -> SourceName
_rdeqSource :: SourceName
  }

instance FromJSON (RedeliverEventQuery b) where
  parseJSON :: Value -> Parser (RedeliverEventQuery b)
parseJSON = String
-> (Object -> Parser (RedeliverEventQuery b))
-> Value
-> Parser (RedeliverEventQuery b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"RedeliverEventQuery" ((Object -> Parser (RedeliverEventQuery b))
 -> Value -> Parser (RedeliverEventQuery b))
-> (Object -> Parser (RedeliverEventQuery b))
-> Value
-> Parser (RedeliverEventQuery b)
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    EventId -> SourceName -> RedeliverEventQuery b
forall (b :: BackendType).
EventId -> SourceName -> RedeliverEventQuery b
RedeliverEventQuery
      (EventId -> SourceName -> RedeliverEventQuery b)
-> Parser EventId -> Parser (SourceName -> RedeliverEventQuery b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o
      Object -> Key -> Parser EventId
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"event_id"
      Parser (SourceName -> RedeliverEventQuery b)
-> Parser SourceName -> Parser (RedeliverEventQuery b)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source"
      Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource

data InvokeEventTriggerQuery (b :: BackendType) = InvokeEventTriggerQuery
  { forall (b :: BackendType). InvokeEventTriggerQuery b -> TriggerName
_ietqName :: TriggerName,
    forall (b :: BackendType). InvokeEventTriggerQuery b -> SourceName
_ietqSource :: SourceName,
    forall (b :: BackendType). InvokeEventTriggerQuery b -> Value
_ietqPayload :: Value
  }

instance (Backend b) => FromJSON (InvokeEventTriggerQuery b) where
  parseJSON :: Value -> Parser (InvokeEventTriggerQuery b)
parseJSON = String
-> (Object -> Parser (InvokeEventTriggerQuery b))
-> Value
-> Parser (InvokeEventTriggerQuery b)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"InvokeEventTriggerQuery" ((Object -> Parser (InvokeEventTriggerQuery b))
 -> Value -> Parser (InvokeEventTriggerQuery b))
-> (Object -> Parser (InvokeEventTriggerQuery b))
-> Value
-> Parser (InvokeEventTriggerQuery b)
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    TriggerName -> SourceName -> Value -> InvokeEventTriggerQuery b
forall (b :: BackendType).
TriggerName -> SourceName -> Value -> InvokeEventTriggerQuery b
InvokeEventTriggerQuery
      (TriggerName -> SourceName -> Value -> InvokeEventTriggerQuery b)
-> Parser TriggerName
-> Parser (SourceName -> Value -> InvokeEventTriggerQuery b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o
      Object -> Key -> Parser TriggerName
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"name"
      Parser (SourceName -> Value -> InvokeEventTriggerQuery b)
-> Parser SourceName -> Parser (Value -> InvokeEventTriggerQuery b)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser (Maybe SourceName)
forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source"
      Parser (Maybe SourceName) -> SourceName -> Parser SourceName
forall a. Parser (Maybe a) -> a -> Parser a
.!= SourceName
defaultSource
      Parser (Value -> InvokeEventTriggerQuery b)
-> Parser Value -> Parser (InvokeEventTriggerQuery b)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o
      Object -> Key -> Parser Value
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"payload"

-- | This typeclass have the implementation logic for the event trigger log cleanup.
--
-- TODO: this doesn't belong here in the DDL folder, but should be part of
-- Hasura.Eventing. It could even be made a Service, since the whole point of it
-- is to implement features differently between OSS and Pro.
class (Monad m) => MonadEventLogCleanup m where
  -- Deletes the logs of event triggers
  runLogCleaner ::
    SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)

  -- Generates the cleanup schedules for event triggers which have log cleaners installed
  generateCleanupSchedules ::
    AB.AnyBackend SourceInfo -> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())

  -- | `updateTriggerCleanupSchedules` is primarily used to update the
  --    cleanup schedules associated with an event trigger in case the cleanup
  --    config has changed while replacing the metadata.
  --
  --    In case,
  --    i. a source has been dropped -
  --           We don't need to clear the cleanup schedules
  --           because the event log cleanup table is dropped as part
  --           of the post drop source hook.
  --    ii. a table or an event trigger has been dropped/updated -
  --           Older cleanup events will be deleted first and in case of
  --           an update, new cleanup events will be generated and inserted
  --           into the table.
  --    iii. a new event trigger with cleanup config has been added -
  --             Generate the cleanup events and insert it.
  --    iv. a new source has been added -
  --           Generate the cleanup events and insert it.
  --    v. the cron schedule for event trigger cleanup config has changed -
  --           Delete cleanup events with older cron schedule and generate
  --           cleanup events with new cron schedule.
  updateTriggerCleanupSchedules ::
    L.Logger L.Hasura ->
    InsOrdHashMap SourceName BackendSourceMetadata ->
    InsOrdHashMap SourceName BackendSourceMetadata ->
    SchemaCache ->
    m (Either QErr ())

instance (MonadEventLogCleanup m) => MonadEventLogCleanup (ReaderT r m) where
  runLogCleaner :: SourceCache
-> TriggerLogCleanupConfig -> ReaderT r m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf = m (Either QErr EncJSON) -> ReaderT r m (Either QErr EncJSON)
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr EncJSON) -> ReaderT r m (Either QErr EncJSON))
-> m (Either QErr EncJSON) -> ReaderT r m (Either QErr EncJSON)
forall a b. (a -> b) -> a -> b
$ SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
forall (m :: * -> *).
MonadEventLogCleanup m =>
SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf
  generateCleanupSchedules :: AnyBackend SourceInfo
-> TriggerName
-> AutoTriggerLogCleanupConfig
-> ReaderT r m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig = m (Either QErr ()) -> ReaderT r m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> ReaderT r m (Either QErr ()))
-> m (Either QErr ()) -> ReaderT r m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig
  updateTriggerCleanupSchedules :: Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> ReaderT r m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache = m (Either QErr ()) -> ReaderT r m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> ReaderT r m (Either QErr ()))
-> m (Either QErr ()) -> ReaderT r m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache

instance (MonadEventLogCleanup m) => MonadEventLogCleanup (ExceptT e m) where
  runLogCleaner :: SourceCache
-> TriggerLogCleanupConfig -> ExceptT e m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf = m (Either QErr EncJSON) -> ExceptT e m (Either QErr EncJSON)
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr EncJSON) -> ExceptT e m (Either QErr EncJSON))
-> m (Either QErr EncJSON) -> ExceptT e m (Either QErr EncJSON)
forall a b. (a -> b) -> a -> b
$ SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
forall (m :: * -> *).
MonadEventLogCleanup m =>
SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf
  generateCleanupSchedules :: AnyBackend SourceInfo
-> TriggerName
-> AutoTriggerLogCleanupConfig
-> ExceptT e m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig = m (Either QErr ()) -> ExceptT e m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> ExceptT e m (Either QErr ()))
-> m (Either QErr ()) -> ExceptT e m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig
  updateTriggerCleanupSchedules :: Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> ExceptT e m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache = m (Either QErr ()) -> ExceptT e m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> ExceptT e m (Either QErr ()))
-> m (Either QErr ()) -> ExceptT e m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache

instance (MonadEventLogCleanup m) => MonadEventLogCleanup (MetadataT m) where
  runLogCleaner :: SourceCache
-> TriggerLogCleanupConfig -> MetadataT m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf = m (Either QErr EncJSON) -> MetadataT m (Either QErr EncJSON)
forall (m :: * -> *) a. Monad m => m a -> MetadataT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr EncJSON) -> MetadataT m (Either QErr EncJSON))
-> m (Either QErr EncJSON) -> MetadataT m (Either QErr EncJSON)
forall a b. (a -> b) -> a -> b
$ SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
forall (m :: * -> *).
MonadEventLogCleanup m =>
SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf
  generateCleanupSchedules :: AnyBackend SourceInfo
-> TriggerName
-> AutoTriggerLogCleanupConfig
-> MetadataT m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig = m (Either QErr ()) -> MetadataT m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> MetadataT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> MetadataT m (Either QErr ()))
-> m (Either QErr ()) -> MetadataT m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig
  updateTriggerCleanupSchedules :: Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> MetadataT m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache = m (Either QErr ()) -> MetadataT m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> MetadataT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> MetadataT m (Either QErr ()))
-> m (Either QErr ()) -> MetadataT m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache

instance (MonadEventLogCleanup m) => MonadEventLogCleanup (TraceT m) where
  runLogCleaner :: SourceCache
-> TriggerLogCleanupConfig -> TraceT m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf = m (Either QErr EncJSON) -> TraceT m (Either QErr EncJSON)
forall (m :: * -> *) a. Monad m => m a -> TraceT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr EncJSON) -> TraceT m (Either QErr EncJSON))
-> m (Either QErr EncJSON) -> TraceT m (Either QErr EncJSON)
forall a b. (a -> b) -> a -> b
$ SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
forall (m :: * -> *).
MonadEventLogCleanup m =>
SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf
  generateCleanupSchedules :: AnyBackend SourceInfo
-> TriggerName
-> AutoTriggerLogCleanupConfig
-> TraceT m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig = m (Either QErr ()) -> TraceT m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> TraceT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> TraceT m (Either QErr ()))
-> m (Either QErr ()) -> TraceT m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig
  updateTriggerCleanupSchedules :: Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> TraceT m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache = m (Either QErr ()) -> TraceT m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> TraceT m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> TraceT m (Either QErr ()))
-> m (Either QErr ()) -> TraceT m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache

instance (MonadEventLogCleanup m) => MonadEventLogCleanup (StateT w m) where
  runLogCleaner :: SourceCache
-> TriggerLogCleanupConfig -> StateT w m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf = m (Either QErr EncJSON) -> StateT w m (Either QErr EncJSON)
forall (m :: * -> *) a. Monad m => m a -> StateT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr EncJSON) -> StateT w m (Either QErr EncJSON))
-> m (Either QErr EncJSON) -> StateT w m (Either QErr EncJSON)
forall a b. (a -> b) -> a -> b
$ SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
forall (m :: * -> *).
MonadEventLogCleanup m =>
SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf
  generateCleanupSchedules :: AnyBackend SourceInfo
-> TriggerName
-> AutoTriggerLogCleanupConfig
-> StateT w m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig = m (Either QErr ()) -> StateT w m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> StateT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> StateT w m (Either QErr ()))
-> m (Either QErr ()) -> StateT w m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
generateCleanupSchedules AnyBackend SourceInfo
sourceInfo TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig
  updateTriggerCleanupSchedules :: Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> StateT w m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache = m (Either QErr ()) -> StateT w m (Either QErr ())
forall (m :: * -> *) a. Monad m => m a -> StateT w m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Either QErr ()) -> StateT w m (Either QErr ()))
-> m (Either QErr ()) -> StateT w m (Either QErr ())
forall a b. (a -> b) -> a -> b
$ Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
Logger Hasura
-> InsOrdHashMap SourceName BackendSourceMetadata
-> InsOrdHashMap SourceName BackendSourceMetadata
-> SchemaCache
-> m (Either QErr ())
updateTriggerCleanupSchedules Logger Hasura
logger InsOrdHashMap SourceName BackendSourceMetadata
oldSources InsOrdHashMap SourceName BackendSourceMetadata
newSources SchemaCache
schemaCache

resolveEventTriggerQuery ::
  forall b m.
  (Backend b, UserInfoM m, QErrM m, CacheRM m) =>
  CreateEventTriggerQuery b ->
  m (Bool, EventTriggerConf b)
resolveEventTriggerQuery :: forall (b :: BackendType) (m :: * -> *).
(Backend b, UserInfoM m, QErrM m, CacheRM m) =>
CreateEventTriggerQuery b -> m (Bool, EventTriggerConf b)
resolveEventTriggerQuery (CreateEventTriggerQuery SourceName
source TriggerName
name TableName b
qt Maybe (SubscribeOpSpec b)
insert Maybe (SubscribeOpSpec b)
update Maybe (SubscribeOpSpec b)
delete Maybe Bool
enableManual Maybe RetryConf
retryConf Maybe InputWebhook
webhook Maybe Text
webhookFromEnv Maybe [HeaderConf]
mheaders Bool
replace Maybe RequestTransform
reqTransform Maybe MetadataResponseTransform
respTransform Maybe AutoTriggerLogCleanupConfig
cleanupConfig TriggerOnReplication
triggerOnReplication) = do
  TableCoreInfo b
ti <- SourceName -> TableName b -> m (TableCoreInfo b)
forall (b :: BackendType) (m :: * -> *).
(QErrM m, CacheRM m, Backend b) =>
SourceName -> TableName b -> m (TableCoreInfo b)
askTableCoreInfo SourceName
source TableName b
qt
  -- can only replace for same table
  Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
replace (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
    TableCoreInfo b
ti' <- TableInfo b -> TableCoreInfo b
forall (b :: BackendType). TableInfo b -> TableCoreInfo b
_tiCoreInfo (TableInfo b -> TableCoreInfo b)
-> m (TableInfo b) -> m (TableCoreInfo b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRM m) =>
SourceName -> TriggerName -> m (TableInfo b)
askTabInfoFromTrigger @b SourceName
source TriggerName
name
    Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TableCoreInfo b -> TableName b
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> TableName b
_tciName TableCoreInfo b
ti' TableName b -> TableName b -> Bool
forall a. Eq a => a -> a -> Bool
/= TableCoreInfo b -> TableName b
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> TableName b
_tciName TableCoreInfo b
ti) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"cannot replace table or schema for trigger"

  TableCoreInfo b -> Maybe (SubscribeOpSpec b) -> m ()
assertCols TableCoreInfo b
ti Maybe (SubscribeOpSpec b)
insert
  TableCoreInfo b -> Maybe (SubscribeOpSpec b) -> m ()
assertCols TableCoreInfo b
ti Maybe (SubscribeOpSpec b)
update
  TableCoreInfo b -> Maybe (SubscribeOpSpec b) -> m ()
assertCols TableCoreInfo b
ti Maybe (SubscribeOpSpec b)
delete

  let rconf :: RetryConf
rconf = RetryConf -> Maybe RetryConf -> RetryConf
forall a. a -> Maybe a -> a
fromMaybe RetryConf
defaultRetryConf Maybe RetryConf
retryConf
  (Bool, EventTriggerConf b) -> m (Bool, EventTriggerConf b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Bool
replace, TriggerName
-> TriggerOpsDef b
-> Maybe InputWebhook
-> Maybe Text
-> RetryConf
-> Maybe [HeaderConf]
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> Maybe AutoTriggerLogCleanupConfig
-> TriggerOnReplication
-> EventTriggerConf b
forall (b :: BackendType).
TriggerName
-> TriggerOpsDef b
-> Maybe InputWebhook
-> Maybe Text
-> RetryConf
-> Maybe [HeaderConf]
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> Maybe AutoTriggerLogCleanupConfig
-> TriggerOnReplication
-> EventTriggerConf b
EventTriggerConf TriggerName
name (Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe Bool
-> TriggerOpsDef b
forall (b :: BackendType).
Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe (SubscribeOpSpec b)
-> Maybe Bool
-> TriggerOpsDef b
TriggerOpsDef Maybe (SubscribeOpSpec b)
insert Maybe (SubscribeOpSpec b)
update Maybe (SubscribeOpSpec b)
delete Maybe Bool
enableManual) Maybe InputWebhook
webhook Maybe Text
webhookFromEnv RetryConf
rconf Maybe [HeaderConf]
mheaders Maybe RequestTransform
reqTransform Maybe MetadataResponseTransform
respTransform Maybe AutoTriggerLogCleanupConfig
cleanupConfig TriggerOnReplication
triggerOnReplication)
  where
    assertCols :: TableCoreInfo b -> Maybe (SubscribeOpSpec b) -> m ()
    assertCols :: TableCoreInfo b -> Maybe (SubscribeOpSpec b) -> m ()
assertCols TableCoreInfo b
ti Maybe (SubscribeOpSpec b)
opSpec = Maybe (SubscribeOpSpec b) -> (SubscribeOpSpec b -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Maybe (SubscribeOpSpec b)
opSpec \SubscribeOpSpec b
sos -> case SubscribeOpSpec b -> SubscribeColumns b
forall (b :: BackendType). SubscribeOpSpec b -> SubscribeColumns b
sosColumns SubscribeOpSpec b
sos of
      SubscribeColumns b
SubCStar -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      SubCArray [Column b]
columns -> [Column b] -> (Column b -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Column b]
columns (forall (backend :: BackendType) (m :: * -> *).
(MonadError QErr m, Backend backend) =>
FieldInfoMap (FieldInfo backend) -> Text -> Column backend -> m ()
assertColumnExists @b (TableCoreInfo b -> FieldInfoMap (FieldInfo b)
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> FieldInfoMap field
_tciFieldInfoMap TableCoreInfo b
ti) Text
"")

droppedTriggerOps :: TriggerOpsDef b -> TriggerOpsDef b -> HashSet Ops
droppedTriggerOps :: forall (b :: BackendType).
TriggerOpsDef b -> TriggerOpsDef b -> HashSet Ops
droppedTriggerOps TriggerOpsDef b
oldEventTriggerOps TriggerOpsDef b
newEventTriggerOps =
  [Ops] -> HashSet Ops
forall a. (Eq a, Hashable a) => [a] -> HashSet a
Set.fromList
    ([Ops] -> HashSet Ops) -> [Ops] -> HashSet Ops
forall a b. (a -> b) -> a -> b
$ [Maybe Ops] -> [Ops]
forall a. [Maybe a] -> [a]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes
    ([Maybe Ops] -> [Ops]) -> [Maybe Ops] -> [Ops]
forall a b. (a -> b) -> a -> b
$ [ (Maybe Ops -> Maybe Ops -> Bool -> Maybe Ops
forall a. a -> a -> Bool -> a
bool Maybe Ops
forall a. Maybe a
Nothing (Ops -> Maybe Ops
forall a. a -> Maybe a
Just Ops
INSERT) (Maybe (SubscribeOpSpec b) -> Maybe (SubscribeOpSpec b) -> Bool
forall {a} {a}. Maybe a -> Maybe a -> Bool
isDroppedOp (TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
forall (b :: BackendType).
TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
tdInsert TriggerOpsDef b
oldEventTriggerOps) (TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
forall (b :: BackendType).
TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
tdInsert TriggerOpsDef b
newEventTriggerOps))),
        (Maybe Ops -> Maybe Ops -> Bool -> Maybe Ops
forall a. a -> a -> Bool -> a
bool Maybe Ops
forall a. Maybe a
Nothing (Ops -> Maybe Ops
forall a. a -> Maybe a
Just Ops
UPDATE) (Maybe (SubscribeOpSpec b) -> Maybe (SubscribeOpSpec b) -> Bool
forall {a} {a}. Maybe a -> Maybe a -> Bool
isDroppedOp (TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
forall (b :: BackendType).
TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
tdUpdate TriggerOpsDef b
oldEventTriggerOps) (TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
forall (b :: BackendType).
TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
tdUpdate TriggerOpsDef b
newEventTriggerOps))),
        (Maybe Ops -> Maybe Ops -> Bool -> Maybe Ops
forall a. a -> a -> Bool -> a
bool Maybe Ops
forall a. Maybe a
Nothing (Ops -> Maybe Ops
forall a. a -> Maybe a
Just Ops
DELETE) (Maybe (SubscribeOpSpec b) -> Maybe (SubscribeOpSpec b) -> Bool
forall {a} {a}. Maybe a -> Maybe a -> Bool
isDroppedOp (TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
forall (b :: BackendType).
TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
tdDelete TriggerOpsDef b
oldEventTriggerOps) (TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
forall (b :: BackendType).
TriggerOpsDef b -> Maybe (SubscribeOpSpec b)
tdDelete TriggerOpsDef b
newEventTriggerOps)))
      ]
  where
    isDroppedOp :: Maybe a -> Maybe a -> Bool
isDroppedOp Maybe a
old Maybe a
new = Maybe a -> Bool
forall a. Maybe a -> Bool
isJust Maybe a
old Bool -> Bool -> Bool
&& Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing Maybe a
new

createEventTriggerQueryMetadata ::
  forall b m r.
  ( BackendMetadata b,
    QErrM m,
    UserInfoM m,
    CacheRWM m,
    MetadataM m,
    BackendEventTrigger b,
    MonadIO m,
    MonadEventLogCleanup m,
    MonadReader r m,
    Has (L.Logger L.Hasura) r
  ) =>
  CreateEventTriggerQuery b ->
  m ()
createEventTriggerQueryMetadata :: forall (b :: BackendType) (m :: * -> *) r.
(BackendMetadata b, QErrM m, UserInfoM m, CacheRWM m, MetadataM m,
 BackendEventTrigger b, MonadIO m, MonadEventLogCleanup m,
 MonadReader r m, Has (Logger Hasura) r) =>
CreateEventTriggerQuery b -> m ()
createEventTriggerQueryMetadata CreateEventTriggerQuery b
q = do
  (Bool
replace, EventTriggerConf b
triggerConf) <- CreateEventTriggerQuery b -> m (Bool, EventTriggerConf b)
forall (b :: BackendType) (m :: * -> *).
(Backend b, UserInfoM m, QErrM m, CacheRM m) =>
CreateEventTriggerQuery b -> m (Bool, EventTriggerConf b)
resolveEventTriggerQuery CreateEventTriggerQuery b
q
  let table :: TableName b
table = CreateEventTriggerQuery b -> TableName b
forall (b :: BackendType). CreateEventTriggerQuery b -> TableName b
_cetqTable CreateEventTriggerQuery b
q
      source :: SourceName
source = CreateEventTriggerQuery b -> SourceName
forall (b :: BackendType). CreateEventTriggerQuery b -> SourceName
_cetqSource CreateEventTriggerQuery b
q
      triggerName :: TriggerName
triggerName = EventTriggerConf b -> TriggerName
forall (b :: BackendType). EventTriggerConf b -> TriggerName
etcName EventTriggerConf b
triggerConf
      metadataObj :: MetadataObjId
metadataObj =
        SourceName -> AnyBackend SourceMetadataObjId -> MetadataObjId
MOSourceObjId SourceName
source
          (AnyBackend SourceMetadataObjId -> MetadataObjId)
-> AnyBackend SourceMetadataObjId -> MetadataObjId
forall a b. (a -> b) -> a -> b
$ SourceMetadataObjId b -> AnyBackend SourceMetadataObjId
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend
          (SourceMetadataObjId b -> AnyBackend SourceMetadataObjId)
-> SourceMetadataObjId b -> AnyBackend SourceMetadataObjId
forall a b. (a -> b) -> a -> b
$ forall (b :: BackendType).
TableName b -> TableMetadataObjId -> SourceMetadataObjId b
SMOTableObj @b TableName b
table
          (TableMetadataObjId -> SourceMetadataObjId b)
-> TableMetadataObjId -> SourceMetadataObjId b
forall a b. (a -> b) -> a -> b
$ TriggerName -> TableMetadataObjId
MTOTrigger TriggerName
triggerName
  SourceInfo b
sourceInfo <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MetadataM m, MonadError QErr m, Backend b) =>
SourceName -> m (SourceInfo b)
askSourceInfo @b SourceName
source
  let sourceConfig :: SourceConfig b
sourceConfig = (SourceInfo b -> SourceConfig b
forall (b :: BackendType). SourceInfo b -> SourceConfig b
_siConfiguration SourceInfo b
sourceInfo)
      newConfig :: Maybe AutoTriggerLogCleanupConfig
newConfig = CreateEventTriggerQuery b -> Maybe AutoTriggerLogCleanupConfig
forall (b :: BackendType).
CreateEventTriggerQuery b -> Maybe AutoTriggerLogCleanupConfig
_cteqCleanupConfig CreateEventTriggerQuery b
q

  -- Check for existence of a trigger with 'triggerName' only when 'replace' is not set
  if Bool
replace
    then do
      TriggerOpsDef b
existingEventTriggerOps <- EventTriggerInfo b -> TriggerOpsDef b
forall (b :: BackendType). EventTriggerInfo b -> TriggerOpsDef b
etiOpsDef (EventTriggerInfo b -> TriggerOpsDef b)
-> m (EventTriggerInfo b) -> m (TriggerOpsDef b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (b :: BackendType) (m :: * -> *).
(QErrM m, CacheRM m, Backend b) =>
SourceName -> TriggerName -> m (EventTriggerInfo b)
askEventTriggerInfo @b SourceName
source TriggerName
triggerName
      let droppedOps :: HashSet Ops
droppedOps = TriggerOpsDef b -> TriggerOpsDef b -> HashSet Ops
forall (b :: BackendType).
TriggerOpsDef b -> TriggerOpsDef b -> HashSet Ops
droppedTriggerOps TriggerOpsDef b
existingEventTriggerOps (EventTriggerConf b -> TriggerOpsDef b
forall (b :: BackendType). EventTriggerConf b -> TriggerOpsDef b
etcDefinition EventTriggerConf b
triggerConf)
      forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> TriggerName -> TableName b -> HashSet Ops -> m ()
dropDanglingSQLTrigger @b (SourceInfo b -> SourceConfig b
forall (b :: BackendType). SourceInfo b -> SourceConfig b
_siConfiguration SourceInfo b
sourceInfo) TriggerName
triggerName TableName b
table HashSet Ops
droppedOps

      -- check if cron schedule for the cleanup config has changed then delete the scheduled cleanups
      Maybe AutoTriggerLogCleanupConfig
oldConfig <- EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
forall (b :: BackendType).
EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
etiCleanupConfig (EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig)
-> m (EventTriggerInfo b) -> m (Maybe AutoTriggerLogCleanupConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (b :: BackendType) (m :: * -> *).
(QErrM m, CacheRM m, Backend b) =>
SourceName -> TriggerName -> m (EventTriggerInfo b)
askEventTriggerInfo @b SourceName
source TriggerName
triggerName
      Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe AutoTriggerLogCleanupConfig
-> Maybe AutoTriggerLogCleanupConfig -> Bool
hasCleanupCronScheduleUpdated Maybe AutoTriggerLogCleanupConfig
oldConfig Maybe AutoTriggerLogCleanupConfig
newConfig) do
        forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> TriggerName -> m ()
deleteAllScheduledCleanups @b SourceConfig b
sourceConfig TriggerName
triggerName
        Maybe AutoTriggerLogCleanupConfig
-> (AutoTriggerLogCleanupConfig -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Maybe AutoTriggerLogCleanupConfig
newConfig \AutoTriggerLogCleanupConfig
cleanupConfig -> do
          (Either QErr () -> (QErr -> m ()) -> m ()
forall (m :: * -> *) e a.
Applicative m =>
Either e a -> (e -> m a) -> m a
`onLeft` QErr -> m ()
forall r (m :: * -> *).
(MonadReader r m, Has (Logger Hasura) r, MonadIO m) =>
QErr -> m ()
logQErr) (Either QErr () -> m ()) -> m (Either QErr ()) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
generateCleanupSchedules (SourceInfo b -> AnyBackend SourceInfo
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend SourceInfo b
sourceInfo) TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig
    else do
      Bool
doesTriggerExists <- forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> TriggerName -> HashSet Ops -> m Bool
checkIfTriggerExists @b SourceConfig b
sourceConfig TriggerName
triggerName ([Ops] -> HashSet Ops
forall a. (Eq a, Hashable a) => [a] -> HashSet a
Set.fromList [Ops
INSERT, Ops
UPDATE, Ops
DELETE])
      if Bool
doesTriggerExists
        then Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
AlreadyExists (Text
"Event trigger with name " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName -> Text
triggerNameToTxt TriggerName
triggerName Text -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" already exists")
        else Maybe AutoTriggerLogCleanupConfig
-> (AutoTriggerLogCleanupConfig -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ Maybe AutoTriggerLogCleanupConfig
newConfig \AutoTriggerLogCleanupConfig
cleanupConfig -> do
          (Either QErr () -> (QErr -> m ()) -> m ()
forall (m :: * -> *) e a.
Applicative m =>
Either e a -> (e -> m a) -> m a
`onLeft` QErr -> m ()
forall r (m :: * -> *).
(MonadReader r m, Has (Logger Hasura) r, MonadIO m) =>
QErr -> m ()
logQErr) (Either QErr () -> m ()) -> m (Either QErr ()) -> m ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
forall (m :: * -> *).
MonadEventLogCleanup m =>
AnyBackend SourceInfo
-> TriggerName -> AutoTriggerLogCleanupConfig -> m (Either QErr ())
generateCleanupSchedules (SourceInfo b -> AnyBackend SourceInfo
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend SourceInfo b
sourceInfo) TriggerName
triggerName AutoTriggerLogCleanupConfig
cleanupConfig

  MetadataObjId -> MetadataModifier -> m ()
forall (m :: * -> *).
(QErrM m, CacheRWM m, MetadataM m) =>
MetadataObjId -> MetadataModifier -> m ()
buildSchemaCacheFor MetadataObjId
metadataObj
    (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
table
    ASetter' Metadata (TableMetadata b)
-> ((EventTriggers b -> Identity (EventTriggers b))
    -> TableMetadata b -> Identity (TableMetadata b))
-> (EventTriggers b -> Identity (EventTriggers 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))
 -> Metadata -> Identity Metadata)
-> (EventTriggers b -> EventTriggers b) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ if Bool
replace
      then 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))
 -> EventTriggers b -> Identity (EventTriggers b))
-> EventTriggerConf b -> EventTriggers b -> EventTriggers b
forall s t a b. ASetter s t a b -> b -> s -> t
.~ EventTriggerConf b
triggerConf
      else TriggerName
-> EventTriggerConf b -> EventTriggers b -> EventTriggers b
forall k v.
(Eq k, Hashable k) =>
k -> v -> InsOrdHashMap k v -> InsOrdHashMap k v
InsOrdHashMap.insert TriggerName
triggerName EventTriggerConf b
triggerConf

runCreateEventTriggerQuery ::
  forall b m r.
  ( BackendMetadata b,
    BackendEventTrigger b,
    QErrM m,
    UserInfoM m,
    CacheRWM m,
    MetadataM m,
    MonadIO m,
    MonadEventLogCleanup m,
    MonadReader r m,
    Has (L.Logger L.Hasura) r
  ) =>
  CreateEventTriggerQuery b ->
  m EncJSON
runCreateEventTriggerQuery :: forall (b :: BackendType) (m :: * -> *) r.
(BackendMetadata b, BackendEventTrigger b, QErrM m, UserInfoM m,
 CacheRWM m, MetadataM m, MonadIO m, MonadEventLogCleanup m,
 MonadReader r m, Has (Logger Hasura) r) =>
CreateEventTriggerQuery b -> m EncJSON
runCreateEventTriggerQuery CreateEventTriggerQuery b
q = do
  forall (b :: BackendType) (m :: * -> *) r.
(BackendMetadata b, QErrM m, UserInfoM m, CacheRWM m, MetadataM m,
 BackendEventTrigger b, MonadIO m, MonadEventLogCleanup m,
 MonadReader r m, Has (Logger Hasura) r) =>
CreateEventTriggerQuery b -> m ()
createEventTriggerQueryMetadata @b CreateEventTriggerQuery b
q
  EncJSON -> m EncJSON
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure EncJSON
successMsg

runDeleteEventTriggerQuery ::
  forall b m.
  (BackendEventTrigger b, MonadError QErr m, CacheRWM m, MonadIO m, MetadataM m) =>
  DeleteEventTriggerQuery b ->
  m EncJSON
runDeleteEventTriggerQuery :: forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadError QErr m, CacheRWM m, MonadIO m,
 MetadataM m) =>
DeleteEventTriggerQuery b -> m EncJSON
runDeleteEventTriggerQuery (DeleteEventTriggerQuery SourceName
sourceName TriggerName
triggerName) = do
  SourceConfig b
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @b SourceName
sourceName
  TableName b
tableName <- (TableCoreInfoG b (FieldInfo b) (ColumnInfo b) -> TableName b
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> TableName b
_tciName (TableCoreInfoG b (FieldInfo b) (ColumnInfo b) -> TableName b)
-> (TableInfo b -> TableCoreInfoG b (FieldInfo b) (ColumnInfo b))
-> TableInfo b
-> TableName 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) (TableInfo b -> TableName b) -> m (TableInfo b) -> m (TableName b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRM m) =>
SourceName -> TriggerName -> m (TableInfo b)
askTabInfoFromTrigger @b SourceName
sourceName TriggerName
triggerName

  m () -> m ()
forall (m :: * -> *) a. (QErrM m, CacheRM m) => m a -> m a
withNewInconsistentObjsCheck
    (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ MetadataModifier -> m ()
forall (m :: * -> *).
(MetadataM m, CacheRWM m) =>
MetadataModifier -> m ()
buildSchemaCache
    (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
sourceName TableName b
tableName
    ASetter' Metadata (TableMetadata b)
-> (TableMetadata b -> TableMetadata b) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ TriggerName -> TableMetadata b -> TableMetadata b
forall (b :: BackendType).
TriggerName -> TableMetadata b -> TableMetadata b
dropEventTriggerInMetadata TriggerName
triggerName

  forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> TriggerName -> TableName b -> m ()
dropTriggerAndArchiveEvents @b SourceConfig b
sourceConfig TriggerName
triggerName TableName b
tableName

  forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> TriggerName -> m ()
deleteAllScheduledCleanups @b SourceConfig b
sourceConfig TriggerName
triggerName

  EncJSON -> m EncJSON
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure EncJSON
successMsg

runRedeliverEvent ::
  forall b m.
  (BackendEventTrigger b, MonadIO m, CacheRM m, QErrM m, MetadataM m) =>
  RedeliverEventQuery b ->
  m EncJSON
runRedeliverEvent :: forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, CacheRM m, QErrM m,
 MetadataM m) =>
RedeliverEventQuery b -> m EncJSON
runRedeliverEvent (RedeliverEventQuery EventId
eventId SourceName
source) = do
  SourceConfig b
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @b SourceName
source
  forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> EventId -> m ()
redeliverEvent @b SourceConfig b
sourceConfig EventId
eventId
  EncJSON -> m EncJSON
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure EncJSON
successMsg

runInvokeEventTrigger ::
  forall b m.
  ( MonadIO m,
    QErrM m,
    CacheRM m,
    MetadataM m,
    Tracing.MonadTrace m,
    UserInfoM m,
    BackendEventTrigger b
  ) =>
  InvokeEventTriggerQuery b ->
  m EncJSON
runInvokeEventTrigger :: forall (b :: BackendType) (m :: * -> *).
(MonadIO m, QErrM m, CacheRM m, MetadataM m, MonadTrace m,
 UserInfoM m, BackendEventTrigger b) =>
InvokeEventTriggerQuery b -> m EncJSON
runInvokeEventTrigger (InvokeEventTriggerQuery TriggerName
name SourceName
source Value
payload) = do
  EventTriggerInfo b
trigInfo <- forall (b :: BackendType) (m :: * -> *).
(QErrM m, CacheRM m, Backend b) =>
SourceName -> TriggerName -> m (EventTriggerInfo b)
askEventTriggerInfo @b SourceName
source TriggerName
name
  TriggerOpsDef b -> m ()
forall {m :: * -> *} {b :: BackendType}.
MonadError QErr m =>
TriggerOpsDef b -> m ()
assertManual (TriggerOpsDef b -> m ()) -> TriggerOpsDef b -> m ()
forall a b. (a -> b) -> a -> b
$ EventTriggerInfo b -> TriggerOpsDef b
forall (b :: BackendType). EventTriggerInfo b -> TriggerOpsDef b
etiOpsDef EventTriggerInfo b
trigInfo
  TableInfo b
ti <- SourceName -> TriggerName -> m (TableInfo b)
forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRM m) =>
SourceName -> TriggerName -> m (TableInfo b)
askTabInfoFromTrigger SourceName
source TriggerName
name
  SourceConfig b
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @b SourceName
source
  Maybe TraceContext
traceCtx <- m (Maybe TraceContext)
forall (m :: * -> *). MonadTrace m => m (Maybe TraceContext)
Tracing.currentContext
  UserInfo
userInfo <- m UserInfo
forall (m :: * -> *). UserInfoM m => m UserInfo
askUserInfo
  EventId
eid <- forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b
-> TableName b
-> TriggerName
-> Value
-> UserInfo
-> Maybe TraceContext
-> m EventId
insertManualEvent @b SourceConfig b
sourceConfig (forall (b :: BackendType). TableInfo b -> TableName b
tableInfoName @b TableInfo b
ti) TriggerName
name (Value -> Value
forall {v}. ToJSON v => v -> Value
makePayload Value
payload) UserInfo
userInfo Maybe TraceContext
traceCtx
  EncJSON -> m EncJSON
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (EncJSON -> m EncJSON) -> EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ Value -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue (Value -> EncJSON) -> Value -> EncJSON
forall a b. (a -> b) -> a -> b
$ [Pair] -> Value
object [Key
"event_id" Key -> EventId -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
.= EventId
eid]
  where
    makePayload :: v -> Value
makePayload v
o = [Pair] -> Value
object [Key
"old" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
.= Value
Null, Key
"new" Key -> v -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
.= v
o]

    assertManual :: TriggerOpsDef b -> m ()
assertManual (TriggerOpsDef Maybe (SubscribeOpSpec b)
_ Maybe (SubscribeOpSpec b)
_ Maybe (SubscribeOpSpec b)
_ Maybe Bool
man) = case Maybe Bool
man of
      Just Bool
True -> () -> m ()
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
      Maybe Bool
_ -> Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotSupported Text
"manual mode is not enabled for event trigger"

askTabInfoFromTrigger ::
  (Backend b, QErrM m, CacheRM m) =>
  SourceName ->
  TriggerName ->
  m (TableInfo b)
askTabInfoFromTrigger :: forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRM m) =>
SourceName -> TriggerName -> m (TableInfo b)
askTabInfoFromTrigger SourceName
sourceName TriggerName
triggerName = do
  SchemaCache
schemaCache <- m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  SchemaCache -> SourceName -> TriggerName -> Maybe (TableInfo b)
forall (b :: BackendType).
Backend b =>
SchemaCache -> SourceName -> TriggerName -> Maybe (TableInfo b)
getTabInfoFromSchemaCache SchemaCache
schemaCache SourceName
sourceName TriggerName
triggerName
    Maybe (TableInfo b) -> m (TableInfo b) -> m (TableInfo b)
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> m (TableInfo b)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists Text
errMsg
  where
    errMsg :: Text
errMsg = Text
"event trigger " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName
triggerName TriggerName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" does not exist"

getTabInfoFromSchemaCache ::
  (Backend b) =>
  SchemaCache ->
  SourceName ->
  TriggerName ->
  Maybe (TableInfo b)
getTabInfoFromSchemaCache :: forall (b :: BackendType).
Backend b =>
SchemaCache -> SourceName -> TriggerName -> Maybe (TableInfo b)
getTabInfoFromSchemaCache SchemaCache
schemaCache SourceName
sourceName TriggerName
triggerName = do
  HashMap (TableName b) (TableInfo b)
tableCache <- SourceName
-> SourceCache -> Maybe (HashMap (TableName b) (TableInfo b))
forall (b :: BackendType).
Backend b =>
SourceName -> SourceCache -> Maybe (TableCache b)
unsafeTableCache SourceName
sourceName (SourceCache -> Maybe (HashMap (TableName b) (TableInfo b)))
-> SourceCache -> Maybe (HashMap (TableName b) (TableInfo b))
forall a b. (a -> b) -> a -> b
$ SchemaCache -> SourceCache
scSources SchemaCache
schemaCache
  (TableInfo b -> Bool) -> [TableInfo b] -> Maybe (TableInfo b)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (Maybe (EventTriggerInfo b) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (EventTriggerInfo b) -> Bool)
-> (TableInfo b -> Maybe (EventTriggerInfo b))
-> TableInfo b
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TriggerName
-> HashMap TriggerName (EventTriggerInfo b)
-> Maybe (EventTriggerInfo b)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup TriggerName
triggerName (HashMap TriggerName (EventTriggerInfo b)
 -> Maybe (EventTriggerInfo b))
-> (TableInfo b -> HashMap TriggerName (EventTriggerInfo b))
-> TableInfo b
-> Maybe (EventTriggerInfo b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TableInfo b -> HashMap TriggerName (EventTriggerInfo b)
forall (b :: BackendType). TableInfo b -> EventTriggerInfoMap b
_tiEventTriggerInfoMap) (HashMap (TableName b) (TableInfo b) -> [TableInfo b]
forall k v. HashMap k v -> [v]
HashMap.elems HashMap (TableName b) (TableInfo b)
tableCache)

askEventTriggerInfo ::
  forall b m.
  (QErrM m, CacheRM m, Backend b) =>
  SourceName ->
  TriggerName ->
  m (EventTriggerInfo b)
askEventTriggerInfo :: forall (b :: BackendType) (m :: * -> *).
(QErrM m, CacheRM m, Backend b) =>
SourceName -> TriggerName -> m (EventTriggerInfo b)
askEventTriggerInfo SourceName
sourceName TriggerName
triggerName = do
  TableInfo b
triggerInfo <- forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRM m) =>
SourceName -> TriggerName -> m (TableInfo b)
askTabInfoFromTrigger @b SourceName
sourceName TriggerName
triggerName
  let eventTriggerInfoMap :: EventTriggerInfoMap b
eventTriggerInfoMap = TableInfo b -> EventTriggerInfoMap b
forall (b :: BackendType). TableInfo b -> EventTriggerInfoMap b
_tiEventTriggerInfoMap TableInfo b
triggerInfo
  TriggerName -> EventTriggerInfoMap b -> Maybe (EventTriggerInfo b)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup TriggerName
triggerName EventTriggerInfoMap b
eventTriggerInfoMap Maybe (EventTriggerInfo b)
-> m (EventTriggerInfo b) -> m (EventTriggerInfo b)
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> m (EventTriggerInfo b)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists Text
errMsg
  where
    errMsg :: Text
errMsg = Text
"event trigger " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName
triggerName TriggerName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" does not exist"

checkIfTriggerNameExists ::
  forall b m.
  (Backend b, CacheRM m) =>
  SourceName ->
  TriggerName ->
  m (Bool)
checkIfTriggerNameExists :: forall (b :: BackendType) (m :: * -> *).
(Backend b, CacheRM m) =>
SourceName -> TriggerName -> m Bool
checkIfTriggerNameExists SourceName
sourceName TriggerName
triggerName = do
  SchemaCache
schemaCache <- m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  -- TODO: The name getTabInfoFromSchemaCache is misleading here.
  -- There is a JIRA ticket that addresses this (https://hasurahq.atlassian.net/browse/GS-535)
  let tableInfoMaybe :: Maybe (TableInfo b)
tableInfoMaybe = forall (b :: BackendType).
Backend b =>
SchemaCache -> SourceName -> TriggerName -> Maybe (TableInfo b)
getTabInfoFromSchemaCache @b SchemaCache
schemaCache SourceName
sourceName TriggerName
triggerName
  case Maybe (TableInfo b)
tableInfoMaybe of
    Maybe (TableInfo b)
Nothing -> Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
    Maybe (TableInfo b)
_ -> Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True

-- This change helps us create functions for the event triggers
-- without the function name being truncated by PG, since PG allows
-- for only 63 chars for identifiers.
-- Reasoning for the 42 characters:
-- 63 - (notify_hasura_) - (_INSERT | _UPDATE | _DELETE)
maxTriggerNameLength :: Int
maxTriggerNameLength :: Int
maxTriggerNameLength = Int
42

-- Consists of a list of environment variables with invalid/missing values
newtype ResolveHeaderError = ResolveHeaderError {ResolveHeaderError -> [Text]
unResolveHeaderError :: [Text]} deriving (Int -> ResolveHeaderError -> ShowS
[ResolveHeaderError] -> ShowS
ResolveHeaderError -> String
(Int -> ResolveHeaderError -> ShowS)
-> (ResolveHeaderError -> String)
-> ([ResolveHeaderError] -> ShowS)
-> Show ResolveHeaderError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ResolveHeaderError -> ShowS
showsPrec :: Int -> ResolveHeaderError -> ShowS
$cshow :: ResolveHeaderError -> String
show :: ResolveHeaderError -> String
$cshowList :: [ResolveHeaderError] -> ShowS
showList :: [ResolveHeaderError] -> ShowS
Show)

instance ToTxt ResolveHeaderError where
  toTxt :: ResolveHeaderError -> Text
toTxt = [Text] -> Text
forall t (f :: * -> *). (ToTxt t, Foldable f) => f t -> Text
commaSeparated ([Text] -> Text)
-> (ResolveHeaderError -> [Text]) -> ResolveHeaderError -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResolveHeaderError -> [Text]
unResolveHeaderError

getHeaderInfosFromConf ::
  (QErrM m) =>
  Env.Environment ->
  [HeaderConf] ->
  m [EventHeaderInfo]
getHeaderInfosFromConf :: forall (m :: * -> *).
QErrM m =>
Environment -> [HeaderConf] -> m [EventHeaderInfo]
getHeaderInfosFromConf Environment
env = (HeaderConf -> m EventHeaderInfo)
-> [HeaderConf] -> m [EventHeaderInfo]
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 HeaderConf -> m EventHeaderInfo
forall (m :: * -> *). QErrM m => HeaderConf -> m EventHeaderInfo
getHeader
  where
    getHeader :: (QErrM m) => HeaderConf -> m EventHeaderInfo
    getHeader :: forall (m :: * -> *). QErrM m => HeaderConf -> m EventHeaderInfo
getHeader HeaderConf
hconf = case HeaderConf
hconf of
      (HeaderConf Text
_ (HVValue Template
val)) -> case Environment -> Template -> Either Text Text
renderTemplate Environment
env Template
val of
        Left Text
err -> Code -> Text -> m EventHeaderInfo
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotFound (Text -> m EventHeaderInfo) -> Text -> m EventHeaderInfo
forall a b. (a -> b) -> a -> b
$ Text
"template cannot be resolved: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
err
        Right Text
resolvedVal -> EventHeaderInfo -> m EventHeaderInfo
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (EventHeaderInfo -> m EventHeaderInfo)
-> EventHeaderInfo -> m EventHeaderInfo
forall a b. (a -> b) -> a -> b
$ HeaderConf -> Text -> EventHeaderInfo
EventHeaderInfo HeaderConf
hconf Text
resolvedVal
      (HeaderConf Text
_ (HVEnv Text
val)) -> do
        Text
envVal <- Environment -> Text -> m Text
forall (m :: * -> *). QErrM m => Environment -> Text -> m Text
getEnv Environment
env Text
val
        EventHeaderInfo -> m EventHeaderInfo
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (EventHeaderInfo -> m EventHeaderInfo)
-> EventHeaderInfo -> m EventHeaderInfo
forall a b. (a -> b) -> a -> b
$ HeaderConf -> Text -> EventHeaderInfo
EventHeaderInfo HeaderConf
hconf Text
envVal

-- This is similar to `getHeaderInfosFromConf` but it doesn't fail when an env var is invalid
getHeaderInfosFromConfEither ::
  Env.Environment ->
  [HeaderConf] ->
  Either ResolveHeaderError [EventHeaderInfo]
getHeaderInfosFromConfEither :: Environment
-> [HeaderConf] -> Either ResolveHeaderError [EventHeaderInfo]
getHeaderInfosFromConfEither Environment
env [HeaderConf]
hConfList =
  if Bool
isHeaderError
    then ResolveHeaderError -> Either ResolveHeaderError [EventHeaderInfo]
forall a b. a -> Either a b
Left ([Text] -> ResolveHeaderError
ResolveHeaderError ([Text] -> ResolveHeaderError) -> [Text] -> ResolveHeaderError
forall a b. (a -> b) -> a -> b
$ [Either Text EventHeaderInfo] -> [Text]
forall a b. [Either a b] -> [a]
lefts [Either Text EventHeaderInfo]
headerInfoList)
    else [EventHeaderInfo] -> Either ResolveHeaderError [EventHeaderInfo]
forall a b. b -> Either a b
Right ([Either Text EventHeaderInfo] -> [EventHeaderInfo]
forall a b. [Either a b] -> [b]
rights [Either Text EventHeaderInfo]
headerInfoList)
  where
    isHeaderError :: Bool
isHeaderError = (Either Text EventHeaderInfo -> Bool)
-> [Either Text EventHeaderInfo] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any Either Text EventHeaderInfo -> Bool
forall a b. Either a b -> Bool
isLeft [Either Text EventHeaderInfo]
headerInfoList
    headerInfoList :: [Either Text EventHeaderInfo]
headerInfoList = (HeaderConf -> Either Text EventHeaderInfo)
-> [HeaderConf] -> [Either Text EventHeaderInfo]
forall a b. (a -> b) -> [a] -> [b]
map HeaderConf -> Either Text EventHeaderInfo
getHeader [HeaderConf]
hConfList
    getHeader :: HeaderConf -> Either Text EventHeaderInfo
    getHeader :: HeaderConf -> Either Text EventHeaderInfo
getHeader HeaderConf
hconf = case HeaderConf
hconf of
      (HeaderConf Text
_ (HVValue Template
val)) -> case Environment -> Template -> Either Text Text
renderTemplate Environment
env Template
val of
        Left Text
err -> Text -> Either Text EventHeaderInfo
forall a b. a -> Either a b
Left (Text -> Either Text EventHeaderInfo)
-> Text -> Either Text EventHeaderInfo
forall a b. (a -> b) -> a -> b
$ Text
"template cannot be resolved: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
forall a. Show a => a -> Text
tshow Text
err
        Right Text
resolvedVal -> EventHeaderInfo -> Either Text EventHeaderInfo
forall a b. b -> Either a b
Right (EventHeaderInfo -> Either Text EventHeaderInfo)
-> EventHeaderInfo -> Either Text EventHeaderInfo
forall a b. (a -> b) -> a -> b
$ HeaderConf -> Text -> EventHeaderInfo
EventHeaderInfo HeaderConf
hconf Text
resolvedVal
      (HeaderConf Text
_ (HVEnv Text
val)) ->
        (EventHeaderInfo -> Either Text EventHeaderInfo
forall a b. b -> Either a b
Right (EventHeaderInfo -> Either Text EventHeaderInfo)
-> (Text -> EventHeaderInfo) -> Text -> Either Text EventHeaderInfo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HeaderConf -> Text -> EventHeaderInfo
EventHeaderInfo HeaderConf
hconf) (Text -> Either Text EventHeaderInfo)
-> Either Text Text -> Either Text EventHeaderInfo
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Environment -> Text -> Either Text Text
getEnvEither Environment
env Text
val

getWebhookInfoFromConf ::
  (QErrM m) =>
  Env.Environment ->
  WebhookConf ->
  m WebhookConfInfo
getWebhookInfoFromConf :: forall (m :: * -> *).
QErrM m =>
Environment -> WebhookConf -> m WebhookConfInfo
getWebhookInfoFromConf Environment
env WebhookConf
webhookConf = case WebhookConf
webhookConf of
  WCValue InputWebhook
w -> do
    ResolvedWebhook
resolvedWebhook <- Environment -> InputWebhook -> m ResolvedWebhook
forall (m :: * -> *).
QErrM m =>
Environment -> InputWebhook -> m ResolvedWebhook
resolveWebhook Environment
env InputWebhook
w
    let urlTemplate :: Text
urlTemplate = Template -> Text
printTemplate (Template -> Text) -> Template -> Text
forall a b. (a -> b) -> a -> b
$ InputWebhook -> Template
unInputWebhook InputWebhook
w
    -- `urlTemplate` can either be the template value({{TEST}}) or a plain text.
    -- When `urlTemplate` is a template value then '_envVarName' of the 'EnvRecord'
    -- will be the template value i.e '{{TEST}}'
    -- When `urlTemplate` is a plain text then '_envVarName' of the 'EnvRecord'  will be the plain text value.
    WebhookConfInfo -> m WebhookConfInfo
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WebhookConfInfo -> m WebhookConfInfo)
-> WebhookConfInfo -> m WebhookConfInfo
forall a b. (a -> b) -> a -> b
$ WebhookConf -> EnvRecord ResolvedWebhook -> WebhookConfInfo
WebhookConfInfo WebhookConf
webhookConf (Text -> ResolvedWebhook -> EnvRecord ResolvedWebhook
forall a. Text -> a -> EnvRecord a
EnvRecord Text
urlTemplate ResolvedWebhook
resolvedWebhook)
  WCEnv Text
webhookEnvVar -> do
    Text
envVal <- Environment -> Text -> m Text
forall (m :: * -> *). QErrM m => Environment -> Text -> m Text
getEnv Environment
env Text
webhookEnvVar
    WebhookConfInfo -> m WebhookConfInfo
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WebhookConfInfo -> m WebhookConfInfo)
-> WebhookConfInfo -> m WebhookConfInfo
forall a b. (a -> b) -> a -> b
$ WebhookConf -> EnvRecord ResolvedWebhook -> WebhookConfInfo
WebhookConfInfo WebhookConf
webhookConf (Text -> ResolvedWebhook -> EnvRecord ResolvedWebhook
forall a. Text -> a -> EnvRecord a
EnvRecord Text
webhookEnvVar (Text -> ResolvedWebhook
ResolvedWebhook Text
envVal))

buildEventTriggerInfo ::
  forall b m.
  (Backend b, QErrM m) =>
  Env.Environment ->
  SourceName ->
  TableName b ->
  EventTriggerConf b ->
  m (EventTriggerInfo b, Seq SchemaDependency)
buildEventTriggerInfo :: forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m) =>
Environment
-> SourceName
-> TableName b
-> EventTriggerConf b
-> m (EventTriggerInfo b, Seq SchemaDependency)
buildEventTriggerInfo
  Environment
env
  SourceName
source
  TableName b
tableName
  ( EventTriggerConf
      TriggerName
name
      TriggerOpsDef b
def
      Maybe InputWebhook
webhook
      Maybe Text
webhookFromEnv
      RetryConf
rconf
      Maybe [HeaderConf]
mheaders
      Maybe RequestTransform
reqTransform
      Maybe MetadataResponseTransform
respTransform
      Maybe AutoTriggerLogCleanupConfig
cleanupConfig
      TriggerOnReplication
triggerOnReplication
    ) = do
    WebhookConf
webhookConf <- case (Maybe InputWebhook
webhook, Maybe Text
webhookFromEnv) of
      (Just InputWebhook
w, Maybe Text
Nothing) -> WebhookConf -> m WebhookConf
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WebhookConf -> m WebhookConf) -> WebhookConf -> m WebhookConf
forall a b. (a -> b) -> a -> b
$ InputWebhook -> WebhookConf
WCValue InputWebhook
w
      (Maybe InputWebhook
Nothing, Just Text
wEnv) -> WebhookConf -> m WebhookConf
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (WebhookConf -> m WebhookConf) -> WebhookConf -> m WebhookConf
forall a b. (a -> b) -> a -> b
$ Text -> WebhookConf
WCEnv Text
wEnv
      (Maybe InputWebhook, Maybe Text)
_ -> Text -> m WebhookConf
forall (m :: * -> *) a. QErrM m => Text -> m a
throw500 Text
"expected webhook or webhook_from_env"
    let headerConfs :: [HeaderConf]
headerConfs = [HeaderConf] -> Maybe [HeaderConf] -> [HeaderConf]
forall a. a -> Maybe a -> a
fromMaybe [] Maybe [HeaderConf]
mheaders
    WebhookConfInfo
webhookInfo <- Environment -> WebhookConf -> m WebhookConfInfo
forall (m :: * -> *).
QErrM m =>
Environment -> WebhookConf -> m WebhookConfInfo
getWebhookInfoFromConf Environment
env WebhookConf
webhookConf
    [EventHeaderInfo]
headerInfos <- Environment -> [HeaderConf] -> m [EventHeaderInfo]
forall (m :: * -> *).
QErrM m =>
Environment -> [HeaderConf] -> m [EventHeaderInfo]
getHeaderInfosFromConf Environment
env [HeaderConf]
headerConfs
    let eTrigInfo :: EventTriggerInfo b
eTrigInfo =
          TriggerName
-> TriggerOpsDef b
-> RetryConf
-> WebhookConfInfo
-> [EventHeaderInfo]
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> Maybe AutoTriggerLogCleanupConfig
-> TriggerOnReplication
-> EventTriggerInfo b
forall (b :: BackendType).
TriggerName
-> TriggerOpsDef b
-> RetryConf
-> WebhookConfInfo
-> [EventHeaderInfo]
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> Maybe AutoTriggerLogCleanupConfig
-> TriggerOnReplication
-> EventTriggerInfo b
EventTriggerInfo
            TriggerName
name
            TriggerOpsDef b
def
            RetryConf
rconf
            WebhookConfInfo
webhookInfo
            [EventHeaderInfo]
headerInfos
            Maybe RequestTransform
reqTransform
            Maybe MetadataResponseTransform
respTransform
            Maybe AutoTriggerLogCleanupConfig
cleanupConfig
            TriggerOnReplication
triggerOnReplication
        tabDep :: SchemaDependency
tabDep =
          SchemaObjId -> DependencyReason -> SchemaDependency
SchemaDependency
            ( 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
tableName
            )
            DependencyReason
DRParent
    (EventTriggerInfo b, Seq SchemaDependency)
-> m (EventTriggerInfo b, Seq SchemaDependency)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EventTriggerInfo b
eTrigInfo, SchemaDependency
tabDep SchemaDependency -> Seq SchemaDependency -> Seq SchemaDependency
forall a. a -> Seq a -> Seq a
Seq.:<| forall (b :: BackendType).
Backend b =>
SourceName
-> TableName b -> TriggerOpsDef b -> Seq SchemaDependency
getTrigDefDeps @b SourceName
source TableName b
tableName TriggerOpsDef b
def)

getTrigDefDeps ::
  forall b.
  (Backend b) =>
  SourceName ->
  TableName b ->
  TriggerOpsDef b ->
  Seq SchemaDependency
getTrigDefDeps :: forall (b :: BackendType).
Backend b =>
SourceName
-> TableName b -> TriggerOpsDef b -> Seq SchemaDependency
getTrigDefDeps SourceName
source TableName b
tableName (TriggerOpsDef Maybe (SubscribeOpSpec b)
mIns Maybe (SubscribeOpSpec b)
mUpd Maybe (SubscribeOpSpec b)
mDel Maybe Bool
_) =
  [Seq SchemaDependency] -> Seq SchemaDependency
forall a. Monoid a => [a] -> a
mconcat
    ([Seq SchemaDependency] -> Seq SchemaDependency)
-> [Seq SchemaDependency] -> Seq SchemaDependency
forall a b. (a -> b) -> a -> b
$ [SchemaDependency] -> Seq SchemaDependency
forall a. [a] -> Seq a
Seq.fromList
    ([SchemaDependency] -> Seq SchemaDependency)
-> [[SchemaDependency]] -> [Seq SchemaDependency]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Maybe [SchemaDependency]] -> [[SchemaDependency]]
forall a. [Maybe a] -> [a]
forall (f :: * -> *) a. Filterable f => f (Maybe a) -> f a
catMaybes
      [ SubscribeOpSpec b -> [SchemaDependency]
subsOpSpecDeps (SubscribeOpSpec b -> [SchemaDependency])
-> Maybe (SubscribeOpSpec b) -> Maybe [SchemaDependency]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (SubscribeOpSpec b)
mIns,
        SubscribeOpSpec b -> [SchemaDependency]
subsOpSpecDeps (SubscribeOpSpec b -> [SchemaDependency])
-> Maybe (SubscribeOpSpec b) -> Maybe [SchemaDependency]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (SubscribeOpSpec b)
mUpd,
        SubscribeOpSpec b -> [SchemaDependency]
subsOpSpecDeps (SubscribeOpSpec b -> [SchemaDependency])
-> Maybe (SubscribeOpSpec b) -> Maybe [SchemaDependency]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (SubscribeOpSpec b)
mDel
      ]
  where
    subsOpSpecDeps :: SubscribeOpSpec b -> [SchemaDependency]
    subsOpSpecDeps :: SubscribeOpSpec b -> [SchemaDependency]
subsOpSpecDeps SubscribeOpSpec b
os =
      let cols :: [Column b]
cols = SubscribeColumns b -> [Column b]
forall {b :: BackendType}. SubscribeColumns b -> [Column b]
getColsFromSub (SubscribeColumns b -> [Column b])
-> SubscribeColumns b -> [Column b]
forall a b. (a -> b) -> a -> b
$ SubscribeOpSpec b -> SubscribeColumns b
forall (b :: BackendType). SubscribeOpSpec b -> SubscribeColumns b
sosColumns SubscribeOpSpec b
os
          mkColDependency :: DependencyReason -> Column b -> SchemaDependency
mkColDependency DependencyReason
dependencyReason Column b
col =
            SchemaObjId -> DependencyReason -> SchemaDependency
SchemaDependency
              ( 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
tableName (forall (b :: BackendType). Column b -> TableObjId b
TOCol @b Column b
col)
              )
              DependencyReason
dependencyReason
          colDeps :: [SchemaDependency]
colDeps = (Column b -> SchemaDependency) -> [Column b] -> [SchemaDependency]
forall a b. (a -> b) -> [a] -> [b]
map (DependencyReason -> Column b -> SchemaDependency
mkColDependency DependencyReason
DRColumn) [Column b]
cols
          payload :: [Column b]
payload = [Column b]
-> (SubscribeColumns b -> [Column b])
-> Maybe (SubscribeColumns b)
-> [Column b]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] SubscribeColumns b -> [Column b]
forall {b :: BackendType}. SubscribeColumns b -> [Column b]
getColsFromSub (SubscribeOpSpec b -> Maybe (SubscribeColumns b)
forall (b :: BackendType).
SubscribeOpSpec b -> Maybe (SubscribeColumns b)
sosPayload SubscribeOpSpec b
os)
          payloadDeps :: [SchemaDependency]
payloadDeps = (Column b -> SchemaDependency) -> [Column b] -> [SchemaDependency]
forall a b. (a -> b) -> [a] -> [b]
map (DependencyReason -> Column b -> SchemaDependency
mkColDependency DependencyReason
DRPayload) [Column b]
payload
       in [SchemaDependency]
colDeps [SchemaDependency] -> [SchemaDependency] -> [SchemaDependency]
forall a. Semigroup a => a -> a -> a
<> [SchemaDependency]
payloadDeps
    getColsFromSub :: SubscribeColumns b -> [Column b]
getColsFromSub SubscribeColumns b
sc = case SubscribeColumns b
sc of
      SubscribeColumns b
SubCStar -> []
      SubCArray [Column b]
cols -> [Column b]
cols

getTriggersMap ::
  SourceMetadata b ->
  InsOrdHashMap TriggerName (EventTriggerConf b)
getTriggersMap :: forall (b :: BackendType).
SourceMetadata b -> InsOrdHashMap TriggerName (EventTriggerConf b)
getTriggersMap = [InsOrdHashMap TriggerName (EventTriggerConf b)]
-> InsOrdHashMap TriggerName (EventTriggerConf b)
forall k (f :: * -> *) v.
(Eq k, Hashable k, Foldable f) =>
f (InsOrdHashMap k v) -> InsOrdHashMap k v
InsOrdHashMap.unions ([InsOrdHashMap TriggerName (EventTriggerConf b)]
 -> InsOrdHashMap TriggerName (EventTriggerConf b))
-> (SourceMetadata b
    -> [InsOrdHashMap TriggerName (EventTriggerConf b)])
-> SourceMetadata b
-> InsOrdHashMap TriggerName (EventTriggerConf b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TableMetadata b -> InsOrdHashMap TriggerName (EventTriggerConf b))
-> [TableMetadata b]
-> [InsOrdHashMap TriggerName (EventTriggerConf b)]
forall a b. (a -> b) -> [a] -> [b]
map TableMetadata b -> InsOrdHashMap TriggerName (EventTriggerConf b)
forall (b :: BackendType). TableMetadata b -> EventTriggers b
_tmEventTriggers ([TableMetadata b]
 -> [InsOrdHashMap TriggerName (EventTriggerConf b)])
-> (SourceMetadata b -> [TableMetadata b])
-> SourceMetadata b
-> [InsOrdHashMap TriggerName (EventTriggerConf b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InsOrdHashMap (TableName b) (TableMetadata b) -> [TableMetadata b]
forall k v. InsOrdHashMap k v -> [v]
InsOrdHashMap.elems (InsOrdHashMap (TableName b) (TableMetadata b)
 -> [TableMetadata b])
-> (SourceMetadata b
    -> InsOrdHashMap (TableName b) (TableMetadata b))
-> SourceMetadata b
-> [TableMetadata b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceMetadata b -> InsOrdHashMap (TableName b) (TableMetadata b)
forall (b :: BackendType). SourceMetadata b -> Tables b
_smTables

getSourceTableAndTriggers ::
  SourceMetadata b ->
  [(TableName b, TriggerName)]
getSourceTableAndTriggers :: forall (b :: BackendType).
SourceMetadata b -> [(TableName b, TriggerName)]
getSourceTableAndTriggers =
  (((TableName b, TableMetadata b) -> [(TableName b, TriggerName)])
-> [(TableName b, TableMetadata b)] -> [(TableName b, TriggerName)]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (TableName b, TableMetadata b) -> [(TableName b, TriggerName)]
forall {t} {b :: BackendType}.
(t, TableMetadata b) -> [(t, TriggerName)]
mkKeyValue) ([(TableName b, TableMetadata b)] -> [(TableName b, TriggerName)])
-> (SourceMetadata b -> [(TableName b, TableMetadata b)])
-> SourceMetadata b
-> [(TableName b, TriggerName)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InsOrdHashMap (TableName b) (TableMetadata b)
-> [(TableName b, TableMetadata b)]
forall k v. InsOrdHashMap k v -> [(k, v)]
InsOrdHashMap.toList (InsOrdHashMap (TableName b) (TableMetadata b)
 -> [(TableName b, TableMetadata b)])
-> (SourceMetadata b
    -> InsOrdHashMap (TableName b) (TableMetadata b))
-> SourceMetadata b
-> [(TableName b, TableMetadata b)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceMetadata b -> InsOrdHashMap (TableName b) (TableMetadata b)
forall (b :: BackendType). SourceMetadata b -> Tables b
_smTables
  where
    mkKeyValue :: (t, TableMetadata b) -> [(t, TriggerName)]
mkKeyValue (t
tableName, TableMetadata b
tableMetadata) = (TriggerName -> (t, TriggerName))
-> [TriggerName] -> [(t, TriggerName)]
forall a b. (a -> b) -> [a] -> [b]
map (t
tableName,) ([TriggerName] -> [(t, TriggerName)])
-> [TriggerName] -> [(t, TriggerName)]
forall a b. (a -> b) -> a -> b
$ InsOrdHashMap TriggerName (EventTriggerConf b) -> [TriggerName]
forall k v. InsOrdHashMap k v -> [k]
InsOrdHashMap.keys (TableMetadata b -> InsOrdHashMap TriggerName (EventTriggerConf b)
forall (b :: BackendType). TableMetadata b -> EventTriggers b
_tmEventTriggers TableMetadata b
tableMetadata)

getTriggerNames ::
  SourceMetadata b ->
  Set.HashSet TriggerName
getTriggerNames :: forall (b :: BackendType). SourceMetadata b -> HashSet TriggerName
getTriggerNames = [TriggerName] -> HashSet TriggerName
forall a. (Eq a, Hashable a) => [a] -> HashSet a
Set.fromList ([TriggerName] -> HashSet TriggerName)
-> (SourceMetadata b -> [TriggerName])
-> SourceMetadata b
-> HashSet TriggerName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InsOrdHashMap TriggerName (EventTriggerConf b) -> [TriggerName]
forall k v. InsOrdHashMap k v -> [k]
InsOrdHashMap.keys (InsOrdHashMap TriggerName (EventTriggerConf b) -> [TriggerName])
-> (SourceMetadata b
    -> InsOrdHashMap TriggerName (EventTriggerConf b))
-> SourceMetadata b
-> [TriggerName]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SourceMetadata b -> InsOrdHashMap TriggerName (EventTriggerConf b)
forall (b :: BackendType).
SourceMetadata b -> InsOrdHashMap TriggerName (EventTriggerConf b)
getTriggersMap

getTableNameFromTrigger ::
  forall b.
  (Backend b) =>
  SchemaCache ->
  SourceName ->
  TriggerName ->
  Maybe (TableName b)
getTableNameFromTrigger :: forall (b :: BackendType).
Backend b =>
SchemaCache -> SourceName -> TriggerName -> Maybe (TableName b)
getTableNameFromTrigger SchemaCache
schemaCache SourceName
sourceName TriggerName
triggerName = do
  TableInfo b
tableInfo <- forall (b :: BackendType).
Backend b =>
SchemaCache -> SourceName -> TriggerName -> Maybe (TableInfo b)
getTabInfoFromSchemaCache @b SchemaCache
schemaCache SourceName
sourceName TriggerName
triggerName
  TableName b -> Maybe (TableName b)
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (TableName b -> Maybe (TableName b))
-> TableName b -> Maybe (TableName b)
forall a b. (a -> b) -> a -> b
$ TableCoreInfoG b (FieldInfo b) (ColumnInfo b) -> TableName b
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> TableName b
_tciName (TableCoreInfoG b (FieldInfo b) (ColumnInfo b) -> TableName b)
-> TableCoreInfoG b (FieldInfo b) (ColumnInfo b) -> TableName b
forall a b. (a -> b) -> a -> b
$ TableInfo b -> TableCoreInfoG b (FieldInfo b) (ColumnInfo b)
forall (b :: BackendType). TableInfo b -> TableCoreInfo b
_tiCoreInfo TableInfo b
tableInfo

runCleanupEventTriggerLog ::
  (MonadEventLogCleanup m, MonadError QErr m, CacheRWM m) =>
  TriggerLogCleanupConfig ->
  m EncJSON
runCleanupEventTriggerLog :: forall (m :: * -> *).
(MonadEventLogCleanup m, MonadError QErr m, CacheRWM m) =>
TriggerLogCleanupConfig -> m EncJSON
runCleanupEventTriggerLog TriggerLogCleanupConfig
conf = do
  SourceCache
sourceCache <- SchemaCache -> SourceCache
scSources (SchemaCache -> SourceCache) -> m SchemaCache -> m SourceCache
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
forall (m :: * -> *).
MonadEventLogCleanup m =>
SourceCache -> TriggerLogCleanupConfig -> m (Either QErr EncJSON)
runLogCleaner SourceCache
sourceCache TriggerLogCleanupConfig
conf m (Either QErr EncJSON) -> (QErr -> m EncJSON) -> m EncJSON
forall (m :: * -> *) e a.
Monad m =>
m (Either e a) -> (e -> m a) -> m a
`onLeftM` QErr -> m EncJSON
forall a. QErr -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError

-- | Updates the cleanup switch in metadata given the source, table and trigger name
-- The Bool value represents the status of the cleaner, whether to start or pause it
updateCleanupStatusInMetadata ::
  forall b m.
  (Backend b, QErrM m, CacheRWM m, MetadataM m) =>
  AutoTriggerLogCleanupConfig ->
  EventTriggerCleanupStatus ->
  SourceName ->
  TableName b ->
  TriggerName ->
  m ()
updateCleanupStatusInMetadata :: forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRWM m, MetadataM m) =>
AutoTriggerLogCleanupConfig
-> EventTriggerCleanupStatus
-> SourceName
-> TableName b
-> TriggerName
-> m ()
updateCleanupStatusInMetadata AutoTriggerLogCleanupConfig
cleanupConfig EventTriggerCleanupStatus
cleanupSwitch SourceName
sourceName TableName b
tableName TriggerName
triggerName = do
  let newCleanupConfig :: Maybe AutoTriggerLogCleanupConfig
newCleanupConfig = AutoTriggerLogCleanupConfig -> Maybe AutoTriggerLogCleanupConfig
forall a. a -> Maybe a
Just (AutoTriggerLogCleanupConfig -> Maybe AutoTriggerLogCleanupConfig)
-> AutoTriggerLogCleanupConfig -> Maybe AutoTriggerLogCleanupConfig
forall a b. (a -> b) -> a -> b
$ AutoTriggerLogCleanupConfig
cleanupConfig {_atlccPaused :: EventTriggerCleanupStatus
_atlccPaused = EventTriggerCleanupStatus
cleanupSwitch}
      metadataObj :: MetadataObjId
metadataObj =
        SourceName -> AnyBackend SourceMetadataObjId -> MetadataObjId
MOSourceObjId SourceName
sourceName
          (AnyBackend SourceMetadataObjId -> MetadataObjId)
-> AnyBackend SourceMetadataObjId -> MetadataObjId
forall a b. (a -> b) -> a -> b
$ SourceMetadataObjId b -> AnyBackend SourceMetadataObjId
forall (b :: BackendType) (i :: BackendType -> *).
HasTag b =>
i b -> AnyBackend i
AB.mkAnyBackend
          (SourceMetadataObjId b -> AnyBackend SourceMetadataObjId)
-> SourceMetadataObjId b -> AnyBackend SourceMetadataObjId
forall a b. (a -> b) -> a -> b
$ forall (b :: BackendType).
TableName b -> TableMetadataObjId -> SourceMetadataObjId b
SMOTableObj @b TableName b
tableName
          (TableMetadataObjId -> SourceMetadataObjId b)
-> TableMetadataObjId -> SourceMetadataObjId b
forall a b. (a -> b) -> a -> b
$ TriggerName -> TableMetadataObjId
MTOTrigger TriggerName
triggerName

  MetadataObjId -> MetadataModifier -> m ()
forall (m :: * -> *).
(QErrM m, CacheRWM m, MetadataM m) =>
MetadataObjId -> MetadataModifier -> m ()
buildSchemaCacheFor MetadataObjId
metadataObj
    (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
sourceName TableName b
tableName
    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
%~ Maybe AutoTriggerLogCleanupConfig
-> EventTriggerConf b -> EventTriggerConf b
forall (b :: BackendType).
Maybe AutoTriggerLogCleanupConfig
-> EventTriggerConf b -> EventTriggerConf b
updateCleanupConfig Maybe AutoTriggerLogCleanupConfig
newCleanupConfig

-- | Function to start/stop the cleanup action based on the event triggers supplied in
-- TriggerLogCleanupToggleConfig conf
toggleEventTriggerCleanupAction ::
  forall m.
  (MonadIO m, QErrM m, CacheRWM m, MetadataM m) =>
  TriggerLogCleanupToggleConfig ->
  EventTriggerCleanupStatus ->
  m EncJSON
toggleEventTriggerCleanupAction :: forall (m :: * -> *).
(MonadIO m, QErrM m, CacheRWM m, MetadataM m) =>
TriggerLogCleanupToggleConfig
-> EventTriggerCleanupStatus -> m EncJSON
toggleEventTriggerCleanupAction TriggerLogCleanupToggleConfig
conf EventTriggerCleanupStatus
cleanupSwitch = do
  SchemaCache
schemaCache <- m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  case TriggerLogCleanupToggleConfig
conf of
    TriggerLogCleanupSources TriggerLogCleanupSources
tlcs -> do
      case TriggerLogCleanupSources
tlcs of
        TriggerLogCleanupSources
TriggerAllSource -> do
          SourceCache
-> (SourceName -> AnyBackend SourceInfo -> m ()) -> m ()
forall i (t :: * -> *) (f :: * -> *) a b.
(FoldableWithIndex i t, Applicative f) =>
t a -> (i -> a -> f b) -> f ()
ifor_ (SchemaCache -> SourceCache
scSources SchemaCache
schemaCache) ((SourceName -> AnyBackend SourceInfo -> m ()) -> m ())
-> (SourceName -> AnyBackend SourceInfo -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \SourceName
sourceName AnyBackend SourceInfo
backendSourceInfo -> do
            forall (c :: BackendType -> Constraint) (i :: BackendType -> *) r.
AllBackendsSatisfy c =>
AnyBackend i -> (forall (b :: BackendType). c b => i b -> r) -> r
AB.dispatchAnyBackend @BackendEventTrigger AnyBackend SourceInfo
backendSourceInfo \(SourceInfo {Maybe QueryTagsConfig
TableCache b
FunctionCache b
StoredProcedureCache b
LogicalModelCache b
NativeQueryCache b
BackendSourceKind b
SourceName
SourceConfig b
ResolvedSourceCustomization
DBObjectsIntrospection b
_siConfiguration :: forall (b :: BackendType). SourceInfo b -> SourceConfig b
_siName :: SourceName
_siSourceKind :: BackendSourceKind b
_siTables :: TableCache b
_siFunctions :: FunctionCache b
_siNativeQueries :: NativeQueryCache b
_siStoredProcedures :: StoredProcedureCache b
_siLogicalModels :: LogicalModelCache b
_siConfiguration :: SourceConfig b
_siQueryTagsConfig :: Maybe QueryTagsConfig
_siCustomization :: ResolvedSourceCustomization
_siDbObjectsIntrospection :: DBObjectsIntrospection b
_siName :: forall (b :: BackendType). SourceInfo b -> SourceName
_siSourceKind :: forall (b :: BackendType). SourceInfo b -> BackendSourceKind b
_siTables :: forall (b :: BackendType). SourceInfo b -> TableCache b
_siFunctions :: forall (b :: BackendType). SourceInfo b -> FunctionCache b
_siNativeQueries :: forall (b :: BackendType). SourceInfo b -> NativeQueryCache b
_siStoredProcedures :: forall (b :: BackendType). SourceInfo b -> StoredProcedureCache b
_siLogicalModels :: forall (b :: BackendType). SourceInfo b -> LogicalModelCache b
_siQueryTagsConfig :: forall (b :: BackendType). SourceInfo b -> Maybe QueryTagsConfig
_siCustomization :: forall (b :: BackendType).
SourceInfo b -> ResolvedSourceCustomization
_siDbObjectsIntrospection :: forall (b :: BackendType). SourceInfo b -> DBObjectsIntrospection b
..} :: SourceInfo b) -> do
              TableCache b -> EventTriggerCleanupStatus -> SourceName -> m ()
forall (b :: BackendType).
Backend b =>
TableCache b -> EventTriggerCleanupStatus -> SourceName -> m ()
traverseTableHelper TableCache b
_siTables EventTriggerCleanupStatus
cleanupSwitch SourceName
sourceName
        TriggerSource NonEmpty SourceName
sourceNameLst -> do
          NonEmpty SourceName -> (SourceName -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ NonEmpty SourceName
sourceNameLst ((SourceName -> m ()) -> m ()) -> (SourceName -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \SourceName
sourceName -> do
            AnyBackend SourceInfo
backendSourceInfo <-
              SourceName -> SourceCache -> Maybe (AnyBackend SourceInfo)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup SourceName
sourceName (SchemaCache -> SourceCache
scSources SchemaCache
schemaCache)
                Maybe (AnyBackend SourceInfo)
-> m (AnyBackend SourceInfo) -> m (AnyBackend SourceInfo)
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> m (AnyBackend SourceInfo)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists (Text
"source with name " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Text
sourceNameToText SourceName
sourceName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" does not exists")

            forall (c :: BackendType -> Constraint) (i :: BackendType -> *) r.
AllBackendsSatisfy c =>
AnyBackend i -> (forall (b :: BackendType). c b => i b -> r) -> r
AB.dispatchAnyBackend @BackendEventTrigger AnyBackend SourceInfo
backendSourceInfo \(SourceInfo {Maybe QueryTagsConfig
TableCache b
FunctionCache b
StoredProcedureCache b
LogicalModelCache b
NativeQueryCache b
BackendSourceKind b
SourceName
SourceConfig b
ResolvedSourceCustomization
DBObjectsIntrospection b
_siConfiguration :: forall (b :: BackendType). SourceInfo b -> SourceConfig b
_siName :: forall (b :: BackendType). SourceInfo b -> SourceName
_siSourceKind :: forall (b :: BackendType). SourceInfo b -> BackendSourceKind b
_siTables :: forall (b :: BackendType). SourceInfo b -> TableCache b
_siFunctions :: forall (b :: BackendType). SourceInfo b -> FunctionCache b
_siNativeQueries :: forall (b :: BackendType). SourceInfo b -> NativeQueryCache b
_siStoredProcedures :: forall (b :: BackendType). SourceInfo b -> StoredProcedureCache b
_siLogicalModels :: forall (b :: BackendType). SourceInfo b -> LogicalModelCache b
_siQueryTagsConfig :: forall (b :: BackendType). SourceInfo b -> Maybe QueryTagsConfig
_siCustomization :: forall (b :: BackendType).
SourceInfo b -> ResolvedSourceCustomization
_siDbObjectsIntrospection :: forall (b :: BackendType). SourceInfo b -> DBObjectsIntrospection b
_siName :: SourceName
_siSourceKind :: BackendSourceKind b
_siTables :: TableCache b
_siFunctions :: FunctionCache b
_siNativeQueries :: NativeQueryCache b
_siStoredProcedures :: StoredProcedureCache b
_siLogicalModels :: LogicalModelCache b
_siConfiguration :: SourceConfig b
_siQueryTagsConfig :: Maybe QueryTagsConfig
_siCustomization :: ResolvedSourceCustomization
_siDbObjectsIntrospection :: DBObjectsIntrospection b
..} :: SourceInfo b) -> do
              TableCache b -> EventTriggerCleanupStatus -> SourceName -> m ()
forall (b :: BackendType).
Backend b =>
TableCache b -> EventTriggerCleanupStatus -> SourceName -> m ()
traverseTableHelper TableCache b
_siTables EventTriggerCleanupStatus
cleanupSwitch SourceName
sourceName
    TriggerQualifier NonEmpty EventTriggerQualifier
qualifierLst -> do
      NonEmpty EventTriggerQualifier
-> (EventTriggerQualifier -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ NonEmpty EventTriggerQualifier
qualifierLst ((EventTriggerQualifier -> m ()) -> m ())
-> (EventTriggerQualifier -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \EventTriggerQualifier
qualifier -> do
        let sourceName :: SourceName
sourceName = EventTriggerQualifier -> SourceName
_etqSourceName EventTriggerQualifier
qualifier
            triggerNames :: NonEmpty TriggerName
triggerNames = EventTriggerQualifier -> NonEmpty TriggerName
_etqEventTriggers EventTriggerQualifier
qualifier

        AnyBackend SourceInfo
backendSourceInfo <-
          SourceName -> SourceCache -> Maybe (AnyBackend SourceInfo)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup SourceName
sourceName (SchemaCache -> SourceCache
scSources SchemaCache
schemaCache)
            Maybe (AnyBackend SourceInfo)
-> m (AnyBackend SourceInfo) -> m (AnyBackend SourceInfo)
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> m (AnyBackend SourceInfo)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists (Text
"source with name " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> SourceName -> Text
sourceNameToText SourceName
sourceName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" does not exists")

        forall (c :: BackendType -> Constraint) (i :: BackendType -> *) r.
AllBackendsSatisfy c =>
AnyBackend i -> (forall (b :: BackendType). c b => i b -> r) -> r
AB.dispatchAnyBackend @BackendEventTrigger AnyBackend SourceInfo
backendSourceInfo \(SourceInfo {} :: SourceInfo b) -> do
          NonEmpty TriggerName -> (TriggerName -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ NonEmpty TriggerName
triggerNames ((TriggerName -> m ()) -> m ()) -> (TriggerName -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \TriggerName
triggerName -> do
            EventTriggerInfo b
eventTriggerInfo <- forall (b :: BackendType) (m :: * -> *).
(QErrM m, CacheRM m, Backend b) =>
SourceName -> TriggerName -> m (EventTriggerInfo b)
askEventTriggerInfo @b SourceName
sourceName TriggerName
triggerName
            TableName b
tableName <-
              forall (b :: BackendType).
Backend b =>
SchemaCache -> SourceName -> TriggerName -> Maybe (TableName b)
getTableNameFromTrigger @b SchemaCache
schemaCache SourceName
sourceName TriggerName
triggerName
                Maybe (TableName b) -> m (TableName b) -> m (TableName b)
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> m (TableName b)
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists (Text
"event trigger " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName
triggerName TriggerName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" does not exist")
            AutoTriggerLogCleanupConfig
cleanupConfig <-
              EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
forall (b :: BackendType).
EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
etiCleanupConfig EventTriggerInfo b
eventTriggerInfo
                Maybe AutoTriggerLogCleanupConfig
-> m AutoTriggerLogCleanupConfig -> m AutoTriggerLogCleanupConfig
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
`onNothing` Code -> Text -> m AutoTriggerLogCleanupConfig
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists (Text
"cleanup config does not exist for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName -> Text
triggerNameToTxt TriggerName
triggerName)
            forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRWM m, MetadataM m) =>
AutoTriggerLogCleanupConfig
-> EventTriggerCleanupStatus
-> SourceName
-> TableName b
-> TriggerName
-> m ()
updateCleanupStatusInMetadata @b AutoTriggerLogCleanupConfig
cleanupConfig EventTriggerCleanupStatus
cleanupSwitch SourceName
sourceName TableName b
tableName TriggerName
triggerName
  EncJSON -> m EncJSON
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure EncJSON
successMsg
  where
    traverseTableHelper ::
      forall b.
      (Backend b) =>
      TableCache b ->
      EventTriggerCleanupStatus ->
      SourceName ->
      m ()
    traverseTableHelper :: forall (b :: BackendType).
Backend b =>
TableCache b -> EventTriggerCleanupStatus -> SourceName -> m ()
traverseTableHelper TableCache b
tableCache EventTriggerCleanupStatus
switch SourceName
sourceName = TableCache b -> (TableInfo b -> m ()) -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ TableCache b
tableCache ((TableInfo b -> m ()) -> m ()) -> (TableInfo b -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \TableInfo b
tableInfo -> do
      let tableName :: TableName b
tableName = (TableCoreInfoG b (FieldInfo b) (ColumnInfo b) -> TableName b
forall (b :: BackendType) field primaryKeyColumn.
TableCoreInfoG b field primaryKeyColumn -> TableName b
_tciName (TableCoreInfoG b (FieldInfo b) (ColumnInfo b) -> TableName b)
-> (TableInfo b -> TableCoreInfoG b (FieldInfo b) (ColumnInfo b))
-> TableInfo b
-> TableName 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) TableInfo b
tableInfo
          eventTriggerInfoMap :: EventTriggerInfoMap b
eventTriggerInfoMap = TableInfo b -> EventTriggerInfoMap b
forall (b :: BackendType). TableInfo b -> EventTriggerInfoMap b
_tiEventTriggerInfoMap TableInfo b
tableInfo
      EventTriggerInfoMap b
-> (TriggerName -> EventTriggerInfo b -> m ()) -> m ()
forall i (t :: * -> *) (f :: * -> *) a b.
(FoldableWithIndex i t, Applicative f) =>
t a -> (i -> a -> f b) -> f ()
ifor_ EventTriggerInfoMap b
eventTriggerInfoMap ((TriggerName -> EventTriggerInfo b -> m ()) -> m ())
-> (TriggerName -> EventTriggerInfo b -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \TriggerName
triggerName EventTriggerInfo b
eventTriggerInfo -> do
        Maybe AutoTriggerLogCleanupConfig
-> (AutoTriggerLogCleanupConfig -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ (EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
forall (b :: BackendType).
EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
etiCleanupConfig EventTriggerInfo b
eventTriggerInfo) ((AutoTriggerLogCleanupConfig -> m ()) -> m ())
-> (AutoTriggerLogCleanupConfig -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \AutoTriggerLogCleanupConfig
cleanupConfig ->
          forall (b :: BackendType) (m :: * -> *).
(Backend b, QErrM m, CacheRWM m, MetadataM m) =>
AutoTriggerLogCleanupConfig
-> EventTriggerCleanupStatus
-> SourceName
-> TableName b
-> TriggerName
-> m ()
updateCleanupStatusInMetadata @b AutoTriggerLogCleanupConfig
cleanupConfig EventTriggerCleanupStatus
switch SourceName
sourceName TableName b
tableName TriggerName
triggerName

runEventTriggerResumeCleanup ::
  forall m.
  (MonadIO m, QErrM m, CacheRWM m, MetadataM m) =>
  TriggerLogCleanupToggleConfig ->
  m EncJSON
runEventTriggerResumeCleanup :: forall (m :: * -> *).
(MonadIO m, QErrM m, CacheRWM m, MetadataM m) =>
TriggerLogCleanupToggleConfig -> m EncJSON
runEventTriggerResumeCleanup TriggerLogCleanupToggleConfig
conf = TriggerLogCleanupToggleConfig
-> EventTriggerCleanupStatus -> m EncJSON
forall (m :: * -> *).
(MonadIO m, QErrM m, CacheRWM m, MetadataM m) =>
TriggerLogCleanupToggleConfig
-> EventTriggerCleanupStatus -> m EncJSON
toggleEventTriggerCleanupAction TriggerLogCleanupToggleConfig
conf EventTriggerCleanupStatus
ETCSUnpaused

runEventTriggerPauseCleanup ::
  (MonadError QErr m, CacheRWM m, MonadIO m, MetadataM m) =>
  TriggerLogCleanupToggleConfig ->
  m EncJSON
runEventTriggerPauseCleanup :: forall (m :: * -> *).
(MonadError QErr m, CacheRWM m, MonadIO m, MetadataM m) =>
TriggerLogCleanupToggleConfig -> m EncJSON
runEventTriggerPauseCleanup TriggerLogCleanupToggleConfig
conf = TriggerLogCleanupToggleConfig
-> EventTriggerCleanupStatus -> m EncJSON
forall (m :: * -> *).
(MonadIO m, QErrM m, CacheRWM m, MetadataM m) =>
TriggerLogCleanupToggleConfig
-> EventTriggerCleanupStatus -> m EncJSON
toggleEventTriggerCleanupAction TriggerLogCleanupToggleConfig
conf EventTriggerCleanupStatus
ETCSPaused

-- | Collects and returns all the event triggers with cleanup config
getAllEventTriggersWithCleanupConfig :: TableInfo b -> [(TriggerName, AutoTriggerLogCleanupConfig)]
getAllEventTriggersWithCleanupConfig :: forall (b :: BackendType).
TableInfo b -> [(TriggerName, AutoTriggerLogCleanupConfig)]
getAllEventTriggersWithCleanupConfig TableInfo b
tInfo = ((TriggerName, EventTriggerInfo b)
 -> Maybe (TriggerName, AutoTriggerLogCleanupConfig))
-> [(TriggerName, EventTriggerInfo b)]
-> [(TriggerName, AutoTriggerLogCleanupConfig)]
forall a b. (a -> Maybe b) -> [a] -> [b]
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (\(TriggerName
triggerName, EventTriggerInfo b
triggerInfo) -> (TriggerName
triggerName,) (AutoTriggerLogCleanupConfig
 -> (TriggerName, AutoTriggerLogCleanupConfig))
-> Maybe AutoTriggerLogCleanupConfig
-> Maybe (TriggerName, AutoTriggerLogCleanupConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
forall (b :: BackendType).
EventTriggerInfo b -> Maybe AutoTriggerLogCleanupConfig
etiCleanupConfig EventTriggerInfo b
triggerInfo) ([(TriggerName, EventTriggerInfo b)]
 -> [(TriggerName, AutoTriggerLogCleanupConfig)])
-> [(TriggerName, EventTriggerInfo b)]
-> [(TriggerName, AutoTriggerLogCleanupConfig)]
forall a b. (a -> b) -> a -> b
$ HashMap TriggerName (EventTriggerInfo b)
-> [(TriggerName, EventTriggerInfo b)]
forall k v. HashMap k v -> [(k, v)]
HashMap.toList (HashMap TriggerName (EventTriggerInfo b)
 -> [(TriggerName, EventTriggerInfo b)])
-> HashMap TriggerName (EventTriggerInfo b)
-> [(TriggerName, EventTriggerInfo b)]
forall a b. (a -> b) -> a -> b
$ TableInfo b -> HashMap TriggerName (EventTriggerInfo b)
forall (b :: BackendType). TableInfo b -> EventTriggerInfoMap b
_tiEventTriggerInfoMap TableInfo b
tInfo

hasCleanupCronScheduleUpdated :: Maybe AutoTriggerLogCleanupConfig -> Maybe AutoTriggerLogCleanupConfig -> Bool
hasCleanupCronScheduleUpdated :: Maybe AutoTriggerLogCleanupConfig
-> Maybe AutoTriggerLogCleanupConfig -> Bool
hasCleanupCronScheduleUpdated Maybe AutoTriggerLogCleanupConfig
Nothing Maybe AutoTriggerLogCleanupConfig
_ = Bool
False
hasCleanupCronScheduleUpdated Maybe AutoTriggerLogCleanupConfig
_ Maybe AutoTriggerLogCleanupConfig
Nothing = Bool
True
hasCleanupCronScheduleUpdated (Just AutoTriggerLogCleanupConfig
oldConfig) (Just AutoTriggerLogCleanupConfig
newConfig) =
  AutoTriggerLogCleanupConfig -> CronSchedule
_atlccSchedule AutoTriggerLogCleanupConfig
oldConfig CronSchedule -> CronSchedule -> Bool
forall a. Eq a => a -> a -> Bool
/= AutoTriggerLogCleanupConfig -> CronSchedule
_atlccSchedule AutoTriggerLogCleanupConfig
newConfig

getAllETWithCleanupConfigInTableMetadata :: TableMetadata b -> [(TriggerName, AutoTriggerLogCleanupConfig)]
getAllETWithCleanupConfigInTableMetadata :: forall (b :: BackendType).
TableMetadata b -> [(TriggerName, AutoTriggerLogCleanupConfig)]
getAllETWithCleanupConfigInTableMetadata TableMetadata b
tMetadata =
  ((TriggerName, EventTriggerConf b)
 -> Maybe (TriggerName, AutoTriggerLogCleanupConfig))
-> [(TriggerName, EventTriggerConf b)]
-> [(TriggerName, AutoTriggerLogCleanupConfig)]
forall a b. (a -> Maybe b) -> [a] -> [b]
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe
    ( \(TriggerName
triggerName, EventTriggerConf b
triggerConf) ->
        (TriggerName
triggerName,)
          (AutoTriggerLogCleanupConfig
 -> (TriggerName, AutoTriggerLogCleanupConfig))
-> Maybe AutoTriggerLogCleanupConfig
-> Maybe (TriggerName, AutoTriggerLogCleanupConfig)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> EventTriggerConf b -> Maybe AutoTriggerLogCleanupConfig
forall (b :: BackendType).
EventTriggerConf b -> Maybe AutoTriggerLogCleanupConfig
etcCleanupConfig EventTriggerConf b
triggerConf
    )
    ([(TriggerName, EventTriggerConf b)]
 -> [(TriggerName, AutoTriggerLogCleanupConfig)])
-> [(TriggerName, EventTriggerConf b)]
-> [(TriggerName, AutoTriggerLogCleanupConfig)]
forall a b. (a -> b) -> a -> b
$ InsOrdHashMap TriggerName (EventTriggerConf b)
-> [(TriggerName, EventTriggerConf b)]
forall k v. InsOrdHashMap k v -> [(k, v)]
InsOrdHashMap.toList
    (InsOrdHashMap TriggerName (EventTriggerConf b)
 -> [(TriggerName, EventTriggerConf b)])
-> InsOrdHashMap TriggerName (EventTriggerConf b)
-> [(TriggerName, EventTriggerConf b)]
forall a b. (a -> b) -> a -> b
$ TableMetadata b -> InsOrdHashMap TriggerName (EventTriggerConf b)
forall (b :: BackendType). TableMetadata b -> EventTriggers b
_tmEventTriggers TableMetadata b
tMetadata

runGetEventLogs ::
  forall b m.
  (MonadIO m, CacheRM m, MonadError QErr m, BackendEventTrigger b, MetadataM m) =>
  GetEventLogs b ->
  m EncJSON
runGetEventLogs :: forall (b :: BackendType) (m :: * -> *).
(MonadIO m, CacheRM m, MonadError QErr m, BackendEventTrigger b,
 MetadataM m) =>
GetEventLogs b -> m EncJSON
runGetEventLogs GetEventLogs b
getEventLogs = do
  SourceConfig b
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @b SourceName
sourceName
  Bool
doesTriggerExists <- forall (b :: BackendType) (m :: * -> *).
(Backend b, CacheRM m) =>
SourceName -> TriggerName -> m Bool
checkIfTriggerNameExists @b SourceName
sourceName TriggerName
triggerName
  if Bool -> Bool
not Bool
doesTriggerExists
    then Code -> Text -> m EncJSON
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists (Text -> m EncJSON) -> Text -> m EncJSON
forall a b. (a -> b) -> a -> b
$ Text
"event trigger " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName
triggerName TriggerName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" does not exist"
    else [EventLog] -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue ([EventLog] -> EncJSON) -> m [EventLog] -> m EncJSON
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SourceConfig b -> GetEventLogs b -> m [EventLog]
forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> GetEventLogs b -> m [EventLog]
forall (m :: * -> *).
(MonadIO m, MonadError QErr m) =>
SourceConfig b -> GetEventLogs b -> m [EventLog]
fetchEventLogs SourceConfig b
sourceConfig GetEventLogs b
getEventLogs
  where
    sourceName :: SourceName
sourceName = GetEventLogs b -> SourceName
forall (b :: BackendType). GetEventLogs b -> SourceName
_gelSourceName GetEventLogs b
getEventLogs
    triggerName :: TriggerName
triggerName = GetEventLogs b -> TriggerName
forall (b :: BackendType). GetEventLogs b -> TriggerName
_gelName GetEventLogs b
getEventLogs

runGetEventInvocationLogs ::
  forall b m.
  (MonadIO m, CacheRM m, MonadError QErr m, BackendEventTrigger b, MetadataM m) =>
  GetEventInvocations b ->
  m EncJSON
runGetEventInvocationLogs :: forall (b :: BackendType) (m :: * -> *).
(MonadIO m, CacheRM m, MonadError QErr m, BackendEventTrigger b,
 MetadataM m) =>
GetEventInvocations b -> m EncJSON
runGetEventInvocationLogs GetEventInvocations b
getEventInvocations = do
  SourceConfig b
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @b SourceName
sourceName
  Bool
doesTriggerExists <- forall (b :: BackendType) (m :: * -> *).
(Backend b, CacheRM m) =>
SourceName -> TriggerName -> m Bool
checkIfTriggerNameExists @b SourceName
sourceName TriggerName
triggerName
  if Bool -> Bool
not Bool
doesTriggerExists
    then Code -> Text -> m EncJSON
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists (Text -> m EncJSON) -> Text -> m EncJSON
forall a b. (a -> b) -> a -> b
$ Text
"event trigger " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName
triggerName TriggerName -> Text -> Text
forall t. ToTxt t => t -> Text -> Text
<<> Text
" does not exist"
    else [EventInvocationLog] -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue ([EventInvocationLog] -> EncJSON)
-> m [EventInvocationLog] -> m EncJSON
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SourceConfig b -> GetEventInvocations b -> m [EventInvocationLog]
forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> GetEventInvocations b -> m [EventInvocationLog]
forall (m :: * -> *).
(MonadIO m, MonadError QErr m) =>
SourceConfig b -> GetEventInvocations b -> m [EventInvocationLog]
fetchEventInvocationLogs SourceConfig b
sourceConfig GetEventInvocations b
getEventInvocations
  where
    sourceName :: SourceName
sourceName = GetEventInvocations b -> SourceName
forall (b :: BackendType). GetEventInvocations b -> SourceName
_geiSourceName GetEventInvocations b
getEventInvocations
    triggerName :: TriggerName
triggerName = GetEventInvocations b -> TriggerName
forall (b :: BackendType). GetEventInvocations b -> TriggerName
_geiName GetEventInvocations b
getEventInvocations

runGetEventById ::
  forall b m.
  (MonadIO m, CacheRM m, MonadError QErr m, BackendEventTrigger b, MetadataM m) =>
  GetEventById b ->
  m EncJSON
runGetEventById :: forall (b :: BackendType) (m :: * -> *).
(MonadIO m, CacheRM m, MonadError QErr m, BackendEventTrigger b,
 MetadataM m) =>
GetEventById b -> m EncJSON
runGetEventById GetEventById b
getEventById = do
  SourceConfig b
sourceConfig <- forall (b :: BackendType) (m :: * -> *).
(CacheRM m, MonadError QErr m, Backend b, MetadataM m) =>
SourceName -> m (SourceConfig b)
askSourceConfig @b SourceName
sourceName
  EventLogWithInvocations -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue (EventLogWithInvocations -> EncJSON)
-> m EventLogWithInvocations -> m EncJSON
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SourceConfig b -> GetEventById b -> m EventLogWithInvocations
forall (b :: BackendType) (m :: * -> *).
(BackendEventTrigger b, MonadIO m, MonadError QErr m) =>
SourceConfig b -> GetEventById b -> m EventLogWithInvocations
forall (m :: * -> *).
(MonadIO m, MonadError QErr m) =>
SourceConfig b -> GetEventById b -> m EventLogWithInvocations
fetchEventById SourceConfig b
sourceConfig GetEventById b
getEventById
  where
    sourceName :: SourceName
sourceName = GetEventById b -> SourceName
forall (b :: BackendType). GetEventById b -> SourceName
_gebiSourceName GetEventById b
getEventById