forked from graphile/graphile-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch-fixtures.sql
100 lines (93 loc) · 2.96 KB
/
watch-fixtures.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
-- @see https://github.com/graphile/postgraphile/blob/886f8752f03d3fa05bdbdd97eeabb153a4d0343e/resources/watch-fixtures.sql
-- Adds the functionality for PostGraphile to watch the database for schema
-- changes. This script is idempotent, you can run it as many times as you
-- would like.
-- Drop the `postgraphile_watch` schema and all of its dependant objects
-- including the event trigger function and the event trigger itself. We will
-- recreate those objects in this script.
drop schema if exists postgraphile_watch cascade;
-- Create a schema for the PostGraphile watch functionality. This schema will
-- hold things like trigger functions that are used to implement schema
-- watching.
create schema postgraphile_watch;
create function postgraphile_watch.notify_watchers_ddl() returns event_trigger as $$
begin
perform pg_notify(
'postgraphile_watch',
json_build_object(
'type',
'ddl',
'payload',
(select json_agg(json_build_object('schema', schema_name, 'command', command_tag)) from pg_event_trigger_ddl_commands() as x)
)::text
);
end;
$$ language plpgsql;
create function postgraphile_watch.notify_watchers_drop() returns event_trigger as $$
begin
perform pg_notify(
'postgraphile_watch',
json_build_object(
'type',
'drop',
'payload',
(select json_agg(distinct x.schema_name) from pg_event_trigger_dropped_objects() as x)
)::text
);
end;
$$ language plpgsql;
-- Create an event trigger which will listen for the completion of all DDL
-- events and report that they happened to PostGraphile. Events are selected by
-- whether or not they modify the static definition of `pg_catalog` that
-- `introspection-query.sql` queries.
create event trigger postgraphile_watch_ddl
on ddl_command_end
when tag in (
-- Ref: https://www.postgresql.org/docs/10/static/event-trigger-matrix.html
'ALTER AGGREGATE',
'ALTER DOMAIN',
'ALTER EXTENSION',
'ALTER FOREIGN TABLE',
'ALTER FUNCTION',
'ALTER POLICY',
'ALTER SCHEMA',
'ALTER TABLE',
'ALTER TYPE',
'ALTER VIEW',
'COMMENT',
'CREATE AGGREGATE',
'CREATE DOMAIN',
'CREATE EXTENSION',
'CREATE FOREIGN TABLE',
'CREATE FUNCTION',
'CREATE INDEX',
'CREATE POLICY',
'CREATE RULE',
'CREATE SCHEMA',
'CREATE TABLE',
'CREATE TABLE AS',
'CREATE VIEW',
'DROP AGGREGATE',
'DROP DOMAIN',
'DROP EXTENSION',
'DROP FOREIGN TABLE',
'DROP FUNCTION',
'DROP INDEX',
'DROP OWNED',
'DROP POLICY',
'DROP RULE',
'DROP SCHEMA',
'DROP TABLE',
'DROP TYPE',
'DROP VIEW',
'GRANT',
'REVOKE',
'SELECT INTO'
)
execute procedure postgraphile_watch.notify_watchers_ddl();
-- Create an event trigger which will listen for drop events because on drops
-- the DDL method seems to get nothing returned from
-- pg_event_trigger_ddl_commands()
create event trigger postgraphile_watch_drop
on sql_drop
execute procedure postgraphile_watch.notify_watchers_drop();