mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 17:31:56 +03:00
44347d2d74
Earlier, while creating the event trigger's internal postgres trigger, we used to get the name of the table from the `TG_TABLE_NAME` special trigger variable. Using this with normal tables works fine, but it breaks when the parent table is partitioned because we associate the ET configuration in the schema only with the original table (as it should be). In this PR, we supply the table name and schema name through template variables instead of using `TG_TABLE_NAME` and `TG_TABLE_SCHEMA`, so that event triggers work with a partitioned table as well. TODO: - [x] Changelog - [x] unit test (ET on partition table) GitOrigin-RevId: 556376881a85525300dcf64da0611ee9ad387eb0
40 lines
1.6 KiB
Plaintext
40 lines
1.6 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;
|
|
$$;
|
|
DROP TRIGGER IF EXISTS #{qualifiedTriggerName} ON #{qualifiedTable};
|
|
CREATE TRIGGER #{qualifiedTriggerName} AFTER #{operation} ON #{qualifiedTable} FOR EACH ROW EXECUTE PROCEDURE hdb_catalog.#{qualifiedTriggerName}();
|