Find admin integration with group-level integrations
What does this MR do?
After adding the group_id
column to the services
table !38499 (merged), we can now save integrations that belongs to a group !39959 (merged).
Integrations can inherit the admin integration from the UI selector. Now, having group-level integration we need to find the right integration to inherit the values from.
We have to find the closest group integration or instance-level integration to pass the object to the view.
Group-level functionality &2543 is under a feature flag group_level_integration
.
SQL Query
Find the closest group integration has been tough. The problem is that we get the ancestors for a project or group, and then we need to find the closest existent service from the ancestors' list.
SELECT "services".*
FROM "services"
WHERE "services"."type" = 'JiraService'
AND "services"."group_id" IN [3, 1, 4] -- [3, 1, 4] is the ancestors array
-- ORDER BY same precedence as the ancestors (group_ids array)
LIMIT 1
This is the query that we are doing here:
WITH RECURSIVE "base_and_ancestors" AS (
(SELECT "namespaces".*
FROM "namespaces"
WHERE "namespaces"."type" = 'Group'
AND "namespaces"."id" = 4)
UNION
(SELECT "namespaces".*
FROM "namespaces",
"base_and_ancestors"
WHERE "namespaces"."type" = 'Group'
AND "namespaces"."id" = "base_and_ancestors"."parent_id"))
SELECT "id"
FROM "base_and_ancestors" AS "namespaces";
SELECT "services".*
FROM "services"
WHERE "services"."type" = 'JiraService'
AND "services"."group_id" IN (4, 1)
ORDER BY array_position((ARRAY[4, 1])::bigint[], services.group_id)
LIMIT 1;
- Find ancestors: https://explain.depesz.com/s/3vFo
- Find closest service: https://explain.depesz.com/s/kuBt
Related to #233938 (closed)