Commit b7c2e24c authored by Stan Hu's avatar Stan Hu

Fix N+1 queries with /api/v4/groups endpoint

This is an EE-only issue since `ldap_group_link` is not in CE.

Fix for gitlab-org/gitlab-ce#42030
parent 8f4b6e2e
---
title: FIx N+1 queries with /api/v4/groups endpoint
merge_request:
author:
type: performance
......@@ -54,6 +54,8 @@ module API
find_params[:parent] = find_group!(params[:id]) if params[:id]
groups = GroupsFinder.new(current_user, find_params).execute
# EE-only
groups = groups.preload(:ldap_group_links)
groups = groups.search(params[:search]) if params[:search].present?
groups = groups.where.not(id: params[:skip_groups]) if params[:skip_groups].present?
groups = groups.reorder(params[:order_by] => params[:sort])
......
......@@ -31,6 +31,21 @@ describe API::Groups do
expect(json_response)
.to satisfy_one { |group| group['name'] == group1.name }
end
it 'avoids N+1 queries' do
# Establish baseline
get api("/groups", admin)
control = ActiveRecord::QueryRecorder.new do
get api("/groups", admin)
end
create(:group)
expect do
get api("/groups", admin)
end.not_to exceed_query_limit(control)
end
end
context "when authenticated as user" do
......
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