Remove the selects when counting the last page

The last page of the first collection is only loaded into memory when
it is being viewed. If it isn't loaded into memory, the `#size` call
triggers a count query.

This `#count` would generate an invalid query if our custom preloaded
counts are included by adding a separate `as count_column` alias on
top of the count aliases. Removing the selects in this case will make
sure a valid `COUNT(*)` is generated.
This commit is contained in:
Bob Van Landuyt 2017-11-17 15:55:43 +01:00
parent 4aa18590b3
commit 5a335c4d95
3 changed files with 20 additions and 1 deletions

View file

@ -0,0 +1,6 @@
---
title: Fix crash when navigating to second page of the group dashbaord when there
are projects and groups on the first page
merge_request: 15456
author:
type: fixed

View file

@ -55,7 +55,9 @@ module Gitlab
def first_collection_last_page_size
return @first_collection_last_page_size if defined?(@first_collection_last_page_size)
@first_collection_last_page_size = paginated_first_collection(first_collection_page_count).count
@first_collection_last_page_size = paginated_first_collection(first_collection_page_count)
.except(:select)
.size
end
end
end

View file

@ -280,6 +280,17 @@ describe Groups::ChildrenController do
expect(assigns(:children)).to contain_exactly(other_subgroup, *next_page_projects.take(per_page - 1))
end
context 'with a mixed first page' do
let!(:first_page_subgroups) { [create(:group, :public, parent: group)] }
let!(:first_page_projects) { create_list(:project, per_page, :public, namespace: group) }
it 'correctly calculates the counts' do
get :index, group_id: group.to_param, sort: 'id_asc', page: 2, format: :json
expect(response).to have_gitlab_http_status(200)
end
end
end
end
end