BigW Consortium Gitlab

Commit a52b54f3 by Toon Claes

Optimize query for User#groups_through_project_authorizations

Instead of plucking paths, and doing LIKE/OR on each of them, use a subquery to find groups that are the ancestor of the projects the user is member of.
parent 3ea9de81
......@@ -493,15 +493,14 @@ class User < ActiveRecord::Base
end
def groups_through_project_authorizations
paths = Project.member_self_and_descendants(id).pluck('routes.path')
return Group.none if paths.empty?
wheres = paths.map do |path|
"#{ActiveRecord::Base.connection.quote(path)} LIKE CONCAT(routes.path, '/%')"
end
Group.joins(:route).where(wheres.join(' OR '))
projects = Project.joins(:project_authorizations).
where('project_authorizations.user_id = ?', id ).
joins(:route).
select('routes.path AS full_path')
Group.joins(:route).
joins("INNER JOIN (#{projects.to_sql}) project_paths
ON project_paths.full_path LIKE CONCAT(routes_namespaces.path, '/%')")
end
def nested_groups
......
......@@ -91,7 +91,7 @@ describe GroupsFinder do
subsubproject = create(:empty_project, :private, namespace: private_subsubgroup)
subsubproject.add_guest(user)
is_expected.to include(private_subsubgroup)
expect(described_class.new(user).execute).to include(private_subsubgroup, private_subgroup, parent_group)
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