summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline_object_hierarchy.rb
blob: e05a617f4fc501c0259b9308eca65b1be72b2aae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module Gitlab
  module Ci
    class PipelineObjectHierarchy < ::Gitlab::ObjectHierarchy
      private

      def middle_table
        ::Ci::Sources::Pipeline.arel_table
      end

      def from_tables(cte)
        [objects_table, cte.table, middle_table]
      end

      def parent_id_column(_cte)
        middle_table[:source_pipeline_id]
      end

      def ancestor_conditions(cte)
        middle_table[:source_pipeline_id].eq(objects_table[:id]).and(
          middle_table[:pipeline_id].eq(cte.table[:id])
        ).and(
          project_condition
        )
      end

      def descendant_conditions(cte)
        middle_table[:pipeline_id].eq(objects_table[:id]).and(
          middle_table[:source_pipeline_id].eq(cte.table[:id])
        ).and(
          project_condition
        )
      end

      def project_condition
        case options[:project_condition]
        when :same then middle_table[:source_project_id].eq(middle_table[:project_id])
        when :different then middle_table[:source_project_id].not_eq(middle_table[:project_id])
        else Arel.sql('TRUE')
        end
      end
    end
  end
end