Skip to content

Commit cde4b0f

Browse files
committed
RE: fixed RE of triggers when user does not have USAGE role
1 parent 33eb1af commit cde4b0f

File tree

1 file changed

+58
-23
lines changed

1 file changed

+58
-23
lines changed

reverse_engineering/helpers/queryConstants.js

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -282,30 +282,65 @@ const queryConstants = {
282282
GET_DATABASES:
283283
'SELECT datname AS database_name FROM pg_catalog.pg_database WHERE datistemplate != TRUE AND datallowconn = TRUE;',
284284
GET_TRIGGERS: `
285-
SELECT
286-
trigger_name,
287-
array_agg(event_manipulation::text) AS trigger_events,
288-
event_object_schema,
289-
event_object_table,
290-
action_orientation,
291-
action_timing,
292-
action_reference_old_table,
293-
action_reference_new_table,
294-
action_condition,
295-
action_statement
296-
FROM information_schema.triggers
285+
SELECT trigger_name,
286+
array_agg(event_manipulation::text) AS trigger_events,
287+
event_object_schema,
288+
event_object_table,
289+
action_orientation,
290+
action_timing,
291+
action_reference_old_table,
292+
action_reference_new_table,
293+
action_condition,
294+
action_statement
295+
FROM
296+
-- This is information_schema.triggers view but without permitions
297+
(SELECT (current_database())::information_schema.sql_identifier AS trigger_catalog,
298+
(n.nspname)::information_schema.sql_identifier AS trigger_schema,
299+
(t.tgname)::information_schema.sql_identifier AS trigger_name,
300+
(em.text)::information_schema.character_data AS event_manipulation,
301+
(current_database())::information_schema.sql_identifier AS event_object_catalog,
302+
(n.nspname)::information_schema.sql_identifier AS event_object_schema,
303+
(c.relname)::information_schema.sql_identifier AS event_object_table,
304+
(rank() OVER (PARTITION BY (n.nspname)::information_schema.sql_identifier,
305+
(c.relname)::information_schema.sql_identifier,
306+
em.num, ((t.tgtype)::integer & 1), ((t.tgtype)::integer & 66)
307+
ORDER BY t.tgname))::information_schema.cardinal_number AS action_order,
308+
(regexp_match(pg_get_triggerdef(t.oid), '.{35,} WHEN \((.+)\) EXECUTE FUNCTION'::text))[1]::information_schema.character_data AS action_condition,
309+
(SUBSTRING(pg_get_triggerdef(t.oid)
310+
FROM (POSITION(('EXECUTE FUNCTION'::text) IN (SUBSTRING(pg_get_triggerdef(t.oid)
311+
FROM 48))) + 47)))::information_schema.character_data AS action_statement,
312+
(CASE ((t.tgtype)::integer & 1)
313+
WHEN 1 THEN 'ROW'::text
314+
ELSE 'STATEMENT'::text
315+
END)::information_schema.character_data AS action_orientation,
316+
(CASE ((t.tgtype)::integer & 66)
317+
WHEN 2 THEN 'BEFORE'::text
318+
WHEN 64 THEN 'INSTEAD OF'::text
319+
ELSE 'AFTER'::text
320+
END)::information_schema.character_data AS action_timing,
321+
(t.tgoldtable)::information_schema.sql_identifier AS action_reference_old_table,
322+
(t.tgnewtable)::information_schema.sql_identifier AS action_reference_new_table,
323+
(NULL::timestamp with time zone)::information_schema.time_stamp AS created
324+
FROM pg_namespace n,
325+
pg_class c,
326+
pg_trigger t, (VALUES (4,'INSERT'::text), (8,'DELETE'::text), (16,'UPDATE'::text)) em(num, text)
327+
WHERE ((n.oid = c.relnamespace)
328+
AND (c.oid = t.tgrelid)
329+
AND (((t.tgtype)::integer & em.num) <> 0)
330+
AND (NOT t.tgisinternal)
331+
AND (NOT pg_is_other_temp_schema(n.oid)))) AS tmp_views
297332
WHERE event_object_schema = $1 AND event_object_table = $2
298-
GROUP BY
299-
trigger_schema,
300-
trigger_name,
301-
event_object_schema,
302-
event_object_table,
303-
action_orientation,
304-
action_timing,
305-
action_reference_old_table,
306-
action_reference_new_table,
307-
action_condition,
308-
action_statement;`,
333+
GROUP BY
334+
trigger_schema,
335+
trigger_name,
336+
event_object_schema,
337+
event_object_table,
338+
action_orientation,
339+
action_timing,
340+
action_reference_old_table,
341+
action_reference_new_table,
342+
action_condition,
343+
action_statement;`,
309344
GET_TRIGGERS_ADDITIONAL_DATA: `
310345
SELECT
311346
pg_catalog.obj_description(pg_trigger.oid, 'pg_trigger') AS description,

0 commit comments

Comments
 (0)