prostgles-server 3.0.99 → 3.0.101

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/AuthHandler.js +6 -6
  2. package/dist/AuthHandler.js.map +1 -1
  3. package/dist/DboBuilder/ViewHandler.js +0 -1
  4. package/dist/DboBuilder/ViewHandler.js.map +1 -1
  5. package/dist/DboBuilder/subscribe.d.ts.map +1 -1
  6. package/dist/DboBuilder/subscribe.js +0 -17
  7. package/dist/DboBuilder/subscribe.js.map +1 -1
  8. package/dist/JSONBValidation/validate_jsonb_schema_sql.d.ts +1 -1
  9. package/dist/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -1
  10. package/dist/JSONBValidation/validate_jsonb_schema_sql.js +8 -2
  11. package/dist/JSONBValidation/validate_jsonb_schema_sql.js.map +1 -1
  12. package/dist/PubSubManager/PubSubManager.d.ts +4 -7
  13. package/dist/PubSubManager/PubSubManager.d.ts.map +1 -1
  14. package/dist/PubSubManager/PubSubManager.js +13 -19
  15. package/dist/PubSubManager/PubSubManager.js.map +1 -1
  16. package/dist/PubSubManager/getInitQuery.d.ts +9 -0
  17. package/dist/PubSubManager/getInitQuery.d.ts.map +1 -0
  18. package/dist/PubSubManager/getInitQuery.js +537 -0
  19. package/dist/PubSubManager/getInitQuery.js.map +1 -0
  20. package/dist/PubSubManager/initPubSubManager.d.ts.map +1 -1
  21. package/dist/PubSubManager/initPubSubManager.js +46 -577
  22. package/dist/PubSubManager/initPubSubManager.js.map +1 -1
  23. package/dist/SchemaWatch.js +1 -0
  24. package/dist/SchemaWatch.js.map +1 -1
  25. package/lib/AuthHandler.js +6 -6
  26. package/lib/AuthHandler.ts +7 -7
  27. package/lib/DboBuilder/ViewHandler.js +0 -1
  28. package/lib/DboBuilder/ViewHandler.ts +1 -1
  29. package/lib/DboBuilder/subscribe.d.ts.map +1 -1
  30. package/lib/DboBuilder/subscribe.js +0 -17
  31. package/lib/DboBuilder/subscribe.ts +0 -25
  32. package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts +1 -1
  33. package/lib/JSONBValidation/validate_jsonb_schema_sql.d.ts.map +1 -1
  34. package/lib/JSONBValidation/validate_jsonb_schema_sql.js +8 -2
  35. package/lib/JSONBValidation/validate_jsonb_schema_sql.ts +8 -2
  36. package/lib/PubSubManager/PubSubManager.d.ts +4 -7
  37. package/lib/PubSubManager/PubSubManager.d.ts.map +1 -1
  38. package/lib/PubSubManager/PubSubManager.js +16 -19
  39. package/lib/PubSubManager/PubSubManager.ts +18 -20
  40. package/lib/PubSubManager/getInitQuery.d.ts +9 -0
  41. package/lib/PubSubManager/getInitQuery.d.ts.map +1 -0
  42. package/lib/PubSubManager/getInitQuery.js +536 -0
  43. package/lib/PubSubManager/getInitQuery.ts +538 -0
  44. package/lib/PubSubManager/initPubSubManager.d.ts.map +1 -1
  45. package/lib/PubSubManager/initPubSubManager.js +46 -577
  46. package/lib/PubSubManager/initPubSubManager.ts +48 -585
  47. package/lib/SchemaWatch.js +1 -0
  48. package/lib/SchemaWatch.ts +1 -1
  49. package/package.json +3 -3
  50. package/tests/client/PID.txt +1 -1
  51. package/tests/server/package-lock.json +1 -1
