-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstructure.sql
More file actions
6362 lines (4598 loc) · 157 KB
/
structure.sql
File metadata and controls
6362 lines (4598 loc) · 157 KB
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
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: pg_trgm; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pg_trgm WITH SCHEMA public;
--
-- Name: EXTENSION pg_trgm; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pg_trgm IS 'text similarity measurement and index searching based on trigrams';
--
-- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
--
CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
--
-- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
--
COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
--
-- Name: source_type; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.source_type AS ENUM (
'manual',
'stripe',
'github',
'unknown'
);
--
-- Name: agg_all_repo_counts(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.agg_all_repo_counts() RETURNS boolean
LANGUAGE plpgsql
AS $$
begin
with src as (
select cnt.repository_id
from repo_counts cnt
group by cnt.repository_id
having count(1) > 1
),
del as (
delete from repo_counts cnt
using src
where cnt.repository_id = src.repository_id
returning cnt.*
),
agg as (
select
del.repository_id,
sum(del.requests)::integer as requests,
sum(del.commits)::integer as commits,
sum(del.branches)::integer as branches,
sum(del.pull_requests)::integer as pull_requests,
sum(del.tags)::integer as tags,
sum(del.builds)::integer as builds,
-- sum(del.stages)::integer as stages,
sum(del.jobs)::integer as jobs
from del
group by del.repository_id
)
insert into repo_counts(
repository_id,
requests,
commits,
branches,
pull_requests,
tags,
builds,
-- stages,
jobs
)
select
agg.repository_id,
agg.requests,
agg.commits,
agg.branches,
agg.pull_requests,
agg.tags,
agg.builds,
-- agg.stages,
agg.jobs
from agg;
return true;
end;
$$;
--
-- Name: agg_repo_counts(integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.agg_repo_counts(_repo_id integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
begin
with src as (
select cnt.repository_id
from repo_counts cnt
where cnt.repository_id = _repo_id
group by cnt.repository_id
having count(1) > 1
),
del as (
delete from repo_counts cnt
using src
where cnt.repository_id = src.repository_id
returning cnt.*
),
agg as (
select
del.repository_id,
sum(del.requests)::integer as requests,
sum(del.commits)::integer as commits,
sum(del.branches)::integer as branches,
sum(del.pull_requests)::integer as pull_requests,
sum(del.tags)::integer as tags,
sum(del.builds)::integer as builds,
-- sum(del.stages)::integer as stages,
sum(del.jobs)::integer as jobs
from del
group by del.repository_id
)
insert into repo_counts(
repository_id,
requests,
commits,
branches,
pull_requests,
tags,
builds,
-- stages,
jobs
)
select
agg.repository_id,
agg.requests,
agg.commits,
agg.branches,
agg.pull_requests,
agg.tags,
agg.builds,
-- agg.stages,
agg.jobs
from agg
where agg.requests > 0 or agg.builds > 0 or agg.jobs > 0;
return true;
end;
$$;
--
-- Name: count_all_branches(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_all_branches(_count integer, _start integer, _end integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare max int;
begin
select id + _count from branches order by id desc limit 1 into max;
for i in _start.._end by _count loop
if i > max then exit; end if;
begin
raise notice 'counting branches %', i;
insert into repo_counts(repository_id, branches, range)
select * from count_branches(i, i + _count - 1);
exception when unique_violation then end;
end loop;
return true;
end
$$;
--
-- Name: count_all_builds(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_all_builds(_count integer, _start integer, _end integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare max int;
begin
select id + _count from builds order by id desc limit 1 into max;
for i in _start.._end by _count loop
if i > max then exit; end if;
begin
raise notice 'counting builds %', i;
insert into repo_counts(repository_id, builds, range)
select * from count_builds(i, i + _count - 1);
exception when unique_violation then end;
end loop;
return true;
end
$$;
--
-- Name: count_all_commits(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_all_commits(_count integer, _start integer, _end integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare max int;
begin
select id + _count from commits order by id desc limit 1 into max;
for i in _start.._end by _count loop
if i > max then exit; end if;
begin
raise notice 'counting commits %', i;
insert into repo_counts(repository_id, commits, range)
select * from count_commits(i, i + _count - 1);
exception when unique_violation then end;
end loop;
return true;
end
$$;
--
-- Name: count_all_jobs(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_all_jobs(_count integer, _start integer, _end integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare max int;
begin
select id + _count from jobs order by id desc limit 1 into max;
for i in _start.._end by _count loop
if i > max then exit; end if;
begin
raise notice 'counting jobs %', i;
insert into repo_counts(repository_id, jobs, range)
select * from count_jobs(i, i + _count - 1);
exception when unique_violation then end;
end loop;
return true;
end
$$;
--
-- Name: count_all_pull_requests(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_all_pull_requests(_count integer, _start integer, _end integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare max int;
begin
select id + _count from pull_requests order by id desc limit 1 into max;
for i in _start.._end by _count loop
if i > max then exit; end if;
begin
raise notice 'counting pull_requests %', i;
insert into repo_counts(repository_id, pull_requests, range)
select * from count_pull_requests(i, i + _count - 1);
exception when unique_violation then end;
end loop;
return true;
end
$$;
--
-- Name: count_all_requests(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_all_requests(_count integer, _start integer, _end integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare max int;
begin
select id + _count from requests order by id desc limit 1 into max;
for i in _start.._end by _count loop
if i > max then exit; end if;
begin
raise notice 'counting requests %', i;
insert into repo_counts(repository_id, requests, range)
select * from count_requests(i, i + _count - 1);
exception when unique_violation then end;
end loop;
return true;
end
$$;
--
-- Name: count_all_tags(integer, integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_all_tags(_count integer, _start integer, _end integer) RETURNS boolean
LANGUAGE plpgsql
AS $$
declare max int;
begin
select id + _count from tags order by id desc limit 1 into max;
for i in _start.._end by _count loop
if i > max then exit; end if;
begin
raise notice 'counting tags %', i;
insert into repo_counts(repository_id, tags, range)
select * from count_tags(i, i + _count - 1);
exception when unique_violation then end;
end loop;
return true;
end
$$;
--
-- Name: count_branches(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_branches() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
c text;
r record;
begin
if tg_argv[0]::int > 0 then r := new; else r := old; end if;
if r.repository_id is not null then
insert into repo_counts(repository_id, branches)
values(r.repository_id, tg_argv[0]::int);
end if;
return r;
exception when others then
get stacked diagnostics c = pg_exception_context;
raise warning '% context: %s', sqlerrm, c;
return r;
end;
$$;
--
-- Name: count_branches(integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_branches(_start integer, _end integer) RETURNS TABLE(repository_id integer, branches bigint, range character varying)
LANGUAGE plpgsql
AS $$
begin
return query select r.id, count(t.id) as branches, ('branches' || ':' || _start || ':' || _end)::varchar as range
from branches as t
join repositories as r on t.repository_id = r.id
where t.id between _start and _end and t.created_at <= '2018-01-01 00:00:00' and t.repository_id is not null
group by r.id;
end;
$$;
--
-- Name: count_builds(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_builds() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
c text;
r record;
begin
if tg_argv[0]::int > 0 then r := new; else r := old; end if;
if r.repository_id is not null then
insert into repo_counts(repository_id, builds)
values(r.repository_id, tg_argv[0]::int);
end if;
return r;
exception when others then
get stacked diagnostics c = pg_exception_context;
raise warning '% context: %s', sqlerrm, c;
return r;
end;
$$;
--
-- Name: count_builds(integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_builds(_start integer, _end integer) RETURNS TABLE(repository_id integer, builds bigint, range character varying)
LANGUAGE plpgsql
AS $$
begin
return query select t.repository_id, count(id) as builds, ('builds' || ':' || _start || ':' || _end)::varchar as range
from builds as t
where t.id between _start and _end and t.created_at <= '2018-01-01 00:00:00' and t.repository_id is not null
group by t.repository_id;
end;
$$;
--
-- Name: count_commits(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_commits() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
c text;
r record;
begin
if tg_argv[0]::int > 0 then r := new; else r := old; end if;
if r.repository_id is not null then
insert into repo_counts(repository_id, commits)
values(r.repository_id, tg_argv[0]::int);
end if;
return r;
exception when others then
get stacked diagnostics c = pg_exception_context;
raise warning '% context: %s', sqlerrm, c;
return r;
end;
$$;
--
-- Name: count_commits(integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_commits(_start integer, _end integer) RETURNS TABLE(repository_id integer, commits bigint, range character varying)
LANGUAGE plpgsql
AS $$
begin
return query select r.id, count(t.id) as commits, ('commits' || ':' || _start || ':' || _end)::varchar as range
from commits as t
join repositories as r on t.repository_id = r.id
where t.id between _start and _end and t.created_at <= '2018-01-01 00:00:00' and t.repository_id is not null
group by r.id;
end;
$$;
--
-- Name: count_jobs(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_jobs() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
c text;
r record;
begin
if tg_argv[0]::int > 0 then r := new; else r := old; end if;
if r.repository_id is not null then
insert into repo_counts(repository_id, jobs)
values(r.repository_id, tg_argv[0]::int);
end if;
return r;
exception when others then
get stacked diagnostics c = pg_exception_context;
raise warning '% context: %s', sqlerrm, c;
return r;
end;
$$;
--
-- Name: count_jobs(integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_jobs(_start integer, _end integer) RETURNS TABLE(repository_id integer, jobs bigint, range character varying)
LANGUAGE plpgsql
AS $$
begin
return query select t.repository_id, count(id) as jobs, ('jobs' || ':' || _start || ':' || _end)::varchar as range
from jobs as t
where t.id between _start and _end and t.created_at <= '2018-01-01 00:00:00' and t.repository_id is not null
group by t.repository_id;
end;
$$;
--
-- Name: count_pull_requests(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_pull_requests() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
c text;
r record;
begin
if tg_argv[0]::int > 0 then r := new; else r := old; end if;
if r.repository_id is not null then
insert into repo_counts(repository_id, pull_requests)
values(r.repository_id, tg_argv[0]::int);
end if;
return r;
exception when others then
get stacked diagnostics c = pg_exception_context;
raise warning '% context: %s', sqlerrm, c;
return r;
end;
$$;
--
-- Name: count_pull_requests(integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_pull_requests(_start integer, _end integer) RETURNS TABLE(repository_id integer, pull_requests bigint, range character varying)
LANGUAGE plpgsql
AS $$
begin
return query select r.id, count(t.id) as pull_requests, ('pull_requests' || ':' || _start || ':' || _end)::varchar as range
from pull_requests as t
join repositories as r on t.repository_id = r.id
where t.id between _start and _end and t.created_at <= '2018-01-01 00:00:00' and t.repository_id is not null
group by r.id;
end;
$$;
--
-- Name: count_requests(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_requests() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
c text;
r record;
begin
if tg_argv[0]::int > 0 then r := new; else r := old; end if;
if r.repository_id is not null then
insert into repo_counts(repository_id, requests)
values(r.repository_id, tg_argv[0]::int);
end if;
return r;
exception when others then
get stacked diagnostics c = pg_exception_context;
raise warning '% context: %s', sqlerrm, c;
return r;
end;
$$;
--
-- Name: count_requests(integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_requests(_start integer, _end integer) RETURNS TABLE(repository_id integer, requests bigint, range character varying)
LANGUAGE plpgsql
AS $$
begin
return query select t.repository_id, count(id) as requests, ('requests' || ':' || _start || ':' || _end)::varchar as range
from requests as t
where t.id between _start and _end and t.created_at <= '2018-01-01 00:00:00' and t.repository_id is not null
group by t.repository_id;
end;
$$;
--
-- Name: count_tags(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_tags() RETURNS trigger
LANGUAGE plpgsql
AS $$
declare
c text;
r record;
begin
if tg_argv[0]::int > 0 then r := new; else r := old; end if;
if r.repository_id is not null is not null then
insert into repo_counts(repository_id, tags)
values(r.repository_id, tg_argv[0]::int);
end if;
return r;
exception when others then
get stacked diagnostics c = pg_exception_context;
raise warning '% context: %s', sqlerrm, c;
return r;
end;
$$;
--
-- Name: count_tags(integer, integer); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.count_tags(_start integer, _end integer) RETURNS TABLE(repository_id integer, tags bigint, range character varying)
LANGUAGE plpgsql
AS $$
begin
return query select r.id, count(t.id) as tags, ('tags' || ':' || _start || ':' || _end)::varchar as range
from tags as t
join repositories as r on t.repository_id = r.id
where t.id between _start and _end and t.created_at <= '2018-01-01 00:00:00' and t.repository_id is not null
group by r.id;
end;
$$;
--
-- Name: is_json(text); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.is_json(text) RETURNS boolean
LANGUAGE plpgsql IMMUTABLE
AS $_$
BEGIN
perform $1::json;
return true;
EXCEPTION WHEN invalid_text_representation THEN
return false;
END
$_$;
--
-- Name: most_recent_job_ids_for_repository_by_state(integer, character varying); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.most_recent_job_ids_for_repository_by_state(rid integer, st character varying) RETURNS TABLE(job_id bigint, repository_id integer)
LANGUAGE plpgsql
AS $$
DECLARE
BEGIN
RETURN QUERY select j.id, j.repository_id from jobs j where j.repository_id = rid and j.state = st order by j.id desc limit 100;
END
$$;
--
-- Name: most_recent_job_ids_for_user_repositories_by_states(integer, character varying); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.most_recent_job_ids_for_user_repositories_by_states(uid integer, states character varying DEFAULT ''::character varying) RETURNS TABLE(id bigint)
LANGUAGE plpgsql
AS $$
DECLARE
rid int;
BEGIN
SET LOCAL work_mem = '16MB';
IF states <> '' THEN
RETURN QUERY WITH matrix AS (
SELECT repository_id, replace(replace(job_state::varchar, '(', ''), ')', '') as job_state
FROM permissions p
CROSS JOIN (
SELECT unnest(regexp_split_to_array(states, ','))
) AS job_state
WHERE p.user_id = uid
)
SELECT recent.id
FROM matrix m
CROSS JOIN LATERAL (
SELECT job_id AS id, repository_id
FROM most_recent_job_ids_for_repository_by_state(m.repository_id, m.job_state::varchar)
) AS recent
ORDER BY id desc;
ELSE
for rid in
SELECT repository_id
FROM permissions
WHERE user_id = uid
LOOP
RETURN QUERY select j.id from jobs j where repository_id = rid order by j.id desc limit 100;
END LOOP;
END IF;
END
$$;
--
-- Name: most_recent_job_ids_for_user_repositories_by_states_lw(integer, character varying); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.most_recent_job_ids_for_user_repositories_by_states_lw(uid integer, states character varying DEFAULT ''::character varying) RETURNS TABLE(id bigint)
LANGUAGE plpgsql
AS $$
DECLARE
rid int;
BEGIN
SET LOCAL work_mem = '16MB';
IF states <> '' THEN
for rid in
SELECT repository_id
FROM permissions
WHERE user_id = uid
LOOP
RETURN QUERY select j.id from jobs j where repository_id = rid and state in (SELECT unnest(regexp_split_to_array(states, ','))) order by j.id desc limit 100;
END LOOP;
ELSE
for rid in
SELECT repository_id
FROM permissions
WHERE user_id = uid
LOOP
RETURN QUERY select j.id from jobs j where repository_id = rid order by j.id desc limit 100;
END LOOP;
END IF;
END
$$;
--
-- Name: set_unique_number(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.set_unique_number() RETURNS trigger
LANGUAGE plpgsql
AS $$
DECLARE
disable boolean;
BEGIN
disable := 'f';
IF TG_OP = 'INSERT' OR TG_OP = 'UPDATE' THEN
BEGIN
disable := current_setting('set_unique_number_on_builds.disable');
EXCEPTION
WHEN others THEN
set set_unique_number_on_builds.disable = 'f';
END;
IF NOT disable THEN
IF NEW.unique_number IS NULL OR NEW.unique_number > 0 THEN
NEW.unique_number := NEW.number;
END IF;
END IF;
END IF;
RETURN NEW;
END;
$$;
--
-- Name: set_updated_at(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.set_updated_at() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
IF TG_OP = 'INSERT' OR
(TG_OP = 'UPDATE' AND NEW.* IS DISTINCT FROM OLD.*) THEN
NEW.updated_at := statement_timestamp();
END IF;
RETURN NEW;
END;
$$;
--
-- Name: soft_delete_repo_data(bigint); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.soft_delete_repo_data(r_id bigint) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
request_raw_config_ids bigint[];
request_raw_configuration_ids bigint[];
request_yaml_config_ids bigint[];
request_config_ids bigint[];
tag_ids bigint[];
ssl_key_ids bigint[];
build_config_ids bigint[];
job_config_ids bigint[];
build_ids bigint[];
pull_request_ids bigint[];
commit_ids bigint[];
request_ids bigint[];
request_payload_ids bigint[];
stage_ids bigint[];
job_ids bigint[];
BEGIN
SELECT INTO job_ids array_agg(id) FROM jobs WHERE repository_id = r_id;
SELECT INTO stage_ids array_agg(id) FROM stages WHERE build_id IN (SELECT id FROM builds WHERE repository_id = r_id);
SELECT INTO request_payload_ids array_agg(id) FROM request_payloads WHERE request_id IN (SELECT id FROM requests WHERE repository_id = r_id);
SELECT INTO request_ids array_agg(id) FROM requests WHERE repository_id = r_id;
SELECT INTO commit_ids array_agg(id) FROM commits WHERE repository_id = r_id;
SELECT INTO pull_request_ids array_agg(id) FROM pull_requests WHERE repository_id = r_id;
SELECT INTO build_ids array_agg(id) FROM builds WHERE repository_id = r_id;
SELECT INTO job_config_ids array_agg(id) FROM job_configs WHERE repository_id = r_id;
SELECT INTO build_config_ids array_agg(id) FROM build_configs WHERE repository_id = r_id;
SELECT INTO ssl_key_ids array_agg(id) FROM ssl_keys WHERE repository_id = r_id;
SELECT INTO tag_ids array_agg(id) FROM tags WHERE repository_id = r_id;
SELECT INTO request_config_ids array_agg(id) FROM request_configs WHERE repository_id = r_id;
SELECT INTO request_yaml_config_ids array_agg(id) FROM request_yaml_configs WHERE repository_id = r_id;
SELECT INTO request_raw_configuration_ids array_agg(id) FROM request_raw_configurations WHERE request_id = ANY(request_ids);
SELECT INTO request_raw_config_ids array_agg(id) FROM request_raw_configs WHERE id IN (SELECT request_raw_config_id FROM request_raw_configurations WHERE request_id = ANY(request_ids));
INSERT INTO deleted_jobs SELECT * FROM jobs WHERE id = ANY(job_ids);
INSERT INTO deleted_stages SELECT * FROM stages WHERE id = ANY(stage_ids);
INSERT INTO deleted_request_payloads SELECT * FROM request_payloads WHERE id = ANY(request_payload_ids);
INSERT INTO deleted_requests SELECT * FROM requests WHERE id = ANY(request_ids);
INSERT INTO deleted_commits SELECT * FROM commits WHERE id = ANY(commit_ids);
INSERT INTO deleted_pull_requests SELECT * FROM pull_requests WHERE id = ANY(pull_request_ids);
INSERT INTO deleted_builds SELECT * FROM builds WHERE id = ANY(build_ids);
INSERT INTO deleted_job_configs SELECT * FROM job_configs WHERE id = ANY(job_config_ids);
INSERT INTO deleted_build_configs SELECT * FROM build_configs WHERE id = ANY(build_config_ids);
INSERT INTO deleted_ssl_keys SELECT * FROM ssl_keys WHERE id = ANY(ssl_key_ids);
INSERT INTO deleted_tags SELECT * FROM tags WHERE id = ANY(tag_ids);
INSERT INTO deleted_request_configs SELECT * FROM request_configs WHERE id = ANY(request_config_ids);
INSERT INTO deleted_request_yaml_configs SELECT * FROM request_yaml_configs WHERE id = ANY(request_yaml_config_ids);
INSERT INTO deleted_request_raw_configurations SELECT * FROM request_raw_configurations WHERE id = ANY(request_raw_configuration_ids);
INSERT INTO deleted_request_raw_configs SELECT * FROM request_raw_configs WHERE id = ANY(request_raw_config_ids);
DELETE FROM jobs WHERE id = ANY(job_ids);
DELETE FROM stages WHERE id = ANY(stage_ids);
DELETE FROM request_payloads WHERE id = ANY(request_payload_ids);
DELETE FROM requests WHERE id = ANY(request_ids);
DELETE FROM commits WHERE id = ANY(commit_ids);
DELETE FROM pull_requests WHERE id = ANY(pull_request_ids);
DELETE FROM builds WHERE id = ANY(build_ids);
DELETE FROM job_configs WHERE id = ANY(job_config_ids);
DELETE FROM build_configs WHERE id = ANY(build_config_ids);
DELETE FROM ssl_keys WHERE id = ANY(ssl_key_ids);
DELETE FROM tags WHERE id = ANY(tag_ids);
DELETE FROM request_configs WHERE id = ANY(request_config_ids);
DELETE FROM request_yaml_configs WHERE id = ANY(request_yaml_config_ids);
DELETE FROM request_raw_configurations WHERE id = ANY(request_raw_configuration_ids);
DELETE FROM request_raw_configs WHERE id = ANY(request_raw_config_ids);
END;
$$;
SET default_tablespace = '';
--
-- Name: abuses; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.abuses (
id integer NOT NULL,
owner_type character varying,
owner_id integer,
request_id integer,
level integer NOT NULL,
reason character varying NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);
--
-- Name: abuses_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.abuses_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: abuses_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.abuses_id_seq OWNED BY public.abuses.id;
--
-- Name: ar_internal_metadata; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.ar_internal_metadata (
key character varying NOT NULL,
value character varying,
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);
--
-- Name: audits; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.audits (
id bigint NOT NULL,
owner_id integer,
owner_type character varying,
created_at timestamp without time zone,
change_source character varying,
source_changes json,
source_id integer,
source_type character varying
);
--
-- Name: audits_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.audits_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: audits_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.audits_id_seq OWNED BY public.audits.id;
--
-- Name: beta_features; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.beta_features (
id integer NOT NULL,
name character varying,
description text,
feedback_url character varying,
staff_only boolean,
default_enabled boolean,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
--
-- Name: beta_features_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.beta_features_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: beta_features_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.beta_features_id_seq OWNED BY public.beta_features.id;
--
-- Name: beta_migration_requests; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.beta_migration_requests (
id integer NOT NULL,
owner_id integer,
owner_name character varying,
owner_type character varying,
created_at timestamp without time zone,
accepted_at timestamp without time zone
);