Commit 847c698b authored by Imre Farkas's avatar Imre Farkas

Optimize querying User#manageable_groups

parent ad570fa6
...@@ -101,6 +101,10 @@ class User < ActiveRecord::Base ...@@ -101,6 +101,10 @@ class User < ActiveRecord::Base
has_many :groups, through: :group_members has_many :groups, through: :group_members
has_many :owned_groups, -> { where(members: { access_level: Gitlab::Access::OWNER }) }, through: :group_members, source: :group has_many :owned_groups, -> { where(members: { access_level: Gitlab::Access::OWNER }) }, through: :group_members, source: :group
has_many :maintainers_groups, -> { where(members: { access_level: Gitlab::Access::MAINTAINER }) }, through: :group_members, source: :group has_many :maintainers_groups, -> { where(members: { access_level: Gitlab::Access::MAINTAINER }) }, through: :group_members, source: :group
has_many :owned_or_maintainers_groups,
-> { where(members: { access_level: [Gitlab::Access::MAINTAINER, Gitlab::Access::OWNER] }) },
through: :group_members,
source: :group
alias_attribute :masters_groups, :maintainers_groups alias_attribute :masters_groups, :maintainers_groups
# Projects # Projects
...@@ -982,15 +986,7 @@ class User < ActiveRecord::Base ...@@ -982,15 +986,7 @@ class User < ActiveRecord::Base
end end
def manageable_groups def manageable_groups
union_sql = Gitlab::SQL::Union.new([owned_groups.select(:id), maintainers_groups.select(:id)]).to_sql Gitlab::GroupHierarchy.new(owned_or_maintainers_groups).base_and_descendants
# Update this line to not use raw SQL when migrated to Rails 5.2.
# Either ActiveRecord or Arel constructions are fine.
# This was replaced with the raw SQL construction because of bugs in the arel gem.
# Bugs were fixed in arel 9.0.0 (Rails 5.2).
owned_and_maintainer_groups = Group.where("namespaces.id IN (#{union_sql})") # rubocop:disable GitlabSecurity/SqlInjection
Gitlab::GroupHierarchy.new(owned_and_maintainer_groups).base_and_descendants
end end
def namespaces def namespaces
...@@ -1244,11 +1240,6 @@ class User < ActiveRecord::Base ...@@ -1244,11 +1240,6 @@ class User < ActiveRecord::Base
!terms_accepted? !terms_accepted?
end end
def owned_or_maintainers_groups
union = Gitlab::SQL::Union.new([owned_groups, maintainers_groups])
Group.from("(#{union.to_sql}) namespaces")
end
# @deprecated # @deprecated
alias_method :owned_or_masters_groups, :owned_or_maintainers_groups alias_method :owned_or_masters_groups, :owned_or_maintainers_groups
......
---
title: Optimize querying User#manageable_groups
merge_request: 21050
author:
type: performance
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