Welcome to mirror list, hosted at ThFree Co, Russian Federation.

as_with_materialized.rb « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07809c5b59250717e34e72590f5816f60654037d (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
# frozen_string_literal: true

module Gitlab
  module Database
    # This class is a special Arel node which allows optionally define the `MATERIALIZED` keyword for CTE and Recursive CTE queries.
    class AsWithMaterialized < Arel::Nodes::As
      extend Gitlab::Utils::StrongMemoize

      MATERIALIZED = 'MATERIALIZED '

      def initialize(left, right, materialized: true)
        if materialized && self.class.materialized_supported?
          right.prepend(MATERIALIZED)
        end

        super(left, right)
      end

      # Note: to be deleted after the minimum PG version is set to 12.0
      def self.materialized_supported?
        strong_memoize(:materialized_supported) do
          Gitlab::Database.main.version.match?(/^1[2-9]\./) # version 12.x and above
        end
      end

      # Note: to be deleted after the minimum PG version is set to 12.0
      # Update the documentation together when deleting the method
      # https://docs.gitlab.com/ee/development/merge_request_performance_guidelines.html#use-ctes-wisely
      def self.materialized_if_supported
        materialized_supported? ? 'MATERIALIZED' : ''
      end
    end
  end
end