Skip to content

Commit 388bdcc

Browse files
committed
add tests
1 parent 9d71cf8 commit 388bdcc

7 files changed

Lines changed: 598 additions & 24 deletions

File tree

‎migrations/db/migrations/20260923021047_update_pgrst_watch_triggers.sql‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,33 @@ CREATE OR REPLACE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger
55
AS $$
66
DECLARE
77
cmd record;
8+
obj_schema text;
89
BEGIN
910
FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
1011
LOOP
12+
-- triggers and rules have no schema of their own (schema_name is NULL),
13+
-- so use the schema of the table they belong to
14+
obj_schema := CASE
15+
WHEN cmd.object_type IN ('trigger', 'rule')
16+
THEN (pg_identify_object_as_address(cmd.classid, cmd.objid, cmd.objsubid)).object_names[1]
17+
ELSE cmd.schema_name
18+
END;
19+
1120
IF cmd.command_tag IN (
1221
'CREATE SCHEMA', 'ALTER SCHEMA'
1322
, 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
1423
, 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
1524
, 'CREATE VIEW', 'ALTER VIEW'
1625
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
1726
, 'CREATE FUNCTION', 'ALTER FUNCTION'
27+
, 'CREATE TRIGGER'
1828
, 'CREATE TYPE', 'ALTER TYPE'
1929
, 'CREATE RULE'
2030
, 'COMMENT'
2131
)
2232
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
2333
-- also exclude any objects inside Supabase schemas
24-
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
25-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
26-
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
34+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
2735
THEN
2836
NOTIFY pgrst, 'reload schema';
2937
END IF;
@@ -36,24 +44,31 @@ CREATE OR REPLACE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger
3644
AS $$
3745
DECLARE
3846
obj record;
47+
obj_schema text;
3948
BEGIN
4049
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
4150
LOOP
51+
-- triggers and rules have no schema of their own (schema_name is NULL),
52+
-- so use the schema of the table they belonged to
53+
obj_schema := CASE
54+
WHEN obj.object_type IN ('trigger', 'rule') THEN obj.address_names[1]
55+
ELSE obj.schema_name
56+
END;
57+
4258
IF obj.object_type IN (
4359
'schema'
4460
, 'table'
4561
, 'foreign table'
4662
, 'view'
4763
, 'materialized view'
4864
, 'function'
65+
, 'trigger'
4966
, 'type'
5067
, 'rule'
5168
)
5269
AND obj.is_temporary IS false -- no pg_temp objects
5370
-- also exclude any objects inside Supabase schemas
54-
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
55-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
56-
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
71+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
5772
THEN
5873
NOTIFY pgrst, 'reload schema';
5974
END IF;

‎migrations/schema-15.sql‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,25 +329,33 @@ CREATE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger
329329
AS $$
330330
DECLARE
331331
cmd record;
332+
obj_schema text;
332333
BEGIN
333334
FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
334335
LOOP
336+
-- triggers and rules have no schema of their own (schema_name is NULL),
337+
-- so use the schema of the table they belong to
338+
obj_schema := CASE
339+
WHEN cmd.object_type IN ('trigger', 'rule')
340+
THEN (pg_identify_object_as_address(cmd.classid, cmd.objid, cmd.objsubid)).object_names[1]
341+
ELSE cmd.schema_name
342+
END;
343+
335344
IF cmd.command_tag IN (
336345
'CREATE SCHEMA', 'ALTER SCHEMA'
337346
, 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
338347
, 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
339348
, 'CREATE VIEW', 'ALTER VIEW'
340349
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
341350
, 'CREATE FUNCTION', 'ALTER FUNCTION'
351+
, 'CREATE TRIGGER'
342352
, 'CREATE TYPE', 'ALTER TYPE'
343353
, 'CREATE RULE'
344354
, 'COMMENT'
345355
)
346356
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
347357
-- also exclude any objects inside Supabase schemas
348-
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
349-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
350-
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
358+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
351359
THEN
352360
NOTIFY pgrst, 'reload schema';
353361
END IF;
@@ -364,24 +372,31 @@ CREATE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger
364372
AS $$
365373
DECLARE
366374
obj record;
375+
obj_schema text;
367376
BEGIN
368377
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
369378
LOOP
379+
-- triggers and rules have no schema of their own (schema_name is NULL),
380+
-- so use the schema of the table they belonged to
381+
obj_schema := CASE
382+
WHEN obj.object_type IN ('trigger', 'rule') THEN obj.address_names[1]
383+
ELSE obj.schema_name
384+
END;
385+
370386
IF obj.object_type IN (
371387
'schema'
372388
, 'table'
373389
, 'foreign table'
374390
, 'view'
375391
, 'materialized view'
376392
, 'function'
393+
, 'trigger'
377394
, 'type'
378395
, 'rule'
379396
)
380397
AND obj.is_temporary IS false -- no pg_temp objects
381398
-- also exclude any objects inside Supabase schemas
382-
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
383-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
384-
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
399+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
385400
THEN
386401
NOTIFY pgrst, 'reload schema';
387402
END IF;

