-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema-dump.sql
5490 lines (4042 loc) · 163 KB
/
schema-dump.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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- PostgreSQL database dump
--
-- Dumped from database version 14.4 (Ubuntu 14.4-1.pgdg18.04+1)
-- Dumped by pg_dump version 14.4 (Ubuntu 14.4-1.pgdg18.04+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: public; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA public;
ALTER SCHEMA public OWNER TO postgres;
--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres
--
COMMENT ON SCHEMA public IS 'standard public schema';
--
-- Name: billing; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA billing;
ALTER SCHEMA billing OWNER TO postgres;
--
-- Name: geo; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA geo;
ALTER SCHEMA geo OWNER TO postgres;
--
-- Name: lookup; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA lookup;
ALTER SCHEMA lookup OWNER TO postgres;
--
-- Name: sms; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA sms;
ALTER SCHEMA sms OWNER TO postgres;
--
-- Name: worker; Type: SCHEMA; Schema: -; Owner: postgres
--
CREATE SCHEMA worker;
ALTER SCHEMA worker OWNER TO postgres;
--
-- Name: pricing_version; Type: TYPE; Schema: billing; Owner: postgres
--
CREATE TYPE billing.pricing_version AS ENUM (
'v1',
'v2',
'byot',
'other',
'peoples-action'
);
ALTER TYPE billing.pricing_version OWNER TO postgres;
--
-- Name: usage_type; Type: TYPE; Schema: billing; Owner: postgres
--
CREATE TYPE billing.usage_type AS ENUM (
'lrn',
'phone_number',
'sms_outbound',
'sms_inbound',
'mms_outbound',
'mms_inbound'
);
ALTER TYPE billing.usage_type OWNER TO postgres;
--
-- Name: access_fulfillment_state; Type: TYPE; Schema: lookup; Owner: postgres
--
CREATE TYPE lookup.access_fulfillment_state AS ENUM (
'done',
'waiting',
'fetching'
);
ALTER TYPE lookup.access_fulfillment_state OWNER TO postgres;
--
-- Name: billing_status_enum; Type: TYPE; Schema: lookup; Owner: postgres
--
CREATE TYPE lookup.billing_status_enum AS ENUM (
'billed',
'cached'
);
ALTER TYPE lookup.billing_status_enum OWNER TO postgres;
--
-- Name: phone_type_enum; Type: TYPE; Schema: lookup; Owner: postgres
--
CREATE TYPE lookup.phone_type_enum AS ENUM (
'landline',
'mobile',
'voip',
'unknown',
'invalid'
);
ALTER TYPE lookup.phone_type_enum OWNER TO postgres;
--
-- Name: request_progress_result; Type: TYPE; Schema: lookup; Owner: postgres
--
CREATE TYPE lookup.request_progress_result AS (
completed_at timestamp without time zone,
progress numeric
);
ALTER TYPE lookup.request_progress_result OWNER TO postgres;
--
-- Name: service_option; Type: TYPE; Schema: lookup; Owner: postgres
--
CREATE TYPE lookup.service_option AS ENUM (
'telnyx'
);
ALTER TYPE lookup.service_option OWNER TO postgres;
--
-- Name: area_code; Type: DOMAIN; Schema: public; Owner: postgres
--
CREATE DOMAIN public.area_code AS text
CONSTRAINT area_code_check CHECK ((VALUE ~* '[0-9]{3}'::text));
ALTER DOMAIN public.area_code OWNER TO postgres;
--
-- Name: phone_number; Type: DOMAIN; Schema: public; Owner: postgres
--
CREATE DOMAIN public.phone_number AS text
CONSTRAINT e164 CHECK ((VALUE ~* '\+1[0-9]{10}'::text));
ALTER DOMAIN public.phone_number OWNER TO postgres;
--
-- Name: slug; Type: DOMAIN; Schema: public; Owner: postgres
--
CREATE DOMAIN public.slug AS text
CONSTRAINT slug_check CHECK ((VALUE ~* '[a-z0-9\-]+'::text));
ALTER DOMAIN public.slug OWNER TO postgres;
--
-- Name: url; Type: DOMAIN; Schema: public; Owner: postgres
--
CREATE DOMAIN public.url AS text
CONSTRAINT url_check CHECK ((VALUE ~* '(https?:\/\/)?([\w\-])+\.{1}([a-zA-Z]{2,63})([\/\w-]*)*\/?\??([^#\n\r]*)?#?([^\n\r]*)'::text));
ALTER DOMAIN public.url OWNER TO postgres;
--
-- Name: us_state; Type: DOMAIN; Schema: public; Owner: postgres
--
CREATE DOMAIN public.us_state AS text
CONSTRAINT us_state_check CHECK ((VALUE ~* '[A-Z]{2}'::text));
ALTER DOMAIN public.us_state OWNER TO postgres;
--
-- Name: zip_code; Type: DOMAIN; Schema: public; Owner: postgres
--
CREATE DOMAIN public.zip_code AS text
CONSTRAINT zip_code_check CHECK ((VALUE ~* '[0-9]{5}'::text));
ALTER DOMAIN public.zip_code OWNER TO postgres;
--
-- Name: bandwidth_credentials; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.bandwidth_credentials AS (
account_id text,
username text,
encrypted_password text,
site_id text,
location_id text,
application_id text,
callback_username text,
callback_encrypted_password text
);
ALTER TYPE sms.bandwidth_credentials OWNER TO postgres;
--
-- Name: delivery_report_event; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.delivery_report_event AS ENUM (
'queued',
'sending',
'sent',
'delivered',
'sending_failed',
'delivery_failed',
'delivery_unconfirmed'
);
ALTER TYPE sms.delivery_report_event OWNER TO postgres;
--
-- Name: number_purchasing_strategy; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.number_purchasing_strategy AS ENUM (
'exact-area-codes',
'same-state-by-distance'
);
ALTER TYPE sms.number_purchasing_strategy OWNER TO postgres;
--
-- Name: outbound_message_stages; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.outbound_message_stages AS ENUM (
'processing',
'awaiting-number',
'queued',
'sent',
'failed'
);
ALTER TYPE sms.outbound_message_stages OWNER TO postgres;
--
-- Name: profile_service_option; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.profile_service_option AS ENUM (
'twilio',
'telnyx',
'bandwidth',
'tcr',
'bandwidth-dry-run'
);
ALTER TYPE sms.profile_service_option OWNER TO postgres;
--
-- Name: tcr_credentials; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.tcr_credentials AS (
api_key_label text,
api_key text,
encrypted_secret text
);
ALTER TYPE sms.tcr_credentials OWNER TO postgres;
--
-- Name: telco_status; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.telco_status AS ENUM (
'sent',
'delivered',
'failed'
);
ALTER TYPE sms.telco_status OWNER TO postgres;
--
-- Name: telnyx_credentials; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.telnyx_credentials AS (
public_key text,
encrypted_api_key text
);
ALTER TYPE sms.telnyx_credentials OWNER TO postgres;
--
-- Name: traffic_channel; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.traffic_channel AS ENUM (
'grey-route',
'toll-free',
'10dlc'
);
ALTER TYPE sms.traffic_channel OWNER TO postgres;
--
-- Name: twilio_credentials; Type: TYPE; Schema: sms; Owner: postgres
--
CREATE TYPE sms.twilio_credentials AS (
account_sid text,
encrypted_auth_token text
);
ALTER TYPE sms.twilio_credentials OWNER TO postgres;
--
-- Name: backfill_telco_sent_at_around(timestamp without time zone); Type: FUNCTION; Schema: billing; Owner: postgres
--
CREATE FUNCTION billing.backfill_telco_sent_at_around(fire_date timestamp without time zone) RETURNS bigint
LANGUAGE sql
AS $$
with update_result as (
update sms.outbound_messages_telco mt
set sent_at = mr.processed_at
from sms.outbound_messages_routing mr
where mr.id = mt.id
and mr.processed_at >= date_trunc('hour', fire_date - '1 hour'::interval)
and mr.processed_at < date_trunc('hour', fire_date)
and mr.stage <> 'awaiting-number'
returning 1
)
select count(*)
from update_result
$$;
ALTER FUNCTION billing.backfill_telco_sent_at_around(fire_date timestamp without time zone) OWNER TO postgres;
--
-- Name: current_client_id(); Type: FUNCTION; Schema: billing; Owner: postgres
--
CREATE FUNCTION billing.current_client_id() RETURNS uuid
LANGUAGE sql STABLE
SET search_path TO '$user', 'public'
AS $$
select current_setting('client.id')::uuid
$$;
ALTER FUNCTION billing.current_client_id() OWNER TO postgres;
--
-- Name: generate_usage_rollups(timestamp without time zone); Type: FUNCTION; Schema: billing; Owner: postgres
--
CREATE FUNCTION billing.generate_usage_rollups(fire_date timestamp without time zone) RETURNS void
LANGUAGE plpgsql
AS $$
declare
v_period_end timestamp;
v_period_start timestamp;
begin
select
date_trunc('hour', fire_date) - '1 hour'::interval,
date_trunc('hour', fire_date)
into v_period_start, v_period_end;
-- LRN
insert into billing.lrn_usage_rollups (client_id, period_start, period_end, lrn)
select
lrn_usage.client_id,
v_period_start,
v_period_end,
count(distinct lrn_usage.phone_number)
from lookup.accesses lrn_usage
where
lrn_usage.accessed_at >= v_period_start
and lrn_usage.accessed_at < v_period_end
and not exists (
select 1 from lookup.accesses previous_usage
where
previous_usage.accessed_at < v_period_start
and previous_usage.phone_number = lrn_usage.phone_number
)
group by lrn_usage.client_id
on conflict (client_id, period_start, period_end)
do nothing;
-- Messaging
insert into billing.messaging_usage_rollups (
profile_id,
period_start,
period_end,
outbound_sms_messages,
outbound_sms_segments,
outbound_mms_messages,
outbound_mms_segments,
inbound_sms_messages,
inbound_sms_segments,
inbound_mms_messages,
inbound_mms_segments
)
select
coalesce(outbound.profile_id, inbound.profile_id),
v_period_start,
v_period_end,
coalesce(outbound.sms_messages, 0),
coalesce(outbound.sms_segments, 0),
coalesce(outbound.mms_messages, 0),
coalesce(outbound.mms_segments, 0),
coalesce(inbound.sms_messages, 0),
coalesce(inbound.sms_segments, 0),
coalesce(inbound.mms_messages, 0),
coalesce(inbound.mms_segments, 0)
from (
-- gather usage post-split
select
ob.profile_id,
count(*) filter (where mt.num_media = 0) as sms_messages,
sum(mt.num_segments) filter (where mt.num_media = 0) as sms_segments,
count(*) filter (where mt.num_media > 0) as mms_messages,
sum(mt.num_segments) filter (where mt.num_media > 0) as mms_segments
from sms.outbound_messages ob
join sms.outbound_messages_telco as mt on mt.id = ob.id
where true
and mt.sent_at >= v_period_start
and mt.sent_at < v_period_end
and mt.original_created_at >= v_period_start - '1 day'::interval
and mt.original_created_at < v_period_end + '1 day'::interval
and ob.created_at >= v_period_start - '1 day'::interval
and ob.created_at < v_period_end + '1 day'::interval
group by 1
) outbound
full outer join (
select
sl.profile_id,
count(*) filter (where num_media = 0) as sms_messages,
sum(num_segments) filter (where num_media = 0) as sms_segments,
count(*) filter (where num_media > 0) as mms_messages,
sum(num_segments) filter (where num_media > 0) as mms_segments
from sms.inbound_messages im
join sms.sending_locations sl
on sl.id = im.sending_location_id
where true
and received_at >= v_period_start
and received_at < v_period_end
group by 1
) inbound
on outbound.profile_id = inbound.profile_id
on conflict (profile_id, period_start, period_end)
do nothing;
end;
$$;
ALTER FUNCTION billing.generate_usage_rollups(fire_date timestamp without time zone) OWNER TO postgres;
--
-- Name: inbound_message_usage(uuid, timestamp with time zone); Type: FUNCTION; Schema: billing; Owner: postgres
--
CREATE FUNCTION billing.inbound_message_usage(client uuid, month timestamp with time zone) RETURNS TABLE(client_id uuid, period_start timestamp with time zone, period_end timestamp with time zone, service sms.profile_service_option, sms_segments bigint, mms_segments bigint)
LANGUAGE plpgsql
AS $$
declare
v_month_start timestamptz;
v_month_end timestamptz;
begin
select date_trunc('month', month) into v_month_start;
select date_trunc('month', month + '1 month'::interval) into v_month_end;
return query
select
sms.profiles.client_id,
v_month_start as period_start,
v_month_end as period_end,
sms.sending_accounts.service,
sum(num_segments) filter (where num_media = 0) as sms_segments,
sum(num_segments) filter (where num_media > 0) as mms_segments
from sms.inbound_messages
join sms.sending_locations
on sms.sending_locations.id = sms.inbound_messages.sending_location_id
join sms.profiles
on sms.profiles.id = sms.sending_locations.profile_id
join sms.sending_accounts
on sms.sending_accounts.id = sms.profiles.sending_account_id
where true
and sms.profiles.client_id = client
and sms.inbound_messages.received_at >= v_month_start
and sms.inbound_messages.received_at < v_month_end
group by 1, 4
order by
sms_segments desc;
end;
$$;
ALTER FUNCTION billing.inbound_message_usage(client uuid, month timestamp with time zone) OWNER TO postgres;
--
-- Name: incremental_rollup_backfill_from(timestamp without time zone, timestamp without time zone); Type: PROCEDURE; Schema: billing; Owner: postgres
--
CREATE PROCEDURE billing.incremental_rollup_backfill_from(IN start_date timestamp without time zone, IN end_date timestamp without time zone)
LANGUAGE plpgsql
AS $$
declare
v_fire_date timestamp;
v_count_sent bigint;
begin
v_fire_date := start_date;
while v_fire_date <= end_date loop
raise notice 'Backfilling around %', v_fire_date;
select billing.backfill_telco_sent_at_around(v_fire_date)
into v_count_sent;
perform billing.generate_usage_rollups(v_fire_date);
raise notice 'Backfilled billing info for % outbound messages', v_count_sent;
commit;
v_fire_date := v_fire_date + '1 hour'::interval;
end loop;
end
$$;
ALTER PROCEDURE billing.incremental_rollup_backfill_from(IN start_date timestamp without time zone, IN end_date timestamp without time zone) OWNER TO postgres;
--
-- Name: lrn_usage(uuid, timestamp with time zone); Type: FUNCTION; Schema: billing; Owner: postgres
--
CREATE FUNCTION billing.lrn_usage(client uuid, month timestamp with time zone) RETURNS TABLE(client_id uuid, period_start timestamp with time zone, period_end timestamp with time zone, lookup_count bigint)
LANGUAGE plpgsql
AS $$
declare
v_month_start timestamptz;
v_month_end timestamptz;
begin
select date_trunc('month', month) into v_month_start;
select date_trunc('month', month + '1 month'::interval) into v_month_end;
return query
select
lookup.accesses.client_id,
v_month_start as period_start,
v_month_end as period_end,
count(distinct lookup.accesses.phone_number) as lookup_count
from lookup.accesses
where true
and lookup.accesses.client_id = lrn_usage.client
and lookup.accesses.accessed_at >= v_month_start
and lookup.accesses.accessed_at < v_month_end
and not exists (
select 1
from lookup.accesses as previous_accesses
where true
and previous_accesses.client_id = lrn_usage.client
and previous_accesses.phone_number = lookup.accesses.phone_number
and previous_accesses.accessed_at < lookup.accesses.accessed_at
)
group by 1;
end;
$$;
ALTER FUNCTION billing.lrn_usage(client uuid, month timestamp with time zone) OWNER TO postgres;
--
-- Name: phone_number_usage(uuid, timestamp with time zone); Type: FUNCTION; Schema: billing; Owner: postgres
--
CREATE FUNCTION billing.phone_number_usage(client uuid, month timestamp with time zone) RETURNS TABLE(client_id uuid, period_start timestamp with time zone, period_end timestamp with time zone, service sms.profile_service_option, number_months double precision)
LANGUAGE plpgsql
AS $$
declare
v_month_start timestamptz;
v_month_end timestamptz;
v_days_in_month int;
begin
select date_trunc('month', month) into v_month_start;
select date_trunc('month', month + '1 month'::interval) into v_month_end;
select extract(days from v_month_start + '1 month - 1 day'::interval) into v_days_in_month;
return query
select
sms.profiles.client_id,
v_month_start as period_start,
v_month_end as period_end,
sms.sending_accounts.service,
sum(
extract(day from least(v_month_end, released_at) - greatest(v_month_start, created_at)) / v_days_in_month::float
) as number_months
from sms.all_phone_numbers
join sms.sending_locations
on sms.sending_locations.id = sms.all_phone_numbers.sending_location_id
join sms.profiles
on sms.profiles.id = sms.sending_locations.profile_id
join sms.sending_accounts
on sms.sending_accounts.id = sms.profiles.sending_account_id
where true
and sms.profiles.client_id = phone_number_usage.client
and sms.all_phone_numbers.created_at < v_month_end
and (
sms.all_phone_numbers.released_at is null
or sms.all_phone_numbers.released_at >= v_month_start
)
group by 1, 4
order by
number_months desc;
end;
$$;
ALTER FUNCTION billing.phone_number_usage(client uuid, month timestamp with time zone) OWNER TO postgres;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: requests; Type: TABLE; Schema: lookup; Owner: postgres
--
CREATE TABLE lookup.requests (
id uuid DEFAULT public.uuid_generate_v1mc() NOT NULL,
client_id uuid DEFAULT billing.current_client_id() NOT NULL,
created_at timestamp without time zone DEFAULT now() NOT NULL,
closed_at timestamp without time zone,
completed_at timestamp without time zone
);
ALTER TABLE lookup.requests OWNER TO postgres;
--
-- Name: TABLE requests; Type: COMMENT; Schema: lookup; Owner: postgres
--
COMMENT ON TABLE lookup.requests IS '
@omit update
';
--
-- Name: close_request(uuid); Type: FUNCTION; Schema: lookup; Owner: postgres
--
CREATE FUNCTION lookup.close_request(request_id uuid) RETURNS lookup.requests
LANGUAGE plpgsql STRICT
SET search_path TO '$user', 'public'
AS $$
declare
v_request_completed boolean;
v_result lookup.requests;
v_request_id uuid;
begin
select request_id into v_request_id;
select (
select count(*)
from lookup.accesses
where lookup.accesses.request_id = v_request_id
and state <> 'done'
) = 0
into v_request_completed;
if v_request_completed then
update lookup.requests
set completed_at = now()
where lookup.requests.id = v_request_id;
end if;
update lookup.requests
set closed_at = now()
where id = v_request_id;
select * from lookup.requests
where id = v_request_id
into v_result;
return v_result;
end;
$$;
ALTER FUNCTION lookup.close_request(request_id uuid) OWNER TO postgres;
--
-- Name: request_progress(uuid); Type: FUNCTION; Schema: lookup; Owner: postgres
--
CREATE FUNCTION lookup.request_progress(request_id uuid) RETURNS lookup.request_progress_result
LANGUAGE plpgsql
SET search_path TO '$user', 'public'
AS $$
declare
v_total_accesses int;
v_accesses_done int;
v_completed_at timestamp;
v_progress numeric;
v_requests_found int;
begin
select count(*) from lookup.requests
where lookup.requests.id = request_progress.request_id
into v_requests_found;
if v_requests_found = 0 then
raise 'No request found' using errcode = 'no_data_found';
end if;
select count(*)
from lookup.accesses
where lookup.accesses.request_id = request_progress.request_id
into v_total_accesses;
select count(*)
from lookup.accesses
where state = 'done'
and lookup.accesses.request_id = request_progress.request_id
into v_accesses_done;
select null into v_completed_at;
if v_total_accesses - v_accesses_done = 0 then
select now() into v_completed_at;
update lookup.requests
set completed_at = v_completed_at
where lookup.requests.closed_at is not null
and lookup.requests.id = request_progress.request_id;
end if;
select (
case when v_total_accesses = 0 then 0
else v_accesses_done::numeric / v_total_accesses::numeric end
) into v_progress;
return cast(row(v_completed_at, v_progress) as lookup.request_progress_result);
end;
$$;
ALTER FUNCTION lookup.request_progress(request_id uuid) OWNER TO postgres;
--
-- Name: tg__access__fulfill(); Type: FUNCTION; Schema: lookup; Owner: postgres
--
CREATE FUNCTION lookup.tg__access__fulfill() RETURNS trigger
LANGUAGE plpgsql STRICT SECURITY DEFINER
SET search_path TO '$user', 'public'
AS $$
declare
v_fresh_record_exists boolean;
v_already_fetching boolean;
v_job_json json;
begin
select exists(
select 1
from lookup.fresh_phone_data
where
lookup.fresh_phone_data.phone_number = NEW.phone_number
) into v_fresh_record_exists;
select exists(
select 1
from lookup.accesses
where phone_number = NEW.phone_number
and state <> 'done'
) into v_already_fetching;
if not (v_fresh_record_exists or v_already_fetching) then
select json_build_object(
'access_id', NEW.id,
'phone_number', NEW.phone_number
) into v_job_json;
perform graphile_worker.add_job(identifier => 'lookup', payload => v_job_json);
else
NEW.state = 'done';
end if;
return NEW;
end;
$$;
ALTER FUNCTION lookup.tg__access__fulfill() OWNER TO postgres;
--
-- Name: tg__lookup__mark_access_done(); Type: FUNCTION; Schema: lookup; Owner: postgres
--
CREATE FUNCTION lookup.tg__lookup__mark_access_done() RETURNS trigger
LANGUAGE plpgsql STRICT
SET search_path TO '$user', 'public'
AS $$
begin
update lookup.accesses
set state = 'done'::lookup.access_fulfillment_state
where phone_number = NEW.phone_number;
return NEW;
end;
$$;
ALTER FUNCTION lookup.tg__lookup__mark_access_done() OWNER TO postgres;
--
-- Name: tg__lookup__update_phone_data(); Type: FUNCTION; Schema: lookup; Owner: postgres
--
CREATE FUNCTION lookup.tg__lookup__update_phone_data() RETURNS trigger
LANGUAGE plpgsql STRICT
SET search_path TO '$user', 'public'
AS $$
begin
insert into lookup.phone_data (phone_number, carrier_name, phone_type)
values (NEW.phone_number, NEW.carrier_name, NEW.phone_type)
on conflict (phone_number) do
update
set carrier_name = NEW.carrier_name,
phone_type = NEW.phone_type,
last_updated_at = now();
return NEW;
end;
$$;
ALTER FUNCTION lookup.tg__lookup__update_phone_data() OWNER TO postgres;
--
-- Name: add_job_with_sending_account_and_profile_info(text, json, uuid); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.add_job_with_sending_account_and_profile_info(job_name text, core_payload json, param_sending_location_id uuid DEFAULT NULL::uuid) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO '$user', 'public'
AS $$
declare
v_job json;
v_sending_location_id uuid;
v_sending_account_json json;
begin
select coalesce(param_sending_location_id, (core_payload->>'sending_location_id')::uuid)
into v_sending_location_id;
select row_to_json(relevant_sending_account_fields)
from (
select
sending_account.id as sending_account_id,
sending_account.service,
sending_account.twilio_credentials,
sending_account.telnyx_credentials,
sms.profiles.id as profile_id,
sms.profiles.voice_callback_url as voice_callback_url
from sms.sending_locations
join sms.profiles on sms.sending_locations.profile_id = sms.profiles.id
join sms.sending_accounts_as_json as sending_account
on sending_account.id = sms.profiles.sending_account_id
where sms.sending_locations.id = v_sending_location_id
) relevant_sending_account_fields
into v_sending_account_json;
select core_payload::jsonb || v_sending_account_json::jsonb into v_job;
perform graphile_worker.add_job(identifier => job_name, payload => v_job);
end;
$$;
ALTER FUNCTION public.add_job_with_sending_account_and_profile_info(job_name text, core_payload json, param_sending_location_id uuid) OWNER TO postgres;
--
-- Name: attach_10dlc_campaign_to_profile(uuid, text); Type: FUNCTION; Schema: public; Owner: postgres
--
CREATE FUNCTION public.attach_10dlc_campaign_to_profile(profile_id uuid, campaign_identifier text) RETURNS boolean
LANGUAGE plpgsql SECURITY DEFINER
AS $$
declare
v_tendlc_campaign_id uuid;
v_sending_account_json jsonb;
v_overallocated_count bigint;
v_overallocated_sending_location_id uuid;
begin
with payload as (
select
sending_account_id as registrar_account_id,
attach_10dlc_campaign_to_profile.campaign_identifier as registrar_campaign_id
from sms.profiles
where id = attach_10dlc_campaign_to_profile.profile_id
)
insert into sms.tendlc_campaigns (registrar_account_id, registrar_campaign_id)
select registrar_account_id, registrar_campaign_id
from payload
returning id
into v_tendlc_campaign_id;
update sms.profiles
set
tendlc_campaign_id = v_tendlc_campaign_id,
throughput_limit = 4500, -- from 75 per second
daily_contact_limit = 3000000 -- 75 per second
where id = attach_10dlc_campaign_to_profile.profile_id;
-- cordon all except 1 number per sending location
update sms.all_phone_numbers
set cordoned_at = now()
where sms.all_phone_numbers.id <> (
select id
from sms.all_phone_numbers do_not_cordon
where do_not_cordon.sending_location_id = sms.all_phone_numbers.sending_location_id
order by phone_number asc
limit 1
)
and sending_location_id in (
select id
from sms.sending_locations
where sms.sending_locations.profile_id = attach_10dlc_campaign_to_profile.profile_id
);
with jobs_added as (
select
sending_location_id,
graphile_worker.add_job(identifier => 'associate-service-10dlc-campaign', payload => row_to_json(job_payloads))
from (
select
sa.id as sending_account_id,
sa.service as service,
p.id as profile_id,
p.tendlc_campaign_id,
p.voice_callback_url,
sa.twilio_credentials,