Commit 62abdcc4 authored by Mark Chao's avatar Mark Chao

Merge branch '339441-linear-roots-scope' into 'master'

Resolve "Linear roots scope"

See merge request gitlab-org/gitlab!74148
parents c6bfbd76 9ab346f1
......@@ -15,6 +15,13 @@ module Namespaces
select('namespaces.traversal_ids[array_length(namespaces.traversal_ids, 1)] AS id')
end
def roots
return super unless use_traversal_ids_roots?
root_ids = all.select("#{quoted_table_name}.traversal_ids[1]").distinct
unscoped.where(id: root_ids)
end
def self_and_ancestors(include_self: true, hierarchy_order: nil)
return super unless use_traversal_ids_for_ancestor_scopes?
......@@ -83,6 +90,11 @@ module Namespaces
Feature.enabled?(:use_traversal_ids, default_enabled: :yaml)
end
def use_traversal_ids_roots?
Feature.enabled?(:use_traversal_ids_roots, default_enabled: :yaml) &&
use_traversal_ids?
end
def use_traversal_ids_for_ancestor_scopes?
Feature.enabled?(:use_traversal_ids_for_ancestor_scopes, default_enabled: :yaml) &&
use_traversal_ids?
......
......@@ -10,6 +10,13 @@ module Namespaces
select('id')
end
def roots
Gitlab::ObjectHierarchy
.new(all)
.base_and_ancestors
.where(namespaces: { parent_id: nil })
end
def self_and_ancestors(include_self: true, hierarchy_order: nil)
records = Gitlab::ObjectHierarchy.new(all).base_and_ancestors(hierarchy_order: hierarchy_order)
......
---
name: use_traversal_ids_roots
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/74148
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/345438
milestone: '14.5'
type: development
group: group::workspace
default_enabled: false
......@@ -49,6 +49,53 @@ RSpec.shared_examples 'namespace traversal scopes' do
it { is_expected.to eq described_class.column_names }
end
shared_examples '.roots' do
context 'with only sub-groups' do
subject { described_class.where(id: [deep_nested_group_1, nested_group_1, deep_nested_group_2]).roots }
it { is_expected.to contain_exactly(group_1, group_2) }
end
context 'with only root groups' do
subject { described_class.where(id: [group_1, group_2]).roots }
it { is_expected.to contain_exactly(group_1, group_2) }
end
context 'with all groups' do
subject { described_class.where(id: groups).roots }
it { is_expected.to contain_exactly(group_1, group_2) }
end
end
describe '.roots' do
context "use_traversal_ids_roots feature flag is true" do
before do
stub_feature_flags(use_traversal_ids: true)
stub_feature_flags(use_traversal_ids_roots: true)
end
it_behaves_like '.roots'
it 'not make recursive queries' do
expect { described_class.where(id: [nested_group_1]).roots.load }.not_to make_queries_matching(/WITH RECURSIVE/)
end
end
context "use_traversal_ids_roots feature flag is false" do
before do
stub_feature_flags(use_traversal_ids_roots: false)
end
it_behaves_like '.roots'
it 'make recursive queries' do
expect { described_class.where(id: [nested_group_1]).roots.load }.to make_queries_matching(/WITH RECURSIVE/)
end
end
end
shared_examples '.self_and_ancestors' do
subject { described_class.where(id: [nested_group_1, nested_group_2]).self_and_ancestors }
......
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