Commit 9902a915 authored by Corinna Wiesner's avatar Corinna Wiesner Committed by Stan Hu

Use simplified query for user namespace projects

The recursive query for personal namespaces is not performing efficient,
there we'll not use the recursive project lookup for personal
namespaces.
parent ada28f94
...@@ -284,7 +284,8 @@ class Namespace < ApplicationRecord ...@@ -284,7 +284,8 @@ class Namespace < ApplicationRecord
# that belongs to this namespace # that belongs to this namespace
def all_projects def all_projects
if Feature.enabled?(:recursive_approach_for_all_projects) if Feature.enabled?(:recursive_approach_for_all_projects)
Project.where(namespace: self_and_descendants) namespace = user? ? self : self_and_descendants
Project.where(namespace: namespace)
else else
Project.inside_path(full_path) Project.inside_path(full_path)
end end
......
...@@ -623,6 +623,7 @@ RSpec.describe Namespace do ...@@ -623,6 +623,7 @@ RSpec.describe Namespace do
subject { namespace.any_project_with_shared_runners_enabled? } subject { namespace.any_project_with_shared_runners_enabled? }
context 'subgroup with shared runners enabled project' do context 'subgroup with shared runners enabled project' do
let(:namespace) { create(:group) }
let(:subgroup) { create(:group, parent: namespace) } let(:subgroup) { create(:group, parent: namespace) }
let!(:subproject) { create(:project, namespace: subgroup, shared_runners_enabled: true) } let!(:subproject) { create(:project, namespace: subgroup, shared_runners_enabled: true) }
......
...@@ -855,8 +855,8 @@ RSpec.describe Namespace do ...@@ -855,8 +855,8 @@ RSpec.describe Namespace do
end end
describe '#all_projects' do describe '#all_projects' do
shared_examples 'all projects for a namespace' do shared_examples 'all projects for a group' do
let(:namespace) { create(:namespace) } let(:namespace) { create(:group) }
let(:child) { create(:group, parent: namespace) } let(:child) { create(:group, parent: namespace) }
let!(:project1) { create(:project_empty_repo, namespace: namespace) } let!(:project1) { create(:project_empty_repo, namespace: namespace) }
let!(:project2) { create(:project_empty_repo, namespace: child) } let!(:project2) { create(:project_empty_repo, namespace: child) }
...@@ -865,30 +865,34 @@ RSpec.describe Namespace do ...@@ -865,30 +865,34 @@ RSpec.describe Namespace do
it { expect(child.all_projects.to_a).to match_array([project2]) } it { expect(child.all_projects.to_a).to match_array([project2]) }
end end
shared_examples 'all project examples' do shared_examples 'all projects for personal namespace' do
include_examples 'all projects for a namespace' let_it_be(:user) { create(:user) }
let_it_be(:user_namespace) { create(:namespace, owner: user) }
let_it_be(:project) { create(:project, namespace: user_namespace) }
it { expect(user_namespace.all_projects.to_a).to match_array([project]) }
end
context 'with recursive approach' do
context 'when namespace is a group' do context 'when namespace is a group' do
let_it_be(:namespace) { create(:group) } include_examples 'all projects for a group'
include_examples 'all projects for a namespace' it 'queries for the namespace and its descendants' do
expect(Project).to receive(:where).with(namespace: [namespace, child])
namespace.all_projects
end
end end
context 'when namespace is a user namespace' do context 'when namespace is a user namespace' do
let_it_be(:user) { create(:user) } include_examples 'all projects for personal namespace'
let_it_be(:user_namespace) { create(:namespace, owner: user) }
let_it_be(:project) { create(:project, namespace: user_namespace) }
it { expect(user_namespace.all_projects.to_a).to match_array([project]) } it 'only queries for the namespace itself' do
end expect(Project).to receive(:where).with(namespace: user_namespace)
end
context 'with recursive approach' do user_namespace.all_projects
before do end
stub_feature_flags(recursive_approach_for_all_projects: true)
end end
include_examples 'all project examples'
end end
context 'with route path wildcard approach' do context 'with route path wildcard approach' do
...@@ -896,7 +900,13 @@ RSpec.describe Namespace do ...@@ -896,7 +900,13 @@ RSpec.describe Namespace do
stub_feature_flags(recursive_approach_for_all_projects: false) stub_feature_flags(recursive_approach_for_all_projects: false)
end end
include_examples 'all project examples' context 'when namespace is a group' do
include_examples 'all projects for a group'
end
context 'when namespace is a user namespace' do
include_examples 'all projects for personal namespace'
end
end 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