Commit 6e065c62 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch '218703-fix-api-projects-search-n-plus-1' into 'master'

Resolve remaining N+1 in Search API (Advanced) projects scope

Closes #218703

See merge request gitlab-org/gitlab!35833
parents 3a8fdfbe ace04aee
...@@ -287,6 +287,8 @@ class Namespace < ApplicationRecord ...@@ -287,6 +287,8 @@ class Namespace < ApplicationRecord
end end
def root_ancestor def root_ancestor
return self if persisted? && parent_id.nil?
strong_memoize(:root_ancestor) do strong_memoize(:root_ancestor) do
self_and_ancestors.reorder(nil).find_by(parent_id: nil) self_and_ancestors.reorder(nil).find_by(parent_id: nil)
end end
......
...@@ -524,7 +524,7 @@ class Project < ApplicationRecord ...@@ -524,7 +524,7 @@ class Project < ApplicationRecord
scope :with_api_entity_associations, -> { scope :with_api_entity_associations, -> {
preload(:project_feature, :route, :tags, preload(:project_feature, :route, :tags,
group: :ip_restrictions, namespace: [:route, :owner]) group: [:ip_restrictions, :saml_provider], namespace: [:route, :owner])
} }
scope :with_api_commit_entity_associations, -> { scope :with_api_commit_entity_associations, -> {
......
---
title: Resolve N+1 in Search API projects scope
merge_request: 35833
author:
type: performance
...@@ -5,10 +5,10 @@ require 'spec_helper' ...@@ -5,10 +5,10 @@ require 'spec_helper'
RSpec.describe Gitlab::Auth::GroupSaml::GmaMembershipEnforcer do RSpec.describe Gitlab::Auth::GroupSaml::GmaMembershipEnforcer do
include ProjectForksHelper include ProjectForksHelper
let_it_be(:group) { create(:group_with_managed_accounts, :private) } let(:group) { create(:group_with_managed_accounts, :private) }
let_it_be(:project) { create(:project, namespace: group)} let(:project) { create(:project, namespace: group)}
let_it_be(:managed_user) { create(:user, :group_managed, managing_group: group) } let(:managed_user) { create(:user, :group_managed, managing_group: group) }
let_it_be(:managed_user_for_project) { create(:user, :group_managed, managing_group: group) } let(:managed_user_for_project) { create(:user, :group_managed, managing_group: group) }
subject { described_class.new(project) } subject { described_class.new(project) }
......
...@@ -884,8 +884,13 @@ RSpec.describe Namespace do ...@@ -884,8 +884,13 @@ RSpec.describe Namespace do
end end
describe '#root_ancestor' do describe '#root_ancestor' do
let!(:root_group) { create(:group) }
it 'returns root_ancestor for root group without a query' do
expect { root_group.root_ancestor }.not_to exceed_query_limit(0)
end
it 'returns the top most ancestor' do it 'returns the top most ancestor' do
root_group = create(:group)
nested_group = create(:group, parent: root_group) nested_group = create(:group, parent: root_group)
deep_nested_group = create(:group, parent: nested_group) deep_nested_group = create(:group, parent: nested_group)
very_deep_nested_group = create(:group, parent: deep_nested_group) very_deep_nested_group = create(:group, parent: deep_nested_group)
......
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