diff --git a/views/005_component_views.sql b/views/005_component_views.sql index 3cb1b0e8c..91a639469 100644 --- a/views/005_component_views.sql +++ b/views/005_component_views.sql @@ -152,10 +152,13 @@ CREATE OR REPLACE FUNCTION lookup_component_config_id_related_components ( RETURNS TABLE (id UUID) AS $$ BEGIN RETURN QUERY - WITH config_id_paths AS ( + WITH root AS ( + SELECT config_items.path, config_items.id FROM config_items WHERE config_items.id IN (SELECT config_id FROM components WHERE components.id = $1::UUID) + ), + config_id_paths AS ( SELECT config_items.id FROM config_items - WHERE starts_with(path, (SELECT CONCAT(config_items.path, '.', config_items.id) FROM config_items WHERE config_items.id IN (SELECT config_id FROM components WHERE components.id = $1::UUID))) + WHERE starts_with(path, config_item_full_path(root.path, root.id)) ) SELECT components.id FROM components diff --git a/views/006_config_views.sql b/views/006_config_views.sql index b068b536c..dac63030b 100644 --- a/views/006_config_views.sql +++ b/views/006_config_views.sql @@ -1,6 +1,17 @@ -- Add cascade drops first to make sure all functions and views are always recreated DROP VIEW IF EXISTS configs CASCADE; + +CREATE OR REPLACE FUNCTION config_item_full_path(path TEXT, id UUID) +RETURNS TEXT AS $$ +BEGIN + RETURN CASE + WHEN path IS NOT NULL AND path <> '' THEN path || '.' || id::TEXT + ELSE id + END; +END; +$$ LANGUAGE plpgsql; + DROP FUNCTION IF EXISTS related_changes_recursive CASCADE; CREATE MATERIALIZED VIEW IF NOT EXISTS