Commit 08ec6881 authored by Vitali Tatarintev's avatar Vitali Tatarintev

Hide decomposed query behind a feature flag

https://gitlab.com/gitlab-org/gitlab/-/issues/341341
parent 4e95bcd8
...@@ -47,13 +47,49 @@ module Ci ...@@ -47,13 +47,49 @@ module Ci
private private
# rubocop: disable CodeReuse/ActiveRecord
def pipelines_using_cte
sha_relation = merge_request.all_commits.select(:sha).distinct
cte = Gitlab::SQL::CTE.new(:shas, sha_relation)
pipelines_for_merge_requests = triggered_by_merge_request
pipelines_for_branch = filter_by_sha(triggered_for_branch, cte)
Ci::Pipeline.with(cte.to_arel) # rubocop: disable CodeReuse/ActiveRecord
.from_union([pipelines_for_merge_requests, pipelines_for_branch])
end
# rubocop: enable CodeReuse/ActiveRecord
def filter_by_sha(pipelines, cte)
hex = Arel::Nodes::SqlLiteral.new("'hex'")
string_sha = Arel::Nodes::NamedFunction.new('encode', [cte.table[:sha], hex])
join_condition = string_sha.eq(Ci::Pipeline.arel_table[:sha])
filter_by(pipelines, cte, join_condition)
end
def filter_by(pipelines, cte, join_condition)
shas_table =
Ci::Pipeline.arel_table
.join(cte.table, Arel::Nodes::InnerJoin)
.on(join_condition)
.join_sources
pipelines.joins(shas_table) # rubocop: disable CodeReuse/ActiveRecord
end
def all_pipelines_for_merge_request def all_pipelines_for_merge_request
if Feature.enabled?(:decomposed_ci_query_in_pipelines_for_merge_request_finder, source_project, default_enabled: :yaml)
pipelines_using_cte
else
shas = merge_request.all_commit_shas shas = merge_request.all_commit_shas
pipelines_for_merge_request = triggered_by_merge_request pipelines_for_merge_request = triggered_by_merge_request
pipelines_for_branch = triggered_for_branch.for_sha(shas) pipelines_for_branch = triggered_for_branch.for_sha(shas)
Ci::Pipeline.from_union([pipelines_for_merge_request, pipelines_for_branch]) Ci::Pipeline.from_union([pipelines_for_merge_request, pipelines_for_branch])
end end
end
# NOTE: this method returns only parent merge request pipelines. # NOTE: this method returns only parent merge request pipelines.
# Child merge request pipelines have a different source. # Child merge request pipelines have a different source.
......
---
name: decomposed_ci_query_in_pipelines_for_merge_request_finder
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/68549
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/341341
milestone: '14.4'
type: development
group: group::pipeline execution
default_enabled: false
...@@ -134,6 +134,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do ...@@ -134,6 +134,7 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do
end end
end end
shared_examples 'returns all pipelines for merge request' do
context 'when pipelines exist for the branch and merge request' do context 'when pipelines exist for the branch and merge request' do
let(:source_ref) { 'feature' } let(:source_ref) { 'feature' }
let(:target_ref) { 'master' } let(:target_ref) { 'master' }
...@@ -247,4 +248,15 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do ...@@ -247,4 +248,15 @@ RSpec.describe Ci::PipelinesForMergeRequestFinder do
end end
end end
end end
it_behaves_like 'returns all pipelines for merge request'
context 'when `decomposed_ci_query_in_pipelines_for_merge_request_finder` feature flag disabled' do
before do
stub_feature_flags(decomposed_ci_query_in_pipelines_for_merge_request_finder: false)
end
it_behaves_like 'returns all pipelines for merge request'
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment