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

35 lines
1.1 KiB
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
2021-12-11 22:18:48 +05:30
ApplicationRecord.database.version.match?(/^1[2-9]\./) # version 12.x and above
2021-04-29 21:17:54 +05:30
end
end
# Note: to be deleted after the minimum PG version is set to 12.0
2021-09-30 23:02:18 +05:30
# Update the documentation together when deleting the method
# https://docs.gitlab.com/ee/development/merge_request_performance_guidelines.html#use-ctes-wisely
2021-04-29 21:17:54 +05:30
def self.materialized_if_supported
materialized_supported? ? 'MATERIALIZED' : ''
end
end
end
end