Do not paginate pipelines active relation twice

This commit is contained in:
Grzegorz Bizon 2018-05-23 13:29:21 +02:00
parent 8cca6c83a9
commit dab3ae39a2
4 changed files with 18 additions and 12 deletions

View File

@ -15,7 +15,6 @@ class Projects::PipelinesController < Projects::ApplicationController
@pipelines = PipelinesFinder
.new(project, scope: @scope)
.execute
.preload(:stages)
.page(params[:page])
.per(30)
@ -24,8 +23,6 @@ class Projects::PipelinesController < Projects::ApplicationController
@finished_count = limited_pipelines_count(project, 'finished')
@pipelines_count = limited_pipelines_count(project)
Gitlab::Ci::Pipeline::Preloader.preload!(@pipelines)
respond_to do |format|
format.html
format.json do
@ -35,7 +32,7 @@ class Projects::PipelinesController < Projects::ApplicationController
pipelines: PipelineSerializer
.new(project: @project, current_user: @current_user)
.with_pagination(request, response)
.represent(@pipelines, disable_coverage: true),
.represent(@pipelines, disable_coverage: true, preload: true),
count: {
all: @pipelines_count,
running: @running_count,

View File

@ -1,14 +1,12 @@
class PipelineSerializer < BaseSerializer
include WithPagination
InvalidResourceError = Class.new(StandardError)
entity PipelineDetailsEntity
def represent(resource, opts = {})
if resource.is_a?(ActiveRecord::Relation)
resource = resource.preload([
:stages,
:retryable_builds,
:cancelable_statuses,
:trigger_requests,
@ -19,11 +17,12 @@ class PipelineSerializer < BaseSerializer
])
end
if paginated?
super(@paginator.paginate(resource), opts)
else
super(resource, opts)
if opts.delete(:preload)
resource = @paginator.paginate(resource) if paginated?
resource = Gitlab::Ci::Pipeline::Preloader.preload!(resource)
end
super(resource, opts)
end
def represent_status(resource)

View File

@ -36,7 +36,7 @@ describe Projects::PipelinesController do
expect(json_response['count']['running']).to eq '1'
expect(json_response['count']['pending']).to eq '1'
expect(json_response['count']['finished']).to eq '2'
expect(queries.count).to be < 32
expect(queries.count).to be_within(2).of(29)
end
it 'does not include coverage data for the pipelines' do

View File

@ -18,5 +18,15 @@ describe Gitlab::Ci::Pipeline::Preloader do
described_class.preload!([pipeline])
end
it 'returns original collection' do
allow(commit).to receive(:lazy_author)
allow(pipeline).to receive(:number_of_warnings)
allow(stage).to receive(:number_of_warnings)
pipelines = [pipeline, pipeline]
expect(described_class.preload!(pipelines)).to eq pipelines
end
end
end