1 | --- Trigger before update/insert za check na iminjata topic/discussion -> OK
|
---|
2 | --- Trigger za ko ke adnit dete na topic thread sho e vo proekt, da go dodajt kako belongs_to vo proektot
|
---|
3 | --- Trigger za check dali reply na discussion thread pripagjat na ist topic thread kako na toj so mu pret reply
|
---|
4 | --- IMENUVANJE: triggeri so provervat nesto prefix = check, funkcii za istite prefix = validate
|
---|
5 | --- Nemame contraint sho velit deka sekoj topic thread trebat da e moderiran
|
---|
6 | DROP TABLE IF EXISTS users CASCADE;
|
---|
7 | DROP TABLE IF EXISTS moderator CASCADE;
|
---|
8 | DROP TABLE IF EXISTS developer CASCADE;
|
---|
9 | DROP TABLE IF EXISTS project_manager CASCADE;
|
---|
10 | DROP TABLE IF EXISTS thread CASCADE;
|
---|
11 | DROP TABLE IF EXISTS likes CASCADE;
|
---|
12 | DROP TABLE IF EXISTS topic_threads_moderators CASCADE;
|
---|
13 | DROP TABLE IF EXISTS tag CASCADE;
|
---|
14 | DROP TABLE IF EXISTS tag_threads CASCADE;
|
---|
15 | DROP TABLE IF EXISTS topic_thread CASCADE;
|
---|
16 | DROP TABLE IF EXISTS topic_belongs_to_project CASCADE;
|
---|
17 | DROP TABLE IF EXISTS blacklisted_user CASCADE;
|
---|
18 | DROP TABLE IF EXISTS project_thread CASCADE;
|
---|
19 | DROP TABLE IF EXISTS discussion_thread CASCADE;
|
---|
20 | DROP TABLE IF EXISTS developer_associated_with_project CASCADE;
|
---|
21 | DROP TABLE IF EXISTS permissions CASCADE;
|
---|
22 | DROP TABLE IF EXISTS project_roles CASCADE;
|
---|
23 | DROP TABLE IF EXISTS users_project_roles CASCADE;
|
---|
24 | DROP TABLE IF EXISTS project_roles_permissions CASCADE;
|
---|
25 | DROP TABLE IF EXISTS project_request CASCADE;
|
---|
26 | DROP TABLE IF EXISTS report CASCADE;
|
---|
27 | DROP TABLE IF EXISTS channel CASCADE;
|
---|
28 | DROP TABLE IF EXISTS messages CASCADE;
|
---|
29 | DROP TABLE IF EXISTS threads_moderators CASCADE;
|
---|
30 | DROP TYPE IF EXISTS status;
|
---|
31 | DROP VIEW IF EXISTS v_topic_thread CASCADE;
|
---|
32 | DROP VIEW IF EXISTS v_project_thread CASCADE;
|
---|
33 | DROP VIEW IF EXISTS v_discussion_thread CASCADE;
|
---|
34 | DROP VIEW IF EXISTS v_developer CASCADE;
|
---|
35 | DROP VIEW IF EXISTS v_project_owner CASCADE;
|
---|
36 | DROP VIEW IF EXISTS v_moderator CASCADE;
|
---|
37 | DROP FUNCTION IF EXISTS fn_project_insert_child_topic CASCADE;
|
---|
38 | DROP FUNCTION IF EXISTS fn_validate_same_parent CASCADE;
|
---|
39 | DROP TRIGGER IF EXISTS validate_same_parent ON discussion_thread CASCADE;
|
---|
40 | ---- DDL
|
---|
41 | CREATE TABLE users
|
---|
42 | (
|
---|
43 | id SERIAL PRIMARY KEY,
|
---|
44 | username VARCHAR(32) UNIQUE NOT NULL,
|
---|
45 | is_activate bool,
|
---|
46 | password VARCHAR(72),
|
---|
47 | description VARCHAR(200),
|
---|
48 | registered_at TIMESTAMP,
|
---|
49 | sex VARCHAR(1)
|
---|
50 | );
|
---|
51 | CREATE TABLE moderator
|
---|
52 | (
|
---|
53 | id INT PRIMARY KEY REFERENCES users (id) on delete cascade
|
---|
54 | );
|
---|
55 | CREATE TABLE developer
|
---|
56 | (
|
---|
57 | id INT PRIMARY KEY REFERENCES users (id) on delete cascade
|
---|
58 | );
|
---|
59 | CREATE TABLE project_manager
|
---|
60 | (
|
---|
61 | id INT PRIMARY KEY REFERENCES users (id) on delete cascade
|
---|
62 | );
|
---|
63 | CREATE TABLE thread
|
---|
64 | (
|
---|
65 | id SERIAL PRIMARY KEY,
|
---|
66 | content TEXT,
|
---|
67 | user_id INT REFERENCES users (id) NOT NULL
|
---|
68 | );
|
---|
69 | CREATE TABLE topic_thread
|
---|
70 | (
|
---|
71 | title VARCHAR(32) NOT NULL,
|
---|
72 | guidelines jsonb,
|
---|
73 | parent_id INT REFERENCES thread (id),
|
---|
74 | id INT PRIMARY KEY REFERENCES thread (id) on delete cascade
|
---|
75 | );
|
---|
76 | CREATE TABLE discussion_thread
|
---|
77 | (
|
---|
78 | parent_id INT REFERENCES thread (id) NOT NULL,
|
---|
79 | id INT PRIMARY KEY REFERENCES thread (id) on delete cascade
|
---|
80 | );
|
---|
81 | CREATE TABLE project_thread
|
---|
82 | (
|
---|
83 | title VARCHAR(32) NOT NULL,
|
---|
84 | repo_url TEXT,
|
---|
85 | id INT PRIMARY KEY REFERENCES thread (id) on delete cascade
|
---|
86 | );
|
---|
87 | CREATE TABLE likes
|
---|
88 | (
|
---|
89 | user_id INT REFERENCES users (id),
|
---|
90 | thread_id INT REFERENCES thread (id),
|
---|
91 | PRIMARY KEY (user_id, thread_id)
|
---|
92 | );
|
---|
93 | CREATE TABLE topic_threads_moderators
|
---|
94 | (
|
---|
95 | thread_id INT REFERENCES thread (id) ON DELETE CASCADE,
|
---|
96 | user_id INT REFERENCES users (id) ON DELETE CASCADE,
|
---|
97 | PRIMARY KEY (thread_id, user_id)
|
---|
98 | );
|
---|
99 | CREATE TABLE tag
|
---|
100 | (
|
---|
101 | name VARCHAR(64) PRIMARY KEY
|
---|
102 | );
|
---|
103 | CREATE TABLE tag_threads
|
---|
104 | (
|
---|
105 | thread_id INT REFERENCES thread (id),
|
---|
106 | tag_name VARCHAR(64) REFERENCES tag (name),
|
---|
107 | PRIMARY KEY (thread_id, tag_name)
|
---|
108 | );
|
---|
109 |
|
---|
110 | CREATE TABLE topic_belongs_to_project
|
---|
111 | (
|
---|
112 | topic_id INT REFERENCES thread (id) ON DELETE CASCADE,
|
---|
113 | project_id INT REFERENCES thread (id) ON DELETE CASCADE,
|
---|
114 | PRIMARY KEY (topic_id, project_id)
|
---|
115 | );
|
---|
116 | CREATE TABLE blacklisted_user
|
---|
117 | (
|
---|
118 | topic_id INT REFERENCES thread (id) ON DELETE CASCADE,
|
---|
119 | user_id INT REFERENCES users (id) ON DELETE CASCADE,
|
---|
120 | moderator_id INT REFERENCES users (id) ON DELETE CASCADE,
|
---|
121 | start_date TIMESTAMP,
|
---|
122 | end_date TIMESTAMP,
|
---|
123 | reason TEXT,
|
---|
124 | PRIMARY KEY (user_id, moderator_id, topic_id, start_date)
|
---|
125 | );
|
---|
126 |
|
---|
127 | CREATE TABLE developer_associated_with_project
|
---|
128 | (
|
---|
129 | project_id INT REFERENCES thread (id),
|
---|
130 | developer_id INT REFERENCES users (id),
|
---|
131 | started_at TIMESTAMP,
|
---|
132 | ended_at TIMESTAMP,
|
---|
133 | PRIMARY KEY (project_id, developer_id, started_at)
|
---|
134 | );
|
---|
135 | CREATE TABLE permissions
|
---|
136 | (
|
---|
137 | name VARCHAR(32) PRIMARY KEY
|
---|
138 | );
|
---|
139 | CREATE TABLE project_roles
|
---|
140 | (
|
---|
141 | name VARCHAR(32),
|
---|
142 | project_id INT REFERENCES thread (id) ON DELETE CASCADE,
|
---|
143 | description TEXT,
|
---|
144 | PRIMARY KEY (name, project_id)
|
---|
145 | );
|
---|
146 | CREATE TABLE users_project_roles
|
---|
147 | (
|
---|
148 | user_id INT REFERENCES users (id),
|
---|
149 | project_id INT,
|
---|
150 | role_name VARCHAR(32),
|
---|
151 | FOREIGN KEY (role_name, project_id)
|
---|
152 | REFERENCES project_roles (name, project_id),
|
---|
153 | PRIMARY KEY (user_id, project_id, role_name)
|
---|
154 | );
|
---|
155 | CREATE TABLE project_roles_permissions
|
---|
156 | (
|
---|
157 | permission_name VARCHAR(32) REFERENCES permissions (name),
|
---|
158 | role_name VARCHAR(32),
|
---|
159 | project_id INT,
|
---|
160 | PRIMARY KEY (permission_name, role_name, project_id),
|
---|
161 | FOREIGN KEY (role_name, project_id)
|
---|
162 | REFERENCES project_roles (name, project_id)
|
---|
163 | );
|
---|
164 | CREATE TYPE status AS ENUM ('ACCEPTED', 'DENIED', 'PENDING');
|
---|
165 | CREATE TABLE project_request
|
---|
166 | (
|
---|
167 | id SERIAL PRIMARY KEY,
|
---|
168 | description VARCHAR(200),
|
---|
169 | status status NOT NULL,
|
---|
170 | user_id INT REFERENCES users (id) NOT NULL,
|
---|
171 | project_id INT REFERENCES thread (id) NOT NULL
|
---|
172 | );
|
---|
173 | CREATE TABLE report
|
---|
174 | (
|
---|
175 | id SERIAL,
|
---|
176 | created_at TIMESTAMP,
|
---|
177 | description VARCHAR(200) NOT NULL,
|
---|
178 | status status,
|
---|
179 | thread_id INT REFERENCES thread (id),
|
---|
180 | for_user_id INT REFERENCES users (id),
|
---|
181 | by_user_id INT REFERENCES users (id),
|
---|
182 | PRIMARY KEY (id, thread_id, for_user_id, by_user_id)
|
---|
183 | );
|
---|
184 | CREATE TABLE channel
|
---|
185 | (
|
---|
186 | name VARCHAR(64),
|
---|
187 | description VARCHAR(200),
|
---|
188 | project_id INT REFERENCES thread (id) ON DELETE CASCADE,
|
---|
189 | developer_id INT REFERENCES users (id),
|
---|
190 | PRIMARY KEY (name, project_id)
|
---|
191 | );
|
---|
192 | CREATE TABLE messages
|
---|
193 | (
|
---|
194 | sent_at TIMESTAMP,
|
---|
195 | content VARCHAR(200) NOT NULL,
|
---|
196 | sent_by INT REFERENCES users (id),
|
---|
197 | project_id INT,
|
---|
198 | channel_name VARCHAR(64),
|
---|
199 | FOREIGN KEY (channel_name, project_id)
|
---|
200 | REFERENCES channel (name, project_id) ON DELETE CASCADE,
|
---|
201 | PRIMARY KEY (channel_name, project_id, sent_at, sent_by)
|
---|
202 | );
|
---|
203 | ------------------------VIEWS-----------------------------
|
---|
204 | CREATE OR REPLACE VIEW v_project_thread
|
---|
205 | AS
|
---|
206 | SELECT thread.id, content, user_id, title, repo_url
|
---|
207 | FROM project_thread project
|
---|
208 | JOIN thread
|
---|
209 | ON project.id = thread.id;
|
---|
210 | CREATE OR REPLACE VIEW v_discussion_thread
|
---|
211 | AS
|
---|
212 | SELECT thread.id, content, user_id,parent_id
|
---|
213 | FROM discussion_thread discussion
|
---|
214 | JOIN thread
|
---|
215 | ON discussion.id = thread.id;
|
---|
216 | CREATE OR REPLACE VIEW v_topic_thread
|
---|
217 | AS
|
---|
218 | SELECT thread.id, content, user_id, title, guidelines, parent_id
|
---|
219 | FROM topic_thread topic
|
---|
220 | JOIN thread
|
---|
221 | ON topic.id = thread.id;
|
---|
222 | CREATE OR REPLACE VIEW v_moderator
|
---|
223 | AS
|
---|
224 | SELECT users.id, username, is_activate, password, description, registered_at, sex
|
---|
225 | FROM moderator
|
---|
226 | JOIN users ON moderator.id = users.id;
|
---|
227 |
|
---|
228 | CREATE OR REPLACE VIEW v_developer
|
---|
229 | AS
|
---|
230 | SELECT users.id, username, is_activate, password, description, registered_at, sex
|
---|
231 | FROM developer
|
---|
232 | JOIN users ON developer.id = users.id;
|
---|
233 | CREATE OR REPLACE VIEW v_project_owner
|
---|
234 | AS
|
---|
235 | SELECT users.id, username, is_activate, password, description, registered_at, sex
|
---|
236 | FROM project_manager
|
---|
237 | JOIN users ON project_manager.id = users.id;
|
---|
238 | CREATE OR REPLACE VIEW v_moderator
|
---|
239 | AS
|
---|
240 | SELECT users.id, username, is_activate, password, description, registered_at, sex
|
---|
241 | FROM moderator
|
---|
242 | JOIN users ON moderator.id = users.id;
|
---|
243 |
|
---|
244 | CREATE OR REPLACE VIEW v_developer
|
---|
245 | AS
|
---|
246 | SELECT users.id, username, is_activate, password, description, registered_at, sex
|
---|
247 | FROM developer
|
---|
248 | JOIN users ON developer.id = users.id;
|
---|
249 | CREATE OR REPLACE VIEW v_project_owner
|
---|
250 | AS
|
---|
251 | SELECT users.id, username, is_activate, password, description, registered_at, sex
|
---|
252 | FROM project_manager
|
---|
253 | JOIN users ON project_manager.id = users.id;
|
---|
254 | -------------------------- FUNCTIONS ----------------------
|
---|
255 | CREATE OR REPLACE FUNCTION fn_validate_topic_title()
|
---|
256 | RETURNS TRIGGER
|
---|
257 | LANGUAGE plpgsql
|
---|
258 | AS
|
---|
259 | $$
|
---|
260 | BEGIN
|
---|
261 | IF new.title IN
|
---|
262 | (SELECT title
|
---|
263 | FROM topic_thread
|
---|
264 | AS t
|
---|
265 | WHERE t.parent_id = new.parent_id OR (t.parent_id IS NULL AND new.parent_id IS NULL))
|
---|
266 | THEN
|
---|
267 | RAISE EXCEPTION 'There already exists a topic with title % in parent topic with id %',new.title,new.parent_id;
|
---|
268 | END IF;
|
---|
269 | RETURN new;
|
---|
270 | END;
|
---|
271 | $$;
|
---|
272 | CREATE OR REPLACE FUNCTION fn_insert_topics_creator_as_moderator()
|
---|
273 | RETURNS TRIGGER
|
---|
274 | LANGUAGE plpgsql
|
---|
275 | AS
|
---|
276 | $$
|
---|
277 | DECLARE
|
---|
278 | v_user_id INT;
|
---|
279 | BEGIN
|
---|
280 | SELECT v_topic_thread.user_id
|
---|
281 | INTO v_user_id
|
---|
282 | FROM v_topic_thread
|
---|
283 | WHERE v_topic_thread.id = new.id;
|
---|
284 | INSERT INTO topic_threads_moderators(thread_id, user_id) VALUES (new.id, v_user_id);
|
---|
285 | RETURN NEW;
|
---|
286 | END;
|
---|
287 | $$;
|
---|
288 |
|
---|
289 | CREATE OR REPLACE FUNCTION fn_insert_project_manager()
|
---|
290 | RETURNS TRIGGER
|
---|
291 | LANGUAGE plpgsql
|
---|
292 | AS
|
---|
293 | $$
|
---|
294 | DECLARE usrId INT;
|
---|
295 | BEGIN
|
---|
296 | SELECT user_id INTO usrId FROM v_project_thread p WHERE NEW.id = p.id;
|
---|
297 | INSERT INTO developer VALUES (usrId);
|
---|
298 | INSERT INTO project_manager VALUES (usrId);
|
---|
299 | RETURN NEW;
|
---|
300 | END;
|
---|
301 | $$;
|
---|
302 | -------------------------- TRIGGERS ----------------------
|
---|
303 | CREATE OR REPLACE TRIGGER tr_check_topic_name
|
---|
304 | BEFORE INSERT OR UPDATE
|
---|
305 | ON topic_thread
|
---|
306 | FOR EACH ROW
|
---|
307 | EXECUTE FUNCTION fn_validate_topic_title();
|
---|
308 | CREATE OR REPLACE TRIGGER tr_insert_topics_creator_as_moderator
|
---|
309 | AFTER INSERT
|
---|
310 | ON topic_thread
|
---|
311 | FOR EACH ROW
|
---|
312 | EXECUTE FUNCTION fn_insert_topics_creator_as_moderator();
|
---|
313 | CREATE OR REPLACE TRIGGER tr_insert_project_manager
|
---|
314 | AFTER INSERT
|
---|
315 | ON project_thread
|
---|
316 | FOR EACH ROW
|
---|
317 | EXECUTE FUNCTION fn_insert_project_manager();
|
---|
318 |
|
---|