Commit 7db82023 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '352119-namespace-self_and_hierarchy-scope' into 'master'

Namespace.self_and_hierarchy scope

See merge request gitlab-org/gitlab!80045
parents d8e984ea 961d5e4e
......@@ -75,6 +75,12 @@ module Namespaces
end
end
def self_and_hierarchy
return super unless use_traversal_ids_for_self_and_hierarchy_scopes?
unscoped.from_union([all.self_and_ancestors, all.self_and_descendants(include_self: false)])
end
def order_by_depth(hierarchy_order)
return all unless hierarchy_order
......@@ -114,6 +120,11 @@ module Namespaces
use_traversal_ids?
end
def use_traversal_ids_for_self_and_hierarchy_scopes?
Feature.enabled?(:use_traversal_ids_for_self_and_hierarchy_scopes, default_enabled: :yaml) &&
use_traversal_ids?
end
def self_and_descendants_with_comparison_operators(include_self: true)
base = all.select(
:traversal_ids,
......
......@@ -53,6 +53,11 @@ module Namespaces
self_and_descendants(include_self: include_self).as_ids
end
alias_method :recursive_self_and_descendant_ids, :self_and_descendant_ids
def self_and_hierarchy
Gitlab::ObjectHierarchy.new(all).all_objects
end
alias_method :recursive_self_and_hierarchy, :self_and_hierarchy
end
end
end
......
---
name: use_traversal_ids_for_self_and_hierarchy_scopes
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/80045
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/352120
milestone: '14.8'
type: development
group: group::workspace
default_enabled: false
......@@ -299,4 +299,51 @@ RSpec.shared_examples 'namespace traversal scopes' do
include_examples '.self_and_descendant_ids'
end
end
shared_examples '.self_and_hierarchy' do
let(:base_scope) { Group.where(id: base_groups) }
subject { base_scope.self_and_hierarchy }
context 'with ancestors only' do
let(:base_groups) { [group_1, group_2] }
it { is_expected.to match_array(groups) }
end
context 'with descendants only' do
let(:base_groups) { [deep_nested_group_1, deep_nested_group_2] }
it { is_expected.to match_array(groups) }
end
context 'nodes with both ancestors and descendants' do
let(:base_groups) { [nested_group_1, nested_group_2] }
it { is_expected.to match_array(groups) }
end
context 'with duplicate base groups' do
let(:base_groups) { [nested_group_1, nested_group_1] }
it { is_expected.to contain_exactly(group_1, nested_group_1, deep_nested_group_1) }
end
end
describe '.self_and_hierarchy' do
it_behaves_like '.self_and_hierarchy'
context "use_traversal_ids_for_self_and_hierarchy_scopes feature flag is false" do
before do
stub_feature_flags(use_traversal_ids_for_self_and_hierarchy_scopes: false)
end
it_behaves_like '.self_and_hierarchy'
it 'make recursive queries' do
base_groups = Group.where(id: nested_group_1)
expect { base_groups.self_and_hierarchy.load }.to make_queries_matching(/WITH RECURSIVE/)
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