-
Notifications
You must be signed in to change notification settings - Fork 2
/
pg-identity-func-trig-seq2.sql
753 lines (655 loc) · 27.3 KB
/
pg-identity-func-trig-seq2.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
SET search_path TO common_oltp;
CREATE INDEX IF NOT EXISTS email_address_idx ON common_oltp.email
(
address
);
CREATE INDEX IF NOT EXISTS user_activ_code_idx ON common_oltp.user
(
activation_code
);
CREATE INDEX IF NOT EXISTS user_open_id_idx ON common_oltp.user
(
open_id
);
CREATE INDEX IF NOT EXISTS user_status_idx ON common_oltp.user
(
status
);
CREATE TABLE sync_test_id
(
uniqid INTEGER NOT NULL,
description varchar(200),
created_at TIMESTAMP(6) WITH TIME ZONE DEFAULT now(),
PRIMARY KEY (uniqid)
);
CREATE TRIGGER "pg_sync_test_id_trigger"
AFTER INSERT OR DELETE OR UPDATE ON sync_test_id
FOR EACH ROW
EXECUTE PROCEDURE common_oltp.notify_trigger_common_oltp('uniqid', 'description', 'created_at');
ALTER TABLE "common_oltp"."sync_test_id" disable TRIGGER "pg_sync_test_id_trigger"
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;
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;
raise notice 'table name : %', TG_TABLE_NAME;
RAISE info 'hello world';
-- 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 = '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 = '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'));
else
-- RAISE NOTICE ' not boolean';
end case;
--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
|| '}}';
-- Notify the channel
PERFORM pg_notify('dev_db_notifications', payload);
RETURN rec;
END;
$body$ LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION "common_oltp"."proc_email_update" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
pguserval TEXT;
BEGIN
pguserval := (SELECT current_user);
if pguserval != 'pgsyncuser' then
if (OLD.email_type_id != NEW.email_type_id) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('EMAIL_TYPE', OLD.email_type_id, NEW.email_type_id, OLD.user_id);
End If;
if (OLD.status_id != NEW.status_id) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('EMAIL_STATUS', OLD.status_id, NEW.status_id, OLD.user_id);
End If;
if (OLD.address != NEW.address) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('EMAIL_ADDRESS', OLD.address, NEW.address, OLD.user_id);
End If;
if (OLD.primary_ind != NEW.primary_ind) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('EMAIL_PRIMARY_IND', OLD.primary_ind, NEW.primary_ind, OLD.user_id);
End If;
-- if pguserval != 'pgsyncuser' then
NEW.modify_date = current_timestamp;
end if;
RETURN NEW;
END;
$body$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION "common_oltp"."proc_phone_update" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
pguserval TEXT;
BEGIN
pguserval := (SELECT current_user);
if pguserval != 'pgsyncuser' then
if (OLD.phone_type_id != NEW.phone_type_id) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('PHONE_TYPE', OLD.phone_type_id, NEW.phone_type_id, OLD.user_id);
End If;
if (OLD.phone_number != NEW.phone_number) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('PHONE_NUMBER', OLD.phone_number, NEW.phone_number, OLD.user_id);
End If;
if (OLD.primary_ind != NEW.primary_ind) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('PHONE_PRIMARY_IND', OLD.primary_ind, NEW.primary_ind, OLD.user_id);
End If;
NEW.modify_date = current_timestamp;
end if;
RETURN NEW;
END;
$body$ LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION "common_oltp"."proc_user_update" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
pguserval TEXT;
BEGIN
pguserval := (SELECT current_user);
if pguserval != 'pgsyncuser' then
IF (TG_OP = 'UPDATE') THEN
if ((OLD.first_name != NEW.first_name) or (OLD.last_name != NEW.last_name ) or (OLD.middle_name != NEW.middle_name )) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('NAME', NULLIF(OLD.first_name, '') || ' ' || NULLIF(OLD.middle_name, '') || ' ' || NULLIF(OLD.last_name, ''),
NULLIF(NEW.first_name, '') || ' ' || NULLIF(NEW.middle_name, '') || ' ' || NULLIF(NEW.last_name, ''), OLD.user_id);
End if;
if (OLD.handle != NEW.handle) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('HANDLE', OLD.handle, NEW.handle, OLD.user_id);
End If;
if (OLD.status != NEW.status) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('STATUS', OLD.status, NEW.status, OLD.user_id);
End If;
if (OLD.activation_code != NEW.activation_code) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('ACTIVATION_CODE', OLD.activation_code, NEW.activation_code, OLD.user_id);
End If;
if (OLD.timezone_id != NEW.timezone_id) then
insert into common_oltp.audit_user (column_name, old_value, new_value, user_id)
values ('TIMEZONE_ID', OLD.timezone_id, NEW.timezone_id, OLD.user_id);
End If;
NEW.modify_date = current_timestamp;
end if;
END IF;
NEW.handle_lower = lower(NEW.handle);
RETURN NEW;
END;
$body$ LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION "common_oltp"."proc_address_update" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
pguserval TEXT;
user_id DECIMAL(10,0);
BEGIN
user_id := NULLIF((select min(x.user_id) from user_address_xref x where x.address_id = OLD.address_id), -1);
pguserval := (SELECT current_user);
if pguserval != 'pgsyncuser' then
if (user_id > 0 and OLD.address1 != NEW.address1) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS1', OLD.address1, NEW.address1, user_id);
End If;
if (user_id > 0 and OLD.address2 != NEW.address2) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS2', OLD.address2, NEW.address2, user_id);
End If;
if (user_id > 0 and OLD.address3 != NEW.address3) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS3', OLD.address3, NEW.address3, user_id);
End If;
if (user_id > 0 and OLD.city != NEW.city) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS_CITY', OLD.city, NEW.city, user_id);
End If;
if (user_id > 0 and OLD.state_code != NEW.state_code) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS_STATE', OLD.state_code, NEW.state_code, user_id);
End If;
if (user_id > 0 and OLD.province != NEW.province) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS_PROVINCE', OLD.province, NEW.province, user_id);
End If;
if (user_id > 0 and OLD.zip != NEW.zip) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS_ZIP', OLD.zip, NEW.zip, user_id);
End If;
if (user_id > 0 and OLD.country_code != NEW.country_code) then
insert into audit_user (column_name, old_value, new_value, user_id)
values ('ADDRESS_COUNTRY', OLD.country_code, NEW.country_code, user_id);
End If;
NEW.modify_date = current_timestamp;
end if;
RETURN NEW;
END;
$body$ LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION "common_oltp"."proc_user_last_login" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
pguserval TEXT;
BEGIN
pguserval := (SELECT current_user);
if pguserval != 'pgsyncuser' then
if (OLD.last_login != NEW.last_login) then
insert into common_oltp.corona_event(corona_event_type_id, user_id, corona_event_timestamp)
values (1, OLD.user_id, NEW.last_login);
end if;
end if;
RETURN NULL;
END;
$body$ LANGUAGE plpgsql
CREATE TRIGGER "pg_security_groups_trigger"
AFTER INSERT OR DELETE OR UPDATE ON security_groups
FOR EACH ROW
EXECUTE PROCEDURE notify_trigger_common_oltp('group_id', 'description', 'challenge_group_ind', 'create_user_id');
CREATE TRIGGER "pg_social_login_provider_trigger"
AFTER INSERT OR DELETE OR UPDATE ON social_login_provider
FOR EACH ROW
EXECUTE PROCEDURE notify_trigger_common_oltp('social_login_provider_id', 'name');
CREATE TRIGGER "pg_sso_login_provider_trigger"
AFTER INSERT OR DELETE OR UPDATE ON sso_login_provider
FOR EACH ROW
EXECUTE PROCEDURE notify_trigger_common_oltp('sso_login_provider_id', 'name','type','identify_email_enabled','identify_handle_enabled');
CREATE TRIGGER "pg_Country_trigger"
AFTER INSERT OR DELETE OR UPDATE ON Country
FOR EACH ROW
EXECUTE PROCEDURE notify_trigger_common_oltp('country_code', 'country_name','modify_date','participating','default_taxform_id','longitude','latitude','region','iso_name','iso_alpha2_code','iso_alpha3_code');
CREATE TRIGGER "pg_invalid_handles_trigger"
AFTER INSERT OR DELETE OR UPDATE ON invalid_handles
FOR EACH ROW
EXECUTE PROCEDURE notify_trigger_common_oltp('invalid_handle_id', 'invalid_handle');
CREATE TRIGGER "pg_achievement_type_lu_trigger"
AFTER INSERT OR DELETE OR UPDATE ON achievement_type_lu
FOR EACH ROW
EXECUTE PROCEDURE notify_trigger_common_oltp('achievement_type_id','achievement_type_desc');
ALTER TABLE "user" DISABLE TRIGGER pg_user_trigger;
ALTER TABLE email DISABLE TRIGGER pg_email_trigger;
ALTER TABLE security_user DISABLE TRIGGER pg_security_user_trigger;
ALTER TABLE user_sso_login DISABLE TRIGGER pg_user_sso_login_trigger;
ALTER TABLE user_achievement DISABLE TRIGGER pg_user_achievement_trigger;
ALTER TABLE user_group_xref DISABLE TRIGGER pg_user_group_xref_trigger;
ALTER TABLE security_groups DISABLE TRIGGER pg_security_groups_trigger;
ALTER TABLE user_social_login DISABLE TRIGGER pg_user_social_login_trigger;
ALTER TABLE social_login_provider DISABLE TRIGGER pg_social_login_provider_trigger;
ALTER TABLE sso_login_provider DISABLE TRIGGER pg_sso_login_provider_trigger;
ALTER TABLE country DISABLE TRIGGER pg_country_trigger;
ALTER TABLE invalid_handles DISABLE TRIGGER pg_invalid_handles_trigger;
ALTER TABLE achievement_type_lu DISABLE TRIGGER pg_achievement_type_lu_trigger;
ALTER TABLE corana_event DISABLE TRIGGER pg_corana_event_trigger;
ALTER TABLE audit_user DISABLE TRIGGER pg_audit_user_trigger;
DROP sequence "common_oltp"."sequence_user_group_seq";
CREATE SEQUENCE sequence_user_group_seq INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807
START WITH 951000000 NO CYCLE;
DROP sequence "common_oltp"."sequence_user_seq";
CREATE SEQUENCE sequence_user_seq INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH
488770000 NO CYCLE;
ALTER SEQUENCE corona_event_corona_event_id_seq RESTART WITH 577770000;
SET search_path TO 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;
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;
raise notice 'table name : %', TG_TABLE_NAME;
-- RAISE info 'hello world';
-- 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'));
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 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
|| '}}';
-- Notify the channel
PERFORM pg_notify('dev_db_notifications', payload);
RETURN rec;
END;
$body$ LANGUAGE plpgsql
CREATE OR REPLACE FUNCTION "informixoltp"."proc_coder_update" () RETURNS trigger
VOLATILE
AS $body$
DECLARE
pguserval TEXT;
begin
if (OLD.quote != NEW.quote) then
insert into audit_coder (column_name, old_value, new_value, user_id)
values ('QUOTE', OLD.quote , NEW.quote, OLD.coder_id);
end if;
if (OLD.coder_type_id != NEW.coder_type_id) then
insert into audit_coder (column_name, old_value, new_value, user_id)
values ('CODER_TYPE', OLD.coder_type_id , NEW.coder_type_id, OLD.coder_id);
end if;
if (OLD.language_id != NEW.language_id) then
insert into audit_coder (column_name, old_value, new_value, user_id)
values ('LANGUAGE', OLD.language_id , NEW.language_id, OLD.coder_id);
end if;
if (OLD.comp_country_code != NEW.comp_country_code) then
insert into audit_coder (column_name, old_value, new_value, user_id)
values ('COMP_COUNTRY', OLD.comp_country_code , NEW.comp_country_code, OLD.coder_id);
end if;
pguserval := (SELECT current_user);
if pguserval != 'pgsyncuser' then
-- RAISE info 'current_user';
-- raise notice 'inside current_user : %', current_user;
--update coder set modify_date = current_timestamp where coder_id = OLD.coder_id;
NEW.modify_date = current_timestamp;
end if;
return NEW;
end ;
$body$ LANGUAGE plpgsql
CREATE TRIGGER "pg_coder_referral_trigger"
AFTER INSERT OR DELETE OR UPDATE ON coder_referral
FOR EACH ROW
EXECUTE PROCEDURE notify_trigger_informixoltp('coder_id', 'referral_id','reference_id','other');
ALTER TABLE coder DISABLE TRIGGER pg_coder;
ALTER TABLE algo_rating DISABLE TRIGGER pg_algo_rating;
ALTER TABLE coder_referral DISABLE TRIGGER pg_coder_referral_trigger;
SET search_path TO tcs_catalog;
CREATE OR REPLACE FUNCTION "tcs_catalog"."notify_trigger" () 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;
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;
-- 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
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
when
column_name = 'upload_document_required' then
if column_value = 'false' then
column_value = '0';
else
column_value = '1';
end if;
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 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);
RETURN rec;
END;
$body$ LANGUAGE plpgsql;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA common_oltp,informixoltp,tcs_catalog TO pgsyncuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA common_oltp,informixoltp,tcs_catalog TO pgsyncuser;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA common_oltp,informixoltp,tcs_catalog TO coder;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA common_oltp,informixoltp,tcs_catalog TO coder;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA common_oltp,informixoltp,tcs_catalog TO postgres;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA common_oltp,informixoltp,tcs_catalog TO postgres;
grant USAGE ON SCHEMA common_oltp,informixoltp,tcs_catalog To pgsyncuser;
grant USAGE ON SCHEMA common_oltp,informixoltp,tcs_catalog To coder;
grant USAGE ON SCHEMA common_oltp,informixoltp,tcs_catalog To postgres;