Skip to content

Commit 81b6ef6

Browse files
committed
PG: Add RETURNING to view rules
And add a trigger for team inserts/updates/deletes. Maybe that should also be a rule.
1 parent fc92e07 commit 81b6ef6

File tree

2 files changed

+181
-47
lines changed

2 files changed

+181
-47
lines changed

Database/PostgreSQL/pg-build-schema.psql

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,12 @@ CREATE RULE employment_add AS ON INSERT TO employment DO INSTEAD
785785
new.company_assignment_id, new.enterprise_id, new.person_id,
786786
new.is_headquarter, new.is_chief, new."function", new.db_status,
787787
new.start_date, new.end_date
788-
);
789-
788+
)
789+
RETURNING
790+
company_assignment_id, company_id, sub_company_id,
791+
is_headquarter, is_chief, "function", db_status,
792+
start_date, end_date
793+
;
790794
CREATE RULE employment_set AS ON UPDATE TO employment DO INSTEAD
791795
UPDATE company_assignment
792796
SET company_id = new.enterprise_id,
@@ -827,7 +831,12 @@ CREATE RULE company_hierarchy_add AS ON INSERT TO company_hierarchy DO INSTEAD
827831
new.company_assignment_id, new.parent_id, new.company_id,
828832
new.is_headquarter, new.is_chief, new."function", new.db_status,
829833
new.start_date, new.end_date
830-
);
834+
)
835+
RETURNING
836+
company_assignment_id, company_id, sub_company_id,
837+
is_headquarter, is_chief, "function", db_status,
838+
start_date, end_date
839+
;
831840

832841
CREATE RULE company_hierarchy_set AS ON UPDATE TO company_hierarchy DO INSTEAD
833842
UPDATE company_assignment
@@ -870,7 +879,12 @@ CREATE RULE person_relationship_add AS
870879
new.company_assignment_id, new.person_id, new.related_person_id,
871880
new.relationship, new.db_status,
872881
new.start_date, new.end_date
873-
);
882+
)
883+
RETURNING
884+
company_assignment_id, company_id, sub_company_id,
885+
"function", db_status,
886+
start_date, end_date
887+
;
874888

875889
CREATE RULE person_relationship_set AS
876890
ON UPDATE TO person_relationship DO INSTEAD
@@ -901,6 +915,46 @@ CREATE VIEW team_membership AS
901915
FROM company_assignment ca
902916
INNER JOIN team t USING (company_id)
903917
INNER JOIN person p ON (p.company_id = ca.sub_company_id);
918+
CREATE OR REPLACE FUNCTION insert_team_membership() RETURNS trigger AS $$
919+
DECLARE
920+
_team_exists BOOLEAN;
921+
_person_exists BOOLEAN;
922+
BEGIN
923+
SELECT EXISTS ( SELECT 1 FROM team WHERE company_id = NEW.team_id )
924+
INTO _team_exists;
925+
IF NOT _team_exists THEN
926+
RAISE EXCEPTION 'Team with id % does not exist', NEW.team_id;
927+
END IF;
928+
SELECT EXISTS ( SELECT 1 FROM person WHERE company_id = NEW.person_id )
929+
INTO _person_exists;
930+
IF NOT _person_exists THEN
931+
RAISE EXCEPTION 'Person with id % does not exist', NEW.person_id;
932+
END IF;
933+
INSERT INTO company_assignment (
934+
company_assignment_id, company_id, sub_company_id, is_headquarter,
935+
is_chief, "function", db_status, start_date, end_date
936+
)
937+
VALUES (
938+
NEW.company_assignment_id, NEW.team_id, NEW.person_id, NEW.is_headquarter,
939+
NEW.is_chief, NEW."function", NEW.db_status, NEW.start_date, NEW.end_date
940+
);
941+
RETURN NEW;
942+
END;
943+
$$ LANGUAGE plpgsql;
944+
CREATE OR REPLACE FUNCTION delete_team_membership() RETURNS trigger AS $$
945+
BEGIN
946+
DELETE FROM company_assignment
947+
WHERE company_assignment_id = OLD.company_assignment_id;
948+
RETURN OLD;
949+
END;
950+
$$ LANGUAGE plpgsql;
951+
952+
CREATE TRIGGER trigger_insert_team_membership
953+
INSTEAD OF INSERT ON team_membership
954+
FOR EACH ROW EXECUTE FUNCTION insert_team_membership();
955+
CREATE TRIGGER trigger_delete_team_membership
956+
INSTEAD OF DELETE ON team_membership
957+
FOR EACH ROW EXECUTE FUNCTION delete_team_membership();
904958

