debian-mirror-gitlab/lib/gitlab/database/as_with_materialized.rb

33 lines
989 B
Ruby
Raw Normal View History

2021-04-29 21:17:54 +05:30
# 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.
2021-06-08 01:23:25 +05:30
class AsWithMaterialized < Arel::Nodes::As
2021-04-29 21:17:54 +05:30
extend Gitlab::Utils::StrongMemoize
2021-06-08 01:23:25 +05:30
MATERIALIZED = 'MATERIALIZED '
2021-04-29 21:17:54 +05:30
def initialize(left, right, materialized: true)
2021-06-08 01:23:25 +05:30
if materialized && self.class.materialized_supported?
right.prepend(MATERIALIZED)
end
2021-04-29 21:17:54 +05:30
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.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
def self.materialized_if_supported
materialized_supported? ? 'MATERIALIZED' : ''
end
end
end
end