Commit 2651bbe8 authored by Adam Hegyi's avatar Adam Hegyi

Move recursive namespace query to INNER JOIN

Move recursive namespace query to INNER JOIN
parent 7a19b738
......@@ -260,8 +260,13 @@ class Namespace < ApplicationRecord
# Includes projects from this namespace and projects from all subgroups
# that belongs to this namespace
def all_projects
namespace = user? ? self : self_and_descendants
Project.where(namespace: namespace)
return Project.where(namespace: self) if user?
if Feature.enabled?(:recursive_namespace_lookup_as_inner_join, self)
Project.joins("INNER JOIN (#{self_and_descendants.select(:id).to_sql}) namespaces ON namespaces.id=projects.namespace_id")
else
Project.where(namespace: self_and_descendants)
end
end
# Includes pipelines from this namespace and pipelines from all subgroups
......
---
name: recursive_namespace_lookup_as_inner_join
introduced_by_url:
rollout_issue_url:
milestone: '13.10'
type: development
group: group::optimize
default_enabled: false
......@@ -897,10 +897,24 @@ RSpec.describe Namespace do
it { expect(namespace.all_projects.to_a).to match_array([project2, project1]) }
it { expect(child.all_projects.to_a).to match_array([project2]) }
context 'when recursive_namespace_lookup_as_inner_join feature flag is on' do
before do
stub_feature_flags(recursive_namespace_lookup_as_inner_join: true)
end
it 'queries for the namespace and its descendants' do
expect(Project).to receive(:where).with(namespace: [namespace, child])
expect(namespace.all_projects).to match_array([project1, project2])
end
end
namespace.all_projects
context 'when recursive_namespace_lookup_as_inner_join feature flag is off' do
before do
stub_feature_flags(recursive_namespace_lookup_as_inner_join: false)
end
it 'queries for the namespace and its descendants' do
expect(namespace.all_projects).to match_array([project1, project2])
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