File size: 102,974 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
-- This file is auto-generated by "make db-schema"; DO NOT EDIT
-- DATA=50f009ab810d42724027d31ec49f2584a1c8ecd647194c2455de853975d40bd0  -
-- DISK=2f8c154346b509ddac6e6d36a7a4f1bc86c047dcd1f98eb3ee5846bdf4bcf687  -
-- PSQL=2f8c154346b509ddac6e6d36a7a4f1bc86c047dcd1f98eb3ee5846bdf4bcf687  -
--
-- pgdump-lite database dump
--

-- Extensions

CREATE EXTENSION IF NOT EXISTS pgcrypto;

-- Enums

CREATE TYPE engine_processing_type AS ENUM (
	'cleanup',
	'compat',
	'escalation',
	'heartbeat',
	'message',
	'metrics',
	'np_cycle',
	'rotation',
	'schedule',
	'signals',
	'status_update',
	'verify'
);

CREATE TYPE enum_alert_log_event AS ENUM (
	'acknowledged',
	'assignment_changed',
	'closed',
	'created',
	'duplicate_suppressed',
	'escalated',
	'escalation_request',
	'no_notification_sent',
	'notification_sent',
	'policy_updated',
	'reopened',
	'response_received',
	'status_changed'
);

CREATE TYPE enum_alert_log_subject_type AS ENUM (
	'channel',
	'heartbeat_monitor',
	'integration_key',
	'user'
);

CREATE TYPE enum_alert_source AS ENUM (
	'email',
	'generic',
	'grafana',
	'manual',
	'prometheusAlertmanager',
	'site24x7',
	'universal'
);

CREATE TYPE enum_alert_status AS ENUM (
	'active',
	'closed',
	'triggered'
);

CREATE TYPE enum_heartbeat_state AS ENUM (
	'healthy',
	'inactive',
	'unhealthy'
);

CREATE TYPE enum_integration_keys_type AS ENUM (
	'email',
	'generic',
	'grafana',
	'prometheusAlertmanager',
	'site24x7',
	'universal'
);

CREATE TYPE enum_limit_type AS ENUM (
	'calendar_subscriptions_per_user',
	'contact_methods_per_user',
	'ep_actions_per_step',
	'ep_steps_per_policy',
	'heartbeat_monitors_per_service',
	'integration_keys_per_service',
	'notification_rules_per_user',
	'participants_per_rotation',
	'pending_signals_per_dest_per_service',
	'pending_signals_per_service',
	'rules_per_schedule',
	'targets_per_schedule',
	'unacked_alerts_per_service',
	'user_overrides_per_schedule'
);

CREATE TYPE enum_notif_channel_type AS ENUM (
	'DEST',
	'SLACK',
	'SLACK_USER_GROUP',
	'WEBHOOK'
);

CREATE TYPE enum_outgoing_messages_status AS ENUM (
	'bundled',
	'delivered',
	'failed',
	'pending',
	'queued_remotely',
	'read',
	'sending',
	'sent'
);

CREATE TYPE enum_outgoing_messages_type AS ENUM (
	'alert_notification',
	'alert_notification_bundle',
	'alert_status_update',
	'alert_status_update_bundle',
	'schedule_on_call_notification',
	'signal_message',
	'test_notification',
	'verification_message'
);

CREATE TYPE enum_rotation_type AS ENUM (
	'daily',
	'hourly',
	'monthly',
	'weekly'
);

CREATE TYPE enum_switchover_state AS ENUM (
	'idle',
	'in_progress',
	'use_next_db'
);

CREATE TYPE enum_throttle_type AS ENUM (
	'notifications',
	'notifications_2'
);

CREATE TYPE enum_user_contact_method_type AS ENUM (
	'DEST',
	'EMAIL',
	'PUSH',
	'SLACK_DM',
	'SMS',
	'VOICE',
	'WEBHOOK'
);

CREATE TYPE enum_user_role AS ENUM (
	'admin',
	'unknown',
	'user'
);

CREATE TYPE river_job_state AS ENUM (
	'available',
	'cancelled',
	'completed',
	'discarded',
	'pending',
	'retryable',
	'running',
	'scheduled'
);

-- Functions

CREATE OR REPLACE FUNCTION public.aquire_user_contact_method_lock(_client_id uuid, _alert_id bigint, _contact_method_id uuid)
 RETURNS uuid
 LANGUAGE plpgsql
AS $function$
        DECLARE
            lock_id UUID = gen_random_uuid();
        BEGIN
            DELETE FROM user_contact_method_locks WHERE alert_id = _alert_id
                AND contact_method_id = _contact_method_id
                AND (timestamp + '5 minutes'::interval) < now();

            INSERT INTO user_contact_method_locks (id, alert_id, contact_method_id, client_id) 
                VALUES (lock_id, _alert_id, _contact_method_id, _client_id)
                RETURNING id INTO lock_id;

            INSERT INTO sent_notifications (id, alert_id, contact_method_id, cycle_id, notification_rule_id)
			SELECT lock_id, _alert_id, _contact_method_id, cycle_id, notification_rule_id
			FROM needs_notification_sent n
			WHERE n.alert_id = _alert_id AND n.contact_method_id = _contact_method_id
			ON CONFLICT DO NOTHING;

            RETURN lock_id;
        END;
    $function$
;

CREATE OR REPLACE FUNCTION public.cm_type_val_to_dest(typename enum_user_contact_method_type, value text)
 RETURNS jsonb
 LANGUAGE plpgsql
AS $function$
BEGIN
    IF typeName = 'EMAIL' THEN
        RETURN jsonb_build_object('Type', 'builtin-smtp-email', 'Args', jsonb_build_object('email_address', value));
    ELSIF typeName = 'VOICE' THEN
        RETURN jsonb_build_object('Type', 'builtin-twilio-voice', 'Args', jsonb_build_object('phone_number', value));
    ELSIF typeName = 'SMS' THEN
        RETURN jsonb_build_object('Type', 'builtin-twilio-sms', 'Args', jsonb_build_object('phone_number', value));
    ELSIF typeName = 'WEBHOOK' THEN
        RETURN jsonb_build_object('Type', 'builtin-webhook', 'Args', jsonb_build_object('webhook_url', value));
    ELSIF typeName = 'SLACK_DM' THEN
        RETURN jsonb_build_object('Type', 'builtin-slack-dm', 'Args', jsonb_build_object('slack_user_id', value));
    ELSE
        -- throw an error
        RAISE EXCEPTION 'Unknown contact method type: %', typeName;
    END IF;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.date_bin(p_bin_interval interval, p_ts timestamp with time zone, p_origin timestamp with time zone)
 RETURNS timestamp with time zone
 LANGUAGE sql
 IMMUTABLE
AS $function$
            SELECT
                to_timestamp (
floor((extract(epoch FROM p_ts ) - extract(epoch FROM p_origin ) ) / extract(epoch FROM p_bin_interval ) ) * extract(epoch FROM p_bin_interval ) + extract(epoch FROM p_origin )
                );
    $function$
;

CREATE OR REPLACE FUNCTION public.escalate_alerts()
 RETURNS void
 LANGUAGE plpgsql
AS $function$
        BEGIN
            UPDATE alerts
            SET escalation_level = escalation_level + 1, last_escalation = now()
            FROM alert_escalation_policy_snapshots e
            WHERE (last_escalation + e.step_delay) < now()
                AND status = 'triggered'
                AND id = e.alert_id
                AND e.step_number = (escalation_level % e.step_max)
                AND (e.repeat = -1 OR (escalation_level+1) / e.step_max <= e.repeat);
        END;
    $function$
;

CREATE OR REPLACE FUNCTION public.fn_advance_or_end_rot_on_part_del()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    new_part UUID;
    active_part UUID;
BEGIN

    SELECT rotation_participant_id
    INTO active_part
    FROM rotation_state
    WHERE rotation_id = OLD.rotation_id;

    IF active_part != OLD.id THEN
        RETURN OLD;
    END IF;

    IF OLD.rotation_id NOT IN (
       SELECT id FROM rotations
    ) THEN
        DELETE FROM rotation_state
        WHERE rotation_id = OLD.rotation_id;
    END IF;

    SELECT id
    INTO new_part
    FROM rotation_participants
    WHERE
        rotation_id = OLD.rotation_id AND
        id != OLD.id AND
        position IN (0, OLD.position+1)
    ORDER BY position DESC
    LIMIT 1;

     IF new_part ISNULL THEN
        DELETE FROM rotation_state
        WHERE rotation_id = OLD.rotation_id;
    ELSE
        UPDATE rotation_state
        SET rotation_participant_id = new_part
        WHERE rotation_id = OLD.rotation_id;
    END IF;

    RETURN OLD;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_clear_dedup_on_close()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    NEW.dedup_key = NULL;
    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_clear_ep_state_on_alert_close()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    DELETE FROM escalation_policy_state
    WHERE alert_id = NEW.id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_clear_ep_state_on_svc_ep_change()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    UPDATE escalation_policy_state
    SET
        escalation_policy_id = NEW.escalation_policy_id,
        escalation_policy_step_id = NULL,
        loop_count = 0,
        last_escalation = NULL,
        next_escalation = NULL,
        force_escalation = false,
        escalation_policy_step_number = 0
    WHERE service_id = NEW.id
    ;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_clear_next_esc_on_alert_ack()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    UPDATE escalation_policy_state
    SET next_escalation = null
    WHERE alert_id = NEW.id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_cm_compat_set_type_val_on_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    IF NEW.dest ->> 'Type' = 'builtin-smtp-email' THEN
        NEW.type = 'EMAIL';
        NEW.value = NEW.dest -> 'Args' ->> 'email_address';
    ELSIF NEW.dest ->> 'Type' = 'builtin-twilio-voice' THEN
        NEW.type = 'VOICE';
        NEW.value = NEW.dest -> 'Args' ->> 'phone_number';
    ELSIF NEW.dest ->> 'Type' = 'builtin-twilio-sms' THEN
        NEW.type = 'SMS';
        NEW.value = NEW.dest -> 'Args' ->> 'phone_number';
    ELSIF NEW.dest ->> 'Type' = 'builtin-webhook' THEN
        NEW.type = 'WEBHOOK';
        NEW.value = NEW.dest -> 'Args' ->> 'webhook_url';
    ELSIF NEW.dest ->> 'Type' = 'builtin-slack-dm' THEN
        NEW.type = 'SLACK_DM';
        NEW.value = NEW.dest -> 'Args' ->> 'slack_user_id';
    ELSE
        NEW.type = 'DEST';
        NEW.value = gen_random_uuid()::text;
    END IF;
    RETURN new;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_cm_set_dest_on_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    NEW.dest = cm_type_val_to_dest(NEW.type, NEW.value);
    RETURN new;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_cm_set_not_pending_on_verify()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    NEW.pending = FALSE;
  RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_decr_ep_step_count_on_del()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    UPDATE escalation_policies
    SET step_count = step_count - 1
    WHERE id = OLD.escalation_policy_id;

    RETURN OLD;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_decr_ep_step_number_on_delete()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    LOCK escalation_policy_steps IN EXCLUSIVE MODE;

    UPDATE escalation_policy_steps
    SET step_number = step_number - 1
    WHERE
        escalation_policy_id = OLD.escalation_policy_id AND
        step_number > OLD.step_number;

    RETURN OLD;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_decr_part_count_on_del()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    UPDATE rotations
    SET participant_count = participant_count - 1
    WHERE id = OLD.rotation_id;

    RETURN OLD;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_decr_rot_part_position_on_delete()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    LOCK rotation_participants IN EXCLUSIVE MODE;

    UPDATE rotation_participants
    SET position = position - 1
    WHERE
        rotation_id = OLD.rotation_id AND
        position > OLD.position;

    RETURN OLD;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_disable_inserts()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    RAISE EXCEPTION 'inserts are disabled on this table';
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_alert_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'unacked_alerts_per_service';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM alerts
    WHERE service_id = NEW.service_id AND "status" = 'triggered';

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='unacked_alerts_per_service_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_calendar_subscriptions_per_user_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'calendar_subscriptions_per_user';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM user_calendar_subscriptions
    WHERE user_id = NEW.user_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='calendar_subscriptions_per_user_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_contact_method_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'contact_methods_per_user';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM user_contact_methods
    WHERE user_id = NEW.user_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='contact_methods_per_user_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_ep_step_action_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'ep_actions_per_step';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM escalation_policy_actions
    WHERE escalation_policy_step_id = NEW.escalation_policy_step_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='ep_actions_per_step_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_ep_step_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'ep_steps_per_policy';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM escalation_policy_steps
    WHERE escalation_policy_id = NEW.escalation_policy_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='ep_steps_per_policy_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_ep_step_number_no_gaps()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_pos INT := -1;
    step_count INT := 0;