905959
CREATE VIEW team_hierarchy AS
906960
SELECT
@@ -923,21 +977,24 @@ CREATE VIEW project_teams AS
923977
CREATE RULE project_teams_add AS ON INSERT TO project_teams DO INSTEAD
924978
INSERT INTO project_company_assignment (
925979
project_company_assignment_id, company_id, project_id, info,
926-
has_access, access_right, db_status,
927-
start_date, end_date
980+
has_access, access_right, db_status, start_date, end_date
928981
)
929982
VALUES (
930-
new.project_company_assignment_id, new.company_id, new.project_id, new.info,
931-
new.has_access, new.access_right, new.db_status, new.start_date, new.end_date
932-
);
983+
NEW.project_company_assignment_id, NEW.company_id, NEW.project_id, NEW.info,
984+
NEW.has_access, NEW.access_right, NEW.db_status,
985+
NEW.start_date, NEW.end_date
986+
)
987+
RETURNING
988+
project_company_assignment_id, company_id, project_id, info,
989+
has_access, access_right, db_status, start_date, end_date
990+
;
933991
CREATE RULE project_teams_set AS ON UPDATE TO project_teams DO INSTEAD
934992
UPDATE project_company_assignment
935-
SET company_id = new.company_id, project_id = new.project_id, info = new.info,
936-
has_access = new.has_access, access_right = new.access_right,
937-
db_status = new.db_status,
938-
start_date = new.start_date, end_date = new.end_date
993+
SET company_id = NEW.company_id, project_id = NEW.project_id, info = NEW.info,
994+
has_access = NEW.has_access, access_right = NEW.access_right,
995+
db_status = NEW.db_status,
996+
start_date = NEW.start_date, end_date = NEW.end_date
939997
WHERE project_company_assignment_id = old.project_company_assignment_id;
940-
941998
CREATE RULE project_teams_del AS ON DELETE TO project_teams DO INSTEAD
942999
DELETE FROM project_company_assignment
9431000
WHERE project_company_assignment_id = old.project_company_assignment_id;
@@ -957,7 +1014,11 @@ CREATE RULE project_persons_add AS ON INSERT TO project_persons DO INSTEAD
9571014
new.project_company_assignment_id, new.company_id, new.project_id, new.info,
9581015
new.has_access, new.access_right, new.db_status,
9591016
new.start_date, new.end_date
960-
);
1017+
)
1018+
RETURNING
1019+
project_company_assignment_id, company_id, project_id, info, has_access,
1020+
access_right, db_status, start_date, end_date
1021+
;
9611022
CREATE RULE project_persons_set AS ON UPDATE TO project_persons DO INSTEAD
9621023
UPDATE project_company_assignment
9631024
SET company_id = new.company_id, project_id = new.project_id, info = new.info,
@@ -985,7 +1046,11 @@ CREATE RULE project_companies_add AS ON INSERT TO project_companies DO INSTEAD
9851046
new.project_company_assignment_id, new.company_id, new.project_id, new.info,
9861047
new.has_access, new.access_right, new.db_status,
9871048
new.start_date, new.end_date
988-
);
1049+
)
1050+
RETURNING
1051+
project_company_assignment_id, company_id, project_id, info, has_access,
1052+
access_right, db_status, start_date, end_date
1053+
;
9891054
CREATE RULE project_companies_set AS ON UPDATE TO project_companies DO INSTEAD
9901055
UPDATE project_company_assignment
9911056
SET company_id = new.company_id, project_id = new.project_id, info = new.info,

Database/PostgreSQL/pg-update-1.0to5.4.psql

