Commit c4387209 authored by Shinya Maeda's avatar Shinya Maeda

Fix paginator for Environment Serializer

This commit fixes that the paginator in the Environment Serializer
is not correctly working.
parent 8b407f0a
...@@ -23,6 +23,8 @@ class EnvironmentSerializer < BaseSerializer ...@@ -23,6 +23,8 @@ class EnvironmentSerializer < BaseSerializer
latest: super(item.latest, opts) } latest: super(item.latest, opts) }
end end
else else
resource = @paginator.paginate(resource) if paginated?
super(batch_load(resource), opts) super(batch_load(resource), opts)
end end
end end
...@@ -52,7 +54,7 @@ class EnvironmentSerializer < BaseSerializer ...@@ -52,7 +54,7 @@ class EnvironmentSerializer < BaseSerializer
def batch_load(resource) def batch_load(resource)
resource = resource.preload(environment_associations) resource = resource.preload(environment_associations)
resource.all.tap do |environments| resource.all.to_a.tap do |environments|
environments.each do |environment| environments.each do |environment|
# Batch loading the commits of the deployments # Batch loading the commits of the deployments
environment.last_deployment&.commit&.try(:lazy_author) environment.last_deployment&.commit&.try(:lazy_author)
......
---
title: Fix paginator of Environment Serializer
merge_request: 59751
author:
type: fixed
...@@ -20,9 +20,27 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do ...@@ -20,9 +20,27 @@ RSpec.shared_examples 'avoid N+1 on environments serialization' do
expect { serialize(grouping: false) }.not_to exceed_query_limit(control.count) expect { serialize(grouping: false) }.not_to exceed_query_limit(control.count)
end end
def serialize(grouping:) it 'does not preload for environments that does not exist in the page', :request_store do
create_environment_with_associations(project)
first_page_query = ActiveRecord::QueryRecorder.new do
serialize(grouping: false, query: { page: 1, per_page: 1 } )
end
second_page_query = ActiveRecord::QueryRecorder.new do
serialize(grouping: false, query: { page: 2, per_page: 1 } )
end
expect(second_page_query.count).to be < first_page_query.count
end
def serialize(grouping:, query: nil)
query ||= { page: 1, per_page: 1 }
request = double(url: "#{Gitlab.config.gitlab.url}:8080/api/v4/projects?#{query.to_query}", query_parameters: query)
EnvironmentSerializer.new(current_user: user, project: project).yield_self do |serializer| EnvironmentSerializer.new(current_user: user, project: project).yield_self do |serializer|
serializer.within_folders if grouping serializer.within_folders if grouping
serializer.with_pagination(request, spy('response'))
serializer.represent(Environment.where(project: project)) serializer.represent(Environment.where(project: project))
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