diff --git a/geotrek/core/path_router.py b/geotrek/core/path_router.py index 2f68724a1e..760818d089 100644 --- a/geotrek/core/path_router.py +++ b/geotrek/core/path_router.py @@ -10,8 +10,20 @@ class PathRouter: def __init__(self): + self.set_pgrouting_fields_to_null() self.set_path_network_topology() + def set_pgrouting_fields_to_null(self): + with connection.cursor() as cursor: + cursor.execute(""" + UPDATE core_path + SET source = NULL, target = NULL + WHERE id IN (SELECT path_id FROM pgrouting_paths_to_set_to_null) + """) + cursor.execute(""" + TRUNCATE pgrouting_paths_to_set_to_null + """) + def set_path_network_topology(self): """ Builds or updates the paths graph (pgRouting network topology) """ cursor = connection.cursor() diff --git a/geotrek/core/templates/core/sql/post_40_paths.sql b/geotrek/core/templates/core/sql/post_40_paths.sql index 643a79ab34..ef878a1dc0 100644 --- a/geotrek/core/templates/core/sql/post_40_paths.sql +++ b/geotrek/core/templates/core/sql/post_40_paths.sql @@ -167,7 +167,7 @@ FOR EACH ROW EXECUTE PROCEDURE path_latest_updated_d(); CREATE FUNCTION {{ schema_geotrek }}.set_pgrouting_values_to_null() RETURNS trigger SECURITY DEFINER AS $$ DECLARE BEGIN - UPDATE core_path SET source = NULL, target = NULL WHERE id = NEW.id; + INSERT INTO pgrouting_paths_to_set_to_null (path_id) VALUES (NEW.id) ON CONFLICT (path_id) DO NOTHING; RETURN NULL; END; $$ LANGUAGE plpgsql; diff --git a/geotrek/core/templates/core/sql/pre_10_cleanup.sql b/geotrek/core/templates/core/sql/pre_10_cleanup.sql index e3956328ec..b6bfb9ae0c 100644 --- a/geotrek/core/templates/core/sql/pre_10_cleanup.sql +++ b/geotrek/core/templates/core/sql/pre_10_cleanup.sql @@ -70,3 +70,8 @@ DROP FUNCTION IF EXISTS ft_merge_path(integer,integer) CASCADE; -- 80 DROP FUNCTION IF EXISTS path_deletion() CASCADE; + +CREATE TABLE IF NOT EXISTS pgrouting_paths_to_set_to_null( + id SERIAL PRIMARY KEY, + path_id INTEGER UNIQUE +);