-
Notifications
You must be signed in to change notification settings - Fork 2
/
producer_notify_triggers.sql
479 lines (439 loc) · 17.5 KB
/
producer_notify_triggers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
CREATE OR REPLACE FUNCTION "tcs_catalog"."notify_trigger" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
rec RECORD;
payload TEXT;
-- payload2 TEXT;
column_name TEXT;
column_value TEXT;
--payload_items TEXT[];
payload2 JSONB;
payload_items JSONB;
payload_items1 JSONB;
pguserval TEXT;
uniquecolumn TEXT;
logtime TEXT;
payloadseqid INTEGER;
channelname TEXT;
kafkatopicname TEXT;
BEGIN
pguserval := (SELECT current_user);
if pguserval = 'pgsyncuser' then
RAISE notice 'pgsyncuser name : %', pguserval;
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
END CASE;
return rec;
-- else
end if;
-- Set record row depending on operation
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
END CASE;
--channel and topic details
RAISE notice ' % ',TG_TABLE_NAME;
SELECT channel, topic into channelname, kafkatopicname from common_oltp.pgifx_tbl_channel_mapping where tablename = TG_TABLE_NAME limit 1;
if (channelname = '') IS NOT FALSE then
channelname = 'dev_db_notifications';
RAISE info 'setting default dev_db_notifications';
end if;
RAISE info 'nofity_common_oltp_trigger';
-- Get required fields
FOREACH column_name IN ARRAY TG_ARGV LOOP
EXECUTE format('SELECT $1.%I::TEXT', column_name)
INTO column_value
USING rec;
case
when
column_name = 'upload_document' then
-- RAISE NOTICE 'upload_document boolean';
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'upload_document_required' then
-- RAISE NOTICE 'upload_document_required boolean';
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
else
-- column_value = regexp_replace(column_value, '^[\\r\\n\\t ]*|[\\r\\n\\t ]*$', '', 'g');
end case;
--payload_items := array_append(payload_items, '"' || replace(column_name, '"', '\"') || '":"' || replace(column_value, '"', '\"') || '"');
--payload_items := coalesce(payload_items,'{}')::jsonb || json_build_object(column_name,column_value)::jsonb;
payload_items := coalesce(payload_items,'{}')::jsonb || json_build_object(column_name,replace(column_value,'"','\"'))::jsonb;
END LOOP;
--RAISE notice 'After guideline json payload 1: "%"', payload_items;
--logtime := (select date_display_tz());
logtime := (SELECT to_char (now()::timestamptz at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'));
payloadseqid := (select nextval('payloadsequence'::regclass));
uniquecolumn := (SELECT c.column_name
FROM information_schema.key_column_usage AS c
LEFT JOIN information_schema.table_constraints AS t
ON t.constraint_name = c.constraint_name
WHERE t.table_name = TG_TABLE_NAME AND t.constraint_type = 'PRIMARY KEY' limit 1);
if (uniquecolumn = '') IS NOT FALSE then
uniquecolumn := 'Not-Available';
end if;
-- exclude any null value columns.
payload_items := jsonb_strip_nulls(payload_items);
RAISE Notice ' payload val: "%"', payload;
-- Build the payload
--payload := ''
-- || '{'
-- || '"topic":"' || 'dev.db.postgres.sync' || '",'
-- || '"originator":"' || 'tc-postgres-delta-processor' || '",'
-- || '"timestamp":"' || logtime || '",'
-- || '"mime-type":"' || 'application/json' || '",'
-- || '"payload": {'
-- || '"payloadseqid":"' || payloadseqid || '",'
-- || '"Uniquecolumn":"' || uniquecolumn || '",'
-- || '"operation":"' || TG_OP || '",'
-- || '"schema":"' || TG_TABLE_SCHEMA || '",'
-- || '"table":"' || TG_TABLE_NAME || '",'
-- || '"data": {' || array_to_string(payload_items, ',') || '}'
-- || '}}';
payload := ''
|| '{'
|| '"topic":"' || 'dev.db.postgres.sync' || '",'
|| '"originator":"' || 'tc-postgres-delta-processor' || '",'
|| '"timestamp":"' || logtime || '",'
|| '"mime-type":"' || 'application/json' || '",'
|| '"payload": {'
|| '"payloadseqid":"' || payloadseqid || '",'
|| '"Uniquecolumn":"' || uniquecolumn || '",'
|| '"operation":"' || TG_OP || '",'
|| '"schema":"' || TG_TABLE_SCHEMA || '",'
|| '"table":"' || TG_TABLE_NAME || '",'
|| '"data":' || payload_items
|| '}}';
-- Notify the channel
--PERFORM pg_notify('dev_db_notifications', payload);
PERFORM pg_notify(channelname, payload);
RETURN rec;
END;
$body$ LANGUAGE plpgsql
-------------------------------------------------------common_oltp.notify_trigger_common_oltp--------------------------------------------
CREATE OR REPLACE FUNCTION "common_oltp"."notify_trigger_common_oltp" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
rec RECORD;
payload TEXT;
column_name TEXT;
column_value TEXT;
pguserval TEXT;
--payload_items TEXT[];
payload_items JSONB;
uniquecolumn TEXT;
logtime TEXT;
payloadseqid INTEGER;
channelname TEXT;
kafkatopicname TEXT;
BEGIN
pguserval := (SELECT current_user);
if pguserval = 'pgsyncuser' then
RAISE notice 'pgsyncuser name : %', pguserval;
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
END CASE;
return rec;
-- else
end if;
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
END CASE;
--channel and topic details
RAISE notice ' % ',TG_TABLE_NAME;
SELECT channel, topic into channelname, kafkatopicname from common_oltp.pgifx_tbl_channel_mapping where tablename = TG_TABLE_NAME limit 1;
if (channelname = '') IS NOT FALSE then
channelname = 'dev_db_notifications';
RAISE info 'setting default dev_db_notifications';
end if;
RAISE info 'nofity_common_oltp_trigger';
-- Get required fields
FOREACH column_name IN ARRAY TG_ARGV LOOP
EXECUTE format('SELECT $1.%I::TEXT', column_name)
INTO column_value
USING rec;
case
when
column_name = 'upload_document' then
-- RAISE NOTICE 'upload_document boolean';
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'upload_document_required' then
-- RAISE NOTICE 'upload_document_required boolean';
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'identify_email_enabled' then
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'identify_handle_enabled' then
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'social_email_verified' then
if column_value = 'false' then
column_value = 'f';
else
column_value = 't';
end if;
/* when
column_name = 'password' then
if (TG_TABLE_NAME = 'security_user') then
column_value := (select regexp_replace(column_value, '([\r\n]+$)', '', 'g'));
end if;*/
when
column_name = 'create_date' then
if (TG_TABLE_NAME = 'user_social_login') then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS'));
else
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
end if;
when
column_name = 'modify_date' then
if (TG_TABLE_NAME = 'security_user') or (TG_TABLE_NAME = 'user_social_login') then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS'));
else
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
end if;
when
column_name = 'last_login' then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
when
column_name = 'last_site_hit_date' then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
when
column_name = 'corona_event_timestamp' then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
when
column_name = 'created_at' then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
--column_name = 'password' then
--column_value := regexp_replace(column_value, '\s', '', 'g');
--column_value := regexp_replace(column_value, E'[\\n\\r]+', '\n\r', 'g');
else
-- RAISE NOTICE ' not boolean';
end case;
-- payload_items := coalesce(payload_items,'{}')::jsonb || json_build_object(column_name,trim(regexp_replace(column_value, E'\n', ' ', 'g')))::jsonb;
--payload_items := coalesce(payload_items,'{}')::jsonb || json_build_object(column_name,column_value)::jsonb;
payload_items := coalesce(payload_items,'{}')::jsonb || json_build_object(column_name,replace(column_value,'"','\"'))::jsonb;
-- payload_items := array_append(payload_items, '"' || replace(column_name, '"', '\"') || '":"' || replace(column_value, '"', '\"') || '"');
END LOOP;
--logtime := (select date_display_tz());
logtime := (SELECT to_char (now()::timestamptz at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'));
payloadseqid := (select nextval('common_oltp.payloadsequence'::regclass));
uniquecolumn := (SELECT c.column_name
FROM information_schema.key_column_usage AS c
LEFT JOIN information_schema.table_constraints AS t
ON t.constraint_name = c.constraint_name
WHERE t.table_name = TG_TABLE_NAME AND t.constraint_type = 'PRIMARY KEY' LIMIT 1);
if (uniquecolumn = '') IS NOT FALSE then
uniquecolumn := 'Not-Available';
end if;
-- exclude any null value columns.
payload_items := jsonb_strip_nulls(payload_items);
-- Build the payload
payload := ''
|| '{'
|| '"topic":"' || 'dev.db.postgres.sync' || '",'
|| '"originator":"' || 'tc-postgres-delta-processor' || '",'
|| '"timestamp":"' || logtime || '",'
|| '"mime-type":"' || 'application/json' || '",'
|| '"payload": {'
|| '"payloadseqid":"' || payloadseqid || '",'
|| '"Uniquecolumn":"' || uniquecolumn || '",'
|| '"operation":"' || TG_OP || '",'
|| '"schema":"' || TG_TABLE_SCHEMA || '",'
|| '"table":"' || TG_TABLE_NAME || '",'
|| '"data": ' || payload_items
--|| '"data": {' || array_to_string(payload_items, ',') || '}'
|| '}}';
-- || '"data": {' || array_to_string(payload_items, ',') || '}'
--|| '"data":' || payload_items
-- Notify the channel
-- PERFORM pg_notify('dev_db_notifications', payload);
PERFORM pg_notify(channelname, payload);
RETURN rec;
END;
$body$ LANGUAGE plpgsql
-------------------------------------------------------informixoltp.notify_trigger_informixoltp--------------------------------------------
CREATE OR REPLACE FUNCTION "informixoltp"."notify_trigger_informixoltp" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
rec RECORD;
payload TEXT;
column_name TEXT;
column_value TEXT;
-- payload_items TEXT[];
payload_items JSONB;
pguserval TEXT;
uniquecolumn TEXT;
logtime TEXT;
payloadseqid INTEGER;
channelname TEXT;
kafkatopicname TEXT;
BEGIN
pguserval := (SELECT current_user);
if pguserval = 'pgsyncuser' then
RAISE notice 'pgsyncuser name : %', pguserval;
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
END CASE;
return rec;
-- else
end if;
CASE TG_OP
WHEN 'INSERT', 'UPDATE' THEN
rec := NEW;
WHEN 'DELETE' THEN
rec := OLD;
ELSE
RAISE EXCEPTION 'Unknown TG_OP: "%". Should not occur!', TG_OP;
END CASE;
--channel and topic details
RAISE notice ' % ',TG_TABLE_NAME;
SELECT channel, topic into channelname, kafkatopicname from common_oltp.pgifx_tbl_channel_mapping where tablename = TG_TABLE_NAME limit 1;
if (channelname = '') IS NOT FALSE then
channelname = 'dev_db_notifications';
RAISE info 'setting default dev_db_notifications';
end if;
RAISE notice '%', channelname;
-- Get required fields
FOREACH column_name IN ARRAY TG_ARGV LOOP
EXECUTE format('SELECT $1.%I::TEXT', column_name)
INTO column_value
USING rec;
case
when
column_name = 'upload_document' then
-- RAISE NOTICE 'upload_document boolean';
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'upload_document_required' then
-- RAISE NOTICE 'upload_document_required boolean';
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'identify_email_enabled' then
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'identify_handle_enabled' then
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'create_date' then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
when
column_name = 'modify_date' then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
when
column_name = 'member_since' then
column_value := (select to_char (column_value::timestamp, 'YYYY-MM-DD HH24:MI:SS.MS'));
-- when
-- column_name = 'achievement_date' then
--column_value := (select to_date (column_value, 'MM/DD/YYYY'));
--column_value := (select to_date (column_value));
else
-- RAISE NOTICE ' not boolean';
end case;
-- payload_items := array_append(payload_items, '"' || replace(column_name, '"', '\"') || '":"' || replace(column_value, '"', '\"') || '"');
--payload_items := coalesce(payload_items,'{}')::jsonb || json_build_object(column_name,column_value)::jsonb;
payload_items := coalesce(payload_items,'{}')::jsonb || json_build_object(column_name,replace(column_value,'"','\"'))::jsonb;
END LOOP;
--logtime := (select date_display_tz());
logtime := (SELECT to_char (now()::timestamptz at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'));
payloadseqid := (select nextval('common_oltp.payloadsequence'::regclass));
uniquecolumn := (SELECT c.column_name
FROM information_schema.key_column_usage AS c
LEFT JOIN information_schema.table_constraints AS t
ON t.constraint_name = c.constraint_name
WHERE t.table_name = TG_TABLE_NAME AND t.constraint_type = 'PRIMARY KEY' LIMIT 1);
if (uniquecolumn = '') IS NOT FALSE then
uniquecolumn := 'Not-Available';
end if;
-- exclude any null value columns.
payload_items := jsonb_strip_nulls(payload_items);
-- Build the payload
payload := ''
|| '{'
|| '"topic":"' || 'dev.db.postgres.sync' || '",'
|| '"originator":"' || 'tc-postgres-delta-processor' || '",'
|| '"timestamp":"' || logtime || '",'
|| '"mime-type":"' || 'application/json' || '",'
|| '"payload": {'
|| '"payloadseqid":"' || payloadseqid || '",'
|| '"Uniquecolumn":"' || uniquecolumn || '",'
|| '"operation":"' || TG_OP || '",'
|| '"schema":"' || TG_TABLE_SCHEMA || '",'
|| '"table":"' || TG_TABLE_NAME || '",'
-- || '"data": {' || array_to_string(payload_items, ',') || '}'
|| '"data": ' || payload_items
|| '}}';
-- Notify the channel
--PERFORM pg_notify('dev_db_notifications', payload);
PERFORM pg_notify(channelname, payload);
RETURN rec;
END;
$body$ LANGUAGE plpgsql