From 9aeac925415d728c897519bc8e435777a9328a97 Mon Sep 17 00:00:00 2001 From: chmnata Date: Thu, 15 Feb 2024 21:36:07 +0000 Subject: [PATCH] #801 add latest mat view and trigger function --- .../sql/create_matview_centreline_latest.sql | 19 +++++++++++++++++++ .../sql/create_trigger_centreline_latest.sql | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 gis/centreline/sql/create_matview_centreline_latest.sql create mode 100644 gis/centreline/sql/create_trigger_centreline_latest.sql diff --git a/gis/centreline/sql/create_matview_centreline_latest.sql b/gis/centreline/sql/create_matview_centreline_latest.sql new file mode 100644 index 000000000..b126bd550 --- /dev/null +++ b/gis/centreline/sql/create_matview_centreline_latest.sql @@ -0,0 +1,19 @@ +CREATE MATERIALIZED VIEW gis_core.centreline_latest AS +SELECT * +FROM gis_core.centreline +WHERE version_date = (SELECT MAX(version_date) FROM gis_core.centreline); + +CREATE TRIGGER refresh_trigger + AFTER INSERT OR UPDATE OR DELETE + ON gis_core.centreline + FOR EACH STATEMENT + EXECUTE PROCEDURE gis_core.centreline_latest_trigger(); + +CREATE INDEX gis_core_centreline_latest_geom + ON gis_core.centreline_latest USING gist(geom); + +ALTER MATERIALIZED VIEW gis_core.centreline_latest OWNER TO gis_admins; + +GRANT SELECT ON gis_core.centreline_latest TO bdit_humans; + +COMMENT ON MATERIALIZED VIEW gis_core.centreline_latest IS 'Materialized view containing the latest version of centreline, derived from gis_core.centreline.' \ No newline at end of file diff --git a/gis/centreline/sql/create_trigger_centreline_latest.sql b/gis/centreline/sql/create_trigger_centreline_latest.sql new file mode 100644 index 000000000..a7641c8ac --- /dev/null +++ b/gis/centreline/sql/create_trigger_centreline_latest.sql @@ -0,0 +1,18 @@ +CREATE OR REPLACE FUNCTION gis_core.centreline_latest_trigger() + RETURNS trigger + LANGUAGE 'plpgsql' + COST 100 + VOLATILE NOT LEAKPROOF SECURITY DEFINER +AS $BODY$ +BEGIN + +REFRESH MATERIALIZED VIEW gis_core.centreline_latest; +RETURN NULL; + +END; +$BODY$; + +ALTER FUNCTION gis_core.centreline_latest_trigger() OWNER TO gis_admins; + +COMMENT ON FUNCTION gis_core.centreline_latest_trigger() IS 'Trigger fuction that refreshes the centreline_latest mat view after an update.'; +