‎migrations/schema-17.sql‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,25 +330,33 @@ CREATE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger
330330
AS $$
331331
DECLARE
332332
cmd record;
333+
obj_schema text;
333334
BEGIN
334335
FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
335336
LOOP
337+
-- triggers and rules have no schema of their own (schema_name is NULL),
338+
-- so use the schema of the table they belong to
339+
obj_schema := CASE
340+
WHEN cmd.object_type IN ('trigger', 'rule')
341+
THEN (pg_identify_object_as_address(cmd.classid, cmd.objid, cmd.objsubid)).object_names[1]
342+
ELSE cmd.schema_name
343+
END;
344+
336345
IF cmd.command_tag IN (
337346
'CREATE SCHEMA', 'ALTER SCHEMA'
338347
, 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
339348
, 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
340349
, 'CREATE VIEW', 'ALTER VIEW'
341350
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
342351
, 'CREATE FUNCTION', 'ALTER FUNCTION'
352+
, 'CREATE TRIGGER'
343353
, 'CREATE TYPE', 'ALTER TYPE'
344354
, 'CREATE RULE'
345355
, 'COMMENT'
346356
)
347357
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
348358
-- also exclude any objects inside Supabase schemas
349-
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
350-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
351-
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
359+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
352360
THEN
353361
NOTIFY pgrst, 'reload schema';
354362
END IF;
@@ -365,24 +373,31 @@ CREATE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger
365373
AS $$
366374
DECLARE
367375
obj record;
376+
obj_schema text;
368377
BEGIN
369378
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
370379
LOOP
380+
-- triggers and rules have no schema of their own (schema_name is NULL),
381+
-- so use the schema of the table they belonged to
382+
obj_schema := CASE
383+
WHEN obj.object_type IN ('trigger', 'rule') THEN obj.address_names[1]
384+
ELSE obj.schema_name
385+
END;
386+
371387
IF obj.object_type IN (
372388
'schema'
373389
, 'table'
374390
, 'foreign table'
375391
, 'view'
376392
, 'materialized view'
377393
, 'function'
394+
, 'trigger'
378395
, 'type'
379396
, 'rule'
380397
)
381398
AND obj.is_temporary IS false -- no pg_temp objects
382399
-- also exclude any objects inside Supabase schemas
383-
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
384-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
385-
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
400+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
386401
THEN
387402
NOTIFY pgrst, 'reload schema';
388403
END IF;

‎migrations/schema-orioledb-17.sql‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -344,25 +344,33 @@ CREATE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger
344344
AS $$
345345
DECLARE
346346
cmd record;
347+
obj_schema text;
347348
BEGIN
348349
FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
349350
LOOP
351+
-- triggers and rules have no schema of their own (schema_name is NULL),
352+
-- so use the schema of the table they belong to
353+
obj_schema := CASE
354+
WHEN cmd.object_type IN ('trigger', 'rule')
355+
THEN (pg_identify_object_as_address(cmd.classid, cmd.objid, cmd.objsubid)).object_names[1]
356+
ELSE cmd.schema_name
357+
END;
358+
350359
IF cmd.command_tag IN (
351360
'CREATE SCHEMA', 'ALTER SCHEMA'
352361
, 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
353362
, 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
354363
, 'CREATE VIEW', 'ALTER VIEW'
355364
, 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
356365
, 'CREATE FUNCTION', 'ALTER FUNCTION'
366+
, 'CREATE TRIGGER'
357367
, 'CREATE TYPE', 'ALTER TYPE'
358368
, 'CREATE RULE'
359369
, 'COMMENT'
360370
)
361371
-- don't notify in case of CREATE TEMP table or other objects created on pg_temp
362372
-- also exclude any objects inside Supabase schemas
363-
AND COALESCE(cmd.schema_name, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
364-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
365-
OR (cmd.command_tag = 'CREATE TRIGGER' AND cmd.object_identity NOT LIKE '% pg\_temp.%')
373+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
366374
THEN
367375
NOTIFY pgrst, 'reload schema';
368376
END IF;
@@ -379,24 +387,31 @@ CREATE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger
379387
AS $$
380388
DECLARE
381389
obj record;
390+
obj_schema text;
382391
BEGIN
383392
FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
384393
LOOP
394+
-- triggers and rules have no schema of their own (schema_name is NULL),
395+
-- so use the schema of the table they belonged to
396+
obj_schema := CASE
397+
WHEN obj.object_type IN ('trigger', 'rule') THEN obj.address_names[1]
398+
ELSE obj.schema_name
399+
END;
400+
385401
IF obj.object_type IN (
386402
'schema'
387403
, 'table'
388404
, 'foreign table'
389405
, 'view'
390406
, 'materialized view'
391407
, 'function'
408+
, 'trigger'
392409
, 'type'
393410
, 'rule'
394411
)
395412
AND obj.is_temporary IS false -- no pg_temp objects
396413
-- also exclude any objects inside Supabase schemas
397-
AND COALESCE(obj.schema_name, '') NOT IN ('auth', 'realtime', '_realtime', 'storage')
398-
-- Exclude any trigger created on temp tables (object_identity = 'trigger on pg_temp.table')
399-
OR ( obj.object_type = 'trigger' AND obj.object_identity NOT LIKE '% pg\_temp.%')
414+
AND COALESCE(obj_schema, '') NOT IN ('pg_temp', 'auth', 'realtime', '_realtime', 'storage')
400415
THEN
401416
NOTIFY pgrst, 'reload schema';
402417
END IF;

‎nix/checks.nix‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
"output_plugin_libraries" # needs wal_level=logical + logical-decoding infra, not exercised in the CLI variant
224224
"btree_gist_nan" # needs btree_gist, not in the CLI prime file
225225
"hstore_copy_binary" # needs hstore
226+
"pgrst_watch_triggers" # needs dblink, not in the CLI prime file
226227
# Version-specific extension tests
227228
"z_17_ext_interface"
228229
"z_17_pg_stat_monitor"

0 commit comments

Comments
 (0)