Commit 01792f50 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Reduce pipeline serialization queries when preloaded

parent 7ebafe16
......@@ -3,8 +3,11 @@ class PipelineSerializer < BaseSerializer
entity PipelineDetailsEntity
def represent(resource, opts = {})
if resource.is_a?(ActiveRecord::Relation)
if paginated? && !resource.respond_to?(:page)
raise Gitlab::Serializer::Pagination::InvalidResourceError
end
if resource.is_a?(ActiveRecord::Relation)
resource = resource.preload([
:stages,
:retryable_builds,
......@@ -20,7 +23,6 @@ class PipelineSerializer < BaseSerializer
end
if opts.delete(:preload)
resource = @paginator.paginate(resource) if paginated?
resource = Gitlab::Ci::Pipeline::Preloader.preload!(resource)
end
......@@ -37,7 +39,7 @@ class PipelineSerializer < BaseSerializer
def represent_stages(resource)
return {} unless resource.present?
data = represent(resource, { only: [{ details: [:stages] }] })
data = represent(resource, { only: [{ details: [:stages] }], preload: true })
data.dig(:details, :stages) || []
end
end
......@@ -99,7 +99,8 @@ describe PipelineSerializer do
end
end
context 'number of queries' do
describe 'number of queries when preloaded' do
subject { serializer.represent(resource, preload: true) }
let(:resource) { Ci::Pipeline.all }
before do
......@@ -120,7 +121,7 @@ describe PipelineSerializer do
it 'verifies number of queries', :request_store do
recorded = ActiveRecord::QueryRecorder.new { subject }
expect(recorded.count).to be_within(1).of(38)
expect(recorded.count).to be_within(1).of(31)
expect(recorded.cached_count).to eq(0)
end
end
......@@ -139,7 +140,7 @@ describe PipelineSerializer do
# pipeline. With the same ref this check is cached but if refs are
# different then there is an extra query per ref
# https://gitlab.com/gitlab-org/gitlab-ce/issues/46368
expect(recorded.count).to be_within(1).of(45)
expect(recorded.count).to be_within(1).of(38)
expect(recorded.cached_count).to eq(0)
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