-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.19.0-to-2.20.0.sql
219 lines (185 loc) · 9.25 KB
/
2.19.0-to-2.20.0.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
---------- CREATED BY MIGRA ----------
drop function if exists "public"."category_path"(category_id uuid);
create table "public"."testimonial_for_project" (
"id" uuid not null default gen_random_uuid(),
"project" uuid not null,
"message" character varying(500) not null,
"source" character varying(200) not null,
"position" integer
);
CREATE UNIQUE INDEX testimonial_for_project_pkey ON public.testimonial_for_project USING btree (id);
alter table "public"."testimonial_for_project" add constraint "testimonial_for_project_pkey" PRIMARY KEY using index "testimonial_for_project_pkey";
alter table "public"."testimonial_for_project" add constraint "testimonial_for_project_project_fkey" FOREIGN KEY (project) REFERENCES project(id);
set check_function_bodies = off;
CREATE OR REPLACE FUNCTION public.delete_community(id uuid)
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
BEGIN
IF id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide the ID of the community to delete';
END IF;
IF
(SELECT rolsuper FROM pg_roles WHERE rolname = SESSION_USER) IS DISTINCT FROM TRUE
AND
(SELECT CURRENT_SETTING('request.jwt.claims', FALSE)::json->>'role') IS DISTINCT FROM 'rsd_admin'
THEN
RAISE EXCEPTION USING MESSAGE = 'You are not allowed to delete this community';
END IF;
DELETE FROM category WHERE category.community = delete_community.id;
DELETE FROM invite_maintainer_for_community WHERE invite_maintainer_for_community.community = delete_community.id;
DELETE FROM keyword_for_community WHERE keyword_for_community.community = delete_community.id;
DELETE FROM maintainer_for_community WHERE maintainer_for_community.community = delete_community.id;
DELETE FROM software_for_community WHERE software_for_community.community = delete_community.id;
DELETE FROM community WHERE community.id = delete_community.id;
END
$function$
;
CREATE OR REPLACE FUNCTION public.delete_organisation(id uuid)
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
DECLARE child_id UUID;
DECLARE child_ids UUID[];
BEGIN
IF id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide the ID of the organisation to delete';
END IF;
IF
(SELECT rolsuper FROM pg_roles WHERE rolname = SESSION_USER) IS DISTINCT FROM TRUE
AND
(SELECT CURRENT_SETTING('request.jwt.claims', FALSE)::json->>'role') IS DISTINCT FROM 'rsd_admin'
THEN
RAISE EXCEPTION USING MESSAGE = 'You are not allowed to delete this organisation';
END IF;
child_ids := ARRAY_REMOVE(ARRAY_AGG((SELECT organisation.id FROM organisation WHERE organisation.parent = delete_organisation.id)), NULL);
FOREACH child_id IN ARRAY child_ids LOOP
PERFORM delete_organisation(child_id);
END LOOP;
DELETE FROM invite_maintainer_for_organisation WHERE invite_maintainer_for_organisation.organisation = delete_organisation.id;
DELETE FROM maintainer_for_organisation WHERE maintainer_for_organisation.organisation = delete_organisation.id;
DELETE FROM project_for_organisation WHERE project_for_organisation.organisation = delete_organisation.id;
DELETE FROM software_for_organisation WHERE software_for_organisation.organisation = delete_organisation.id;
DELETE FROM organisation WHERE organisation.id = delete_organisation.id;
END
$function$
;
CREATE OR REPLACE FUNCTION public.delete_project(id uuid)
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
BEGIN
IF id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide the ID of the project to delete';
END IF;
IF
(SELECT rolsuper FROM pg_roles WHERE rolname = SESSION_USER) IS DISTINCT FROM TRUE
AND
(SELECT CURRENT_SETTING('request.jwt.claims', FALSE)::json->>'role') IS DISTINCT FROM 'rsd_admin'
THEN
RAISE EXCEPTION USING MESSAGE = 'You are not allowed to delete this project';
END IF;
DELETE FROM impact_for_project WHERE impact_for_project.project = delete_project.id;
DELETE FROM invite_maintainer_for_project WHERE invite_maintainer_for_project.project = delete_project.id;
DELETE FROM keyword_for_project WHERE keyword_for_project.project = delete_project.id;
DELETE FROM maintainer_for_project WHERE maintainer_for_project.project = delete_project.id;
DELETE FROM output_for_project WHERE output_for_project.project = delete_project.id;
DELETE FROM project_for_organisation WHERE project_for_organisation.project = delete_project.id;
DELETE FROM project_for_project WHERE project_for_project.origin = delete_project.id OR project_for_project.relation = delete_project.id;
DELETE FROM research_domain_for_project WHERE research_domain_for_project.project = delete_project.id;
DELETE FROM software_for_project WHERE software_for_project.project = delete_project.id;
DELETE FROM team_member WHERE team_member.project = delete_project.id;
DELETE FROM url_for_project WHERE url_for_project.project = delete_project.id;
DELETE FROM project WHERE project.id = delete_project.id;
END
$function$
;
CREATE OR REPLACE FUNCTION public.delete_software(id uuid)
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
BEGIN
IF id IS NULL THEN
RAISE EXCEPTION USING MESSAGE = 'Please provide the ID of the software to delete';
END IF;
IF
(SELECT rolsuper FROM pg_roles WHERE rolname = SESSION_USER) IS DISTINCT FROM TRUE
AND
(SELECT CURRENT_SETTING('request.jwt.claims', FALSE)::json->>'role') IS DISTINCT FROM 'rsd_admin'
THEN
RAISE EXCEPTION USING MESSAGE = 'You are not allowed to delete this software';
END IF;
DELETE FROM category_for_software WHERE category_for_software.software_id = delete_software.id;
DELETE FROM contributor WHERE contributor.software = delete_software.id;
DELETE FROM invite_maintainer_for_software WHERE invite_maintainer_for_software.software = delete_software.id;
DELETE FROM keyword_for_software WHERE keyword_for_software.software = delete_software.id;
DELETE FROM license_for_software WHERE license_for_software.software = delete_software.id;
DELETE FROM maintainer_for_software WHERE maintainer_for_software.software = delete_software.id;
DELETE FROM mention_for_software WHERE mention_for_software.software = delete_software.id;
DELETE FROM package_manager WHERE package_manager.software = delete_software.id;
DELETE FROM reference_paper_for_software WHERE reference_paper_for_software.software = delete_software.id;
DELETE FROM release_version WHERE release_version.release_id = delete_software.id;
DELETE FROM release WHERE release.software = delete_software.id;
DELETE FROM repository_url WHERE repository_url.software = delete_software.id;
DELETE FROM software_for_community WHERE software_for_community.software = delete_software.id;
DELETE FROM software_for_organisation WHERE software_for_organisation.software = delete_software.id;
DELETE FROM software_for_project WHERE software_for_project.software = delete_software.id;
DELETE FROM software_for_software WHERE software_for_software.origin = delete_software.id OR software_for_software.relation = delete_software.id;
DELETE FROM software_highlight WHERE software_highlight.software = delete_software.id;
DELETE FROM testimonial WHERE testimonial.software = delete_software.id;
DELETE FROM software WHERE software.id = delete_software.id;
END
$function$
;
CREATE OR REPLACE FUNCTION public.sanitise_insert_testimonial_for_project()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.id = gen_random_uuid();
return NEW;
END
$function$
;
CREATE OR REPLACE FUNCTION public.sanitise_update_testimonial_for_project()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.id = OLD.id;
return NEW;
END
$function$
;
CREATE OR REPLACE FUNCTION public.category_path(category_id uuid)
RETURNS TABLE(id uuid, parent uuid, community uuid, short_name character varying, name character varying, properties jsonb, provenance_iri character varying)
LANGUAGE sql
STABLE
AS $function$
WITH RECURSIVE cat_path AS (
SELECT *, 1 AS r_index
FROM category WHERE id = category_id
UNION ALL
SELECT category.*, cat_path.r_index+1
FROM category
JOIN cat_path
ON category.id = cat_path.parent
)
-- 1. How can we reverse the output rows without injecting a new column (r_index)?
-- 2. How a table row "type" could be used here Now we have to list all columns of `category` explicitly
-- I want to have something like `* without 'r_index'` to be independent from modifications of `category`
-- 3. Maybe this could be improved by using SEARCH keyword.
SELECT id, parent, community, short_name, name, properties, provenance_iri
FROM cat_path
ORDER BY r_index DESC;
$function$
;
CREATE TRIGGER check_testimonial_for_project_before_delete BEFORE DELETE ON public.testimonial_for_project FOR EACH STATEMENT EXECUTE FUNCTION check_user_agreement_on_delete_action();
CREATE TRIGGER check_testimonial_for_project_before_insert BEFORE INSERT ON public.testimonial_for_project FOR EACH STATEMENT EXECUTE FUNCTION check_user_agreement_on_action();
CREATE TRIGGER check_testimonial_for_project_before_update BEFORE UPDATE ON public.testimonial_for_project FOR EACH STATEMENT EXECUTE FUNCTION check_user_agreement_on_action();
CREATE TRIGGER sanitise_insert_testimonial_for_project BEFORE INSERT ON public.testimonial_for_project FOR EACH ROW EXECUTE FUNCTION sanitise_insert_testimonial_for_project();
CREATE TRIGGER sanitise_update_testimonial_for_project BEFORE UPDATE ON public.testimonial_for_project FOR EACH ROW EXECUTE FUNCTION sanitise_update_testimonial_for_project();