mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-16 01:44:03 +03:00
32a316aef7
## Description ✍️ This PR introduces a new feature to enable/disable event triggers during logical replication of table data for PostgreSQL and MS-SQL data sources. We introduce a new field `trigger_on_replication` in the `*_create_event_trigger` metadata API. By default the event triggers will not fire for logical data replication. ## Changelog ✍️ __Component__ : server __Type__: feature __Product__: community-edition ### Short Changelog Add option to enable/disable event triggers on logically replicated tables ### Related Issues ✍ https://github.com/hasura/graphql-engine/issues/8814 https://hasurahq.atlassian.net/browse/GS-252 ### Solution and Design - By default, triggers do **not** fire when the session mode is `replica` in Postgres, so if the `triggerOnReplication` is set to `true` for an event trigger we run the query `ALTER TABLE #{tableTxt} ENABLE ALWAYS TRIGGER #{triggerNameTxt};` so that the trigger fires always irrespective of the `session_replication_role` - By default, triggers do fire in case of replication in MS-SQL, so if the `triggerOnReplication` is set to `false` for an event trigger we add a clause `NOT FOR REPLICATION` to the the SQL when the trigger is created/altered, which sets the `is_not_for_replication` for the trigger as `true` and it does not fire during logical replication. ### Steps to test and verify ✍ - Run hspec integration tests for HGE ## Server checklist ✍ ### Metadata ✍ Does this PR add a new Metadata feature? - ✅ Yes - Does `export_metadata`/`replace_metadata` supports the new metadata added? - ✅ PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6953 Co-authored-by: Puru Gupta <32328846+purugupta99@users.noreply.github.com> Co-authored-by: Sean Park-Ross <94021366+seanparkross@users.noreply.github.com> GitOrigin-RevId: 92731328a2bbdcad2302c829f26f9acb33c36135
102 lines
3.2 KiB
Haskell
102 lines
3.2 KiB
Haskell
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
module Hasura.Server.Migrate.Internal
|
|
( getCatalogVersion,
|
|
from3To4,
|
|
setCatalogVersion,
|
|
)
|
|
where
|
|
|
|
import Data.Aeson qualified as A
|
|
import Data.Text qualified as T
|
|
import Data.Time.Clock (UTCTime)
|
|
import Database.PG.Query qualified as PG
|
|
import Hasura.Backends.Postgres.Connection
|
|
import Hasura.Base.Error
|
|
import Hasura.Prelude
|
|
import Hasura.RQL.Types.Backend (Backend)
|
|
import Hasura.RQL.Types.Common (InputWebhook, TriggerOnReplication (..))
|
|
import Hasura.RQL.Types.EventTrigger
|
|
import Hasura.SQL.Backend
|
|
import Hasura.Server.Migrate.Version
|
|
|
|
-- | The old 0.8 catalog version is non-integral, so the version has always been
|
|
-- stored as a string.
|
|
getCatalogVersion :: PG.TxE QErr MetadataCatalogVersion
|
|
getCatalogVersion = do
|
|
versionText <-
|
|
runIdentity . PG.getRow
|
|
<$> PG.withQE
|
|
defaultTxErrorHandler
|
|
[PG.sql| SELECT version FROM hdb_catalog.hdb_version |]
|
|
()
|
|
False
|
|
onLeft (readEither $ T.unpack versionText) $
|
|
\err -> throw500 $ "Unexpected: couldn't convert read catalog version " <> versionText <> ", err:" <> tshow err
|
|
|
|
from3To4 :: forall m. (Backend ('Postgres 'Vanilla), MonadTx m) => m ()
|
|
from3To4 = liftTx $
|
|
PG.catchE defaultTxErrorHandler $ do
|
|
PG.unitQ
|
|
[PG.sql|
|
|
ALTER TABLE hdb_catalog.event_triggers
|
|
ADD COLUMN configuration JSON |]
|
|
()
|
|
False
|
|
eventTriggers <-
|
|
map uncurryEventTrigger
|
|
<$> PG.withQ
|
|
[PG.sql|
|
|
SELECT e.name, e.definition::json, e.webhook, e.num_retries, e.retry_interval, e.headers::json
|
|
FROM hdb_catalog.event_triggers e |]
|
|
()
|
|
False
|
|
forM_ eventTriggers updateEventTrigger3To4
|
|
PG.unitQ
|
|
[PG.sql|
|
|
ALTER TABLE hdb_catalog.event_triggers
|
|
DROP COLUMN definition,
|
|
DROP COLUMN query,
|
|
DROP COLUMN webhook,
|
|
DROP COLUMN num_retries,
|
|
DROP COLUMN retry_interval,
|
|
DROP COLUMN headers,
|
|
DROP COLUMN metadataTransform|]
|
|
()
|
|
False
|
|
where
|
|
uncurryEventTrigger ::
|
|
( TriggerName,
|
|
PG.ViaJSON (TriggerOpsDef ('Postgres 'Vanilla)),
|
|
InputWebhook,
|
|
Int,
|
|
Int,
|
|
PG.ViaJSON (Maybe [HeaderConf])
|
|
) ->
|
|
EventTriggerConf ('Postgres 'Vanilla)
|
|
uncurryEventTrigger (trn, PG.ViaJSON tDef, w, nr, rint, PG.ViaJSON headers) =
|
|
EventTriggerConf trn tDef (Just w) Nothing (RetryConf nr rint Nothing) headers Nothing Nothing Nothing TORDisableTrigger
|
|
updateEventTrigger3To4 etc@(EventTriggerConf name _ _ _ _ _ _ _ _ _) =
|
|
PG.unitQ
|
|
[PG.sql|
|
|
UPDATE hdb_catalog.event_triggers
|
|
SET
|
|
configuration = $1
|
|
WHERE name = $2
|
|
|]
|
|
(PG.ViaJSON $ A.toJSON etc, name)
|
|
True
|
|
|
|
setCatalogVersion :: MonadTx m => Text -> UTCTime -> m ()
|
|
setCatalogVersion ver time =
|
|
liftTx $
|
|
PG.unitQE
|
|
defaultTxErrorHandler
|
|
[PG.sql|
|
|
INSERT INTO hdb_catalog.hdb_version (version, upgraded_on) VALUES ($1, $2)
|
|
ON CONFLICT ((version IS NOT NULL))
|
|
DO UPDATE SET version = $1, upgraded_on = $2
|
|
|]
|
|
(ver, time)
|
|
False
|