Lines changed: 100 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ CREATE VIEW employment AS
133133
INNER JOIN enterprise e USING (company_id)
134134
INNER JOIN person p ON (p.company_id = ca.sub_company_id);
135135

136-
CREATE RULE employment_add AS ON INSERT TO employment DO INSTEAD
136+
CREATE OR REPLACE RULE employment_add AS ON INSERT TO employment DO INSTEAD
137137
INSERT INTO company_assignment (
138138
company_assignment_id, company_id, sub_company_id,
139139
is_headquarter, is_chief, "function", db_status,
@@ -143,7 +143,12 @@ CREATE RULE employment_add AS ON INSERT TO employment DO INSTEAD
143143
new.company_assignment_id, new.enterprise_id, new.person_id,
144144
new.is_headquarter, new.is_chief, new."function", new.db_status,
145145
new.start_date, new.end_date
146-
);
146+
)
147+
RETURNING
148+
company_assignment_id, company_id, sub_company_id,
149+
is_headquarter, is_chief, "function", db_status,
150+
start_date, end_date
151+
;
147152

148153
CREATE RULE employment_set AS ON UPDATE TO employment DO INSTEAD
149154
UPDATE company_assignment
@@ -175,7 +180,8 @@ CREATE VIEW company_hierarchy AS
175180
INNER JOIN enterprise e1 USING (company_id)
176181
INNER JOIN enterprise e2 ON (e2.company_id = ca.sub_company_id);
177182

178-
CREATE RULE company_hierarchy_add AS ON INSERT TO company_hierarchy DO INSTEAD
183+
CREATE OR REPLACE RULE company_hierarchy_add AS
184+
ON INSERT TO company_hierarchy DO INSTEAD
179185
INSERT INTO company_assignment (
180186
company_assignment_id, company_id, sub_company_id,
181187
is_headquarter, is_chief, "function", db_status,
@@ -185,8 +191,12 @@ CREATE RULE company_hierarchy_add AS ON INSERT TO company_hierarchy DO INSTEAD
185191
new.company_assignment_id, new.parent_id, new.company_id,
186192
new.is_headquarter, new.is_chief, new."function", new.db_status,
187193
new.start_date, new.end_date
188-
);
189-
194+
)
195+
RETURNING
196+
company_assignment_id, company_id, sub_company_id,
197+
is_headquarter, is_chief, "function", db_status,
198+
start_date, end_date
199+
;
190200
CREATE RULE company_hierarchy_set AS ON UPDATE TO company_hierarchy DO INSTEAD
191201
UPDATE company_assignment
192202
SET company_id = new.parent_id,
@@ -217,7 +227,7 @@ CREATE VIEW person_relationship AS
217227
INNER JOIN person e USING (company_id)
218228
INNER JOIN person p ON (p.company_id = ca.sub_company_id);
219229

220-
CREATE RULE person_relationship_add AS
230+
CREATE OR REPLACE RULE person_relationship_add AS
221231
ON INSERT TO person_relationship DO INSTEAD
222232
INSERT INTO company_assignment (
223233
company_assignment_id, company_id, sub_company_id,
@@ -228,7 +238,12 @@ CREATE RULE person_relationship_add AS
228238
new.company_assignment_id, new.person_id, new.related_person_id,
229239
new.relationship, new.db_status,
230240
new.start_date, new.end_date
231-
);
241+
)
242+
RETURNING
243+
company_assignment_id, company_id, sub_company_id,
244+
"function", db_status,
245+
start_date, end_date
246+
;
232247

