Skip to content

Draft: Change query filter

Arturo Herrero requested to merge batch-query-propagating-to-descendants into master

What does this MR do?

I don't understand why this test fails.

Before

[1] pry> Service.inherited_descendants_from_self_or_ancestors_from(integration).where(id: min_id..max_id)
=> [#<RedmineService:0x00007fb721ac3098
  id: 2,
  type: "RedmineService",
  project_id: nil,
  created_at: Fri, 30 Oct 2020 11:03:04 UTC +00:00,
  updated_at: Fri, 30 Oct 2020 11:03:04 UTC +00:00,
  active: true,
  properties: nil,
  push_events: true,
  issues_events: true,
  merge_requests_events: true,
  tag_push_events: true,
  note_events: true,
  category: "issue_tracker",
  wiki_page_events: true,
  pipeline_events: true,
  confidential_issues_events: true,
  commit_events: true,
  job_events: true,
  confidential_note_events: true,
  deployment_events: false,
  comment_on_event_enabled: true,
  template: false,
  instance: false,
  comment_detail: nil,
  inherit_from_id: 1,
  alert_events: true,
  group_id: 2>]
SELECT "services".*
FROM (
        (SELECT "services".*
         FROM "services"
         WHERE "services"."type" = 'RedmineService'
           AND "services"."inherit_from_id" IN
             (SELECT "services"."id"
              FROM "services"
              WHERE "services"."type" = 'RedmineService'
                AND ("services"."group_id" IN
                       (SELECT "namespaces"."id"
                        FROM "namespaces"
                        WHERE "namespaces"."type" = 'Group'
                          AND "namespaces"."id" = 1)
                     OR "services"."instance" = TRUE))
           AND "services"."group_id" IN
             (WITH RECURSIVE "base_and_descendants" AS (
                                                          (SELECT "namespaces".*
                                                           FROM "namespaces"
                                                           WHERE "namespaces"."type" = 'Group'
                                                             AND "namespaces"."parent_id" = 1)
                                                        UNION
                                                          (SELECT "namespaces".*
                                                           FROM "namespaces",
                                                                "base_and_descendants"
                                                           WHERE "namespaces"."type" = 'Group'
                                                             AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT id
              FROM "base_and_descendants" AS "namespaces"))
      UNION
        (SELECT "services".*
         FROM "services"
         WHERE "services"."type" = 'RedmineService'
           AND "services"."inherit_from_id" IN
             (SELECT "services"."id"
              FROM "services"
              WHERE "services"."type" = 'RedmineService'
                AND ("services"."group_id" IN
                       (SELECT "namespaces"."id"
                        FROM "namespaces"
                        WHERE "namespaces"."type" = 'Group'
                          AND "namespaces"."id" = 1)
                     OR "services"."instance" = TRUE))
           AND "services"."project_id" IN
             (SELECT "projects"."id"
              FROM "projects"
              WHERE "projects"."namespace_id" IN
                  (WITH RECURSIVE "base_and_descendants" AS (
                                                               (SELECT "namespaces".*
                                                                FROM "namespaces"
                                                                WHERE "namespaces"."type" = 'Group'
                                                                  AND "namespaces"."id" = 1)
                                                             UNION
                                                               (SELECT "namespaces".*
                                                                FROM "namespaces",
                                                                     "base_and_descendants"
                                                                WHERE "namespaces"."type" = 'Group'
                                                                  AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT "namespaces"."id"
                   FROM "base_and_descendants" AS "namespaces")))) services
WHERE "services"."id" BETWEEN 2 AND 2

After

[2] pry> Service.where(id: min_id..max_id).inherited_descendants_from_self_or_ancestors_from(integration)
=> []
SELECT "services".*
FROM (
        (SELECT "services".*
         FROM "services"
         WHERE "services"."id" BETWEEN 2 AND 2
           AND "services"."type" = 'RedmineService'
           AND "services"."inherit_from_id" IN
             (SELECT "services"."id"
              FROM "services"
              WHERE "services"."id" BETWEEN 2 AND 2
                AND "services"."type" = 'RedmineService'
                AND ("services"."group_id" IN
                       (SELECT "namespaces"."id"
                        FROM "namespaces"
                        WHERE "namespaces"."type" = 'Group'
                          AND "namespaces"."id" = 1)
                     OR "services"."instance" = TRUE))
           AND "services"."group_id" IN
             (WITH RECURSIVE "base_and_descendants" AS (
                                                          (SELECT "namespaces".*
                                                           FROM "namespaces"
                                                           WHERE "namespaces"."type" = 'Group'
                                                             AND "namespaces"."parent_id" = 1)
                                                        UNION
                                                          (SELECT "namespaces".*
                                                           FROM "namespaces",
                                                                "base_and_descendants"
                                                           WHERE "namespaces"."type" = 'Group'
                                                             AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT id
              FROM "base_and_descendants" AS "namespaces"))
      UNION
        (SELECT "services".*
         FROM "services"
         WHERE "services"."id" BETWEEN 2 AND 2
           AND "services"."type" = 'RedmineService'
           AND "services"."inherit_from_id" IN
             (SELECT "services"."id"
              FROM "services"
              WHERE "services"."id" BETWEEN 2 AND 2
                AND "services"."type" = 'RedmineService'
                AND ("services"."group_id" IN
                       (SELECT "namespaces"."id"
                        FROM "namespaces"
                        WHERE "namespaces"."type" = 'Group'
                          AND "namespaces"."id" = 1)
                     OR "services"."instance" = TRUE))
           AND "services"."project_id" IN
             (SELECT "projects"."id"
              FROM "projects"
              WHERE "projects"."namespace_id" IN
                  (WITH RECURSIVE "base_and_descendants" AS (
                                                               (SELECT "namespaces".*
                                                                FROM "namespaces"
                                                                WHERE "namespaces"."type" = 'Group'
                                                                  AND "namespaces"."id" = 1)
                                                             UNION
                                                               (SELECT "namespaces".*
                                                                FROM "namespaces",
                                                                     "base_and_descendants"
                                                                WHERE "namespaces"."type" = 'Group'
                                                                  AND "namespaces"."parent_id" = "base_and_descendants"."id")) SELECT "namespaces"."id"
                   FROM "base_and_descendants" AS "namespaces")))) services
WHERE "services"."id" BETWEEN 2 AND 2

Screen_Shot_2020-10-30_at_11.18.49

Edited by Arturo Herrero

Merge request reports