mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-17 04:24:35 +03:00
39 lines
1.2 KiB
Django/Jinja
39 lines
1.2 KiB
Django/Jinja
CREATE OR REPLACE function hdb_views.{{QUALIFIED_TRIGGER_NAME}}() RETURNS trigger
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
DECLARE
|
|
id text;
|
|
_old record;
|
|
_new record;
|
|
_data json;
|
|
payload json;
|
|
BEGIN
|
|
id := gen_random_uuid();
|
|
IF TG_OP = 'UPDATE' THEN
|
|
_old := {{OLD_ROW}};
|
|
_new := {{NEW_ROW}};
|
|
ELSE
|
|
/* initialize _old and _new with dummy values */
|
|
_old := row((select 1));
|
|
_new := row((select 1));
|
|
END IF;
|
|
_data := json_build_object(
|
|
'old', {{OLD_PAYLOAD_EXPRESSION}},
|
|
'new', {{NEW_PAYLOAD_EXPRESSION}}
|
|
);
|
|
payload := json_build_object(
|
|
'op', TG_OP,
|
|
'data', _data
|
|
)::text;
|
|
IF (TG_OP <> 'UPDATE') OR (_old <> _new) THEN
|
|
INSERT INTO
|
|
hdb_catalog.event_log (id, schema_name, table_name, trigger_name, trigger_id, payload)
|
|
VALUES
|
|
(id, TG_TABLE_SCHEMA, TG_TABLE_NAME, '{{NAME}}', '{{ID}}', payload);
|
|
END IF;
|
|
RETURN NULL;
|
|
END;
|
|
$$;
|
|
DROP TRIGGER IF EXISTS {{QUALIFIED_TRIGGER_NAME}} ON {{QUALIFIED_TABLE}};
|
|
CREATE TRIGGER {{QUALIFIED_TRIGGER_NAME}} AFTER {{OPERATION}} ON {{QUALIFIED_TABLE}} FOR EACH ROW EXECUTE PROCEDURE hdb_views.{{QUALIFIED_TRIGGER_NAME}}();
|