mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 17:02:49 +03:00
330f9b6e26
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4101 GitOrigin-RevId: 12a2736e4d88dfb8379cd54f4beaedac32795d7d
38 lines
1.4 KiB
Plaintext
38 lines
1.4 KiB
Plaintext
CREATE OR REPLACE function hdb_catalog.#{qualifiedTriggerName}() RETURNS trigger
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
DECLARE
|
|
_old record;
|
|
_new record;
|
|
_data json;
|
|
BEGIN
|
|
IF TG_OP = 'UPDATE' THEN
|
|
_old := #{oldRow};
|
|
_new := #{newRow};
|
|
ELSE
|
|
/* initialize _old and _new with dummy values for INSERT and UPDATE events*/
|
|
_old := row((select 1));
|
|
_new := row((select 1));
|
|
END IF;
|
|
_data := json_build_object(
|
|
'old', #{oldPayloadExpression},
|
|
'new', #{newPayloadExpression}
|
|
);
|
|
BEGIN
|
|
/* NOTE: formerly we used TG_TABLE_NAME in place of tableName here. However in the case of
|
|
partitioned tables this will give the name of the partitioned table and since we use the table name to
|
|
get the event trigger configuration from the schema, this fails because the event trigger is only created
|
|
on the original table. */
|
|
IF (TG_OP <> 'UPDATE') OR (_old <> _new) THEN
|
|
PERFORM hdb_catalog.insert_event_log(CAST(#{schemaName} AS text), CAST(#{tableName} AS text), CAST('#{name}' AS text), TG_OP, _data);
|
|
END IF;
|
|
EXCEPTION WHEN undefined_function THEN
|
|
IF (TG_OP <> 'UPDATE') OR (_old *<> _new) THEN
|
|
PERFORM hdb_catalog.insert_event_log(CAST(#{schemaName} AS text), CAST(#{tableName} AS text), CAST('#{name}' AS text), TG_OP, _data);
|
|
END IF;
|
|
END;
|
|
|
|
RETURN NULL;
|
|
END;
|
|
$$;
|