mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
52 lines
1.7 KiB
Django/Jinja
52 lines
1.7 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;
|
|
session_variables json;
|
|
server_version_num int;
|
|
BEGIN
|
|
id := gen_random_uuid();
|
|
server_version_num := current_setting('server_version_num');
|
|
IF server_version_num >= 90600 THEN
|
|
session_variables := current_setting('hasura.user', 't');
|
|
ELSE
|
|
BEGIN
|
|
session_variables := current_setting('hasura.user');
|
|
EXCEPTION WHEN OTHERS THEN
|
|
session_variables := NULL;
|
|
END;
|
|
END IF;
|
|
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,
|
|
'session_variables', session_variables
|
|
)::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}}();
|