2018-11-13 08:58:55 +03:00
CREATE OR REPLACE function hdb_views.{{QUALIFIED_TRIGGER_NAME}}() RETURNS trigger
2018-09-05 14:26:46 +03:00
LANGUAGE plpgsql
AS $$
DECLARE
id text;
2018-10-25 10:22:51 +03:00
_old record;
_new record;
_data json;
payload json;
2018-09-05 14:26:46 +03:00
BEGIN
id := gen_random_uuid();
2018-10-25 10:22:51 +03:00
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;
2018-09-05 14:26:46 +03:00
_data := json_build_object(
2018-10-25 10:22:51 +03:00
'old', {{OLD_PAYLOAD_EXPRESSION}},
'new', {{NEW_PAYLOAD_EXPRESSION}}
2018-09-05 14:26:46 +03:00
);
payload := json_build_object(
'op', TG_OP,
'data', _data
)::text;
2018-10-25 10:22:51 +03:00
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;
2018-09-05 14:26:46 +03:00
RETURN NULL;
END;
$$;
2018-11-13 08:58:55 +03:00
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}}();