BEGIN
    IF NEW.escalation_policy_id != OLD.escalation_policy_id THEN
        RAISE 'must not change escalation_policy_id of existing step';
    END IF;

    SELECT max(step_number), count(*)
    INTO max_pos, step_count
    FROM escalation_policy_steps
    WHERE escalation_policy_id = NEW.escalation_policy_id;

    IF max_pos >= step_count THEN
        RAISE 'must not have gap in step_numbers';
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_heartbeat_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'heartbeat_monitors_per_service';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM heartbeat_monitors
    WHERE service_id = NEW.service_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='heartbeat_monitors_per_service_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_integration_key_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'integration_keys_per_service';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM integration_keys
    WHERE service_id = NEW.service_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='integration_keys_per_service_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_notification_rule_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'notification_rules_per_user';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM user_notification_rules
    WHERE user_id = NEW.user_id;

    IF max_count != -1 AND val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='notification_rules_per_user_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_rot_part_position_no_gaps()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_pos INT := -1;
    part_count INT := 0;
BEGIN
    IF NEW.rotation_id != OLD.rotation_id THEN
        RAISE 'must not change rotation_id of existing participant';
    END IF;

    SELECT max(position), count(*)
    INTO max_pos, part_count
    FROM rotation_participants
    WHERE rotation_id = NEW.rotation_id;

    IF max_pos >= part_count THEN
        RAISE 'must not have gap in participant positions';
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_rotation_participant_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'participants_per_rotation';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM rotation_participants
    WHERE rotation_id = NEW.rotation_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='participants_per_rotation_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_schedule_rule_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'rules_per_schedule';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM schedule_rules
    WHERE schedule_id = NEW.schedule_id;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='rules_per_schedule_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_schedule_target_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'targets_per_schedule';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM (
        SELECT DISTINCT tgt_user_id, tgt_rotation_id
        FROM schedule_rules
        WHERE schedule_id = NEW.schedule_id
    ) as tmp;

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='targets_per_schedule_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_signals_per_dest_per_service_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count int := - 1;
    val_count int := 0;
BEGIN
    SELECT
        INTO max_count max
    FROM
        config_limits
    WHERE
        id = 'pending_signals_per_dest_per_service';
    IF max_count = - 1 THEN
        RETURN NEW;
    END IF;
    SELECT
        INTO val_count COUNT(*)
    FROM
        pending_signals
    WHERE
        service_id = NEW.service_id
        AND dest_id = NEW.dest_id
        AND message_id IS NULL;
    IF val_count > max_count THEN
        RAISE 'limit exceeded'
        USING ERRCODE = 'check_violation', CONSTRAINT = 'pending_signals_per_dest_per_service_limit', HINT = 'max=' || max_count;
    END IF;
        RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_signals_per_service_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count int := - 1;
    val_count int := 0;
BEGIN
    SELECT
        INTO max_count max
    FROM
        config_limits
    WHERE
        id = 'pending_signals_per_service';
    IF max_count = - 1 THEN
        RETURN NEW;
    END IF;
    SELECT
        INTO val_count COUNT(*)
    FROM
        pending_signals
    WHERE
        service_id = NEW.service_id
        AND message_id IS NULL;
    IF val_count > max_count THEN
        RAISE 'limit exceeded'
        USING ERRCODE = 'check_violation', CONSTRAINT = 'pending_signals_per_service_limit', HINT = 'max=' || max_count;
    END IF;
        RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_status_update_same_user()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    _cm_user_id UUID;
BEGIN
    IF NEW.alert_status_log_contact_method_id ISNULL THEN
        RETURN NEW;
    END IF;

    SELECT INTO _cm_user_id user_id
    FROM user_contact_methods
    WHERE id = NEW.alert_status_log_contact_method_id;

    IF NEW.id != _cm_user_id THEN
        RAISE 'wrong user_id' USING ERRCODE='check_violation', CONSTRAINT='alert_status_user_id_match';
    END IF;
    
    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_user_overide_no_conflict()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    conflict UUID := NULL;
BEGIN
    EXECUTE 'LOCK user_overrides IN EXCLUSIVE MODE';

    SELECT id INTO conflict
    FROM user_overrides
    WHERE
        id != NEW.id AND
        tgt_schedule_id = NEW.tgt_schedule_id AND
        (
            add_user_id in (NEW.remove_user_id, NEW.add_user_id) OR
            remove_user_id in (NEW.remove_user_id, NEW.add_user_id)
        ) AND
        (start_time, end_time) OVERLAPS (NEW.start_time, NEW.end_time)
    LIMIT 1;
  
    IF conflict NOTNULL THEN
        RAISE 'override conflict' USING ERRCODE='check_violation', CONSTRAINT='user_override_no_conflict_allowed', HINT='CONFLICTING_ID='||conflict::text;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_enforce_user_override_schedule_limit()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    max_count INT := -1;
    val_count INT := 0;
BEGIN
    SELECT INTO max_count max
    FROM config_limits
    WHERE id = 'user_overrides_per_schedule';

    IF max_count = -1 THEN
        RETURN NEW;
    END IF;

    SELECT INTO val_count COUNT(*)
    FROM user_overrides
    WHERE
        tgt_schedule_id = NEW.tgt_schedule_id AND
        end_time > now();

    IF val_count > max_count THEN
        RAISE 'limit exceeded' USING ERRCODE='check_violation', CONSTRAINT='user_overrides_per_schedule_limit', HINT='max='||max_count;
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_inc_ep_step_number_on_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    LOCK escalation_policy_steps IN EXCLUSIVE MODE;

    SELECT count(*)
    INTO NEW.step_number
    FROM escalation_policy_steps
    WHERE escalation_policy_id = NEW.escalation_policy_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_inc_rot_part_position_on_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    LOCK rotation_participants IN EXCLUSIVE MODE;

    SELECT count(*)
    INTO NEW.position
    FROM rotation_participants
    WHERE rotation_id = NEW.rotation_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_incr_ep_step_count_on_add()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    UPDATE escalation_policies
    SET step_count = step_count + 1
    WHERE id = NEW.escalation_policy_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_incr_part_count_on_add()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    UPDATE rotations
    SET participant_count = participant_count + 1
    WHERE id = NEW.rotation_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_insert_basic_user()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    INSERT INTO auth_subjects (provider_id, subject_id, user_id)
    VALUES ('basic', NEW.username, NEW.user_id)
    ON CONFLICT (provider_id, subject_id) DO UPDATE
    SET user_id = NEW.user_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_insert_ep_state_on_alert_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    INSERT INTO escalation_policy_state (alert_id, service_id, escalation_policy_id)
    SELECT NEW.id, NEW.service_id, svc.escalation_policy_id
    FROM services svc
    JOIN escalation_policies ep ON ep.id = svc.escalation_policy_id AND ep.step_count > 0
    WHERE svc.id = NEW.service_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_insert_ep_state_on_step_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    INSERT INTO escalation_policy_state (alert_id, service_id, escalation_policy_id)
    SELECT a.id, a.service_id, NEW.escalation_policy_id
    FROM alerts a
    JOIN services svc ON
        svc.id = a.service_id AND
        svc.escalation_policy_id = NEW.escalation_policy_id
    WHERE a.status != 'closed';

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_insert_message_status_history()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    INSERT INTO message_status_history(message_id, status, timestamp, status_details)
        VALUES(NEW.id, NEW.last_status, NEW.last_status_at, NEW.status_details);
    RETURN new;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_insert_user_last_alert_log()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    INSERT INTO user_last_alert_log (user_id, alert_id, log_id, next_log_id)
    VALUES (NEW.sub_user_id, NEW.alert_id, NEW.id, NEW.id)
    ON CONFLICT DO NOTHING;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_lock_svc_on_force_escalation()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    -- lock service first
    PERFORM 1
    FROM services svc
    WHERE svc.id = NEW.service_id
    FOR UPDATE;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_nc_compat_set_type_val_on_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    IF NEW.dest ->> 'Type' = 'builtin-slack-channel' THEN
        NEW.type = 'SLACK';
        NEW.value = NEW.dest -> 'Args' ->> 'slack_channel_id';
    ELSIF NEW.dest ->> 'Type' = 'builtin-slack-usergroup' THEN
        NEW.type = 'SLACK_USER_GROUP';
        NEW.value =(NEW.dest -> 'Args' ->> 'slack_usergroup_id') || ':' ||(NEW.dest -> 'Args' ->> 'slack_channel_id');
    ELSIF NEW.dest ->> 'Type' = 'builtin-webhook' THEN
        NEW.type = 'WEBHOOK';
        NEW.value = NEW.dest -> 'Args' ->> 'webhook_url';
    ELSE
        NEW.type = 'DEST';
        NEW.value = gen_random_uuid()::text;
    END IF;
    RETURN new;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_nc_set_dest_on_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    NEW.dest = nc_type_val_to_dest(NEW.type, NEW.value);
    RETURN new;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_notification_rule_same_user()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    _cm_user_id UUID;
BEGIN
    SELECT INTO _cm_user_id user_id
    FROM user_contact_methods
    WHERE id = NEW.contact_method_id;

    IF NEW.user_id != _cm_user_id THEN
        RAISE 'wrong user_id' USING ERRCODE='check_violation', CONSTRAINT='notification_rule_user_id_match';
    END IF;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_notify_config_refresh()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
    BEGIN
        NOTIFY "/goalert/config-refresh";
        RETURN NEW;
    END;
    $function$
;

CREATE OR REPLACE FUNCTION public.fn_prevent_reopen()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
    BEGIN
        IF OLD.status = 'closed' THEN
            RAISE EXCEPTION 'cannot change status of closed alert';
        END IF;
        RETURN NEW;
    END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_set_ep_state_svc_id_on_insert()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    SELECT service_id INTO NEW.service_id
    FROM alerts
    WHERE id = NEW.alert_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_set_rot_state_pos_on_active_change()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    SELECT position INTO NEW.position
    FROM rotation_participants
    WHERE id = NEW.rotation_participant_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_set_rot_state_pos_on_part_reorder()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    UPDATE rotation_state
    SET position = NEW.position
    WHERE rotation_participant_id = NEW.id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_start_rotation_on_first_part_add()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
DECLARE
    first_part UUID;
BEGIN
    SELECT id
    INTO first_part
    FROM rotation_participants
    WHERE rotation_id = NEW.rotation_id AND position = 0;

    INSERT INTO rotation_state (
        rotation_id, rotation_participant_id, shift_start
    ) VALUES (
        NEW.rotation_id, first_part, now()
    ) ON CONFLICT DO NOTHING;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_track_rotation_updates()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN
    IF TG_TABLE_NAME = 'rotations' THEN
        INSERT INTO entity_updates(entity_type, entity_id)
            VALUES('rotation', NEW.id);
    ELSIF TG_OP = 'DELETE' THEN
        INSERT INTO entity_updates(entity_type, entity_id)
            VALUES('rotation', OLD.rotation_id);
        RETURN OLD;
    ELSE
        INSERT INTO entity_updates(entity_type, entity_id)
            VALUES('rotation', NEW.rotation_id);
    END IF;
    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_trig_alert_on_force_escalation()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    UPDATE alerts
    SET "status" = 'triggered'
    WHERE id = NEW.alert_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.fn_update_user_last_alert_log()
 RETURNS trigger
 LANGUAGE plpgsql
AS $function$
BEGIN

    UPDATE user_last_alert_log last
    SET next_log_id = NEW.id
    WHERE
        last.alert_id = NEW.alert_id AND
        NEW.id > last.next_log_id;

    RETURN NEW;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.move_escalation_policy_step(_id uuid, _new_pos integer)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
  DECLARE
    _old_pos INT;
    _epid UUID;
  BEGIN
    SELECT step_number, escalation_policy_id into _old_pos, _epid FROM escalation_policy_steps WHERE id = _id;
    IF _old_pos > _new_pos THEN
      UPDATE escalation_policy_steps
      SET step_number = step_number + 1
      WHERE escalation_policy_id = _epid
        AND step_number < _old_pos
        AND step_number >= _new_pos;
    ELSE
      UPDATE escalation_policy_steps
      SET step_number = step_number - 1
      WHERE escalation_policy_id = _epid
        AND step_number > _old_pos
        AND step_number <= _new_pos;
    END IF;
    UPDATE escalation_policy_steps
    SET step_number = _new_pos
    WHERE id = _id;
  END;
  $function$
;

CREATE OR REPLACE FUNCTION public.move_rotation_position(_id uuid, _new_pos integer)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
    DECLARE
        _old_pos INT;
        _rid UUID;
    BEGIN
        SELECT position,rotation_id into _old_pos, _rid FROM rotation_participants WHERE id = _id;
        IF _old_pos > _new_pos THEN
            UPDATE rotation_participants SET position = position + 1 WHERE rotation_id = _rid AND position < _old_pos AND position >= _new_pos;
        ELSE
            UPDATE rotation_participants SET position = position - 1 WHERE rotation_id = _rid AND position > _old_pos AND position <= _new_pos;
        END IF;
        UPDATE rotation_participants SET position = _new_pos WHERE id = _id;
    END;
    $function$
;

CREATE OR REPLACE FUNCTION public.nc_type_val_to_dest(typename enum_notif_channel_type, value text)
 RETURNS jsonb
 LANGUAGE plpgsql