@@ -0,0 +1,538 @@
1
+
2
+ import { asValue, log, PubSubManager } from "./PubSubManager";
3
+ const { version } = require("../../package.json");
4
+
5
+ export const DB_OBJ_NAMES = {
6
+ trigger_add_remove_func: "prostgles.trigger_add_remove_func",
7
+ data_watch_func: "prostgles.prostgles_trigger_function",
8
+ schema_watch_func: "prostgles.schema_watch_func",
9
+ schema_watch_trigger: "prostgles_schema_watch_trigger_new"
10
+ } as const;
11
+
12
+ export const getInitQuery = async function(this: PubSubManager): Promise<string> {
13
+
14
+ const getQuery = async (withoutHash = false): Promise<string> => {
15
+ const { schema_md5 = "none" } = withoutHash? {} : await this.db.oneOrNone("SELECT md5($1) as schema_md5", [await getQuery(true)]);
16
+
17
+ return `
18
+
19
+ BEGIN; -- ISOLATION LEVEL SERIALIZABLE;-- TRANSACTION ISOLATION LEVEL SERIALIZABLE;
20
+
21
+ --SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
22
+
23
+ /*
24
+ * ${PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
25
+ */
26
+
27
+ DO
28
+ $do$
29
+ BEGIN
30
+
31
+ /* Reduce deadlocks */
32
+ PERFORM pg_sleep(random());
33
+
34
+ /* Drop older version. */
35
+ IF EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'prostgles') THEN
36
+
37
+ /* The seemingly useless IF nesting is done to prevent pg evaluating the entire condition and throw a 'schema_md5 column does not exist' */
38
+ IF
39
+ /* Cannot check schema version */
40
+ NOT EXISTS(
41
+ SELECT 1
42
+ FROM information_schema.columns
43
+ WHERE table_schema = 'prostgles'
44
+ AND table_name = 'versions'
45
+ AND column_name = 'schema_md5'
46
+ )
47
+ THEN
48
+ DROP SCHEMA IF EXISTS prostgles CASCADE;
49
+ ELSIF
50
+ /* There is no newer schema */
51
+ NOT EXISTS(
52
+ SELECT 1
53
+ FROM prostgles.versions
54
+ WHERE schema_md5 <> ${asValue(schema_md5)}
55
+ AND version >= ${asValue(version)}
56
+ )
57
+ THEN
58
+ DROP SCHEMA IF EXISTS prostgles CASCADE;
59
+
60
+ END IF;
61
+
62
+ END IF;
63
+
64
+
65
+ IF NOT EXISTS (
66
+ SELECT 1
67
+ FROM information_schema.schemata
68
+ WHERE schema_name = 'prostgles'
69
+ )
70
+ THEN
71
+
72
+ CREATE SCHEMA IF NOT EXISTS prostgles;
73
+ COMMENT ON SCHEMA prostgles IS 'Used by prostgles-server to enable data/schema change tracking through subscribe/sync/watchSchema';
74
+
75
+ CREATE TABLE IF NOT EXISTS prostgles.versions(
76
+ version TEXT PRIMARY KEY,
77
+ schema_md5 TEXT NOT NULL
78
+ );
79
+ COMMENT ON TABLE prostgles.versions IS 'Stores the prostgles schema creation query hash and package version number to identify when a newer schema needs to be re-created';
80
+
81
+ INSERT INTO prostgles.versions(version, schema_md5)
82
+ VALUES(${asValue(version)}, ${asValue(schema_md5)})
83
+ ON CONFLICT DO NOTHING;
84
+
85
+
86
+ CREATE OR REPLACE FUNCTION prostgles.random_string(length INTEGER DEFAULT 33) RETURNS TEXT AS $$
87
+ DECLARE
88
+ chars TEXT[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
89
+ result TEXT := '';
90
+ i INTEGER := 0;
91
+ BEGIN
92
+
93
+ IF length < 0 THEN
94
+ RAISE exception 'Given length cannot be less than 0';
95
+ END IF;
96
+
97
+ FOR i IN 1..length LOOP
98
+ result := result || chars[1+random()*(array_length(chars, 1)-1)];
99
+ END LOOP;
100
+
101
+ RETURN result;
102
+
103
+ END;
104
+ $$ language plpgsql;
105
+ COMMENT ON FUNCTION prostgles.random_string IS 'UUIDs without installing pgcrypto';
106
+
107
+
108
+ CREATE OR REPLACE FUNCTION prostgles.debug(VARIADIC args TEXT[]) RETURNS VOID AS $$
109
+ BEGIN
110
+
111
+ --PERFORM pg_notify('debug', concat_ws(' ', args));
112
+ IF
113
+ NOT EXISTS (
114
+ SELECT 1
115
+ FROM information_schema.tables
116
+ WHERE table_schema = 'prostgles'
117
+ AND table_name = 'debug'
118
+ )
119
+ THEN
120
+ CREATE TABLE IF NOT EXISTS prostgles.debug(m TEXT);
121
+ END IF;
122
+
123
+ INSERT INTO prostgles.debug(m) VALUES(concat_ws(' ', args));
124
+
125
+ END;
126
+ $$ LANGUAGE plpgsql;
127
+ COMMENT ON FUNCTION prostgles.debug IS 'Used for internal debugging';
128
+
129
+
130
+ CREATE TABLE IF NOT EXISTS prostgles.apps (
131
+ id TEXT PRIMARY KEY DEFAULT prostgles.random_string(),
132
+ added TIMESTAMP DEFAULT NOW(),
133
+ application_name TEXT,
134
+ last_check TIMESTAMP NOT NULL DEFAULT NOW(),
135
+ last_check_ended TIMESTAMP NOT NULL DEFAULT NOW(),
136
+ watching_schema BOOLEAN DEFAULT FALSE,
137
+ check_frequency_ms INTEGER NOT NULL
138
+ );
139
+ COMMENT ON TABLE prostgles.apps IS 'Keep track of prostgles server apps connected to db to combine common triggers. Heartbeat used due to no logout triggers in postgres';
140
+
141
+ CREATE TABLE IF NOT EXISTS prostgles.app_triggers (
142
+ app_id TEXT NOT NULL,
143
+ table_name TEXT NOT NULL,
144
+ condition TEXT NOT NULL,
145
+
146
+ /* The view from the root subscription, found in the condition.
147
+ We need this because old_table/new_table data is not reflected in the view inside the AFTER trigger
148
+ */
149
+ related_view_name TEXT,
150
+ related_view_def TEXT, /* view definition */
151
+
152
+ inserted TIMESTAMP NOT NULL DEFAULT NOW(),
153
+ last_used TIMESTAMP NOT NULL DEFAULT NOW(),
154
+ PRIMARY KEY (app_id, table_name, condition) /* This unqique index limits the condition column value to be less than 'SELECT current_setting('block_size'); */
155
+ );
156
+ COMMENT ON TABLE prostgles.app_triggers IS 'Tables and conditions that are currently subscribed/synced';
157
+
158
+
159
+ CREATE OR REPLACE VIEW prostgles.v_triggers AS
160
+ SELECT *
161
+ , (ROW_NUMBER() OVER( ORDER BY table_name, condition ))::text AS id
162
+ , ROW_NUMBER() OVER(PARTITION BY app_id, table_name ORDER BY table_name, condition ) - 1 AS c_id
163
+ FROM prostgles.app_triggers;
164
+ COMMENT ON VIEW prostgles.v_triggers IS 'Augment trigger table with natural IDs and per app IDs';
165
+
166
+
167
+ CREATE OR REPLACE FUNCTION ${DB_OBJ_NAMES.data_watch_func}() RETURNS TRIGGER
168
+ AS $$
169
+
170
+ DECLARE t_ids TEXT[];
171
+ DECLARE c_ids INTEGER[];
172
+ DECLARE err_c_ids INTEGER[];
173
+ DECLARE unions TEXT := '';
174
+ DECLARE query TEXT := '';
175
+ DECLARE nrw RECORD;
176
+ DECLARE erw RECORD;
177
+ DECLARE has_errors BOOLEAN := FALSE;
178
+
179
+ DECLARE err_text TEXT;
180
+ DECLARE err_detail TEXT;
181
+ DECLARE err_hint TEXT;
182
+
183
+ DECLARE view_def_query TEXT := '';
184
+
185
+ BEGIN
186
+
187
+ -- PERFORM pg_notify('debug', concat_ws(' ', 'TABLE', TG_TABLE_NAME, TG_OP));
188
+
189
+ SELECT string_agg(
190
+ format(
191
+ $c$
192
+ SELECT CASE WHEN EXISTS(
193
+ SELECT 1 FROM %I WHERE %s
194
+ ) THEN %s::text END AS t_ids
195
+ $c$,
196
+ table_name,
197
+ condition,
198
+ id
199
+ ),
200
+ E' UNION \n '
201
+ )
202
+ INTO unions
203
+ FROM prostgles.v_triggers
204
+ WHERE table_name = TG_TABLE_NAME;
205
+
206
+
207
+ /* unions = 'old_table union new_table' or any one of the tables */
208
+ IF unions IS NOT NULL THEN
209
+
210
+ SELECT
211
+ format(
212
+ E'WITH %I AS (\n %s \n) ',
213
+ TG_TABLE_NAME,
214
+ concat_ws(
215
+ E' UNION ALL \n ',
216
+ CASE WHEN (TG_OP = 'DELETE' OR TG_OP = 'UPDATE') THEN ' SELECT * FROM old_table ' END,
217
+ CASE WHEN (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN ' SELECT * FROM new_table ' END
218
+ )
219
+ )
220
+ ||
221
+ COALESCE((
222
+ SELECT ', ' || string_agg(format(E' %I AS ( \n %s \n ) ', related_view_name, related_view_def), ', ')
223
+ FROM (
224
+ SELECT DISTINCT related_view_name, related_view_def
225
+ FROM prostgles.v_triggers
226
+ WHERE table_name = TG_TABLE_NAME
227
+ AND related_view_name IS NOT NULL
228
+ AND related_view_def IS NOT NULL
229
+ ) t
230
+ ), '')
231
+ ||
232
+ format(
233
+ $c$
234
+ SELECT ARRAY_AGG(DISTINCT t.t_ids)
235
+ FROM (
236
+ %s
237
+ ) t
238
+ $c$, unions
239
+ )
240
+ INTO query;
241
+
242
+ BEGIN
243
+ EXECUTE query INTO t_ids;
244
+
245
+ --RAISE NOTICE 'trigger fired ok';
246
+
247
+ EXCEPTION WHEN OTHERS THEN
248
+
249
+ has_errors := TRUE;
250
+
251
+ GET STACKED DIAGNOSTICS
252
+ err_text = MESSAGE_TEXT,
253
+ err_detail = PG_EXCEPTION_DETAIL,
254
+ err_hint = PG_EXCEPTION_HINT;
255
+
256
+
257
+ END;
258
+
259
+ --RAISE NOTICE 'has_errors: % ', has_errors;
260
+ --RAISE NOTICE 'unions: % , cids: %', unions, c_ids;
261
+
262
+ IF (t_ids IS NOT NULL OR has_errors) THEN
263
+
264
+ FOR nrw IN
265
+ SELECT app_id, string_agg(c_id::text, ',') as cids
266
+ FROM prostgles.v_triggers
267
+ WHERE id = ANY(t_ids)
268
+ OR has_errors
269
+ GROUP BY app_id
270
+ LOOP
271
+
272
+ PERFORM pg_notify(
273
+ ${asValue(this.NOTIF_CHANNEL.preffix)} || nrw.app_id ,
274
+ concat_ws(
275
+ ${asValue(PubSubManager.DELIMITER)},
276
+
277
+ ${asValue(this.NOTIF_TYPE.data)},
278
+ COALESCE(TG_TABLE_NAME, 'MISSING'),
279
+ COALESCE(TG_OP, 'MISSING'),
280
+ CASE WHEN has_errors
281
+ THEN concat_ws('; ', 'error', err_text, err_detail, err_hint, 'query: ' || query )
282
+ ELSE COALESCE(nrw.cids, '')
283
+ END
284
+ ${this.dboBuilder.prostgles.opts.DEBUG_MODE? (", (select json_agg(t)::TEXT FROM (SELECT * from old_table) t), query") : ""}
285
+ )
286
+ );
287
+ END LOOP;
288
+
289
+
290
+ IF has_errors THEN
291
+
292
+ DELETE FROM prostgles.app_triggers;
293
+ RAISE NOTICE 'trigger dropped due to exception: % % %', err_text, err_detail, err_hint;
294
+
295
+ END IF;
296
+
297
+
298
+ END IF;
299
+ END IF;
300
+
301
+
302
+ RETURN NULL;
303
+
304
+ /*
305
+ EXCEPTION WHEN OTHERS THEN
306
+ DELETE FROM prostgles.app_triggers; -- delete all or will need to loop through all conditions to find issue;
307
+ RAISE NOTICE 'trigger dropped due to exception';
308
+ ${"--EXCEPTION_WHEN_COLUMN_WAS_RENAMED_THEN_DROP_TRIGGER"};
309
+
310
+
311
+
312
+ RETURN NULL;
313
+ */
314
+ END;
315
+
316
+ --COMMIT;
317
+ $$ LANGUAGE plpgsql;
318
+ COMMENT ON FUNCTION ${DB_OBJ_NAMES.data_watch_func} IS 'Prostgles internal function used to notify when data in the table changed';
319
+
320
+
321
+
322
+ CREATE OR REPLACE FUNCTION ${DB_OBJ_NAMES.trigger_add_remove_func}() RETURNS TRIGGER
323
+ AS $$
324
+
325
+ DECLARE operations TEXT[] := ARRAY['insert', 'update', 'delete'];
326
+ DECLARE op TEXT;
327
+ DECLARE query TEXT;
328
+ DECLARE trw RECORD;
329
+
330
+ BEGIN
331
+
332
+
333
+ --RAISE NOTICE 'prostgles.app_triggers % ', TG_OP;
334
+
335
+ /* If no other listeners on table then DROP triggers */
336
+ IF TG_OP = 'DELETE' THEN
337
+
338
+ --RAISE NOTICE 'DELETE trigger_add_remove_func table: % ', ' ' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM prostgles.app_triggers) , ' 0 ');
339
+ --RAISE NOTICE 'DELETE trigger_add_remove_func old_table: % ', '' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM old_table), ' 0 ');
340
+
341
+
342
+ /* Drop actual triggers if needed */
343
+ FOR trw IN
344
+ SELECT DISTINCT table_name FROM old_table ot
345
+ WHERE NOT EXISTS (
346
+ SELECT 1 FROM prostgles.app_triggers t
347
+ WHERE t.table_name = ot.table_name
348
+ )
349
+ LOOP
350
+
351
+ FOREACH op IN ARRAY operations
352
+ LOOP
353
+ --RAISE NOTICE ' DROP DATA TRIGGER FOR: % ', trw.table_name;
354
+ EXECUTE format(' DROP TRIGGER IF EXISTS %I ON %I ;' , 'prostgles_triggers_' || trw.table_name || '_' || op, trw.table_name);
355
+ END LOOP;
356
+
357
+ END LOOP;
358
+
359
+ /* If newly added listeners on table then CREATE triggers */
360
+ ELSIF TG_OP = 'INSERT' THEN
361
+
362
+
363
+ --RAISE NOTICE 'INSERT trigger_add_remove_func table: % ', ' ' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM prostgles.triggers) , ' 0 ');
364
+ --RAISE NOTICE 'INSERT trigger_add_remove_func new_table: % ', '' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM new_table), ' 0 ');
365
+
366
+ /* Loop through newly added tables */
367
+ FOR trw IN
368
+
369
+ SELECT DISTINCT table_name
370
+ FROM new_table nt
371
+
372
+ /* Table did not exist prior to this insert */
373
+ WHERE NOT EXISTS (
374
+ SELECT 1
375
+ FROM prostgles.app_triggers t
376
+ WHERE t.table_name = nt.table_name
377
+ AND t.inserted < nt.inserted -- exclude current record (this is an after trigger). Turn into before trigger?
378
+ )
379
+
380
+ /* Table is valid */
381
+ AND EXISTS (
382
+ SELECT 1
383
+ FROM information_schema.tables
384
+ WHERE table_schema = 'public'
385
+ AND table_name = nt.table_name
386
+ )
387
+ LOOP
388
+
389
+ /*
390
+ RAISE NOTICE ' CREATE DATA TRIGGER FOR: % TABLE EXISTS?', trw.table_name, SELECT EXISTS (
391
+ SELECT 1
392
+ FROM information_schema.tables
393
+ WHERE table_schema = 'public'
394
+ AND table_name = nt.table_name
395
+ );
396
+ */
397
+
398
+ query := format(
399
+ $q$
400
+ DROP TRIGGER IF EXISTS %1$I ON %2$I;
401
+ CREATE TRIGGER %1$I
402
+ AFTER INSERT ON %2$I
403
+ REFERENCING NEW TABLE AS new_table
404
+ FOR EACH STATEMENT EXECUTE PROCEDURE ${DB_OBJ_NAMES.data_watch_func}();
405
+ COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
406
+ $q$,
407
+ 'prostgles_triggers_' || trw.table_name || '_insert', trw.table_name
408
+ ) || format(
409
+ $q$
410
+ DROP TRIGGER IF EXISTS %1$I ON %2$I;
411
+ CREATE TRIGGER %1$I
412
+ AFTER UPDATE ON %2$I
413
+ REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
414
+ FOR EACH STATEMENT EXECUTE PROCEDURE ${DB_OBJ_NAMES.data_watch_func}();
415
+ COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
416
+ $q$,
417
+ 'prostgles_triggers_' || trw.table_name || '_update', trw.table_name
418
+ ) || format(
419
+ $q$
420
+ DROP TRIGGER IF EXISTS %1$I ON %2$I;
421
+ CREATE TRIGGER %1$I
422
+ AFTER DELETE ON %2$I
423
+ REFERENCING OLD TABLE AS old_table
424
+ FOR EACH STATEMENT EXECUTE PROCEDURE ${DB_OBJ_NAMES.data_watch_func}();
425
+ COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
426
+ $q$,
427
+ 'prostgles_triggers_' || trw.table_name || '_delete', trw.table_name
428
+ );
429
+
430
+ --RAISE NOTICE ' % ', query;
431
+
432
+
433
+ query := format(
434
+ $q$
435
+ DO $e$
436
+ BEGIN
437
+
438
+ IF EXISTS (
439
+ SELECT 1
440
+ FROM information_schema.tables
441
+ WHERE table_schema = 'public'
442
+ AND table_name = %L
443
+ ) THEN
444
+
445
+ %s
446
+
447
+ END IF;
448
+
449
+ END $e$;
450
+ $q$,
451
+ trw.table_name,
452
+ query
453
+ ) ;
454
+
455
+
456
+ EXECUTE query;
457
+
458
+ END LOOP;
459
+
460
+ END IF;
461
+
462
+
463
+ RETURN NULL;
464
+ END;
465
+
466
+ $$ LANGUAGE plpgsql;
467
+ COMMENT ON FUNCTION ${DB_OBJ_NAMES.trigger_add_remove_func} IS 'Used to add/remove table watch triggers concurrently ';
468
+
469
+ DROP TRIGGER IF EXISTS prostgles_triggers_insert ON prostgles.app_triggers;
470
+ CREATE TRIGGER prostgles_triggers_insert
471
+ AFTER INSERT ON prostgles.app_triggers
472
+ REFERENCING NEW TABLE AS new_table
473
+ FOR EACH STATEMENT EXECUTE PROCEDURE ${DB_OBJ_NAMES.trigger_add_remove_func}();
474
+
475
+ DROP TRIGGER IF EXISTS prostgles_triggers_delete ON prostgles.app_triggers;
476
+ CREATE TRIGGER prostgles_triggers_delete
477
+ AFTER DELETE ON prostgles.app_triggers
478
+ REFERENCING OLD TABLE AS old_table
479
+ FOR EACH STATEMENT EXECUTE PROCEDURE ${DB_OBJ_NAMES.trigger_add_remove_func}();
480
+
481
+
482
+ CREATE OR REPLACE FUNCTION ${DB_OBJ_NAMES.schema_watch_func}() RETURNS event_trigger AS $$
483
+
484
+ DECLARE curr_query TEXT := '';
485
+ DECLARE arw RECORD;
486
+
487
+ BEGIN
488
+
489
+ --RAISE NOTICE 'SCHEMA_WATCH: %', tg_tag;
490
+
491
+ /*
492
+ This event trigger will outlive a prostgles app instance.
493
+ Must ensure it only fires if an app instance is running
494
+ */
495
+ IF
496
+ EXISTS (
497
+ SELECT 1
498
+ FROM information_schema.tables
499
+ WHERE table_schema = 'prostgles'
500
+ AND table_name = 'apps'
501
+ )
502
+ THEN
503
+
504
+ SELECT LEFT(COALESCE(current_query(), ''), 5000)
505
+ INTO curr_query;
506
+
507
+ FOR arw IN
508
+ SELECT * FROM prostgles.apps WHERE watching_schema IS TRUE
509
+
510
+ LOOP
511
+ PERFORM pg_notify(
512
+ ${asValue(this.NOTIF_CHANNEL.preffix)} || arw.id,
513
+ concat_ws(
514
+ ${asValue(PubSubManager.DELIMITER)},
515
+ ${asValue(this.NOTIF_TYPE.schema)}, tg_tag , TG_event, curr_query
516
+ )
517
+ );
518
+ END LOOP;
519
+
520
+ END IF;
521
+
522
+ END;
523
+ $$ LANGUAGE plpgsql;
524
+ COMMENT ON FUNCTION ${DB_OBJ_NAMES.schema_watch_func} IS 'Prostgles internal function used to notify when schema has changed';
525
+
526
+ END IF;
527
+
528
+ END
529
+ $do$;
530
+
531
+
532
+ COMMIT;
533
+ `};
534
+
535
+ const res = getQuery();
536
+
537
+ return res;
538
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"initPubSubManager.d.ts","sourceRoot":"","sources":["initPubSubManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAM9D,wBAAsB,iBAAiB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CA4oB/F"}
1
+ {"version":3,"file":"initPubSubManager.d.ts","sourceRoot":"","sources":["initPubSubManager.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAI9D,wBAAsB,iBAAiB,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CAqH/F"}