Commit ace04aee authored by Dmitry Gruzd's avatar Dmitry Gruzd Committed by Dylan Griffith

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

parent 6566448f
......@@ -287,6 +287,8 @@ class Namespace < ApplicationRecord
end
def root_ancestor
return self if persisted? && parent_id.nil?
strong_memoize(:root_ancestor) do
self_and_ancestors.reorder(nil).find_by(parent_id: nil)
end
......
......@@ -524,7 +524,7 @@ class Project < ApplicationRecord
scope :with_api_entity_associations, -> {
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, -> {
......
---
title: Resolve N+1 in Search API projects scope
merge_request: 35833
author:
type: performance
......@@ -5,10 +5,10 @@ require 'spec_helper'
RSpec.describe Gitlab::Auth::GroupSaml::GmaMembershipEnforcer do
include ProjectForksHelper
let_it_be(:group) { create(:group_with_managed_accounts, :private) }
let_it_be(:project) { create(:project, namespace: group)}
let_it_be(:managed_user) { create(:user, :group_managed, managing_group: group) }
let_it_be(:managed_user_for_project) { create(:user, :group_managed, managing_group: group) }
let(:group) { create(:group_with_managed_accounts, :private) }
let(:project) { create(:project, namespace: group)}
let(:managed_user) { create(:user, :group_managed, managing_group: group) }
let(:managed_user_for_project) { create(:user, :group_managed, managing_group: group) }
subject { described_class.new(project) }
......
......@@ -884,8 +884,13 @@ RSpec.describe Namespace do
end
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
root_group = create(:group)
nested_group = create(:group, parent: root_group)
deep_nested_group = create(:group, parent: 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