AS $function$
BEGIN
    IF typeName = 'SLACK' THEN
        RETURN jsonb_build_object('Type', 'builtin-slack-channel', 'Args', jsonb_build_object('slack_channel_id', value));
    ELSIF typeName = 'WEBHOOK' THEN
        RETURN jsonb_build_object('Type', 'builtin-webhook', 'Args', jsonb_build_object('webhook_url', value));
    ELSIF typeName = 'SLACK_USER_GROUP' THEN
        RETURN jsonb_build_object('Type', 'builtin-slack-usergroup', 'Args', jsonb_build_object('slack_usergroup_id', split_part(value, ':', 1), 'slack_channel_id', split_part(value, ':', 2)));
    ELSE
        -- throw an error
        RAISE EXCEPTION 'Unknown notification channel type: %', typeName;
    END IF;
END;
$function$
;

CREATE OR REPLACE FUNCTION public.release_user_contact_method_lock(_client_id uuid, _id uuid, success boolean)
 RETURNS void
 LANGUAGE plpgsql
AS $function$
        BEGIN
            DELETE FROM user_contact_method_locks WHERE id = _id AND client_id = _client_id;
            IF success
            THEN
                UPDATE sent_notifications SET sent_at = now() WHERE id = _id;
            ELSE
                DELETE FROM sent_notifications WHERE id = _id;
            END IF;
        END;
    $function$
;

CREATE OR REPLACE FUNCTION public.remove_rotation_participant(_id uuid)
 RETURNS uuid
 LANGUAGE plpgsql
AS $function$
    DECLARE
        _old_pos INT;
        _rid UUID;
    BEGIN
        SELECT position,rotation_id into _old_pos, _rid FROM rotation_participants WHERE id = _id;
        DELETE FROM rotation_participants WHERE id = _id;
        UPDATE rotation_participants SET position = position - 1 WHERE rotation_id = _rid AND position > _old_pos;
        RETURN _rid;
    END;
    $function$
;

CREATE OR REPLACE FUNCTION public.river_job_state_in_bitmask(bitmask bit, state river_job_state)
 RETURNS boolean
 LANGUAGE sql
 IMMUTABLE
AS $function$
    SELECT CASE state
        WHEN 'available' THEN get_bit(bitmask, 7)
        WHEN 'cancelled' THEN get_bit(bitmask, 6)
        WHEN 'completed' THEN get_bit(bitmask, 5)
        WHEN 'discarded' THEN get_bit(bitmask, 4)
        WHEN 'pending' THEN get_bit(bitmask, 3)
        WHEN 'retryable' THEN get_bit(bitmask, 2)
        WHEN 'running' THEN get_bit(bitmask, 1)
        WHEN 'scheduled' THEN get_bit(bitmask, 0)
        ELSE 0
    END = 1;
$function$
;

CREATE OR REPLACE FUNCTION public.update_notification_cycles()
 RETURNS void
 LANGUAGE plpgsql
AS $function$
        BEGIN
			INSERT INTO user_notification_cycles (user_id, alert_id, escalation_level)
			SELECT user_id, alert_id, escalation_level
			FROM on_call_alert_users
			WHERE status = 'triggered'
                AND user_id IS NOT NULL
			ON CONFLICT DO NOTHING;

			UPDATE user_notification_cycles c
			SET escalation_level = a.escalation_level
			FROM
				alerts a,
				user_notification_cycle_state s
			WHERE a.id = c.alert_id
				AND s.user_id = c.user_id
				AND s.alert_id = c.alert_id;

			DELETE FROM user_notification_cycles c
			WHERE (
				SELECT count(notification_rule_id)
				FROM user_notification_cycle_state s
				WHERE s.alert_id = c.alert_id AND s.user_id = c.user_id
				LIMIT 1
			) = 0
				AND c.escalation_level != (SELECT escalation_level FROM alerts WHERE id = c.alert_id);

        END;
    $function$
;

-- Tables

CREATE TABLE alert_data (
	alert_id bigint NOT NULL,
	id bigint DEFAULT nextval('alert_data_id_seq'::regclass) NOT NULL,
	metadata jsonb,
	CONSTRAINT alert_data_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT alert_data_id_key UNIQUE (id),
	CONSTRAINT alert_data_pkey PRIMARY KEY (alert_id)
);

CREATE UNIQUE INDEX alert_data_id_key ON public.alert_data USING btree (id);
CREATE UNIQUE INDEX alert_data_pkey ON public.alert_data USING btree (alert_id);


CREATE TABLE alert_feedback (
	alert_id bigint NOT NULL,
	id bigint DEFAULT nextval('alert_feedback_id_seq'::regclass) NOT NULL,
	noise_reason text NOT NULL,
	CONSTRAINT alert_feedback_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT alert_feedback_id_key UNIQUE (id),
	CONSTRAINT alert_feedback_pkey PRIMARY KEY (alert_id)
);

CREATE UNIQUE INDEX alert_feedback_id_key ON public.alert_feedback USING btree (id);
CREATE UNIQUE INDEX alert_feedback_pkey ON public.alert_feedback USING btree (alert_id);