233248
CREATE RULE person_relationship_set AS
234249
ON UPDATE TO person_relationship DO INSTEAD
@@ -259,6 +274,46 @@ CREATE VIEW team_membership AS
259274
FROM company_assignment ca
260275
INNER JOIN team t USING (company_id)
261276
INNER JOIN person p ON (p.company_id = ca.sub_company_id);
277+
CREATE OR REPLACE FUNCTION insert_team_membership() RETURNS trigger AS $$
278+
DECLARE
279+
_team_exists BOOLEAN;
280+
_person_exists BOOLEAN;
281+
BEGIN
282+
SELECT EXISTS ( SELECT 1 FROM team WHERE company_id = NEW.team_id )
283+
INTO _team_exists;
284+
IF NOT _team_exists THEN
285+
RAISE EXCEPTION 'Team with id % does not exist', NEW.team_id;
286+
END IF;
287+
SELECT EXISTS ( SELECT 1 FROM person WHERE company_id = NEW.person_id )
288+
INTO _person_exists;
289+
IF NOT _person_exists THEN
290+
RAISE EXCEPTION 'Person with id % does not exist', NEW.person_id;
291+
END IF;
292+
INSERT INTO company_assignment (
293+
company_assignment_id, company_id, sub_company_id, is_headquarter,
294+
is_chief, "function", db_status, start_date, end_date
295+
)
296+
VALUES (
297+
NEW.company_assignment_id, NEW.team_id, NEW.person_id, NEW.is_headquarter,
298+
NEW.is_chief, NEW."function", NEW.db_status, NEW.start_date, NEW.end_date
299+
);
300+
RETURN NEW;
301+
END;
302+
$$ LANGUAGE plpgsql;
303+
CREATE OR REPLACE FUNCTION delete_team_membership() RETURNS trigger AS $$
304+
BEGIN
305+
DELETE FROM company_assignment
306+
WHERE company_assignment_id = OLD.company_assignment_id;
307+
RETURN OLD;
308+
END;
309+
$$ LANGUAGE plpgsql;
310+
311+
CREATE TRIGGER trigger_insert_team_membership
312+
INSTEAD OF INSERT ON team_membership
313+
FOR EACH ROW EXECUTE FUNCTION insert_team_membership();
314+
CREATE TRIGGER trigger_delete_team_membership
315+
INSTEAD OF DELETE ON team_membership
316+
FOR EACH ROW EXECUTE FUNCTION delete_team_membership();
262317

263318
CREATE VIEW team_hierarchy AS
264319
SELECT
@@ -278,22 +333,27 @@ CREATE VIEW project_teams AS
278333
FROM project_company_assignment pca
279334
JOIN team e USING (company_id);
280335

281-
CREATE RULE project_teams_add AS ON INSERT TO project_teams DO INSTEAD
336+
CREATE OR REPLACE RULE project_teams_add AS ON INSERT TO project_teams DO INSTEAD
282337
INSERT INTO project_company_assignment (
283338
project_company_assignment_id, company_id, project_id, info,
284-
has_access, access_right, db_status
339+
has_access, access_right, db_status, start_date, end_date
285340
)
286341
VALUES (
287-
new.project_company_assignment_id, new.company_id, new.project_id, new.info,
288-
new.has_access, new.access_right, new.db_status
289-
);
290-
CREATE RULE project_teams_set AS ON UPDATE TO project_teams DO INSTEAD
342+
NEW.project_company_assignment_id, NEW.company_id, NEW.project_id, NEW.info,
343+
NEW.has_access, NEW.access_right, NEW.db_status,
344+
NEW.start_date, NEW.end_date
345+
)
346+
RETURNING
347+
project_company_assignment_id, company_id, project_id, info,
348+
has_access, access_right, db_status, start_date, end_date
349+
;
350+
CREATE OR REPLACE RULE project_teams_set AS ON UPDATE TO project_teams DO INSTEAD
291351
UPDATE project_company_assignment
292-
SET company_id = new.company_id, project_id = new.project_id, info = new.info,
293-
has_access = new.has_access, access_right = new.access_right,
294-
db_status = new.db_status
352+
SET company_id = NEW.company_id, project_id = NEW.project_id, info = NEW.info,
353+
has_access = NEW.has_access, access_right = NEW.access_right,
354+
db_status = NEW.db_status,
355+
start_date = NEW.start_date, end_date = NEW.end_date
295356
WHERE project_company_assignment_id = old.project_company_assignment_id;
296-
297357
CREATE RULE project_teams_del AS ON DELETE TO project_teams DO INSTEAD
298358
DELETE FROM project_company_assignment
299359
WHERE project_company_assignment_id = old.project_company_assignment_id;
@@ -304,23 +364,27 @@ CREATE VIEW project_persons AS
304364
FROM project_company_assignment pca
305365
JOIN person p USING (company_id);
306366

