From 2172543772bcf78696af749e15f613e214184942 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Fri, 21 Jun 2024 11:06:19 +0545 Subject: [PATCH] chore: revert indentation to see the diffs properly --- views/006_config_views.sql | 98 +++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/views/006_config_views.sql b/views/006_config_views.sql index 9acc9553..d45e3ec4 100644 --- a/views/006_config_views.sql +++ b/views/006_config_views.sql @@ -431,60 +431,60 @@ CREATE OR REPLACE FUNCTION config_relationships_recursive ( incoming_relation TEXT DEFAULT 'both', -- hard or both (hard & soft) outgoing_relation TEXT DEFAULT 'both' -- hard or both (hard & soft) ) RETURNS TABLE (id UUID, related_id UUID, relation_type TEXT, direction TEXT, depth INT) AS $$ - BEGIN +BEGIN - IF type_filter NOT IN ('incoming', 'outgoing', 'all') THEN - RAISE EXCEPTION 'Invalid type_filter value. Allowed values are: ''incoming'', ''outgoing'', ''all'''; - END IF; +IF type_filter NOT IN ('incoming', 'outgoing', 'all') THEN + RAISE EXCEPTION 'Invalid type_filter value. Allowed values are: ''incoming'', ''outgoing'', ''all'''; +END IF; - IF type_filter = 'outgoing' THEN - RETURN query - WITH RECURSIVE cte (config_id, related_id, relation, direction, depth) AS ( - SELECT parent.config_id, parent.related_id, parent.relation, 'outgoing', 1::int - FROM config_relationships parent - WHERE parent.config_id = config_relationships_recursive.config_id - AND (outgoing_relation = 'both' OR (outgoing_relation = 'hard' AND parent.relation = 'hard')) +IF type_filter = 'outgoing' THEN + RETURN query + WITH RECURSIVE cte (config_id, related_id, relation, direction, depth) AS ( + SELECT parent.config_id, parent.related_id, parent.relation, 'outgoing', 1::int + FROM config_relationships parent + WHERE parent.config_id = config_relationships_recursive.config_id + AND (outgoing_relation = 'both' OR (outgoing_relation = 'hard' AND parent.relation = 'hard')) + AND deleted_at IS NULL + UNION ALL + SELECT + parent.related_id as config_id, child.related_id, child.relation, 'outgoing', parent.depth +1 + FROM config_relationships child, cte parent + WHERE child.config_id = parent.related_id + AND parent.depth <= max_depth + AND (outgoing_relation = 'both' OR (outgoing_relation = 'hard' AND child.relation = 'hard')) AND deleted_at IS NULL - UNION ALL - SELECT - parent.related_id as config_id, child.related_id, child.relation, 'outgoing', parent.depth +1 - FROM config_relationships child, cte parent - WHERE child.config_id = parent.related_id - AND parent.depth <= max_depth - AND (outgoing_relation = 'both' OR (outgoing_relation = 'hard' AND child.relation = 'hard')) - AND deleted_at IS NULL - ) CYCLE config_id SET is_cycle USING path - SELECT DISTINCT cte.config_id, cte.related_id, cte.relation as "relation_type", type_filter as "direction", cte.depth - FROM cte - ORDER BY cte.depth asc; - ELSIF type_filter = 'incoming' THEN - RETURN query - WITH RECURSIVE cte (config_id, related_id, relation, direction, depth) AS ( - SELECT parent.config_id, parent.related_id as related_id, parent.relation, 'incoming', 1::int - FROM config_relationships parent - WHERE parent.related_id = config_relationships_recursive.config_id - AND (incoming_relation = 'both' OR (incoming_relation = 'hard' AND parent.relation = 'hard')) + ) CYCLE config_id SET is_cycle USING path + SELECT DISTINCT cte.config_id, cte.related_id, cte.relation as "relation_type", type_filter as "direction", cte.depth + FROM cte + ORDER BY cte.depth asc; +ELSIF type_filter = 'incoming' THEN + RETURN query + WITH RECURSIVE cte (config_id, related_id, relation, direction, depth) AS ( + SELECT parent.config_id, parent.related_id as related_id, parent.relation, 'incoming', 1::int + FROM config_relationships parent + WHERE parent.related_id = config_relationships_recursive.config_id + AND (incoming_relation = 'both' OR (incoming_relation = 'hard' AND parent.relation = 'hard')) + AND deleted_at IS NULL + UNION ALL + SELECT + child.config_id, child.related_id as related_id, child.relation, 'incoming', parent.depth +1 + FROM config_relationships child, cte parent + WHERE child.related_id = parent.config_id + AND parent.depth <= max_depth + AND (incoming_relation = 'both' OR (incoming_relation = 'hard' AND child.relation = 'hard')) AND deleted_at IS NULL - UNION ALL - SELECT - child.config_id, child.related_id as related_id, child.relation, 'incoming', parent.depth +1 - FROM config_relationships child, cte parent - WHERE child.related_id = parent.config_id - AND parent.depth <= max_depth - AND (incoming_relation = 'both' OR (incoming_relation = 'hard' AND child.relation = 'hard')) - AND deleted_at IS NULL - ) CYCLE config_id SET is_cycle USING path - SELECT DISTINCT cte.config_id, cte.related_id, cte.relation AS "relation_type", type_filter as "direction", cte.depth - FROM cte - ORDER BY cte.depth asc; - ELSE - RETURN query - SELECT * FROM config_relationships_recursive(config_id, 'incoming', max_depth, incoming_relation, outgoing_relation) - UNION - SELECT * FROM config_relationships_recursive(config_id, 'outgoing', max_depth, incoming_relation, outgoing_relation); - END IF; + ) CYCLE config_id SET is_cycle USING path + SELECT DISTINCT cte.config_id, cte.related_id, cte.relation AS "relation_type", type_filter as "direction", cte.depth + FROM cte + ORDER BY cte.depth asc; +ELSE + RETURN query + SELECT * FROM config_relationships_recursive(config_id, 'incoming', max_depth, incoming_relation, outgoing_relation) + UNION + SELECT * FROM config_relationships_recursive(config_id, 'outgoing', max_depth, incoming_relation, outgoing_relation); +END IF; - END; +END; $$ LANGUAGE plpgsql; -- related configs recursively