Commit c4ea2f24 authored by David Fernandez's avatar David Fernandez

Merge branch 'n_plus_one_jobs' into 'master'

Make sure that CI jobs are preloaded

See merge request gitlab-org/gitlab!55841
parents 4a783817 6b119359
...@@ -136,11 +136,7 @@ module Ci ...@@ -136,11 +136,7 @@ module Ci
scope :for_sha, ->(sha, project_id) { joins(job: :pipeline).where(ci_pipelines: { sha: sha, project_id: project_id }) } scope :for_sha, ->(sha, project_id) { joins(job: :pipeline).where(ci_pipelines: { sha: sha, project_id: project_id }) }
scope :for_job_name, ->(name) { joins(:job).where(ci_builds: { name: name }) } scope :for_job_name, ->(name) { joins(:job).where(ci_builds: { name: name }) }
scope :with_job, -> do scope :with_job, -> { joins(:job).includes(:job) }
if Feature.enabled?(:non_public_artifacts, type: :development)
joins(:job).includes(:job)
end
end
scope :with_file_types, -> (file_types) do scope :with_file_types, -> (file_types) do
types = self.file_types.select { |file_type| file_types.include?(file_type) }.values types = self.file_types.select { |file_type| file_types.include?(file_type) }.values
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'merge request content spec' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let_it_be(:merge_request) { create(:merge_request, :with_head_pipeline, target_project: project, source_project: project) }
let_it_be(:ci_build) { create(:ci_build, :artifacts, pipeline: merge_request.head_pipeline) }
before do
sign_in(user)
project.add_maintainer(user)
end
shared_examples 'cached widget request' do
it 'avoids N+1 queries when multiple job artifacts are present' do
control = ActiveRecord::QueryRecorder.new(skip_cached: false) do
get cached_widget_project_json_merge_request_path(project, merge_request, format: :json)
end
create_list(:ci_build, 10, :artifacts, pipeline: merge_request.head_pipeline)
expect do
get cached_widget_project_json_merge_request_path(project, merge_request, format: :json)
end.not_to exceed_query_limit(control)
end
end
describe 'GET cached_widget' do
it_behaves_like 'cached widget request'
context 'with non_public_artifacts disabled' do
before do
stub_feature_flags(non_public_artifacts: false)
end
it_behaves_like 'cached widget request'
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