307-
CREATE RULE project_persons_add AS ON INSERT TO project_persons DO INSTEAD
367+
CREATE OR REPLACE RULE project_persons_add AS ON INSERT TO project_persons DO INSTEAD
308368
INSERT INTO project_company_assignment (
309369
project_company_assignment_id, company_id, project_id, info, has_access,
310370
access_right, db_status, start_date, end_date
311371
)
312372
VALUES (
313-
new.project_company_assignment_id, new.company_id, new.project_id, new.info,
314-
new.has_access, new.access_right, new.db_status,
315-
new.start_date, new.end_date
316-
);
373+
NEW.project_company_assignment_id, NEW.company_id, NEW.project_id, NEW.info,
374+
NEW.has_access, NEW.access_right, NEW.db_status,
375+
NEW.start_date, NEW.end_date
376+
)
377+
RETURNING
378+
project_company_assignment_id, company_id, project_id, info, has_access,
379+
access_right, db_status, start_date, end_date
380+
;
317381
CREATE RULE project_persons_set AS ON UPDATE TO project_persons DO INSTEAD
318382
UPDATE project_company_assignment
319-
SET company_id = new.company_id, project_id = new.project_id, info = new.info,
320-
has_access = new.has_access, access_right = new.access_right,
321-
db_status = new.db_status,
322-
start_date = new.start_date, end_date = new.end_date
323-
WHERE project_company_assignment_id = old.project_company_assignment_id;
383+
SET company_id = NEW.company_id, project_id = NEW.project_id, info = NEW.info,
384+
has_access = NEW.has_access, access_right = NEW.access_right,
385+
db_status = NEW.db_status,
386+
start_date = NEW.start_date, end_date = NEW.end_date
387+
WHERE project_company_assignment_id = OLD.project_company_assignment_id;
324388

325389
CREATE RULE project_persons_del AS ON DELETE TO project_persons DO INSTEAD
326390
DELETE FROM project_company_assignment
@@ -332,7 +396,7 @@ CREATE VIEW project_companies AS
332396
FROM project_company_assignment pca
333397
JOIN enterprise e USING (company_id);
334398

335-
CREATE RULE project_companies_add AS ON INSERT TO project_companies DO INSTEAD
399+
CREATE OR REPLACE RULE project_companies_add AS ON INSERT TO project_companies DO INSTEAD
336400
INSERT INTO project_company_assignment (
337401
project_company_assignment_id, company_id, project_id, info, has_access,
338402
access_right, db_status, start_date, end_date
@@ -341,7 +405,11 @@ CREATE RULE project_companies_add AS ON INSERT TO project_companies DO INSTEAD
341405
new.project_company_assignment_id, new.company_id, new.project_id, new.info,
342406
new.has_access, new.access_right, new.db_status,
343407
new.start_date, new.end_date
344-
);
408+
)
409+
RETURNING
410+
project_company_assignment_id, company_id, project_id, info, has_access,
411+
access_right, db_status, start_date, end_date
412+
;
345413
CREATE RULE project_companies_set AS ON UPDATE TO project_companies DO INSTEAD
346414
UPDATE project_company_assignment
347415
SET company_id = new.company_id, project_id = new.project_id, info = new.info,
@@ -350,9 +418,10 @@ CREATE RULE project_companies_set AS ON UPDATE TO project_companies DO INSTEAD
350418
start_date = new.start_date, end_date = new.end_date
351419
WHERE project_company_assignment_id = old.project_company_assignment_id;
352420

353-
CREATE RULE project_companies_del AS ON DELETE TO project_companies DO INSTEAD
421+
-- this does not seem to work
422+
CREATE OR REPLACE RULE project_companies_del AS ON DELETE TO project_companies DO INSTEAD
354423
DELETE FROM project_company_assignment
355-
WHERE project_company_assignment_id = old.project_company_assignment_id;
424+
WHERE project_company_assignment.project_company_assignment_id = OLD.project_company_assignment_id;
356425

357426

358427
-- unused? sounds useful, but maybe its overkill

0 commit comments

Comments
 (0)