-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_ci--0.2.sql
1524 lines (1300 loc) · 44.9 KB
/
postgres_ci--0.2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\echo Use "CREATE EXTENSION postgres_ci" to load this file. \quit
set statement_timeout = 0;
set client_encoding = 'UTF8';
set client_min_messages = warning;
set escape_string_warning = off;
set standard_conforming_strings = on;
/* source file: src/schema.sql */
--create schema postgres_ci;
create or replace function postgres_ci.sha1(_value text) returns text as $$
begin
return encode(digest(_value, 'sha1'), 'hex');
end;
$$ language plpgsql;
create sequence postgres_ci.users_seq;
create table postgres_ci.users (
user_id int not null default nextval('postgres_ci.users_seq') primary key,
user_name text not null,
user_login text not null,
user_email text not null,
hash text not null,
salt text not null,
is_superuser boolean not null default false,
is_deleted boolean not null default false,
created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp,
constraint check_user_hash check(length(hash) = 40),
constraint check_user_salt check(length(salt) = 40),
constraint check_user_email check(strpos(user_email, '@') > 0)
);
create unique index unique_user_login on postgres_ci.users (lower(user_login));
create unique index unique_user_email on postgres_ci.users (lower(user_email));
create index find_user on postgres_ci.users using gin(lower(user_name || user_login || user_email) gin_trgm_ops);
create unlogged table postgres_ci.sessions(
session_id text not null default postgres_ci.sha1(gen_salt('md5') || gen_salt('md5')) primary key,
user_id int not null references postgres_ci.users(user_id),
expires_at timestamptz not null default current_timestamp
);
create index idx_sessions_expires_at on postgres_ci.sessions(expires_at);
create type postgres_ci.status as enum(
'pending',
'accepted',
'running',
'failed',
'success'
);
create sequence postgres_ci.projects_seq;
create table postgres_ci.projects(
project_id int not null default nextval('postgres_ci.projects_seq') primary key,
project_name text not null,
project_token uuid not null default gen_random_uuid(),
project_owner_id int not null,
repository_url text not null,
github_name text not null,
github_secret text not null,
last_build_id int,
is_deleted boolean not null default false,
created_at timestamptz not null default current_timestamp,
updated_at timestamptz not null default current_timestamp,
unique(project_token)
);
create unique index udx_is_github_repo on postgres_ci.projects (github_name) where github_name <> '';
alter table postgres_ci.projects add constraint fk_project_owner_id foreign key (project_owner_id) references postgres_ci.users(user_id);
create sequence postgres_ci.branches_seq;
create table postgres_ci.branches(
branch_id int not null default nextval('postgres_ci.branches_seq'),
branch text not null,
project_id int not null references postgres_ci.projects(project_id),
created_at timestamptz not null default current_timestamp,
primary key (project_id, branch_id)
);
create index idx_branch on postgres_ci.branches (branch_id);
create sequence postgres_ci.commits_seq;
create table postgres_ci.commits(
commit_id int not null default nextval('postgres_ci.commits_seq') primary key,
project_id int not null,
branch_id int not null,
commit_sha text not null,
commit_message text not null,
committed_at timestamptz not null,
committer_name text not null,
committer_email text not null,
author_name text not null,
author_email text not null,
created_at timestamptz not null default current_timestamp,
constraint check_commit_sha check(length(commit_sha) = 40),
constraint uniq_commit unique(branch_id, commit_sha),
foreign key (project_id, branch_id) references postgres_ci.branches(project_id, branch_id) match full
);
create sequence postgres_ci.builds_seq;
create table postgres_ci.builds(
build_id int not null default nextval('postgres_ci.builds_seq') primary key,
project_id int not null,
branch_id int not null,
commit_id int not null references postgres_ci.commits(commit_id),
config text not null,
status postgres_ci.status not null,
error text not null default '',
created_at timestamptz not null default current_timestamp,
started_at timestamptz,
finished_at timestamptz,
foreign key (project_id, branch_id) references postgres_ci.branches(project_id, branch_id) match full
);
create index idx_new_build on postgres_ci.builds(status) where status in ('pending', 'accepted', 'running');
create index idx_p_b_build on postgres_ci.builds(project_id, branch_id);
alter table postgres_ci.projects add foreign key (last_build_id) references postgres_ci.builds(build_id);
create table postgres_ci.builds_counters(
project_id int not null,
branch_id int not null,
counter bigint not null,
constraint unique_builds_counters unique(project_id, branch_id),
foreign key (project_id, branch_id) references postgres_ci.branches(project_id, branch_id) match full
);
create sequence postgres_ci.parts_seq;
create table postgres_ci.parts(
part_id int not null default nextval('postgres_ci.parts_seq') primary key,
build_id int not null references postgres_ci.builds(build_id),
image text not null,
container_id text not null,
version text not null,
output text not null,
success boolean not null,
started_at timestamptz not null,
finished_at timestamptz not null
);
create index idx_parts_build on postgres_ci.parts(build_id);
create table postgres_ci.tests(
part_id int not null references postgres_ci.parts(part_id),
function text not null,
errors jsonb not null default '[]',
duration real not null
);
create index idx_part_tests on postgres_ci.tests(part_id);
/*
select * from users.add('user', 'password', 'User', '[email protected]', true);
*/
/* source file: src/packages.sql */
create schema auth;
create schema hook;
create schema users;
create schema build;
create schema project;
create schema password;
grant usage on schema auth to public;
grant usage on schema hook to public;
grant usage on schema users to public;
grant usage on schema build to public;
grant usage on schema project to public;
grant usage on schema password to public;
grant usage on schema postgres_ci to public;
grant execute on all functions in schema auth to public;
grant execute on all functions in schema hook to public;
grant execute on all functions in schema users to public;
grant execute on all functions in schema build to public;
grant execute on all functions in schema project to public;
grant execute on all functions in schema password to public;
grant execute on all functions in schema postgres_ci to public;
/* source file: src/pg_extension_config_dump.sql */
select pg_catalog.pg_extension_config_dump('postgres_ci.users', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.users_seq', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.projects', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.projects_seq', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.branches', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.branches_seq', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.commits', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.commits_seq', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.builds', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.builds_seq', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.parts', '');
select pg_catalog.pg_extension_config_dump('postgres_ci.parts_seq', '');
/* source file: src/functions/auth/gc.sql */
create or replace function auth.gc() returns void as $$
begin
DELETE FROM postgres_ci.sessions WHERE expires_at < CURRENT_TIMESTAMP;
end;
$$ language plpgsql security definer;
/* source file: src/functions/auth/get_user.sql */
create or replace function auth.get_user(
_session_id text
) returns table(
user_id int,
user_name text,
user_login text,
user_email text,
is_superuser boolean,
created_at timestamptz
) as $$
begin
return query
SELECT
U.user_id,
U.user_name,
U.user_login,
U.user_email,
U.is_superuser,
U.created_at
FROM postgres_ci.users AS U
JOIN postgres_ci.sessions AS S USING(user_id)
WHERE U.is_deleted = false
AND S.session_id = _session_id
AND S.expires_at > CURRENT_TIMESTAMP;
IF FOUND THEN
UPDATE postgres_ci.sessions
SET
expires_at = CURRENT_TIMESTAMP + '1 hour'::interval
WHERE session_id = _session_id;
END IF;
end;
$$ language plpgsql security definer rows 1;
/* source file: src/functions/auth/login.sql */
create or replace function auth.login(
_login text,
_password text,
out session_id text
) returns text as $$
declare
_user_id int;
_invalid_password boolean;
begin
SELECT
U.user_id,
encode(digest(U.salt || _password, 'sha1'), 'hex') != U.hash
INTO
_user_id,
_invalid_password
FROM postgres_ci.users AS U
WHERE lower(U.user_login) = lower(_login)
AND is_deleted = false;
CASE
WHEN NOT FOUND THEN
RAISE EXCEPTION 'NOT_FOUND' USING ERRCODE = 'no_data_found';
WHEN _invalid_password THEN
RAISE EXCEPTION 'INVALID_PASSWORD' USING ERRCODE = 'invalid_password';
ELSE
INSERT INTO postgres_ci.sessions (
user_id,
expires_at
) VALUES (
_user_id,
CURRENT_TIMESTAMP + '1 hour'::interval
) RETURNING sessions.session_id INTO session_id;
END CASE;
end;
$$ language plpgsql security definer;
/* source file: src/functions/auth/logout.sql */
create or replace function auth.logout(_session_id text) returns void as $$
begin
DELETE FROM postgres_ci.sessions WHERE session_id = _session_id;
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/accept.sql */
create or replace function build.accept(
_build_id int,
out accept boolean
) returns boolean as $$
begin
UPDATE postgres_ci.builds
SET status = 'accepted'
WHERE status = 'pending'
AND build_id = _build_id;
IF NOT FOUND THEN
accept = false;
return;
END IF;
accept = true;
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/add_part.sql */
create or replace function build.add_part(
_build_id int,
_version text,
_image text,
_container_id text,
_output text,
_started_at timestamptz,
_tests jsonb,
out part_id int
) returns int as $$
declare
_test postgres_ci.tests;
begin
INSERT INTO postgres_ci.parts (
build_id,
version,
image,
container_id,
output,
success,
started_at,
finished_at
) VALUES (
_build_id,
_version,
_image,
_container_id,
_output,
true,
_started_at,
CURRENT_TIMESTAMP
) RETURNING parts.part_id INTO add_part.part_id;
INSERT INTO postgres_ci.tests (
part_id,
function,
errors,
duration
)
SELECT
add_part.part_id,
E.function,
E.errors,
E.duration
FROM jsonb_populate_recordset(null::postgres_ci.tests, _tests) AS E;
IF EXISTS(
SELECT
FROM postgres_ci.tests
WHERE tests.part_id = add_part.part_id
AND jsonb_array_length(errors) > 0
LIMIT 1
) THEN
UPDATE postgres_ci.parts SET success = false WHERE parts.part_id = add_part.part_id;
END IF;
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/fetch.sql */
create or replace function build.fetch() returns table (
build_id int,
created_at timestamptz
) as $$
declare
_build_id int;
_created_at timestamptz;
begin
SELECT
B.build_id,
B.created_at
INTO
_build_id,
_created_at
FROM postgres_ci.builds AS B
WHERE B.status = 'pending'
ORDER BY B.build_id
LIMIT 1
FOR UPDATE SKIP LOCKED;
IF NOT FOUND THEN
return;
END IF;
UPDATE postgres_ci.builds AS B SET status = 'accepted' WHERE B.build_id = _build_id;
return query
SELECT _build_id, _created_at;
end;
$$ language plpgsql security definer rows 1;
/* source file: src/functions/build/gc.sql */
create or replace function build.gc() returns void as $$
declare
_build_id int;
begin
FOR _build_id IN
SELECT
build_id
FROM postgres_ci.builds
WHERE status IN ('accepted', 'running')
AND created_at < (current_timestamp - '1 hour'::interval)
ORDER BY build_id
LOOP
UPDATE postgres_ci.builds AS B
SET
status = 'failed',
error = 'Execution timeout',
finished_at = current_timestamp
WHERE B.build_id = _build_id;
PERFORM
pg_notify('postgres-ci::stop_container', (
SELECT to_json(T.*) FROM (
SELECT
P.container_id,
current_timestamp AS created_at
) T
)::text
)
FROM postgres_ci.parts AS P
WHERE P.build_id = _build_id;
END LOOP;
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/list.sql */
create or replace function build.list(
_project_id int,
_branch_id int,
_limit int,
_offset int,
out project_id int,
out project_name text,
out total bigint,
out branches jsonb,
out items jsonb
) returns record as $$
begin
SELECT
P.project_id,
P.project_name,
(
SELECT
COALESCE(SUM(C.counter), 0)
FROM postgres_ci.builds_counters AS C
WHERE C.project_id = _project_id
AND (
CASE WHEN _branch_id <> 0
THEN branch_id = _branch_id
ELSE true
END
)
),
(
SELECT
COALESCE(array_to_json(array_agg(R.*)), '[]')
FROM (
SELECT
B.branch_id,
B.branch
FROM postgres_ci.branches AS B
WHERE
B.project_id = _project_id
ORDER BY branch
) AS R
),
(
SELECT
COALESCE(array_to_json(array_agg(R.*)), '[]')
FROM (
SELECT
BD.build_id,
BD.project_id,
BD.status,
BD.error,
BD.created_at,
BD.started_at,
BD.finished_at,
C.commit_sha,
C.commit_message,
B.branch,
B.branch_id
FROM postgres_ci.builds AS BD
JOIN postgres_ci.commits AS C USING(commit_id)
JOIN postgres_ci.branches AS B ON B.branch_id = C.branch_id
WHERE
BD.project_id = _project_id
AND (
CASE WHEN _branch_id <> 0
THEN BD.branch_id = _branch_id
ELSE true
END
)
ORDER BY build_id DESC
LIMIT _limit
OFFSET _offset
) AS R
)
FROM postgres_ci.projects AS P
WHERE P.project_id = _project_id
AND P.is_deleted = false
INTO project_id, project_name, total, branches, items;
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/new.sql */
create or replace function build.new(
_project_id int,
_branch_id int,
_commit_id int,
out build_id int
) returns int as $$
begin
INSERT INTO postgres_ci.builds (
project_id,
branch_id,
commit_id,
config,
status
) VALUES (
_project_id,
_branch_id,
_commit_id,
'',
'pending'
) RETURNING builds.build_id INTO build_id;
INSERT INTO postgres_ci.builds_counters AS C (
project_id,
branch_id,
counter
) VALUES (
_project_id,
_branch_id,
1
) ON CONFLICT
ON CONSTRAINT unique_builds_counters DO UPDATE
SET counter = C.counter + 1;
PERFORM pg_notify('postgres-ci::tasks', (
SELECT to_json(T.*) FROM (
SELECT
new.build_id AS build_id,
CURRENT_TIMESTAMP AS created_at
) T
)::text
);
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/start.sql */
create or replace function build.start(
_build_id int,
out repository_url text,
out branch text,
out revision text
) returns record as $$
declare
_commit_id int;
_project_id int;
begin
UPDATE postgres_ci.builds AS B
SET
status = 'running',
started_at = current_timestamp
WHERE B.build_id = _build_id
AND B.status = 'accepted'
RETURNING B.commit_id INTO _commit_id;
IF NOT FOUND THEN
RAISE EXCEPTION 'NOT_FOUND' USING ERRCODE = 'no_data_found';
END IF;
SELECT
P.project_id,
P.repository_url,
B.branch,
C.commit_sha
INTO
_project_id,
repository_url,
branch,
revision
FROM postgres_ci.projects AS P
JOIN postgres_ci.branches AS B USING(project_id)
JOIN postgres_ci.commits AS C ON C.branch_id = B.branch_id
WHERE C.commit_id = _commit_id;
UPDATE postgres_ci.projects SET last_build_id = _build_id WHERE project_id = _project_id;
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/stop.sql */
create or replace function build.stop(_build_id int, _config text, _error text) returns void as $$
begin
UPDATE postgres_ci.builds
SET
config = _config,
error = _error,
status = (
CASE
WHEN _error = '' THEN 'success'
ELSE 'failed'
END
)::postgres_ci.status,
finished_at = current_timestamp
WHERE build_id = _build_id
AND status = 'running';
IF NOT FOUND THEN
RAISE EXCEPTION 'NOT_FOUND' USING ERRCODE = 'no_data_found';
END IF;
IF EXISTS(
SELECT
FROM postgres_ci.parts
WHERE build_id = _build_id
AND success IS False
LIMIT 1
) THEN
UPDATE postgres_ci.builds SET status = 'failed'::postgres_ci.status WHERE build_id = _build_id;
END IF;
end;
$$ language plpgsql security definer;
/* source file: src/functions/build/view.sql */
create or replace function build.view(
_build_id int,
out project_id int,
out project_name text,
out branch_id int,
out branch_name text,
out config text,
out error text,
out status postgres_ci.status,
out commit_sha text,
out commit_message text,
out committed_at timestamptz,
out committer_name text,
out committer_email text,
out author_name text,
out author_email text,
out created_at timestamptz,
out parts jsonb
) returns record as $$
begin
SELECT
B.project_id,
P.project_name,
B.branch_id,
BR.branch,
B.config,
B.error,
B.status,
C.commit_sha,
C.commit_message,
C.committed_at,
C.committer_name,
C.committer_email,
C.author_name,
C.author_email,
B.created_at,
(
SELECT
COALESCE(array_to_json(array_agg(P.*)), '[]')
FROM (
SELECT
part_id,
image,
version,
output,
success,
started_at,
finished_at,
(
SELECT
COALESCE(array_to_json(array_agg(T.*)), '[]')
FROM (
SELECT
function,
errors,
duration
FROM postgres_ci.tests
WHERE part_id = parts.part_id
ORDER BY
jsonb_array_length(errors) DESC,
function
) AS T
) AS tests
FROM postgres_ci.parts
WHERE build_id = B.build_id
ORDER BY part_id
) AS P
)
INTO
project_id,
project_name,
branch_id,
branch_name,
config,
error,
status,
commit_sha,
commit_message,
committed_at,
committer_name,
committer_email,
author_name,
author_email,
created_at,
parts
FROM postgres_ci.builds AS B
JOIN postgres_ci.projects AS P USING(project_id)
JOIN postgres_ci.branches AS BR ON BR.branch_id = B.branch_id
JOIN postgres_ci.commits AS C ON C.commit_id = B.commit_id
WHERE B.build_id = _build_id;
IF NOT FOUND THEN
RAISE EXCEPTION 'NOT_FOUND' USING ERRCODE = 'no_data_found';
END IF;
end;
$$ language plpgsql security definer;
/* source file: src/functions/project/add_commit.sql */
create or replace function project.add_commit(
_project_id int,
_branch text,
_commit_sha text,
_commit_message text,
_committed_at timestamptz,
_committer_name text,
_committer_email text,
_author_name text,
_author_email text,
out commit_id int
) returns int as $$
declare
_branch_id int;
begin
_branch_id = project.get_branch_id(_project_id, _branch);
BEGIN
INSERT INTO postgres_ci.commits (
project_id,
branch_id,
commit_sha,
commit_message,
committed_at,
committer_name,
committer_email,
author_name,
author_email
) VALUES (
_project_id,
_branch_id,
_commit_sha,
_commit_message,
_committed_at,
_committer_name,
_committer_email,
_author_name,
_author_email
) RETURNING commits.commit_id INTO commit_id;
PERFORM build.new(_project_id, _branch_id, commit_id);
EXCEPTION WHEN unique_violation THEN
SELECT
C.commit_id INTO commit_id
FROM postgres_ci.commits C
WHERE C.branch_id = _branch_id
AND C.commit_sha = _commit_sha;
END;
end;
$$ language plpgsql security definer;
/* source file: src/functions/project/add.sql */
create or replace function project.add(
_project_name text,
_project_owner_id int,
_repository_url text,
_github_secret text,
out project_id int
) returns int as $$
begin
INSERT INTO postgres_ci.projects (
project_name,
project_owner_id,
repository_url,
github_name,
github_secret
) VALUES (
_project_name,
_project_owner_id,
_repository_url,
project.github_name(_repository_url),
_github_secret
) RETURNING projects.project_id INTO project_id;
end;
$$ language plpgsql security definer;
/* source file: src/functions/project/delete.sql */
create or replace function project.delete(_project_id int) returns void as $$
begin
UPDATE postgres_ci.projects
SET
is_deleted = true,
updated_at = current_timestamp
WHERE project_id = _project_id;
end;
$$ language plpgsql security definer;
/* source file: src/functions/project/get_branch_id.sql */
create or replace function project.get_branch_id(_project_id int, _branch text, out branch_id int) returns int as $$
begin
SELECT
B.branch_id INTO branch_id
FROM postgres_ci.branches AS B
WHERE B.project_id = _project_id
AND B.branch = _branch;
IF NOT FOUND THEN
INSERT INTO postgres_ci.branches (
project_id,
branch
) VALUES (
_project_id,
_branch
) RETURNING branches.branch_id INTO branch_id;
END IF;
end;
$$ language plpgsql security definer;
/* source file: src/functions/project/get_github_secret.sql */
create or replace function project.get_github_secret(_github_name text) returns table(
secret text
) as $$
begin
return query
SELECT
github_secret
FROM postgres_ci.projects
WHERE github_name = _github_name
AND is_deleted = false;
end
$$ language plpgsql security definer rows 1;
/* source file: src/functions/project/get_possible_owners.sql */
create or replace function project.get_possible_owners() returns table (
user_id int,
user_name text
) as $$
begin
return query
SELECT
U.user_id,
U.user_name
FROM postgres_ci.users AS U
WHERE U.is_deleted = false
ORDER BY U.user_id;
end;
$$ language plpgsql security definer;
/* source file: src/functions/project/get.sql */
create or replace function project.get(
_project_id int,
out project_id int,
out project_name text,
out project_token text,
out repository_url text,
out project_owner_id int,
out possible_owners jsonb,
out github_secret text,
out created_at timestamptz,
out updated_at timestamptz
) returns record as $$
begin
SELECT
P.project_id,
P.project_name,
P.project_token,
P.repository_url,
P.project_owner_id,
(
SELECT
COALESCE(array_to_json(array_agg(U.*)), '[]')
FROM (
SELECT
user_id,
user_name
FROM postgres_ci.users
WHERE is_deleted = false
ORDER BY user_id
) U
),
P.github_secret,
P.created_at,
P.updated_at
INTO
project_id,
project_name,
project_token,
repository_url,
project_owner_id,
possible_owners,
github_secret,
created_at,
updated_at
FROM postgres_ci.projects AS P
WHERE P.project_id = _project_id
AND P.is_deleted = false;
IF NOT FOUND THEN
RAISE EXCEPTION 'NOT_FOUND' USING ERRCODE = 'no_data_found';
END IF;
end;
$$ language plpgsql security definer;
/* source file: src/functions/project/github_name.sql */
create or replace function project.github_name(_repository_url text) returns text as $$
begin