Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update functions and triggers #491

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions datamodel/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def create_app(
run_sql_file("functions/modification_functions.sql", pg_service)
run_sql_file("functions/symbology_functions.sql", pg_service)
run_sql_file("functions/reach_direction_change.sql", pg_service, variables)
run_sql_file("functions/14_geometry_functions.sql", pg_service, variables)
run_sql_file("functions/geometry_functions.sql", pg_service, variables)
run_sql_file("functions/update_catchment_area_totals.sql", pg_service, variables)
run_sql_file("functions/organisation_functions.sql", pg_service, variables)
run_sql_file("functions/meta_functions.sql", pg_service, variables)
run_sql_file("functions/network_functions.sql", pg_service)

# open YAML files
if tww_reach_extra:
Expand Down Expand Up @@ -134,6 +135,10 @@ def create_app(
"drainage_system": "zone",
}

# Triggers
# Has to be fired before view creation otherwise it won't work and will only fail in CI
run_sql_file("triggers/create_triggers.sql", pg_service)

set_defaults_and_triggers(pg_service, SingleInheritances)

for key in SingleInheritances:
Expand Down Expand Up @@ -245,8 +250,7 @@ def create_app(
pg_service,
).create()

run_sql_file("triggers/network.sql", pg_service)

# Roles
run_sql_file("tww_app_roles.sql", pg_service, variables)


Expand Down
41 changes: 40 additions & 1 deletion datamodel/app/functions/modification_functions.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
------ LAST MODIFIED -----
--------------------------------------------------
-- ON REACH DELETE
--------------------------------------------------
-- Remove linked network element and reach points
-- Remove channel if no reach is left
--------------------------------------------------

CREATE OR REPLACE FUNCTION tww_app.modification_on_reach_delete()
RETURNS trigger AS
$BODY$
DECLARE
channel_id text;
reach_count integer;
BEGIN
-- get channel obj_id
SELECT fk_wastewater_structure INTO channel_id
FROM tww_od.wastewater_networkelement
WHERE wastewater_networkelement.obj_id = OLD.obj_id;

DELETE FROM tww_od.wastewater_networkelement WHERE obj_id = OLD.obj_id;
DELETE FROM tww_od.reach_point WHERE obj_id = OLD.fk_reach_point_from;
DELETE FROM tww_od.reach_point WHERE obj_id = OLD.fk_reach_point_to;

-- delete channel if no reach left
SELECT COUNT(fk_wastewater_structure) INTO reach_count
FROM tww_od.wastewater_networkelement
WHERE fk_wastewater_structure = channel_id;
IF reach_count = 0 THEN
RAISE NOTICE 'Removing channel (%) since no reach is left', channel_id;
DELETE FROM tww_od.channel WHERE obj_id = channel_id;
DELETE FROM tww_od.wastewater_structure WHERE obj_id = channel_id;
END IF;
RETURN NEW;
END; $BODY$
LANGUAGE plpgsql VOLATILE;

--------------------------------------------------
-- LAST MODIFIED
--------------------------------------------------

CREATE FUNCTION tww_app.modification_last_modified() RETURNS trigger AS $$
BEGIN
NEW.last_modification := TIMEOFDAY();
Expand Down
113 changes: 2 additions & 111 deletions datamodel/app/functions/symbology_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -681,39 +681,6 @@ END; $BODY$
LANGUAGE plpgsql VOLATILE;


--------------------------------------------------
-- ON REACH DELETE
--------------------------------------------------

CREATE OR REPLACE FUNCTION tww_app.symbology_on_reach_delete()
RETURNS trigger AS
$BODY$
DECLARE
channel_id text;
reach_count integer;
BEGIN
-- get channel obj_id
SELECT fk_wastewater_structure INTO channel_id
FROM tww_od.wastewater_networkelement
WHERE wastewater_networkelement.obj_id = OLD.obj_id;

DELETE FROM tww_od.wastewater_networkelement WHERE obj_id = OLD.obj_id;
DELETE FROM tww_od.reach_point WHERE obj_id = OLD.fk_reach_point_from;
DELETE FROM tww_od.reach_point WHERE obj_id = OLD.fk_reach_point_to;

-- delete channel if no reach left
SELECT COUNT(fk_wastewater_structure) INTO reach_count
FROM tww_od.wastewater_networkelement
WHERE fk_wastewater_structure = channel_id;
IF reach_count = 0 THEN
RAISE NOTICE 'Removing channel (%) since no reach is left', channel_id;
DELETE FROM tww_od.channel WHERE obj_id = channel_id;
DELETE FROM tww_od.wastewater_structure WHERE obj_id = channel_id;
END IF;
RETURN NEW;
END; $BODY$
LANGUAGE plpgsql VOLATILE;

--------------------------------------------------
-- ON WASTEWATER NODE CHANGE
--------------------------------------------------
Expand Down Expand Up @@ -822,81 +789,5 @@ BEGIN
END IF;
RETURN NEW;

END;
$BODY$
LANGUAGE plpgsql VOLATILE;


-- only update -> insert and delete are handled by reach trigger
CREATE TRIGGER on_reach_point_update
AFTER UPDATE
ON tww_od.reach_point
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_reach_point_update();

CREATE TRIGGER on_reach_2_change
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_reach_change();

CREATE TRIGGER on_reach_1_delete
AFTER DELETE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_reach_delete();

CREATE TRIGGER calculate_reach_length
BEFORE INSERT OR UPDATE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_calculate_reach_length();

CREATE TRIGGER ws_symbology_update_by_reach
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.ws_symbology_update_by_reach();

CREATE TRIGGER on_wastewater_structure_update
AFTER UPDATE
ON tww_od.wastewater_structure
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_wastewater_structure_update();

CREATE TRIGGER ws_label_update_by_wastewater_networkelement
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.wastewater_networkelement
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_structure_part_change_networkelement();

CREATE TRIGGER on_structure_part_change
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.structure_part
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_structure_part_change_networkelement();

CREATE TRIGGER on_cover_change
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.cover
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_cover_change();

CREATE TRIGGER on_wasterwaternode_change
AFTER INSERT OR UPDATE
ON tww_od.wastewater_node
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_wastewater_node_change();

CREATE TRIGGER ws_symbology_update_by_channel
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.channel
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_update_by_channel();

-- only update -> insert and delete are handled by reach trigger
CREATE TRIGGER ws_symbology_update_by_reach_point
AFTER UPDATE
ON tww_od.reach_point
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_update_by_reach_point();
END; $BODY$
LANGUAGE plpgsql VOLATILE;
73 changes: 73 additions & 0 deletions datamodel/app/triggers/create_triggers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
-- only update -> insert and delete are handled by reach trigger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of separating the code which create the trigger from the ones which create the trigger functions.

CREATE TRIGGER on_reach_point_update
AFTER UPDATE
ON tww_od.reach_point
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_reach_point_update();

CREATE TRIGGER on_reach_2_change
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_reach_change();

CREATE TRIGGER on_reach_1_delete
AFTER DELETE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.modification_on_reach_delete();

CREATE TRIGGER calculate_reach_length
BEFORE INSERT OR UPDATE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_calculate_reach_length();

CREATE TRIGGER ws_symbology_update_by_reach
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.reach
FOR EACH ROW
EXECUTE PROCEDURE tww_app.ws_symbology_update_by_reach();

CREATE TRIGGER on_wastewater_structure_update
AFTER UPDATE
ON tww_od.wastewater_structure
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_wastewater_structure_update();

CREATE TRIGGER ws_label_update_by_wastewater_networkelement
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.wastewater_networkelement
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_structure_part_change_networkelement();

CREATE TRIGGER on_structure_part_change
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.structure_part
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_structure_part_change_networkelement();

CREATE TRIGGER on_cover_change
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.cover
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_cover_change();

CREATE TRIGGER on_wasterwaternode_change
AFTER INSERT OR UPDATE
ON tww_od.wastewater_node
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_on_wastewater_node_change();

CREATE TRIGGER ws_symbology_update_by_channel
AFTER INSERT OR UPDATE OR DELETE
ON tww_od.channel
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_update_by_channel();

-- only update -> insert and delete are handled by reach trigger
CREATE TRIGGER ws_symbology_update_by_reach_point
AFTER UPDATE
ON tww_od.reach_point
FOR EACH ROW
EXECUTE PROCEDURE tww_app.symbology_update_by_reach_point();