Commit cda2c5e7 authored by Douwe Maan's avatar Douwe Maan

Only preload member records for the relevant projects/groups/user in projects API

parent 62d96d88
---
title: Only preload member records for the relevant projects/groups/user in projects
API
merge_request:
author:
type: performance
...@@ -832,8 +832,8 @@ module API ...@@ -832,8 +832,8 @@ module API
class ProjectWithAccess < Project class ProjectWithAccess < Project
expose :permissions do expose :permissions do
expose :project_access, using: Entities::ProjectAccess do |project, options| expose :project_access, using: Entities::ProjectAccess do |project, options|
if options.key?(:project_members) if options[:project_members]
(options[:project_members] || []).find { |member| member.source_id == project.id } options[:project_members].find { |member| member.source_id == project.id }
else else
project.project_member(options[:current_user]) project.project_member(options[:current_user])
end end
...@@ -841,8 +841,8 @@ module API ...@@ -841,8 +841,8 @@ module API
expose :group_access, using: Entities::GroupAccess do |project, options| expose :group_access, using: Entities::GroupAccess do |project, options|
if project.group if project.group
if options.key?(:group_members) if options[:group_members]
(options[:group_members] || []).find { |member| member.source_id == project.namespace_id } options[:group_members].find { |member| member.source_id == project.namespace_id }
else else
project.group.group_member(options[:current_user]) project.group.group_member(options[:current_user])
end end
...@@ -853,13 +853,24 @@ module API ...@@ -853,13 +853,24 @@ module API
def self.preload_relation(projects_relation, options = {}) def self.preload_relation(projects_relation, options = {})
relation = super(projects_relation, options) relation = super(projects_relation, options)
unless options.key?(:group_members) # MySQL doesn't support LIMIT inside an IN subquery
relation = relation.preload(group: [group_members: [:source, user: [notification_settings: :source]]]) if Gitlab::Database.mysql?
project_ids = relation.pluck('projects.id')
namespace_ids = relation.pluck(:namespace_id)
else
project_ids = relation.select('projects.id')
namespace_ids = relation.select(:namespace_id)
end end
unless options.key?(:project_members) options[:project_members] = options[:current_user]
relation = relation.preload(project_members: [:source, user: [notification_settings: :source]]) .project_members
end .where(source_id: project_ids)
.preload(:source, user: [notification_settings: :source])
options[:group_members] = options[:current_user]
.group_members
.where(source_id: namespace_ids)
.preload(:source, user: [notification_settings: :source])
relation relation
end end
......
...@@ -58,16 +58,9 @@ module API ...@@ -58,16 +58,9 @@ module API
projects = paginate(projects) projects = paginate(projects)
projects, options = with_custom_attributes(projects, options) projects, options = with_custom_attributes(projects, options)
if current_user
project_members = current_user.project_members.preload(:source, user: [notification_settings: :source])
group_members = current_user.group_members.preload(:source, user: [notification_settings: :source])
end
options = options.reverse_merge( options = options.reverse_merge(
with: current_user ? Entities::ProjectWithAccess : Entities::BasicProjectDetails, with: current_user ? Entities::ProjectWithAccess : Entities::BasicProjectDetails,
statistics: params[:statistics], statistics: params[:statistics],
project_members: project_members,
group_members: group_members,
current_user: current_user current_user: current_user
) )
options[:with] = Entities::BasicProjectDetails if params[:simple] options[:with] = Entities::BasicProjectDetails if params[:simple]
......
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