diff options
author | Douwe Maan <douwe@gitlab.com> | 2019-01-21 11:25:26 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2019-01-21 11:25:26 +0000 |
commit | 10d421be3fea1c6b69d773aeba3c08dfa1da38d0 (patch) | |
tree | 9d9bd3c51d23b7c1d582854cc77e7d951e56bddb | |
parent | 46b881de986d11d15c0042d10357e88ebba8a7e4 (diff) | |
parent | f9e217872d26c161bc64dc7bb9456b1b22bc3259 (diff) | |
download | gitlab-ce-10d421be3fea1c6b69d773aeba3c08dfa1da38d0.tar.gz |
Merge branch 'sh-preload-associations-for-group-api' into 'master'
Eliminate N+1 queries in /api/groups/:id
Closes #49845
See merge request gitlab-org/gitlab-ce!24513
-rw-r--r-- | changelogs/unreleased/sh-preload-associations-for-group-api.yml | 5 | ||||
-rw-r--r-- | lib/api/entities.rb | 8 | ||||
-rw-r--r-- | spec/requests/api/groups_spec.rb | 14 |
3 files changed, 25 insertions, 2 deletions
diff --git a/changelogs/unreleased/sh-preload-associations-for-group-api.yml b/changelogs/unreleased/sh-preload-associations-for-group-api.yml new file mode 100644 index 00000000000..24e424b7efb --- /dev/null +++ b/changelogs/unreleased/sh-preload-associations-for-group-api.yml @@ -0,0 +1,5 @@ +--- +title: Eliminate N+1 queries in /api/groups/:id +merge_request: 24513 +author: +type: performance diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 97b421e8651..e0a48908122 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -344,19 +344,23 @@ module API class GroupDetail < Group expose :projects, using: Entities::Project do |group, options| - GroupProjectsFinder.new( + projects = GroupProjectsFinder.new( group: group, current_user: options[:current_user], options: { only_owned: true } ).execute + + Entities::Project.prepare_relation(projects) end expose :shared_projects, using: Entities::Project do |group, options| - GroupProjectsFinder.new( + projects = GroupProjectsFinder.new( group: group, current_user: options[:current_user], options: { only_shared: true } ).execute + + Entities::Project.prepare_relation(projects) end end diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb index c9dfc5c4a7e..7176bc23e34 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -382,6 +382,20 @@ describe API::Groups do expect(response_project_ids(json_response, 'shared_projects')) .to contain_exactly(projects[:public].id, projects[:internal].id) end + + it 'avoids N+1 queries' do + get api("/groups/#{group1.id}", admin) + + control_count = ActiveRecord::QueryRecorder.new do + get api("/groups/#{group1.id}", admin) + end.count + + create(:project, namespace: group1) + + expect do + get api("/groups/#{group1.id}", admin) + end.not_to exceed_query_limit(control_count) + end end context "when authenticated as admin" do |