CREATE TABLE alert_logs (
	alert_id bigint,
	event enum_alert_log_event NOT NULL,
	id bigint DEFAULT nextval('alert_logs_id_seq'::regclass) NOT NULL,
	message text NOT NULL,
	meta json,
	sub_channel_id uuid,
	sub_classifier text DEFAULT ''::text NOT NULL,
	sub_hb_monitor_id uuid,
	sub_integration_key_id uuid,
	sub_type enum_alert_log_subject_type,
	sub_user_id uuid,
	timestamp timestamp with time zone DEFAULT now(),
	CONSTRAINT alert_logs_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX alert_logs_pkey ON public.alert_logs USING btree (id);
CREATE INDEX idx_alert_logs_alert_event ON public.alert_logs USING btree (alert_id, event);
CREATE INDEX idx_alert_logs_alert_id ON public.alert_logs USING btree (alert_id);
CREATE INDEX idx_alert_logs_channel_id ON public.alert_logs USING btree (sub_channel_id);
CREATE INDEX idx_alert_logs_hb_id ON public.alert_logs USING btree (sub_hb_monitor_id);
CREATE INDEX idx_alert_logs_int_id ON public.alert_logs USING btree (sub_integration_key_id);
CREATE INDEX idx_alert_logs_user_id ON public.alert_logs USING btree (sub_user_id);
CREATE INDEX idx_closed_events ON public.alert_logs USING btree ("timestamp") WHERE (event = 'closed'::enum_alert_log_event);


CREATE TABLE alert_metrics (
	alert_id bigint NOT NULL,
	closed_at timestamp with time zone NOT NULL,
	escalated boolean DEFAULT false NOT NULL,
	id bigint DEFAULT nextval('alert_metrics_id_seq'::regclass) NOT NULL,
	service_id uuid NOT NULL,
	time_to_ack interval,
	time_to_close interval,
	CONSTRAINT alert_metrics_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT alert_metrics_id_key UNIQUE (id),
	CONSTRAINT alert_metrics_pkey PRIMARY KEY (alert_id)
);

CREATE INDEX alert_metrics_closed_date_idx ON public.alert_metrics USING btree (date(timezone('UTC'::text, closed_at)));
CREATE UNIQUE INDEX alert_metrics_id_key ON public.alert_metrics USING btree (id);
CREATE UNIQUE INDEX alert_metrics_pkey ON public.alert_metrics USING btree (alert_id);


CREATE TABLE alert_status_subscriptions (
	alert_id bigint NOT NULL,
	channel_id uuid,
	contact_method_id uuid,
	id bigint DEFAULT nextval('alert_status_subscriptions_id_seq'::regclass) NOT NULL,
	last_alert_status enum_alert_status NOT NULL,
	updated_at timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT alert_status_subscriptions_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT alert_status_subscriptions_channel_id_contact_method_id_ale_key UNIQUE (channel_id, contact_method_id, alert_id),
	CONSTRAINT alert_status_subscriptions_channel_id_fkey FOREIGN KEY (channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE,
	CONSTRAINT alert_status_subscriptions_check CHECK ((channel_id IS NULL) <> (contact_method_id IS NULL)),
	CONSTRAINT alert_status_subscriptions_contact_method_id_fkey FOREIGN KEY (contact_method_id) REFERENCES user_contact_methods(id) ON DELETE CASCADE,
	CONSTRAINT alert_status_subscriptions_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX alert_status_subscriptions_channel_id_contact_method_id_ale_key ON public.alert_status_subscriptions USING btree (channel_id, contact_method_id, alert_id);
CREATE UNIQUE INDEX alert_status_subscriptions_pkey ON public.alert_status_subscriptions USING btree (id);
CREATE INDEX alert_status_subscriptions_updated_at_idx ON public.alert_status_subscriptions USING btree (updated_at);


CREATE TABLE alerts (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	dedup_key text,
	details text DEFAULT ''::text NOT NULL,
	escalation_level integer DEFAULT 0 NOT NULL,
	id bigint DEFAULT nextval('alerts_id_seq'::regclass) NOT NULL,
	last_escalation timestamp with time zone DEFAULT now(),
	last_processed timestamp with time zone,
	service_id uuid,
	source enum_alert_source DEFAULT 'manual'::enum_alert_source NOT NULL,
	status enum_alert_status DEFAULT 'triggered'::enum_alert_status NOT NULL,
	summary text NOT NULL,
	CONSTRAINT alerts_pkey PRIMARY KEY (id),
	CONSTRAINT alerts_services_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE,
	CONSTRAINT dedup_key_only_for_open_alerts CHECK ((status = 'closed'::enum_alert_status) = (dedup_key IS NULL))
);

CREATE UNIQUE INDEX alerts_pkey ON public.alerts USING btree (id);
CREATE INDEX idx_alert_cleanup ON public.alerts USING btree (id, created_at) WHERE (status = 'closed'::enum_alert_status);
CREATE INDEX idx_alert_service_id ON public.alerts USING btree (service_id);
CREATE INDEX idx_dedup_alerts ON public.alerts USING btree (dedup_key);
CREATE UNIQUE INDEX idx_no_alert_duplicates ON public.alerts USING btree (service_id, dedup_key);
CREATE INDEX idx_search_alerts_summary_eng ON public.alerts USING gin (to_tsvector('english'::regconfig, replace(lower(summary), '.'::text, ' '::text)));
CREATE INDEX idx_unacked_alert_service ON public.alerts USING btree (status, service_id);

CREATE TRIGGER trg_10_clear_ep_state_on_alert_close AFTER UPDATE ON public.alerts FOR EACH ROW WHEN (((old.status <> new.status) AND (new.status = 'closed'::enum_alert_status))) EXECUTE FUNCTION fn_clear_ep_state_on_alert_close();
CREATE TRIGGER trg_10_insert_ep_state_on_alert_insert AFTER INSERT ON public.alerts FOR EACH ROW WHEN ((new.status <> 'closed'::enum_alert_status)) EXECUTE FUNCTION fn_insert_ep_state_on_alert_insert();
CREATE TRIGGER trg_20_clear_next_esc_on_alert_ack AFTER UPDATE ON public.alerts FOR EACH ROW WHEN (((new.status <> old.status) AND (old.status = 'active'::enum_alert_status))) EXECUTE FUNCTION fn_clear_next_esc_on_alert_ack();
CREATE TRIGGER trg_clear_dedup_on_close BEFORE UPDATE ON public.alerts FOR EACH ROW WHEN (((new.status <> old.status) AND (new.status = 'closed'::enum_alert_status))) EXECUTE FUNCTION fn_clear_dedup_on_close();
CREATE CONSTRAINT TRIGGER trg_enforce_alert_limit AFTER INSERT ON public.alerts NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_alert_limit();
CREATE TRIGGER trg_prevent_reopen BEFORE UPDATE OF status ON public.alerts FOR EACH ROW EXECUTE FUNCTION fn_prevent_reopen();


CREATE TABLE auth_basic_users (
	id bigint DEFAULT nextval('auth_basic_users_id_seq'::regclass) NOT NULL,
	password_hash text NOT NULL,
	user_id uuid NOT NULL,
	username text NOT NULL,
	CONSTRAINT auth_basic_users_pkey PRIMARY KEY (user_id),
	CONSTRAINT auth_basic_users_uniq_id UNIQUE (id),
	CONSTRAINT auth_basic_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
	CONSTRAINT auth_basic_users_username_key UNIQUE (username)
);

CREATE UNIQUE INDEX auth_basic_users_pkey ON public.auth_basic_users USING btree (user_id);
CREATE UNIQUE INDEX auth_basic_users_uniq_id ON public.auth_basic_users USING btree (id);
CREATE UNIQUE INDEX auth_basic_users_username_key ON public.auth_basic_users USING btree (username);

CREATE TRIGGER trg_insert_basic_user AFTER INSERT ON public.auth_basic_users FOR EACH ROW EXECUTE FUNCTION fn_insert_basic_user();


CREATE TABLE auth_link_requests (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	expires_at timestamp with time zone NOT NULL,
	id uuid NOT NULL,
	metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
	provider_id text NOT NULL,
	subject_id text NOT NULL,
	CONSTRAINT auth_link_requests_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX auth_link_requests_pkey ON public.auth_link_requests USING btree (id);


CREATE TABLE auth_nonce (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	id uuid NOT NULL,
	CONSTRAINT auth_nonce_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX auth_nonce_pkey ON public.auth_nonce USING btree (id);


CREATE TABLE auth_subjects (
	cm_id uuid,
	id bigint DEFAULT nextval('auth_subjects_id_seq'::regclass) NOT NULL,
	provider_id text NOT NULL,
	subject_id text NOT NULL,
	user_id uuid NOT NULL,
	CONSTRAINT auth_subjects_cm_id_fkey FOREIGN KEY (cm_id) REFERENCES user_contact_methods(id) ON DELETE CASCADE,
	CONSTRAINT auth_subjects_pkey PRIMARY KEY (provider_id, subject_id),
	CONSTRAINT auth_subjects_uniq_id UNIQUE (id),
	CONSTRAINT auth_subjects_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX auth_subjects_pkey ON public.auth_subjects USING btree (provider_id, subject_id);
CREATE UNIQUE INDEX auth_subjects_uniq_id ON public.auth_subjects USING btree (id);


CREATE TABLE auth_user_sessions (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	id uuid NOT NULL,
	last_access_at timestamp with time zone DEFAULT now() NOT NULL,
	user_agent text DEFAULT ''::text NOT NULL,
	user_id uuid,
	CONSTRAINT auth_user_sessions_pkey PRIMARY KEY (id),
	CONSTRAINT auth_user_sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX auth_user_sessions_pkey ON public.auth_user_sessions USING btree (id);


CREATE TABLE config (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	data bytea NOT NULL,
	id integer DEFAULT nextval('config_id_seq'::regclass) NOT NULL,
	schema integer NOT NULL,
	CONSTRAINT config_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX config_pkey ON public.config USING btree (id);

CREATE TRIGGER trg_config_update AFTER INSERT ON public.config FOR EACH ROW EXECUTE FUNCTION fn_notify_config_refresh();


CREATE TABLE config_limits (
	id enum_limit_type NOT NULL,
	max integer DEFAULT '-1'::integer NOT NULL,
	CONSTRAINT config_limits_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX config_limits_pkey ON public.config_limits USING btree (id);


CREATE TABLE engine_processing_versions (
	state jsonb DEFAULT '{}'::jsonb NOT NULL,
	type_id engine_processing_type NOT NULL,
	version integer DEFAULT 1 NOT NULL,
	CONSTRAINT engine_processing_versions_pkey PRIMARY KEY (type_id)
);

CREATE UNIQUE INDEX engine_processing_versions_pkey ON public.engine_processing_versions USING btree (type_id);


CREATE TABLE entity_updates (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	entity_id uuid NOT NULL,
	entity_type text NOT NULL,
	id bigint DEFAULT nextval('entity_updates_id_seq'::regclass) NOT NULL,
	CONSTRAINT entity_updates_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX entity_updates_pkey ON public.entity_updates USING btree (id);
CREATE INDEX idx_entity_updates_entity_type ON public.entity_updates USING btree (entity_type);


CREATE TABLE ep_step_on_call_users (
	end_time timestamp with time zone,
	ep_step_id uuid NOT NULL,
	id bigint DEFAULT nextval('ep_step_on_call_users_id_seq'::regclass) NOT NULL,
	start_time timestamp with time zone DEFAULT now() NOT NULL,
	user_id uuid NOT NULL,
	CONSTRAINT ep_step_on_call_users_ep_step_id_fkey FOREIGN KEY (ep_step_id) REFERENCES escalation_policy_steps(id) ON DELETE CASCADE,
	CONSTRAINT ep_step_on_call_users_uniq_id UNIQUE (id),
	CONSTRAINT ep_step_on_call_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX ep_step_on_call_users_uniq_id ON public.ep_step_on_call_users USING btree (id);
CREATE UNIQUE INDEX idx_ep_step_on_call ON public.ep_step_on_call_users USING btree (user_id, ep_step_id) WHERE (end_time IS NULL);


CREATE TABLE escalation_policies (
	description text DEFAULT ''::text NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	name text NOT NULL,
	repeat integer DEFAULT 0 NOT NULL,
	step_count integer DEFAULT 0 NOT NULL,
	CONSTRAINT escalation_policies_name_key UNIQUE (name),
	CONSTRAINT escalation_policies_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX escalation_policies_name ON public.escalation_policies USING btree (lower(name));
CREATE UNIQUE INDEX escalation_policies_name_key ON public.escalation_policies USING btree (name);
CREATE UNIQUE INDEX escalation_policies_pkey ON public.escalation_policies USING btree (id);
CREATE INDEX idx_search_escalation_policies_desc_eng ON public.escalation_policies USING gin (to_tsvector('english'::regconfig, replace(lower(description), '.'::text, ' '::text)));
CREATE INDEX idx_search_escalation_policies_name_eng ON public.escalation_policies USING gin (to_tsvector('english'::regconfig, replace(lower(name), '.'::text, ' '::text)));


CREATE TABLE escalation_policy_actions (
	channel_id uuid,
	escalation_policy_step_id uuid NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	rotation_id uuid,
	schedule_id uuid,
	user_id uuid,
	CONSTRAINT epa_no_duplicate_channels UNIQUE (escalation_policy_step_id, channel_id),
	CONSTRAINT epa_no_duplicate_rotations UNIQUE (escalation_policy_step_id, rotation_id),
	CONSTRAINT epa_no_duplicate_schedules UNIQUE (escalation_policy_step_id, schedule_id),
	CONSTRAINT epa_no_duplicate_users UNIQUE (escalation_policy_step_id, user_id),
	CONSTRAINT epa_there_can_only_be_one CHECK ((
CASE
    WHEN user_id IS NOT NULL THEN 1
    ELSE 0
END +
CASE
    WHEN schedule_id IS NOT NULL THEN 1
    ELSE 0
END +
CASE
    WHEN rotation_id IS NOT NULL THEN 1
    ELSE 0
END +
CASE
    WHEN channel_id IS NOT NULL THEN 1
    ELSE 0
END) = 1),
	CONSTRAINT escalation_policy_actions_channel_id_fkey FOREIGN KEY (channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_actions_escalation_policy_step_id_fkey FOREIGN KEY (escalation_policy_step_id) REFERENCES escalation_policy_steps(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_actions_pkey PRIMARY KEY (id),
	CONSTRAINT escalation_policy_actions_rotation_id_fkey FOREIGN KEY (rotation_id) REFERENCES rotations(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_actions_schedule_id_fkey1 FOREIGN KEY (schedule_id) REFERENCES schedules(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_actions_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX epa_no_duplicate_channels ON public.escalation_policy_actions USING btree (escalation_policy_step_id, channel_id);
CREATE UNIQUE INDEX epa_no_duplicate_rotations ON public.escalation_policy_actions USING btree (escalation_policy_step_id, rotation_id);
CREATE UNIQUE INDEX epa_no_duplicate_schedules ON public.escalation_policy_actions USING btree (escalation_policy_step_id, schedule_id);
CREATE UNIQUE INDEX epa_no_duplicate_users ON public.escalation_policy_actions USING btree (escalation_policy_step_id, user_id);
CREATE UNIQUE INDEX escalation_policy_actions_pkey ON public.escalation_policy_actions USING btree (id);
CREATE INDEX idx_ep_action_steps ON public.escalation_policy_actions USING btree (escalation_policy_step_id);

CREATE CONSTRAINT TRIGGER trg_enforce_ep_step_action_limit AFTER INSERT ON public.escalation_policy_actions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_ep_step_action_limit();


CREATE TABLE escalation_policy_state (
	alert_id bigint NOT NULL,
	escalation_policy_id uuid NOT NULL,
	escalation_policy_step_id uuid,
	escalation_policy_step_number integer DEFAULT 0 NOT NULL,
	force_escalation boolean DEFAULT false NOT NULL,
	id bigint DEFAULT nextval('escalation_policy_state_id_seq'::regclass) NOT NULL,
	last_escalation timestamp with time zone,
	loop_count integer DEFAULT 0 NOT NULL,
	next_escalation timestamp with time zone,
	service_id uuid NOT NULL,
	CONSTRAINT escalation_policy_state_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_state_escalation_policy_id_fkey FOREIGN KEY (escalation_policy_id) REFERENCES escalation_policies(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_state_escalation_policy_step_id_fkey FOREIGN KEY (escalation_policy_step_id) REFERENCES escalation_policy_steps(id) ON DELETE SET NULL,
	CONSTRAINT escalation_policy_state_pkey PRIMARY KEY (alert_id),
	CONSTRAINT escalation_policy_state_service_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_state_uniq_id UNIQUE (id),
	CONSTRAINT svc_ep_fkey FOREIGN KEY (service_id, escalation_policy_id) REFERENCES services(id, escalation_policy_id) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE
);

CREATE INDEX escalation_policy_state_next_escalation_force_escalation_idx ON public.escalation_policy_state USING btree (next_escalation, force_escalation);
CREATE UNIQUE INDEX escalation_policy_state_pkey ON public.escalation_policy_state USING btree (alert_id);
CREATE UNIQUE INDEX escalation_policy_state_uniq_id ON public.escalation_policy_state USING btree (id);
CREATE INDEX idx_escalation_policy_state_policy_ids ON public.escalation_policy_state USING btree (escalation_policy_id, service_id);

CREATE TRIGGER trg_10_set_ep_state_svc_id_on_insert BEFORE INSERT ON public.escalation_policy_state FOR EACH ROW WHEN ((new.service_id IS NULL)) EXECUTE FUNCTION fn_set_ep_state_svc_id_on_insert();
CREATE TRIGGER trg_20_lock_svc_on_force_escalation BEFORE UPDATE ON public.escalation_policy_state FOR EACH ROW WHEN (((new.force_escalation <> old.force_escalation) AND new.force_escalation)) EXECUTE FUNCTION fn_lock_svc_on_force_escalation();
CREATE TRIGGER trg_30_trig_alert_on_force_escalation AFTER UPDATE ON public.escalation_policy_state FOR EACH ROW WHEN (((new.force_escalation <> old.force_escalation) AND new.force_escalation)) EXECUTE FUNCTION fn_trig_alert_on_force_escalation();


CREATE TABLE escalation_policy_steps (
	delay integer DEFAULT 1 NOT NULL,
	escalation_policy_id uuid NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	step_number integer DEFAULT '-1'::integer NOT NULL,
	CONSTRAINT escalation_policy_steps_escalation_policy_id_fkey FOREIGN KEY (escalation_policy_id) REFERENCES escalation_policies(id) ON DELETE CASCADE,
	CONSTRAINT escalation_policy_steps_escalation_policy_id_step_number_key UNIQUE (escalation_policy_id, step_number) DEFERRABLE INITIALLY DEFERRED,
	CONSTRAINT escalation_policy_steps_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX escalation_policy_steps_escalation_policy_id_step_number_key ON public.escalation_policy_steps USING btree (escalation_policy_id, step_number);
CREATE UNIQUE INDEX escalation_policy_steps_pkey ON public.escalation_policy_steps USING btree (id);
CREATE INDEX idx_ep_step_policies ON public.escalation_policy_steps USING btree (escalation_policy_id);

CREATE TRIGGER trg_10_decr_ep_step_count_on_del BEFORE DELETE ON public.escalation_policy_steps FOR EACH ROW EXECUTE FUNCTION fn_decr_ep_step_count_on_del();
CREATE TRIGGER trg_10_incr_ep_step_count_on_add BEFORE INSERT ON public.escalation_policy_steps FOR EACH ROW EXECUTE FUNCTION fn_incr_ep_step_count_on_add();
CREATE TRIGGER trg_10_insert_ep_state_on_step_insert AFTER INSERT ON public.escalation_policy_steps FOR EACH ROW WHEN ((new.step_number = 0)) EXECUTE FUNCTION fn_insert_ep_state_on_step_insert();
CREATE TRIGGER trg_decr_ep_step_number_on_delete AFTER DELETE ON public.escalation_policy_steps FOR EACH ROW EXECUTE FUNCTION fn_decr_ep_step_number_on_delete();
CREATE CONSTRAINT TRIGGER trg_enforce_ep_step_limit AFTER INSERT ON public.escalation_policy_steps NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_ep_step_limit();
CREATE CONSTRAINT TRIGGER trg_ep_step_number_no_gaps AFTER UPDATE ON public.escalation_policy_steps DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION fn_enforce_ep_step_number_no_gaps();
CREATE TRIGGER trg_inc_ep_step_number_on_insert BEFORE INSERT ON public.escalation_policy_steps FOR EACH ROW EXECUTE FUNCTION fn_inc_ep_step_number_on_insert();


CREATE TABLE gorp_migrations (
	applied_at timestamp with time zone,
	id text NOT NULL,
	CONSTRAINT gorp_migrations_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX gorp_migrations_pkey ON public.gorp_migrations USING btree (id);


CREATE TABLE gql_api_key_usage (
	api_key_id uuid,
	id bigint DEFAULT nextval('gql_api_key_usage_id_seq'::regclass) NOT NULL,
	ip_address inet,
	used_at timestamp with time zone DEFAULT now() NOT NULL,
	user_agent text,
	CONSTRAINT gql_api_key_usage_api_key_id_fkey FOREIGN KEY (api_key_id) REFERENCES gql_api_keys(id) ON DELETE CASCADE,
	CONSTRAINT gql_api_key_usage_api_key_id_key UNIQUE (api_key_id),
	CONSTRAINT gql_api_key_usage_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX gql_api_key_usage_api_key_id_key ON public.gql_api_key_usage USING btree (api_key_id);
CREATE UNIQUE INDEX gql_api_key_usage_pkey ON public.gql_api_key_usage USING btree (id);


CREATE TABLE gql_api_keys (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	created_by uuid,
	deleted_at timestamp with time zone,
	deleted_by uuid,
	description text NOT NULL,
	expires_at timestamp with time zone NOT NULL,
	id uuid NOT NULL,
	name text NOT NULL,
	policy json NOT NULL,
	updated_at timestamp with time zone DEFAULT now() NOT NULL,
	updated_by uuid,
	CONSTRAINT gql_api_keys_created_by_fkey FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE SET NULL,
	CONSTRAINT gql_api_keys_deleted_by_fkey FOREIGN KEY (deleted_by) REFERENCES users(id) ON DELETE SET NULL,
	CONSTRAINT gql_api_keys_pkey PRIMARY KEY (id),
	CONSTRAINT gql_api_keys_updated_by_fkey FOREIGN KEY (updated_by) REFERENCES users(id) ON DELETE SET NULL
);

CREATE UNIQUE INDEX gql_api_keys_name_key ON public.gql_api_keys USING btree (name) WHERE (deleted_at IS NULL);
CREATE UNIQUE INDEX gql_api_keys_pkey ON public.gql_api_keys USING btree (id);


CREATE TABLE heartbeat_monitors (
	additional_details text,
	heartbeat_interval interval NOT NULL,
	id uuid NOT NULL,
	last_heartbeat timestamp with time zone,
	last_state enum_heartbeat_state DEFAULT 'inactive'::enum_heartbeat_state NOT NULL,
	muted text,
	name text NOT NULL,
	service_id uuid NOT NULL,
	CONSTRAINT heartbeat_monitors_pkey PRIMARY KEY (id),
	CONSTRAINT heartbeat_monitors_service_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX heartbeat_monitor_name_service_id ON public.heartbeat_monitors USING btree (lower(name), service_id);
CREATE UNIQUE INDEX heartbeat_monitors_pkey ON public.heartbeat_monitors USING btree (id);
CREATE INDEX idx_heartbeat_monitor_service ON public.heartbeat_monitors USING btree (service_id);

CREATE CONSTRAINT TRIGGER trg_enforce_heartbeat_monitor_limit AFTER INSERT ON public.heartbeat_monitors NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_heartbeat_limit();


CREATE TABLE integration_keys (
	external_system_name text,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	name text NOT NULL,
	service_id uuid NOT NULL,
	type enum_integration_keys_type NOT NULL,
	CONSTRAINT integration_keys_pkey PRIMARY KEY (id),
	CONSTRAINT integration_keys_services_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX idx_int_key_name_svc_ext ON public.integration_keys USING btree (lower(name), service_id, COALESCE(external_system_name, ''::text));
CREATE INDEX idx_integration_key_service ON public.integration_keys USING btree (service_id);
CREATE UNIQUE INDEX integration_keys_pkey ON public.integration_keys USING btree (id);

CREATE CONSTRAINT TRIGGER trg_enforce_integration_key_limit AFTER INSERT ON public.integration_keys NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_integration_key_limit();


CREATE TABLE keyring (
	id text NOT NULL,
	next_key bytea NOT NULL,
	next_rotation timestamp with time zone,
	rotation_count bigint NOT NULL,
	signing_key bytea NOT NULL,
	verification_keys bytea NOT NULL,
	CONSTRAINT keyring_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX keyring_pkey ON public.keyring USING btree (id);


CREATE TABLE labels (
	id bigint DEFAULT nextval('labels_id_seq'::regclass) NOT NULL,
	key text NOT NULL,
	tgt_service_id uuid NOT NULL,
	value text NOT NULL,
	CONSTRAINT labels_pkey PRIMARY KEY (id),
	CONSTRAINT labels_tgt_service_id_fkey FOREIGN KEY (tgt_service_id) REFERENCES services(id) ON DELETE CASCADE,
	CONSTRAINT labels_tgt_service_id_key_key UNIQUE (tgt_service_id, key)
);

CREATE INDEX idx_labels_service_id ON public.labels USING btree (tgt_service_id);
CREATE UNIQUE INDEX labels_pkey ON public.labels USING btree (id);
CREATE UNIQUE INDEX labels_tgt_service_id_key_key ON public.labels USING btree (tgt_service_id, key);


CREATE TABLE message_status_history (
	id bigint DEFAULT nextval('message_status_history_id_seq'::regclass) NOT NULL,
	message_id uuid NOT NULL,
	status enum_outgoing_messages_status NOT NULL,
	status_details text NOT NULL,
	timestamp timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT message_status_history_message_id_fkey FOREIGN KEY (message_id) REFERENCES outgoing_messages(id) ON DELETE CASCADE,
	CONSTRAINT message_status_history_pkey PRIMARY KEY (id)
);

CREATE INDEX message_status_history_message_id_idx ON public.message_status_history USING btree (message_id);
CREATE UNIQUE INDEX message_status_history_pkey ON public.message_status_history USING btree (id);


CREATE TABLE notification_channel_duplicates (
	id bigint DEFAULT nextval('notification_channel_duplicates_id_seq'::regclass) NOT NULL,
	new_id uuid NOT NULL,
	old_created_at timestamp with time zone NOT NULL,
	old_id uuid NOT NULL,
	CONSTRAINT notification_channel_duplicates_id_key UNIQUE (id),
	CONSTRAINT notification_channel_duplicates_pkey PRIMARY KEY (old_id)
);

CREATE UNIQUE INDEX notification_channel_duplicates_id_key ON public.notification_channel_duplicates USING btree (id);
CREATE UNIQUE INDEX notification_channel_duplicates_pkey ON public.notification_channel_duplicates USING btree (old_id);


CREATE TABLE notification_channels (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	dest jsonb NOT NULL,
	id uuid NOT NULL,
	meta jsonb DEFAULT '{}'::jsonb NOT NULL,
	name text NOT NULL,
	type enum_notif_channel_type NOT NULL,
	value text NOT NULL,
	CONSTRAINT nc_unique_type_value UNIQUE (type, value),
	CONSTRAINT notification_channels_dest_key UNIQUE (dest),
	CONSTRAINT notification_channels_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX nc_unique_type_value ON public.notification_channels USING btree (type, value);
CREATE UNIQUE INDEX notification_channels_dest_key ON public.notification_channels USING btree (dest);
CREATE UNIQUE INDEX notification_channels_pkey ON public.notification_channels USING btree (id);

CREATE TRIGGER trg_10_nc_compat_set_type_val_on_insert BEFORE INSERT ON public.notification_channels FOR EACH ROW WHEN ((new.dest IS NOT NULL)) EXECUTE FUNCTION fn_nc_compat_set_type_val_on_insert();
CREATE TRIGGER trg_10_nc_compat_set_type_val_on_update BEFORE UPDATE ON public.notification_channels FOR EACH ROW WHEN ((new.dest <> old.dest)) EXECUTE FUNCTION fn_nc_compat_set_type_val_on_insert();
CREATE TRIGGER trg_10_nc_set_dest_on_insert BEFORE INSERT ON public.notification_channels FOR EACH ROW WHEN ((new.dest IS NULL)) EXECUTE FUNCTION fn_nc_set_dest_on_insert();
CREATE TRIGGER trg_10_nc_set_dest_on_update AFTER UPDATE ON public.notification_channels FOR EACH ROW WHEN (((new.dest = old.dest) AND ((new.type <> 'DEST'::enum_notif_channel_type) AND ((new.value <> old.value) OR (new.type <> old.type))))) EXECUTE FUNCTION fn_nc_set_dest_on_insert();


CREATE TABLE notification_policy_cycles (
	alert_id integer NOT NULL,
	checked boolean DEFAULT true NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	last_tick timestamp with time zone,
	repeat_count integer DEFAULT 0 NOT NULL,
	started_at timestamp with time zone DEFAULT now() NOT NULL,
	user_id uuid NOT NULL,
	CONSTRAINT notification_policy_cycles_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT notification_policy_cycles_pkey PRIMARY KEY (id),
	CONSTRAINT notification_policy_cycles_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX idx_np_cycle_alert_id ON public.notification_policy_cycles USING btree (alert_id);
CREATE UNIQUE INDEX notification_policy_cycles_pkey ON public.notification_policy_cycles USING btree (id);


CREATE TABLE outgoing_messages (
	alert_id bigint,
	alert_log_id bigint,
	channel_id uuid,
	contact_method_id uuid,
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	cycle_id uuid,
	escalation_policy_id uuid,
	fired_at timestamp with time zone,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	last_status enum_outgoing_messages_status DEFAULT 'pending'::enum_outgoing_messages_status NOT NULL,
	last_status_at timestamp with time zone DEFAULT now(),
	message_type enum_outgoing_messages_type NOT NULL,
	next_retry_at timestamp with time zone,
	provider_msg_id text,
	provider_seq integer DEFAULT 0 NOT NULL,
	retry_count integer DEFAULT 0 NOT NULL,
	schedule_id uuid,
	sending_deadline timestamp with time zone,
	sent_at timestamp with time zone,
	service_id uuid,
	src_value text,
	status_alert_ids bigint[],
	status_details text DEFAULT ''::text NOT NULL,
	user_id uuid,
	user_verification_code_id uuid,
	CONSTRAINT om_alert_svc_ep_ids CHECK (message_type <> 'alert_notification'::enum_outgoing_messages_type OR alert_id IS NOT NULL AND service_id IS NOT NULL AND escalation_policy_id IS NOT NULL),
	CONSTRAINT om_no_status_bundles CHECK (message_type <> 'alert_status_update_bundle'::enum_outgoing_messages_type OR last_status <> 'pending'::enum_outgoing_messages_status),
	CONSTRAINT om_pending_no_fired_no_sent CHECK (last_status <> 'pending'::enum_outgoing_messages_status OR fired_at IS NULL AND sent_at IS NULL),
	CONSTRAINT om_processed_no_fired_sent CHECK ((last_status = ANY (ARRAY['pending'::enum_outgoing_messages_status, 'sending'::enum_outgoing_messages_status, 'failed'::enum_outgoing_messages_status, 'bundled'::enum_outgoing_messages_status])) OR fired_at IS NULL AND sent_at IS NOT NULL),
	CONSTRAINT om_sending_deadline_reqd CHECK (last_status <> 'sending'::enum_outgoing_messages_status OR sending_deadline IS NOT NULL),
	CONSTRAINT om_sending_fired_no_sent CHECK (last_status <> 'sending'::enum_outgoing_messages_status OR fired_at IS NOT NULL AND sent_at IS NULL),
	CONSTRAINT om_status_alert_ids CHECK (message_type <> 'alert_status_update_bundle'::enum_outgoing_messages_type OR status_alert_ids IS NOT NULL),
	CONSTRAINT om_status_update_log_id CHECK (message_type <> 'alert_status_update'::enum_outgoing_messages_type OR alert_log_id IS NOT NULL),
	CONSTRAINT om_user_cm_or_channel CHECK (user_id IS NOT NULL AND contact_method_id IS NOT NULL AND channel_id IS NULL OR channel_id IS NOT NULL AND contact_method_id IS NULL AND user_id IS NULL),
	CONSTRAINT outgoing_messages_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_alert_log_id_fkey FOREIGN KEY (alert_log_id) REFERENCES alert_logs(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_channel_id_fkey FOREIGN KEY (channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_contact_method_id_fkey FOREIGN KEY (contact_method_id) REFERENCES user_contact_methods(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_cycle_id_fkey FOREIGN KEY (cycle_id) REFERENCES notification_policy_cycles(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_escalation_policy_id_fkey FOREIGN KEY (escalation_policy_id) REFERENCES escalation_policies(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_pkey PRIMARY KEY (id),
	CONSTRAINT outgoing_messages_schedule_id_fkey FOREIGN KEY (schedule_id) REFERENCES schedules(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_service_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
	CONSTRAINT outgoing_messages_user_verification_code_id_fkey FOREIGN KEY (user_verification_code_id) REFERENCES user_verification_codes(id) ON DELETE CASCADE,
	CONSTRAINT verify_needs_id CHECK (message_type <> 'verification_message'::enum_outgoing_messages_type OR user_verification_code_id IS NOT NULL)
);

CREATE INDEX idx_om_alert_log_id ON public.outgoing_messages USING btree (alert_log_id);
CREATE INDEX idx_om_alert_sent ON public.outgoing_messages USING btree (alert_id, sent_at);
CREATE INDEX idx_om_cm_sent ON public.outgoing_messages USING btree (contact_method_id, sent_at);
CREATE INDEX idx_om_ep_sent ON public.outgoing_messages USING btree (escalation_policy_id, sent_at);
CREATE INDEX idx_om_last_status_sent ON public.outgoing_messages USING btree (last_status, sent_at);
CREATE INDEX idx_om_service_sent ON public.outgoing_messages USING btree (service_id, sent_at);
CREATE INDEX idx_om_user_sent ON public.outgoing_messages USING btree (user_id, sent_at);
CREATE INDEX idx_om_vcode_id ON public.outgoing_messages USING btree (user_verification_code_id);
CREATE INDEX idx_outgoing_messages_notif_cycle ON public.outgoing_messages USING btree (cycle_id);
CREATE UNIQUE INDEX idx_outgoing_messages_provider_msg_id ON public.outgoing_messages USING btree (provider_msg_id);
CREATE INDEX om_cm_time_test_verify_idx ON public.outgoing_messages USING btree (contact_method_id, created_at) WHERE (message_type = ANY (ARRAY['test_notification'::enum_outgoing_messages_type, 'verification_message'::enum_outgoing_messages_type]));
CREATE UNIQUE INDEX outgoing_messages_pkey ON public.outgoing_messages USING btree (id);

CREATE TRIGGER trg_insert_message_status_history AFTER INSERT ON public.outgoing_messages FOR EACH ROW EXECUTE FUNCTION fn_insert_message_status_history();
CREATE TRIGGER trg_update_message_status_history AFTER UPDATE OF last_status ON public.outgoing_messages FOR EACH ROW EXECUTE FUNCTION fn_insert_message_status_history();


CREATE TABLE pending_signals (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	dest_id uuid NOT NULL,
	id integer DEFAULT nextval('pending_signals_id_seq'::regclass) NOT NULL,
	message_id uuid,
	params jsonb NOT NULL,
	service_id uuid NOT NULL,
	CONSTRAINT pending_signals_dest_id_fkey FOREIGN KEY (dest_id) REFERENCES notification_channels(id) ON DELETE CASCADE,
	CONSTRAINT pending_signals_message_id_fkey FOREIGN KEY (message_id) REFERENCES outgoing_messages(id) ON DELETE CASCADE,
	CONSTRAINT pending_signals_message_id_key UNIQUE (message_id),
	CONSTRAINT pending_signals_pkey PRIMARY KEY (id),
	CONSTRAINT pending_signals_service_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE
);

CREATE INDEX idx_pending_signals_service_dest_id ON public.pending_signals USING btree (service_id, dest_id);
CREATE UNIQUE INDEX pending_signals_message_id_key ON public.pending_signals USING btree (message_id);
CREATE UNIQUE INDEX pending_signals_pkey ON public.pending_signals USING btree (id);

CREATE CONSTRAINT TRIGGER trg_enforce_signals_per_dest_per_service_limit AFTER INSERT ON public.pending_signals NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_signals_per_dest_per_service_limit();
CREATE CONSTRAINT TRIGGER trg_enforce_signals_per_service_limit AFTER INSERT ON public.pending_signals NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_signals_per_service_limit();


CREATE TABLE region_ids (
	id integer DEFAULT nextval('region_ids_id_seq'::regclass) NOT NULL,
	name text NOT NULL,
	CONSTRAINT region_ids_id_key UNIQUE (id),
	CONSTRAINT region_ids_pkey PRIMARY KEY (name)
);

CREATE UNIQUE INDEX region_ids_id_key ON public.region_ids USING btree (id);
CREATE UNIQUE INDEX region_ids_pkey ON public.region_ids USING btree (name);


CREATE TABLE river_client (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	id text NOT NULL,
	metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
	paused_at timestamp with time zone,
	updated_at timestamp with time zone NOT NULL,
	CONSTRAINT name_length CHECK (char_length(id) > 0 AND char_length(id) < 128),
	CONSTRAINT river_client_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX river_client_pkey ON public.river_client USING btree (id);


CREATE TABLE river_client_queue (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	id bigint DEFAULT nextval('river_client_queue_id_seq'::regclass) NOT NULL,
	max_workers bigint DEFAULT 0 NOT NULL,
	metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
	name text NOT NULL,
	num_jobs_completed bigint DEFAULT 0 NOT NULL,
	num_jobs_running bigint DEFAULT 0 NOT NULL,
	river_client_id text NOT NULL,
	updated_at timestamp with time zone NOT NULL,
	CONSTRAINT name_length CHECK (char_length(name) > 0 AND char_length(name) < 128),
	CONSTRAINT num_jobs_completed_zero_or_positive CHECK (num_jobs_completed >= 0),
	CONSTRAINT num_jobs_running_zero_or_positive CHECK (num_jobs_running >= 0),
	CONSTRAINT river_client_queue_id_key UNIQUE (id),
	CONSTRAINT river_client_queue_pkey PRIMARY KEY (river_client_id, name),
	CONSTRAINT river_client_queue_river_client_id_fkey FOREIGN KEY (river_client_id) REFERENCES river_client(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX river_client_queue_id_key ON public.river_client_queue USING btree (id);
CREATE UNIQUE INDEX river_client_queue_pkey ON public.river_client_queue USING btree (river_client_id, name);


CREATE TABLE river_job (
	args jsonb NOT NULL,
	attempt smallint DEFAULT 0 NOT NULL,
	attempted_at timestamp with time zone,
	attempted_by text[],
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	errors jsonb[],
	finalized_at timestamp with time zone,
	id bigint DEFAULT nextval('river_job_id_seq'::regclass) NOT NULL,
	kind text NOT NULL,
	max_attempts smallint NOT NULL,
	metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
	priority smallint DEFAULT 1 NOT NULL,
	queue text DEFAULT 'default'::text NOT NULL,
	scheduled_at timestamp with time zone DEFAULT now() NOT NULL,
	state river_job_state DEFAULT 'available'::river_job_state NOT NULL,
	tags character varying(255)[] DEFAULT '{}'::character varying[] NOT NULL,
	unique_key bytea,
	unique_states bit(8),
	CONSTRAINT finalized_or_finalized_at_null CHECK (finalized_at IS NULL AND (state <> ALL (ARRAY['cancelled'::river_job_state, 'completed'::river_job_state, 'discarded'::river_job_state])) OR finalized_at IS NOT NULL AND (state = ANY (ARRAY['cancelled'::river_job_state, 'completed'::river_job_state, 'discarded'::river_job_state]))),
	CONSTRAINT kind_length CHECK (char_length(kind) > 0 AND char_length(kind) < 128),
	CONSTRAINT max_attempts_is_positive CHECK (max_attempts > 0),
	CONSTRAINT priority_in_range CHECK (priority >= 1 AND priority <= 4),
	CONSTRAINT queue_length CHECK (char_length(queue) > 0 AND char_length(queue) < 128),
	CONSTRAINT river_job_pkey PRIMARY KEY (id)
);

CREATE INDEX river_job_args_index ON public.river_job USING gin (args);
CREATE INDEX river_job_kind ON public.river_job USING btree (kind);
CREATE INDEX river_job_metadata_index ON public.river_job USING gin (metadata);
CREATE UNIQUE INDEX river_job_pkey ON public.river_job USING btree (id);
CREATE INDEX river_job_prioritized_fetching_index ON public.river_job USING btree (state, queue, priority, scheduled_at, id);
CREATE INDEX river_job_state_and_finalized_at_index ON public.river_job USING btree (state, finalized_at) WHERE (finalized_at IS NOT NULL);
CREATE UNIQUE INDEX river_job_unique_idx ON public.river_job USING btree (unique_key) WHERE ((unique_key IS NOT NULL) AND (unique_states IS NOT NULL) AND river_job_state_in_bitmask(unique_states, state));


CREATE TABLE river_leader (
	elected_at timestamp with time zone NOT NULL,
	expires_at timestamp with time zone NOT NULL,
	id bigint DEFAULT nextval('river_leader_id_seq'::regclass) NOT NULL,
	leader_id text NOT NULL,
	name text DEFAULT 'default'::text NOT NULL,
	CONSTRAINT leader_id_length CHECK (char_length(leader_id) > 0 AND char_length(leader_id) < 128),
	CONSTRAINT name_length CHECK (name = 'default'::text),
	CONSTRAINT river_leader_id_key UNIQUE (id),
	CONSTRAINT river_leader_pkey PRIMARY KEY (name)
);

CREATE UNIQUE INDEX river_leader_id_key ON public.river_leader USING btree (id);
CREATE UNIQUE INDEX river_leader_pkey ON public.river_leader USING btree (name);


CREATE TABLE river_queue (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	id bigint DEFAULT nextval('river_queue_id_seq'::regclass) NOT NULL,
	metadata jsonb DEFAULT '{}'::jsonb NOT NULL,
	name text NOT NULL,
	paused_at timestamp with time zone,
	updated_at timestamp with time zone NOT NULL,
	CONSTRAINT river_queue_id_key UNIQUE (id),
	CONSTRAINT river_queue_pkey PRIMARY KEY (name)
);

CREATE UNIQUE INDEX river_queue_id_key ON public.river_queue USING btree (id);
CREATE UNIQUE INDEX river_queue_pkey ON public.river_queue USING btree (name);


CREATE TABLE rotation_participants (
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	position integer NOT NULL,
	rotation_id uuid NOT NULL,
	user_id uuid NOT NULL,
	CONSTRAINT rotation_participants_pkey PRIMARY KEY (id),
	CONSTRAINT rotation_participants_rotation_id_fkey FOREIGN KEY (rotation_id) REFERENCES rotations(id) ON DELETE CASCADE,
	CONSTRAINT rotation_participants_rotation_id_position_key UNIQUE (rotation_id, "position") DEFERRABLE INITIALLY DEFERRED,
	CONSTRAINT rotation_participants_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX idx_participant_rotation ON public.rotation_participants USING btree (rotation_id);
CREATE UNIQUE INDEX rotation_participants_pkey ON public.rotation_participants USING btree (id);
CREATE UNIQUE INDEX rotation_participants_rotation_id_position_key ON public.rotation_participants USING btree (rotation_id, "position");

CREATE TRIGGER trg_10_decr_part_count_on_del BEFORE DELETE ON public.rotation_participants FOR EACH ROW EXECUTE FUNCTION fn_decr_part_count_on_del();
CREATE TRIGGER trg_20_decr_rot_part_position_on_delete AFTER DELETE ON public.rotation_participants FOR EACH ROW EXECUTE FUNCTION fn_decr_rot_part_position_on_delete();
CREATE TRIGGER trg_30_advance_or_end_rot_on_part_del BEFORE DELETE ON public.rotation_participants FOR EACH ROW EXECUTE FUNCTION fn_advance_or_end_rot_on_part_del();
CREATE CONSTRAINT TRIGGER trg_enforce_rot_part_position_no_gaps AFTER UPDATE ON public.rotation_participants DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION fn_enforce_rot_part_position_no_gaps();
CREATE CONSTRAINT TRIGGER trg_enforce_rotation_participant_limit AFTER INSERT ON public.rotation_participants NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_rotation_participant_limit();
CREATE TRIGGER trg_inc_rot_part_position_on_insert BEFORE INSERT ON public.rotation_participants FOR EACH ROW EXECUTE FUNCTION fn_inc_rot_part_position_on_insert();
CREATE TRIGGER trg_incr_part_count_on_add BEFORE INSERT ON public.rotation_participants FOR EACH ROW EXECUTE FUNCTION fn_incr_part_count_on_add();
CREATE TRIGGER trg_set_rot_state_pos_on_part_reorder BEFORE UPDATE ON public.rotation_participants FOR EACH ROW WHEN ((new."position" <> old."position")) EXECUTE FUNCTION fn_set_rot_state_pos_on_part_reorder();
CREATE TRIGGER trg_start_rotation_on_first_part_add AFTER INSERT ON public.rotation_participants FOR EACH ROW EXECUTE FUNCTION fn_start_rotation_on_first_part_add();
CREATE TRIGGER trg_track_rotation_part_updates AFTER INSERT OR DELETE OR UPDATE ON public.rotation_participants FOR EACH ROW EXECUTE FUNCTION fn_track_rotation_updates();


CREATE TABLE rotation_state (
	id bigint DEFAULT nextval('rotation_state_id_seq'::regclass) NOT NULL,
	position integer DEFAULT 0 NOT NULL,
	rotation_id uuid NOT NULL,
	rotation_participant_id uuid NOT NULL,
	shift_start timestamp with time zone NOT NULL,
	version integer DEFAULT 2 NOT NULL,
	CONSTRAINT rotation_state_pkey PRIMARY KEY (rotation_id),
	CONSTRAINT rotation_state_rotation_id_fkey FOREIGN KEY (rotation_id) REFERENCES rotations(id) ON DELETE CASCADE,
	CONSTRAINT rotation_state_rotation_participant_id_fkey FOREIGN KEY (rotation_participant_id) REFERENCES rotation_participants(id) DEFERRABLE,
	CONSTRAINT rotation_state_uniq_id UNIQUE (id)
);

CREATE UNIQUE INDEX rotation_state_pkey ON public.rotation_state USING btree (rotation_id);
CREATE UNIQUE INDEX rotation_state_uniq_id ON public.rotation_state USING btree (id);

CREATE TRIGGER trg_set_rot_state_pos_on_active_change BEFORE UPDATE ON public.rotation_state FOR EACH ROW WHEN ((new.rotation_participant_id <> old.rotation_participant_id)) EXECUTE FUNCTION fn_set_rot_state_pos_on_active_change();
CREATE TRIGGER trg_track_rotation_state_updates AFTER UPDATE ON public.rotation_state FOR EACH ROW EXECUTE FUNCTION fn_track_rotation_updates();


CREATE TABLE rotations (
	description text DEFAULT ''::text NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	last_processed timestamp with time zone,
	name text NOT NULL,
	participant_count integer DEFAULT 0 NOT NULL,
	shift_length bigint DEFAULT 1 NOT NULL,
	start_time timestamp with time zone DEFAULT now() NOT NULL,
	time_zone text NOT NULL,
	type enum_rotation_type NOT NULL,
	CONSTRAINT rotations_name_unique UNIQUE (name),
	CONSTRAINT rotations_pkey PRIMARY KEY (id),
	CONSTRAINT rotations_shift_length_check CHECK (shift_length > 0)
);

CREATE INDEX idx_search_rotations_desc_eng ON public.rotations USING gin (to_tsvector('english'::regconfig, replace(lower(description), '.'::text, ' '::text)));
CREATE INDEX idx_search_rotations_name_eng ON public.rotations USING gin (to_tsvector('english'::regconfig, replace(lower(name), '.'::text, ' '::text)));
CREATE UNIQUE INDEX rotations_name ON public.rotations USING btree (lower(name));
CREATE UNIQUE INDEX rotations_name_unique ON public.rotations USING btree (name);
CREATE UNIQUE INDEX rotations_pkey ON public.rotations USING btree (id);

CREATE TRIGGER trg_track_rotation_updates AFTER INSERT OR UPDATE ON public.rotations FOR EACH ROW EXECUTE FUNCTION fn_track_rotation_updates();


CREATE TABLE schedule_data (
	data jsonb NOT NULL,
	id bigint DEFAULT nextval('schedule_data_id_seq'::regclass) NOT NULL,
	last_cleanup_at timestamp with time zone,
	schedule_id uuid NOT NULL,
	CONSTRAINT schedule_data_id_key UNIQUE (id),
	CONSTRAINT schedule_data_pkey PRIMARY KEY (schedule_id),
	CONSTRAINT schedule_data_schedule_id_fkey FOREIGN KEY (schedule_id) REFERENCES schedules(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX schedule_data_id_key ON public.schedule_data USING btree (id);
CREATE UNIQUE INDEX schedule_data_pkey ON public.schedule_data USING btree (schedule_id);


CREATE TABLE schedule_on_call_users (
	end_time timestamp with time zone,
	id bigint DEFAULT nextval('schedule_on_call_users_id_seq'::regclass) NOT NULL,
	schedule_id uuid NOT NULL,
	start_time timestamp with time zone DEFAULT now() NOT NULL,
	user_id uuid NOT NULL,
	CONSTRAINT schedule_on_call_users_check CHECK (end_time IS NULL OR end_time > start_time),
	CONSTRAINT schedule_on_call_users_schedule_id_fkey FOREIGN KEY (schedule_id) REFERENCES schedules(id) ON DELETE CASCADE,
	CONSTRAINT schedule_on_call_users_uniq_id UNIQUE (id),
	CONSTRAINT schedule_on_call_users_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX idx_sched_oncall_times ON public.schedule_on_call_users USING spgist (tstzrange(start_time, end_time));
CREATE UNIQUE INDEX idx_schedule_on_call_once ON public.schedule_on_call_users USING btree (schedule_id, user_id) WHERE (end_time IS NULL);
CREATE UNIQUE INDEX schedule_on_call_users_uniq_id ON public.schedule_on_call_users USING btree (id);


CREATE TABLE schedule_rules (
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	end_time time without time zone DEFAULT '23:59:59'::time without time zone NOT NULL,
	friday boolean DEFAULT true NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	is_active boolean DEFAULT false NOT NULL,
	monday boolean DEFAULT true NOT NULL,
	saturday boolean DEFAULT true NOT NULL,
	schedule_id uuid NOT NULL,
	start_time time without time zone DEFAULT '00:00:00'::time without time zone NOT NULL,
	sunday boolean DEFAULT true NOT NULL,
	tgt_rotation_id uuid,
	tgt_user_id uuid,
	thursday boolean DEFAULT true NOT NULL,
	tuesday boolean DEFAULT true NOT NULL,
	wednesday boolean DEFAULT true NOT NULL,
	CONSTRAINT schedule_rules_check CHECK (tgt_user_id IS NULL AND tgt_rotation_id IS NOT NULL OR tgt_user_id IS NOT NULL AND tgt_rotation_id IS NULL),
	CONSTRAINT schedule_rules_pkey PRIMARY KEY (id),
	CONSTRAINT schedule_rules_schedule_id_fkey FOREIGN KEY (schedule_id) REFERENCES schedules(id) ON DELETE CASCADE,
	CONSTRAINT schedule_rules_tgt_rotation_id_fkey FOREIGN KEY (tgt_rotation_id) REFERENCES rotations(id) ON DELETE CASCADE,
	CONSTRAINT schedule_rules_tgt_user_id_fkey FOREIGN KEY (tgt_user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX idx_rule_schedule ON public.schedule_rules USING btree (schedule_id);
CREATE INDEX idx_target_schedule ON public.schedule_rules USING btree (schedule_id, tgt_rotation_id, tgt_user_id);
CREATE UNIQUE INDEX schedule_rules_pkey ON public.schedule_rules USING btree (id);

CREATE CONSTRAINT TRIGGER trg_enforce_schedule_rule_limit AFTER INSERT ON public.schedule_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_schedule_rule_limit();
CREATE CONSTRAINT TRIGGER trg_enforce_schedule_target_limit AFTER INSERT ON public.schedule_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_schedule_target_limit();


CREATE TABLE schedules (
	description text DEFAULT ''::text NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	last_processed timestamp with time zone,
	name text NOT NULL,
	time_zone text NOT NULL,
	CONSTRAINT schedules_name_key UNIQUE (name),
	CONSTRAINT schedules_pkey PRIMARY KEY (id)
);

CREATE INDEX idx_search_schedules_desc_eng ON public.schedules USING gin (to_tsvector('english'::regconfig, replace(lower(description), '.'::text, ' '::text)));
CREATE INDEX idx_search_schedules_name_eng ON public.schedules USING gin (to_tsvector('english'::regconfig, replace(lower(name), '.'::text, ' '::text)));
CREATE UNIQUE INDEX schedules_name ON public.schedules USING btree (lower(name));
CREATE UNIQUE INDEX schedules_name_key ON public.schedules USING btree (name);
CREATE UNIQUE INDEX schedules_pkey ON public.schedules USING btree (id);


CREATE TABLE services (
	description text DEFAULT ''::text NOT NULL,
	escalation_policy_id uuid NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	maintenance_expires_at timestamp with time zone,
	name text NOT NULL,
	CONSTRAINT services_escalation_policy_id_fkey FOREIGN KEY (escalation_policy_id) REFERENCES escalation_policies(id),
	CONSTRAINT services_name_key UNIQUE (name),
	CONSTRAINT services_pkey PRIMARY KEY (id),
	CONSTRAINT svc_ep_uniq UNIQUE (id, escalation_policy_id)
);

CREATE INDEX idx_search_services_desc_eng ON public.services USING gin (to_tsvector('english'::regconfig, replace(lower(description), '.'::text, ' '::text)));
CREATE INDEX idx_search_services_name_eng ON public.services USING gin (to_tsvector('english'::regconfig, replace(lower(name), '.'::text, ' '::text)));
CREATE UNIQUE INDEX services_name ON public.services USING btree (lower(name));
CREATE UNIQUE INDEX services_name_key ON public.services USING btree (name);
CREATE UNIQUE INDEX services_pkey ON public.services USING btree (id);
CREATE UNIQUE INDEX svc_ep_uniq ON public.services USING btree (id, escalation_policy_id);

CREATE TRIGGER trg_10_clear_ep_state_on_svc_ep_change AFTER UPDATE ON public.services FOR EACH ROW WHEN ((old.escalation_policy_id <> new.escalation_policy_id)) EXECUTE FUNCTION fn_clear_ep_state_on_svc_ep_change();


CREATE TABLE switchover_log (
	data jsonb NOT NULL,
	id bigint NOT NULL,
	timestamp timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT switchover_log_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX switchover_log_pkey ON public.switchover_log USING btree (id);


CREATE TABLE switchover_state (
	current_state enum_switchover_state NOT NULL,
	db_id uuid DEFAULT gen_random_uuid() NOT NULL,
	ok boolean NOT NULL,
	CONSTRAINT switchover_state_ok_check CHECK (ok),
	CONSTRAINT switchover_state_pkey PRIMARY KEY (ok)
);

CREATE UNIQUE INDEX switchover_state_pkey ON public.switchover_state USING btree (ok);


CREATE TABLE twilio_sms_callbacks (
	alert_id bigint,
	callback_id uuid NOT NULL,
	code integer NOT NULL,
	id bigint DEFAULT nextval('twilio_sms_callbacks_id_seq'::regclass) NOT NULL,
	phone_number text NOT NULL,
	sent_at timestamp with time zone DEFAULT now() NOT NULL,
	service_id uuid,
	CONSTRAINT twilio_sms_callbacks_alert_id_fkey FOREIGN KEY (alert_id) REFERENCES alerts(id) ON DELETE CASCADE,
	CONSTRAINT twilio_sms_callbacks_service_id_fkey FOREIGN KEY (service_id) REFERENCES services(id) ON DELETE CASCADE,
	CONSTRAINT twilio_sms_callbacks_uniq_id UNIQUE (id)
);

CREATE INDEX idx_twilio_sms_alert_id ON public.twilio_sms_callbacks USING btree (alert_id);
CREATE UNIQUE INDEX idx_twilio_sms_codes ON public.twilio_sms_callbacks USING btree (phone_number, code);
CREATE INDEX idx_twilio_sms_service_id ON public.twilio_sms_callbacks USING btree (service_id);
CREATE UNIQUE INDEX twilio_sms_callbacks_uniq_id ON public.twilio_sms_callbacks USING btree (id);


CREATE TABLE twilio_sms_errors (
	error_message text NOT NULL,
	id bigint DEFAULT nextval('twilio_sms_errors_id_seq'::regclass) NOT NULL,
	occurred_at timestamp with time zone DEFAULT now() NOT NULL,
	outgoing boolean NOT NULL,
	phone_number text NOT NULL,
	CONSTRAINT twilio_sms_errors_uniq_id UNIQUE (id)
);

CREATE INDEX twilio_sms_errors_phone_number_outgoing_occurred_at_idx ON public.twilio_sms_errors USING btree (phone_number, outgoing, occurred_at);
CREATE UNIQUE INDEX twilio_sms_errors_uniq_id ON public.twilio_sms_errors USING btree (id);


CREATE TABLE twilio_voice_errors (
	error_message text NOT NULL,
	id bigint DEFAULT nextval('twilio_voice_errors_id_seq'::regclass) NOT NULL,
	occurred_at timestamp with time zone DEFAULT now() NOT NULL,
	outgoing boolean NOT NULL,
	phone_number text NOT NULL,
	CONSTRAINT twilio_voice_errors_uniq_id UNIQUE (id)
);

CREATE INDEX twilio_voice_errors_phone_number_outgoing_occurred_at_idx ON public.twilio_voice_errors USING btree (phone_number, outgoing, occurred_at);
CREATE UNIQUE INDEX twilio_voice_errors_uniq_id ON public.twilio_voice_errors USING btree (id);


CREATE TABLE uik_config (
	config jsonb NOT NULL,
	id uuid NOT NULL,
	primary_token uuid,
	primary_token_hint text,
	secondary_token uuid,
	secondary_token_hint text,
	CONSTRAINT uik_config_id_fkey FOREIGN KEY (id) REFERENCES integration_keys(id) ON DELETE CASCADE,
	CONSTRAINT uik_config_pkey PRIMARY KEY (id),
	CONSTRAINT uik_config_primary_token_key UNIQUE (primary_token),
	CONSTRAINT uik_config_secondary_token_key UNIQUE (secondary_token)
);

CREATE UNIQUE INDEX uik_config_pkey ON public.uik_config USING btree (id);
CREATE UNIQUE INDEX uik_config_primary_token_key ON public.uik_config USING btree (primary_token);
CREATE UNIQUE INDEX uik_config_secondary_token_key ON public.uik_config USING btree (secondary_token);


CREATE TABLE user_calendar_subscriptions (
	config jsonb NOT NULL,
	created_at timestamp with time zone DEFAULT now() NOT NULL,
	disabled boolean DEFAULT false NOT NULL,
	id uuid NOT NULL,
	last_access timestamp with time zone,
	last_update timestamp with time zone DEFAULT now() NOT NULL,
	name text NOT NULL,
	schedule_id uuid NOT NULL,
	user_id uuid NOT NULL,
	CONSTRAINT user_calendar_subscriptions_name_schedule_id_user_id_key UNIQUE (name, schedule_id, user_id),
	CONSTRAINT user_calendar_subscriptions_pkey PRIMARY KEY (id),
	CONSTRAINT user_calendar_subscriptions_schedule_id_fkey FOREIGN KEY (schedule_id) REFERENCES schedules(id) ON DELETE CASCADE,
	CONSTRAINT user_calendar_subscriptions_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE UNIQUE INDEX user_calendar_subscriptions_name_schedule_id_user_id_key ON public.user_calendar_subscriptions USING btree (name, schedule_id, user_id);
CREATE UNIQUE INDEX user_calendar_subscriptions_pkey ON public.user_calendar_subscriptions USING btree (id);

CREATE CONSTRAINT TRIGGER trg_enforce_calendar_subscriptions_per_user_limit AFTER INSERT ON public.user_calendar_subscriptions NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_calendar_subscriptions_per_user_limit();


CREATE TABLE user_contact_methods (
	dest jsonb NOT NULL,
	disabled boolean DEFAULT false NOT NULL,
	enable_status_updates boolean DEFAULT false NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	last_test_verify_at timestamp with time zone,
	metadata jsonb,
	name text NOT NULL,
	pending boolean DEFAULT true NOT NULL,
	type enum_user_contact_method_type NOT NULL,
	user_id uuid NOT NULL,
	value text NOT NULL,
	CONSTRAINT user_contact_methods_dest_key UNIQUE (dest),
	CONSTRAINT user_contact_methods_pkey PRIMARY KEY (id),
	CONSTRAINT user_contact_methods_type_value_key UNIQUE (type, value),
	CONSTRAINT user_contact_methods_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX idx_contact_method_users ON public.user_contact_methods USING btree (user_id);
CREATE INDEX idx_valid_contact_methods ON public.user_contact_methods USING btree (id) WHERE (NOT disabled);
CREATE UNIQUE INDEX user_contact_methods_dest_key ON public.user_contact_methods USING btree (dest);
CREATE UNIQUE INDEX user_contact_methods_pkey ON public.user_contact_methods USING btree (id);
CREATE UNIQUE INDEX user_contact_methods_type_value_key ON public.user_contact_methods USING btree (type, value);

CREATE TRIGGER trg_10_cm_set_dest_on_insert BEFORE INSERT ON public.user_contact_methods FOR EACH ROW WHEN ((new.dest IS NULL)) EXECUTE FUNCTION fn_cm_set_dest_on_insert();
CREATE TRIGGER trg_10_cm_set_dest_on_update AFTER UPDATE ON public.user_contact_methods FOR EACH ROW WHEN (((new.dest = old.dest) AND ((new.type <> 'DEST'::enum_user_contact_method_type) AND ((new.value <> old.value) OR (new.type <> old.type))))) EXECUTE FUNCTION fn_cm_set_dest_on_insert();
CREATE TRIGGER trg_10_compat_set_type_val_on_insert BEFORE INSERT ON public.user_contact_methods FOR EACH ROW WHEN ((new.dest IS NOT NULL)) EXECUTE FUNCTION fn_cm_compat_set_type_val_on_insert();
CREATE TRIGGER trg_10_compat_set_type_val_on_update BEFORE UPDATE ON public.user_contact_methods FOR EACH ROW WHEN ((new.dest <> old.dest)) EXECUTE FUNCTION fn_cm_compat_set_type_val_on_insert();
CREATE TRIGGER trg_cm_set_not_pending_on_verify BEFORE UPDATE OF disabled ON public.user_contact_methods FOR EACH ROW WHEN (((NOT new.disabled) AND old.pending)) EXECUTE FUNCTION fn_cm_set_not_pending_on_verify();
CREATE CONSTRAINT TRIGGER trg_enforce_contact_method_limit AFTER INSERT ON public.user_contact_methods NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_contact_method_limit();


CREATE TABLE user_favorites (
	id bigint DEFAULT nextval('user_favorites_id_seq'::regclass) NOT NULL,
	tgt_escalation_policy_id uuid,
	tgt_rotation_id uuid,
	tgt_schedule_id uuid,
	tgt_service_id uuid,
	tgt_user_id uuid,
	user_id uuid NOT NULL,
	CONSTRAINT user_favorites_tgt_escalation_policy_id_fkey FOREIGN KEY (tgt_escalation_policy_id) REFERENCES escalation_policies(id) ON DELETE CASCADE,
	CONSTRAINT user_favorites_tgt_rotation_id_fkey FOREIGN KEY (tgt_rotation_id) REFERENCES rotations(id) ON DELETE CASCADE,
	CONSTRAINT user_favorites_tgt_schedule_id_fkey FOREIGN KEY (tgt_schedule_id) REFERENCES schedules(id) ON DELETE CASCADE,
	CONSTRAINT user_favorites_tgt_service_id_fkey FOREIGN KEY (tgt_service_id) REFERENCES services(id) ON DELETE CASCADE,
	CONSTRAINT user_favorites_tgt_user_id_fkey FOREIGN KEY (tgt_user_id) REFERENCES users(id) ON DELETE CASCADE,
	CONSTRAINT user_favorites_uniq_id UNIQUE (id),
	CONSTRAINT user_favorites_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
	CONSTRAINT user_favorites_user_id_tgt_escalation_policy_id_key UNIQUE (user_id, tgt_escalation_policy_id),
	CONSTRAINT user_favorites_user_id_tgt_rotation_id_key UNIQUE (user_id, tgt_rotation_id),
	CONSTRAINT user_favorites_user_id_tgt_schedule_id_key UNIQUE (user_id, tgt_schedule_id),
	CONSTRAINT user_favorites_user_id_tgt_service_id_key UNIQUE (user_id, tgt_service_id),
	CONSTRAINT user_favorites_user_id_tgt_user_id_key UNIQUE (user_id, tgt_user_id)
);

CREATE UNIQUE INDEX user_favorites_uniq_id ON public.user_favorites USING btree (id);
CREATE UNIQUE INDEX user_favorites_user_id_tgt_escalation_policy_id_key ON public.user_favorites USING btree (user_id, tgt_escalation_policy_id);
CREATE UNIQUE INDEX user_favorites_user_id_tgt_rotation_id_key ON public.user_favorites USING btree (user_id, tgt_rotation_id);
CREATE UNIQUE INDEX user_favorites_user_id_tgt_schedule_id_key ON public.user_favorites USING btree (user_id, tgt_schedule_id);
CREATE UNIQUE INDEX user_favorites_user_id_tgt_service_id_key ON public.user_favorites USING btree (user_id, tgt_service_id);
CREATE UNIQUE INDEX user_favorites_user_id_tgt_user_id_key ON public.user_favorites USING btree (user_id, tgt_user_id);


CREATE TABLE user_notification_rules (
	contact_method_id uuid NOT NULL,
	created_at timestamp with time zone DEFAULT now(),
	delay_minutes integer DEFAULT 0 NOT NULL,
	id uuid DEFAULT gen_random_uuid() NOT NULL,
	user_id uuid NOT NULL,
	CONSTRAINT user_notification_rules_contact_method_id_delay_minutes_key UNIQUE (contact_method_id, delay_minutes),
	CONSTRAINT user_notification_rules_contact_method_id_fkey FOREIGN KEY (contact_method_id) REFERENCES user_contact_methods(id) ON DELETE CASCADE,
	CONSTRAINT user_notification_rules_pkey PRIMARY KEY (id),
	CONSTRAINT user_notification_rules_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE INDEX idx_notif_rule_creation_time ON public.user_notification_rules USING btree (user_id, created_at);
CREATE INDEX idx_notification_rule_users ON public.user_notification_rules USING btree (user_id);
CREATE UNIQUE INDEX user_notification_rules_contact_method_id_delay_minutes_key ON public.user_notification_rules USING btree (contact_method_id, delay_minutes);
CREATE UNIQUE INDEX user_notification_rules_pkey ON public.user_notification_rules USING btree (id);

CREATE CONSTRAINT TRIGGER trg_enforce_notification_rule_limit AFTER INSERT ON public.user_notification_rules NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_notification_rule_limit();
CREATE TRIGGER trg_notification_rule_same_user BEFORE INSERT OR UPDATE ON public.user_notification_rules FOR EACH ROW EXECUTE FUNCTION fn_notification_rule_same_user();


CREATE TABLE user_overrides (
	add_user_id uuid,
	end_time timestamp with time zone NOT NULL,
	id uuid NOT NULL,
	remove_user_id uuid,
	start_time timestamp with time zone NOT NULL,
	tgt_schedule_id uuid NOT NULL,
	CONSTRAINT user_overrides_add_user_id_fkey FOREIGN KEY (add_user_id) REFERENCES users(id) ON DELETE CASCADE,
	CONSTRAINT user_overrides_check CHECK (end_time > start_time),
	CONSTRAINT user_overrides_check1 CHECK (COALESCE(add_user_id, remove_user_id) IS NOT NULL),
	CONSTRAINT user_overrides_check2 CHECK (add_user_id <> remove_user_id),
	CONSTRAINT user_overrides_pkey PRIMARY KEY (id),
	CONSTRAINT user_overrides_remove_user_id_fkey FOREIGN KEY (remove_user_id) REFERENCES users(id) ON DELETE CASCADE,
	CONSTRAINT user_overrides_tgt_schedule_id_fkey FOREIGN KEY (tgt_schedule_id) REFERENCES schedules(id) ON DELETE CASCADE
);

CREATE INDEX idx_user_overrides_schedule ON public.user_overrides USING btree (tgt_schedule_id, end_time);
CREATE UNIQUE INDEX user_overrides_pkey ON public.user_overrides USING btree (id);

CREATE CONSTRAINT TRIGGER trg_enforce_user_overide_no_conflict AFTER INSERT OR UPDATE ON public.user_overrides NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_user_overide_no_conflict();
CREATE CONSTRAINT TRIGGER trg_enforce_user_override_schedule_limit AFTER INSERT ON public.user_overrides NOT DEFERRABLE INITIALLY IMMEDIATE FOR EACH ROW EXECUTE FUNCTION fn_enforce_user_override_schedule_limit();


CREATE TABLE user_slack_data (
	access_token text NOT NULL,
	id uuid NOT NULL,
	CONSTRAINT user_slack_data_id_fkey FOREIGN KEY (id) REFERENCES users(id) ON DELETE CASCADE,
	CONSTRAINT user_slack_data_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX user_slack_data_pkey ON public.user_slack_data USING btree (id);


CREATE TABLE user_verification_codes (
	code integer NOT NULL,
	contact_method_id uuid NOT NULL,
	expires_at timestamp with time zone NOT NULL,
	id uuid NOT NULL,
	sent boolean DEFAULT false NOT NULL,
	CONSTRAINT user_verification_codes_contact_method_id_fkey FOREIGN KEY (contact_method_id) REFERENCES user_contact_methods(id) ON DELETE CASCADE,
	CONSTRAINT user_verification_codes_contact_method_id_key UNIQUE (contact_method_id),
	CONSTRAINT user_verification_codes_pkey PRIMARY KEY (id)
);

CREATE UNIQUE INDEX user_verification_codes_contact_method_id_key ON public.user_verification_codes USING btree (contact_method_id);
CREATE UNIQUE INDEX user_verification_codes_pkey ON public.user_verification_codes USING btree (id);


CREATE TABLE users (
	alert_status_log_contact_method_id uuid,
	avatar_url text DEFAULT ''::text NOT NULL,
	bio text DEFAULT ''::text NOT NULL,
	email text DEFAULT ''::text NOT NULL,
	id uuid NOT NULL,
	name text NOT NULL,
	role enum_user_role DEFAULT 'unknown'::enum_user_role NOT NULL,
	CONSTRAINT goalert_user_pkey PRIMARY KEY (id),
	CONSTRAINT users_alert_status_log_contact_method_id_fkey FOREIGN KEY (alert_status_log_contact_method_id) REFERENCES user_contact_methods(id) ON DELETE SET NULL DEFERRABLE
);

CREATE UNIQUE INDEX goalert_user_pkey ON public.users USING btree (id);
CREATE INDEX idx_search_users_name_eng ON public.users USING gin (to_tsvector('english'::regconfig, replace(lower(name), '.'::text, ' '::text)));
CREATE INDEX idx_user_status_updates ON public.users USING btree (alert_status_log_contact_method_id) WHERE (alert_status_log_contact_method_id IS NOT NULL);

CREATE TRIGGER trg_enforce_status_update_same_user BEFORE INSERT OR UPDATE ON public.users FOR EACH ROW EXECUTE FUNCTION fn_enforce_status_update_same_user();


-- Sequences

CREATE SEQUENCE incident_number_seq
	START WITH 1
	INCREMENT BY 1
	MINVALUE 1
	MAXVALUE 9223372036854775807
	CACHE 1;