prostgles-server 3.0.99 → 3.0.100

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 +537 -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
@@ -4,515 +4,13 @@ exports.initPubSubManager = void 0;
4
4
  const PostgresNotifListenManager_1 = require("../PostgresNotifListenManager");
5
5
  const PubSubManager_1 = require("./PubSubManager");
6
6
  const REALTIME_TRIGGER_CHECK_QUERY = "prostgles-server internal query used to manage realtime triggers";
7
- const validate_jsonb_schema_sql_1 = require("../JSONBValidation/validate_jsonb_schema_sql");
7
+ const getInitQuery_1 = require("./getInitQuery");
8
8
  async function initPubSubManager() {
9
9
  if (!this.canContinue())
10
10
  return undefined;
11
11
  let tries = 5;
12
12
  try {
13
- const schema_version = 10;
14
- const initQuery = `
15
- BEGIN; -- ISOLATION LEVEL SERIALIZABLE;-- TRANSACTION ISOLATION LEVEL SERIALIZABLE;
16
-
17
- --SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
18
-
19
- /*
20
- * ${PubSubManager_1.PubSubManager.EXCLUDE_QUERY_FROM_SCHEMA_WATCH_ID}
21
- */
22
-
23
- DO
24
- $do$
25
- BEGIN
26
-
27
- /* Reduce deadlocks */
28
- PERFORM pg_sleep(random());
29
-
30
- /* Drop older version */
31
- IF EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'prostgles') THEN
32
-
33
- IF
34
- NOT EXISTS (
35
- SELECT 1
36
- FROM information_schema.tables
37
- WHERE table_schema = 'prostgles'
38
- AND table_name = 'versions'
39
- )
40
- THEN
41
- DROP SCHEMA IF EXISTS prostgles CASCADE;
42
- ELSE
43
- IF NOT EXISTS(SELECT 1 FROM prostgles.versions WHERE version >= ${schema_version}) THEN
44
- DROP SCHEMA IF EXISTS prostgles CASCADE;
45
- END IF;
46
- END IF;
47
-
48
- END IF;
49
-
50
-
51
- IF NOT EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'prostgles')
52
- THEN
53
- --RAISE NOTICE 'CREATE SCHEMA IF NOT EXISTS prostgles';
54
-
55
- CREATE SCHEMA IF NOT EXISTS prostgles;
56
-
57
- CREATE TABLE IF NOT EXISTS prostgles.versions(
58
- version NUMERIC PRIMARY KEY
59
- );
60
- INSERT INTO prostgles.versions(version) VALUES(${schema_version}) ON CONFLICT DO NOTHING;
61
-
62
- ${validate_jsonb_schema_sql_1.validate_jsonb_schema_sql}
63
-
64
- CREATE OR REPLACE FUNCTION prostgles.random_string(length INTEGER DEFAULT 33) RETURNS TEXT AS $$
65
- DECLARE
66
- 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}';
67
- result TEXT := '';
68
- i INTEGER := 0;
69
- BEGIN
70
- IF length < 0 THEN
71
- RAISE exception 'Given length cannot be less than 0';
72
- END IF;
73
- FOR i IN 1..length LOOP
74
- result := result || chars[1+random()*(array_length(chars, 1)-1)];
75
- END LOOP;
76
- RETURN result;
77
- END;
78
- $$ language plpgsql;
79
- COMMENT ON FUNCTION prostgles.random_string IS 'UUIDs without installing pgcrypto';
80
-
81
-
82
- CREATE OR REPLACE FUNCTION prostgles.debug(VARIADIC args TEXT[]) RETURNS VOID AS $$
83
- BEGIN
84
-
85
- --PERFORM pg_notify('debug', concat_ws(' ', args));
86
- IF
87
- NOT EXISTS (
88
- SELECT 1
89
- FROM information_schema.tables
90
- WHERE table_schema = 'prostgles'
91
- AND table_name = 'debug'
92
- )
93
- THEN
94
- CREATE TABLE IF NOT EXISTS prostgles.debug(m TEXT);
95
- END IF;
96
-
97
- INSERT INTO prostgles.debug(m) VALUES(concat_ws(' ', args));
98
-
99
- END;
100
- $$ LANGUAGE plpgsql;
101
- COMMENT ON FUNCTION prostgles.debug IS 'Used for internal debugging';
102
-
103
-
104
- CREATE TABLE IF NOT EXISTS prostgles.apps (
105
- id TEXT PRIMARY KEY DEFAULT prostgles.random_string(),
106
- added TIMESTAMP DEFAULT NOW(),
107
- application_name TEXT,
108
- last_check TIMESTAMP NOT NULL DEFAULT NOW(),
109
- last_check_ended TIMESTAMP NOT NULL DEFAULT NOW(),
110
- watching_schema BOOLEAN DEFAULT FALSE,
111
- check_frequency_ms INTEGER NOT NULL
112
- );
113
- 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';
114
-
115
- CREATE TABLE IF NOT EXISTS prostgles.app_triggers (
116
- app_id TEXT NOT NULL,
117
- table_name TEXT NOT NULL,
118
- condition TEXT NOT NULL,
119
-
120
- /* The view from the root subscription, found in the condition.
121
- We need this because old_table/new_table data is not reflected in the view inside the AFTER trigger
122
- */
123
- related_view_name TEXT,
124
- related_view_def TEXT, /* view definition */
125
-
126
- inserted TIMESTAMP NOT NULL DEFAULT NOW(),
127
- last_used TIMESTAMP NOT NULL DEFAULT NOW(),
128
- PRIMARY KEY (app_id, table_name, condition) /* This unqique index limits the condition column value to be less than 'SELECT current_setting('block_size'); */
129
- );
130
- COMMENT ON TABLE prostgles.app_triggers IS 'Tables and conditions that are currently subscribed/synced';
131
-
132
-
133
- CREATE OR REPLACE VIEW prostgles.v_triggers AS
134
- SELECT *
135
- , (ROW_NUMBER() OVER( ORDER BY table_name, condition ))::text AS id
136
- , ROW_NUMBER() OVER(PARTITION BY app_id, table_name ORDER BY table_name, condition ) - 1 AS c_id
137
- FROM prostgles.app_triggers;
138
- COMMENT ON VIEW prostgles.v_triggers IS 'Augment trigger table with natural IDs and per app IDs';
139
-
140
-
141
- CREATE OR REPLACE FUNCTION ${this.DB_OBJ_NAMES.data_watch_func}() RETURNS TRIGGER
142
- AS $$
143
-
144
- DECLARE t_ids TEXT[];
145
- DECLARE c_ids INTEGER[];
146
- DECLARE err_c_ids INTEGER[];
147
- DECLARE unions TEXT := '';
148
- DECLARE query TEXT := '';
149
- DECLARE nrw RECORD;
150
- DECLARE erw RECORD;
151
- DECLARE has_errors BOOLEAN := FALSE;
152
-
153
- DECLARE err_text TEXT;
154
- DECLARE err_detail TEXT;
155
- DECLARE err_hint TEXT;
156
-
157
- DECLARE view_def_query TEXT := '';
158
-
159
- BEGIN
160
-
161
- -- PERFORM pg_notify('debug', concat_ws(' ', 'TABLE', TG_TABLE_NAME, TG_OP));
162
-
163
- SELECT string_agg(
164
- format(
165
- $c$
166
- SELECT CASE WHEN EXISTS(
167
- SELECT 1 FROM %I WHERE %s
168
- ) THEN %s::text END AS t_ids
169
- $c$,
170
- table_name,
171
- condition,
172
- id
173
- ),
174
- E' UNION \n '
175
- )
176
- INTO unions
177
- FROM prostgles.v_triggers
178
- WHERE table_name = TG_TABLE_NAME;
179
-
180
-
181
- /* unions = 'old_table union new_table' or any one of the tables */
182
- IF unions IS NOT NULL THEN
183
-
184
- SELECT
185
- format(
186
- E'WITH %I AS (\n %s \n) ',
187
- TG_TABLE_NAME,
188
- concat_ws(
189
- E' UNION ALL \n ',
190
- CASE WHEN (TG_OP = 'DELETE' OR TG_OP = 'UPDATE') THEN ' SELECT * FROM old_table ' END,
191
- CASE WHEN (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN ' SELECT * FROM new_table ' END
192
- )
193
- )
194
- ||
195
- COALESCE((
196
- SELECT ', ' || string_agg(format(E' %I AS ( \n %s \n ) ', related_view_name, related_view_def), ', ')
197
- FROM (
198
- SELECT DISTINCT related_view_name, related_view_def
199
- FROM prostgles.v_triggers
200
- WHERE table_name = TG_TABLE_NAME
201
- AND related_view_name IS NOT NULL
202
- AND related_view_def IS NOT NULL
203
- ) t
204
- ), '')
205
- ||
206
- format(
207
- $c$
208
- SELECT ARRAY_AGG(DISTINCT t.t_ids)
209
- FROM (
210
- %s
211
- ) t
212
- $c$, unions
213
- )
214
- INTO query;
215
-
216
- BEGIN
217
- EXECUTE query INTO t_ids;
218
-
219
- --RAISE NOTICE 'trigger fired ok';
220
-
221
- EXCEPTION WHEN OTHERS THEN
222
-
223
- has_errors := TRUE;
224
-
225
- GET STACKED DIAGNOSTICS
226
- err_text = MESSAGE_TEXT,
227
- err_detail = PG_EXCEPTION_DETAIL,
228
- err_hint = PG_EXCEPTION_HINT;
229
-
230
-
231
- END;
232
-
233
- --RAISE NOTICE 'has_errors: % ', has_errors;
234
- --RAISE NOTICE 'unions: % , cids: %', unions, c_ids;
235
-
236
- IF (t_ids IS NOT NULL OR has_errors) THEN
237
-
238
- FOR nrw IN
239
- SELECT app_id, string_agg(c_id::text, ',') as cids
240
- FROM prostgles.v_triggers
241
- WHERE id = ANY(t_ids)
242
- OR has_errors
243
- GROUP BY app_id
244
- LOOP
245
-
246
- PERFORM pg_notify(
247
- ${(0, PubSubManager_1.asValue)(this.NOTIF_CHANNEL.preffix)} || nrw.app_id ,
248
- concat_ws(
249
- ${(0, PubSubManager_1.asValue)(PubSubManager_1.PubSubManager.DELIMITER)},
250
-
251
- ${(0, PubSubManager_1.asValue)(this.NOTIF_TYPE.data)},
252
- COALESCE(TG_TABLE_NAME, 'MISSING'),
253
- COALESCE(TG_OP, 'MISSING'),
254
- CASE WHEN has_errors
255
- THEN concat_ws('; ', 'error', err_text, err_detail, err_hint, 'query: ' || query )
256
- ELSE COALESCE(nrw.cids, '')
257
- END
258
- ${this.dboBuilder.prostgles.opts.DEBUG_MODE ? (", (select json_agg(t)::TEXT FROM (SELECT * from old_table) t), query") : ""}
259
- )
260
- );
261
- END LOOP;
262
-
263
-
264
- IF has_errors THEN
265
-
266
- DELETE FROM prostgles.app_triggers;
267
- RAISE NOTICE 'trigger dropped due to exception: % % %', err_text, err_detail, err_hint;
268
-
269
- END IF;
270
-
271
-
272
- END IF;
273
- END IF;
274
-
275
-
276
- RETURN NULL;
277
-
278
- /*
279
- EXCEPTION WHEN OTHERS THEN
280
- DELETE FROM prostgles.app_triggers; -- delete all or will need to loop through all conditions to find issue;
281
- RAISE NOTICE 'trigger dropped due to exception';
282
- ${"--EXCEPTION_WHEN_COLUMN_WAS_RENAMED_THEN_DROP_TRIGGER"};
283
-
284
-
285
-
286
- RETURN NULL;
287
- */
288
- END;
289
-
290
- --COMMIT;
291
- $$ LANGUAGE plpgsql;
292
- COMMENT ON FUNCTION ${this.DB_OBJ_NAMES.data_watch_func} IS 'Prostgles internal function used to notify when data in the table changed';
293
-
294
-
295
-
296
- CREATE OR REPLACE FUNCTION ${this.DB_OBJ_NAMES.trigger_add_remove_func}() RETURNS TRIGGER
297
- AS $$
298
-
299
- DECLARE operations TEXT[] := ARRAY['insert', 'update', 'delete'];
300
- DECLARE op TEXT;
301
- DECLARE query TEXT;
302
- DECLARE trw RECORD;
303
-
304
- BEGIN
305
-
306
-
307
- --RAISE NOTICE 'prostgles.app_triggers % ', TG_OP;
308
-
309
- /* If no other listeners on table then DROP triggers */
310
- IF TG_OP = 'DELETE' THEN
311
-
312
- --RAISE NOTICE 'DELETE trigger_add_remove_func table: % ', ' ' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM prostgles.app_triggers) , ' 0 ');
313
- --RAISE NOTICE 'DELETE trigger_add_remove_func old_table: % ', '' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM old_table), ' 0 ');
314
-
315
-
316
- /* Drop actual triggers if needed */
317
- FOR trw IN
318
- SELECT DISTINCT table_name FROM old_table ot
319
- WHERE NOT EXISTS (
320
- SELECT 1 FROM prostgles.app_triggers t
321
- WHERE t.table_name = ot.table_name
322
- )
323
- LOOP
324
-
325
- FOREACH op IN ARRAY operations
326
- LOOP
327
- --RAISE NOTICE ' DROP DATA TRIGGER FOR: % ', trw.table_name;
328
- EXECUTE format(' DROP TRIGGER IF EXISTS %I ON %I ;' , 'prostgles_triggers_' || trw.table_name || '_' || op, trw.table_name);
329
- END LOOP;
330
-
331
- END LOOP;
332
-
333
- /* If newly added listeners on table then CREATE triggers */
334
- ELSIF TG_OP = 'INSERT' THEN
335
-
336
-
337
- --RAISE NOTICE 'INSERT trigger_add_remove_func table: % ', ' ' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM prostgles.triggers) , ' 0 ');
338
- --RAISE NOTICE 'INSERT trigger_add_remove_func new_table: % ', '' || COALESCE((SELECT concat_ws(' ', string_agg(table_name, ' & '), count(*), min(inserted) ) FROM new_table), ' 0 ');
339
-
340
- /* Loop through newly added tables */
341
- FOR trw IN
342
-
343
- SELECT DISTINCT table_name
344
- FROM new_table nt
345
-
346
- /* Table did not exist prior to this insert */
347
- WHERE NOT EXISTS (
348
- SELECT 1
349
- FROM prostgles.app_triggers t
350
- WHERE t.table_name = nt.table_name
351
- AND t.inserted < nt.inserted -- exclude current record (this is an after trigger). Turn into before trigger?
352
- )
353
-
354
- /* Table is valid */
355
- AND EXISTS (
356
- SELECT 1
357
- FROM information_schema.tables
358
- WHERE table_schema = 'public'
359
- AND table_name = nt.table_name
360
- )
361
- LOOP
362
-
363
- /*
364
- RAISE NOTICE ' CREATE DATA TRIGGER FOR: % TABLE EXISTS?', trw.table_name, SELECT EXISTS (
365
- SELECT 1
366
- FROM information_schema.tables
367
- WHERE table_schema = 'public'
368
- AND table_name = nt.table_name
369
- );
370
- */
371
-
372
- query := format(
373
- $q$
374
- DROP TRIGGER IF EXISTS %1$I ON %2$I;
375
- CREATE TRIGGER %1$I
376
- AFTER INSERT ON %2$I
377
- REFERENCING NEW TABLE AS new_table
378
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.data_watch_func}();
379
- COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
380
- $q$,
381
- 'prostgles_triggers_' || trw.table_name || '_insert', trw.table_name
382
- ) || format(
383
- $q$
384
- DROP TRIGGER IF EXISTS %1$I ON %2$I;
385
- CREATE TRIGGER %1$I
386
- AFTER UPDATE ON %2$I
387
- REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
388
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.data_watch_func}();
389
- COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
390
- $q$,
391
- 'prostgles_triggers_' || trw.table_name || '_update', trw.table_name
392
- ) || format(
393
- $q$
394
- DROP TRIGGER IF EXISTS %1$I ON %2$I;
395
- CREATE TRIGGER %1$I
396
- AFTER DELETE ON %2$I
397
- REFERENCING OLD TABLE AS old_table
398
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.data_watch_func}();
399
- COMMENT ON TRIGGER %1$I ON %2$I IS 'Prostgles internal trigger used to notify when data in the table changed';
400
- $q$,
401
- 'prostgles_triggers_' || trw.table_name || '_delete', trw.table_name
402
- );
403
-
404
- --RAISE NOTICE ' % ', query;
405
-
406
-
407
- query := format(
408
- $q$
409
- DO $e$
410
- BEGIN
411
-
412
- IF EXISTS (
413
- SELECT 1
414
- FROM information_schema.tables
415
- WHERE table_schema = 'public'
416
- AND table_name = %L
417
- ) THEN
418
-
419
- %s
420
-
421
- END IF;
422
-
423
- END $e$;
424
- $q$,
425
- trw.table_name,
426
- query
427
- ) ;
428
-
429
-
430
- EXECUTE query;
431
-
432
- END LOOP;
433
-
434
- END IF;
435
-
436
-
437
- RETURN NULL;
438
- END;
439
-
440
- $$ LANGUAGE plpgsql;
441
- COMMENT ON FUNCTION ${this.DB_OBJ_NAMES.trigger_add_remove_func} IS 'Used to add/remove table watch triggers concurrently ';
442
-
443
- DROP TRIGGER IF EXISTS prostgles_triggers_insert ON prostgles.app_triggers;
444
- CREATE TRIGGER prostgles_triggers_insert
445
- AFTER INSERT ON prostgles.app_triggers
446
- REFERENCING NEW TABLE AS new_table
447
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.trigger_add_remove_func}();
448
-
449
- DROP TRIGGER IF EXISTS prostgles_triggers_delete ON prostgles.app_triggers;
450
- CREATE TRIGGER prostgles_triggers_delete
451
- AFTER DELETE ON prostgles.app_triggers
452
- REFERENCING OLD TABLE AS old_table
453
- FOR EACH STATEMENT EXECUTE PROCEDURE ${this.DB_OBJ_NAMES.trigger_add_remove_func}();
454
-
455
-
456
- CREATE OR REPLACE FUNCTION ${this.DB_OBJ_NAMES.schema_watch_func}() RETURNS event_trigger AS $$
457
-
458
- DECLARE curr_query TEXT := '';
459
- DECLARE arw RECORD;
460
-
461
- BEGIN
462
-
463
- --RAISE NOTICE 'SCHEMA_WATCH: %', tg_tag;
464
-
465
- /*
466
- This event trigger will outlive a prostgles app instance.
467
- Must ensure it only fires if an app instance is running
468
- */
469
- IF
470
- EXISTS (
471
- SELECT 1
472
- FROM information_schema.tables
473
- WHERE table_schema = 'prostgles'
474
- AND table_name = 'apps'
475
- )
476
- THEN
477
-
478
- SELECT LEFT(COALESCE(current_query(), ''), 5000)
479
- INTO curr_query;
480
-
481
- FOR arw IN
482
- SELECT * FROM prostgles.apps WHERE watching_schema IS TRUE
483
-
484
- LOOP
485
- PERFORM pg_notify(
486
- ${(0, PubSubManager_1.asValue)(this.NOTIF_CHANNEL.preffix)} || arw.id,
487
- concat_ws(
488
- ${(0, PubSubManager_1.asValue)(PubSubManager_1.PubSubManager.DELIMITER)},
489
- ${(0, PubSubManager_1.asValue)(this.NOTIF_TYPE.schema)}, tg_tag , TG_event, curr_query
490
- )
491
- );
492
- END LOOP;
493
-
494
- END IF;
495
-
496
- END;
497
- $$ LANGUAGE plpgsql;
498
- COMMENT ON FUNCTION ${this.DB_OBJ_NAMES.schema_watch_func} IS 'Prostgles internal function used to notify when schema has changed';
499
-
500
- END IF;
501
-
502
- END
503
- $do$;
504
-
505
-
506
- COMMIT;
507
- `;
508
- // const prgl_exists = await this.db.oneOrNone(`
509
- // DROP SCHEMA IF EXISTS prostgles CASCADE;
510
- // SELECT 1 FROM information_schema.schemata WHERE schema_name = 'prostgles'
511
- // `);
512
- // if(!prgl_exists){
513
- // await this.db.any(q);
514
- // }
515
- await this.db.any(initQuery);
13
+ await this.db.any(await getInitQuery_1.getInitQuery.bind(this)());
516
14
  if (!this.canContinue())
517
15
  return;
518
16
  /* Prepare App id */
@@ -524,90 +22,61 @@ async function initPubSubManager() {
524
22
  let appQ = "";
525
23
  try { // drop owned by api
526
24
  this.appChecking = true;
527
- let trgUpdateLastUsed = "", listeners = this.getActiveListeners();
528
- if (listeners.length) {
529
- trgUpdateLastUsed = `
530
- UPDATE prostgles.app_triggers
531
- SET last_used = CASE WHEN (table_name, condition) IN (
532
- ${listeners.map(l => ` ( ${(0, PubSubManager_1.asValue)(l.table_name)}, ${(0, PubSubManager_1.asValue)(l.condition)} ) `).join(", ")}
533
- ) THEN NOW() ELSE last_used END
534
- WHERE app_id = ${(0, PubSubManager_1.asValue)(this.appID)};
535
- `;
536
- }
537
- appQ = `
538
-
539
- DO $$
540
- BEGIN
25
+ const listeners = this.getActiveListeners();
26
+ appQ = `
27
+ DO $$
28
+ BEGIN
541
29
 
542
- /* ${REALTIME_TRIGGER_CHECK_QUERY} */
543
- /* prostgles schema must exist */
544
- IF
545
- EXISTS (
546
- SELECT 1
547
- FROM information_schema.tables
548
- WHERE table_schema = 'prostgles'
549
- AND table_name = 'apps'
550
- )
551
- THEN
30
+ /* ${REALTIME_TRIGGER_CHECK_QUERY} */
31
+ /* prostgles schema must exist */
32
+ IF
33
+ EXISTS (
34
+ SELECT 1
35
+ FROM information_schema.tables
36
+ WHERE table_schema = 'prostgles'
37
+ AND table_name = 'apps'
38
+ )
39
+ THEN
40
+
41
+ UPDATE prostgles.apps
42
+ SET last_check = NOW()
43
+ WHERE id = ${(0, PubSubManager_1.asValue)(this.appID)};
552
44
 
553
-
554
- /* Concurrency control to avoid deadlock
555
- IF NOT EXISTS (
556
- SELECT 1 FROM prostgles.apps
557
- WHERE last_check < last_check_ended
558
- AND last_check_ended > NOW() - interval '5 minutes'
559
- ) THEN
560
- */
561
- UPDATE prostgles.apps
562
- SET last_check = NOW()
563
- WHERE id = ${(0, PubSubManager_1.asValue)(this.appID)};
564
45
 
565
46
 
566
-
567
- /* Delete unused triggers. Might deadlock */
568
- IF EXISTS ( SELECT 1 FROM prostgles.app_triggers)
47
+ /* Delete unused triggers. Might deadlock */
48
+ IF EXISTS ( SELECT 1 FROM prostgles.app_triggers)
49
+
50
+ THEN
569
51
 
570
- /* If this is the latest app then proceed
571
- AND (
572
- SELECT id = ${(0, PubSubManager_1.asValue)(this.appID)}
573
- FROM prostgles.apps
574
- ORDER BY last_check DESC
575
- LIMIT 1
576
- ) = TRUE
577
- */
578
-
579
- THEN
580
-
581
- /* TODO: Fixed deadlocks */
582
- --LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
583
-
584
- /* UPDATE currently used triggers */
585
- ${trgUpdateLastUsed}
586
-
587
- /* DELETE stale triggers for current app. Other triggers will be deleted on app startup */
588
- DELETE FROM prostgles.app_triggers
589
- WHERE app_id = ${(0, PubSubManager_1.asValue)(this.appID)}
590
- AND last_used < NOW() - 4 * ${(0, PubSubManager_1.asValue)(this.appCheckFrequencyMS)} * interval '1 millisecond'; -- 10 seconds at the moment
591
-
592
- END IF;
52
+ /* TODO: Fixed deadlocks */
53
+ --LOCK TABLE prostgles.app_triggers IN ACCESS EXCLUSIVE MODE;
593
54
 
55
+ /* UPDATE currently used triggers */
56
+ ${!listeners.length ? "" : `
57
+ UPDATE prostgles.app_triggers
58
+ SET last_used = CASE WHEN (table_name, condition) IN (
59
+ ${listeners.map(l => ` ( ${(0, PubSubManager_1.asValue)(l.table_name)}, ${(0, PubSubManager_1.asValue)(l.condition)} ) `).join(", ")}
60
+ ) THEN NOW() ELSE last_used END
61
+ WHERE app_id = ${(0, PubSubManager_1.asValue)(this.appID)};
62
+ `}
594
63
 
64
+ /* DELETE stale triggers for current app. Other triggers will be deleted on app startup */
65
+ DELETE FROM prostgles.app_triggers
66
+ WHERE app_id = ${(0, PubSubManager_1.asValue)(this.appID)}
67
+ AND last_used < NOW() - 4 * ${(0, PubSubManager_1.asValue)(this.appCheckFrequencyMS)} * interval '1 millisecond'; -- 10 seconds at the moment
595
68
 
596
- UPDATE prostgles.apps
597
- SET last_check_ended = NOW()
598
- WHERE id = ${(0, PubSubManager_1.asValue)(this.appID)};
69
+ END IF;
599
70
 
600
- /*
601
- END IF;
602
- */
71
+ UPDATE prostgles.apps
72
+ SET last_check_ended = NOW()
73
+ WHERE id = ${(0, PubSubManager_1.asValue)(this.appID)};
603
74
 
604
-
605
- END IF;
606
75
 
607
- -- must not commit without a lock
608
- --COMMIT;
609
- END $$;
610
- `;
76
+ END IF;
77
+
78
+ END $$;
79
+ `;
611
80
  await this.db.any(appQ);
612
81
  tries = 5;
613
82
  (0, PubSubManager_1.log)("updated last_check");