Commit 9c7214c7 authored by Alex Pooley's avatar Alex Pooley Committed by Francisco Javier López

Linear ancestors query with upto parameter

This behavior matches the recursive ObjectHierarchy ancestor parameters
parent 7d3e3a44
......@@ -22,7 +22,7 @@ module Namespaces
unscoped.where(id: root_ids)
end
def self_and_ancestors(include_self: true, hierarchy_order: nil)
def self_and_ancestors(include_self: true, upto: nil, hierarchy_order: nil)
return super unless use_traversal_ids_for_ancestor_scopes?
ancestors_cte, base_cte = ancestor_ctes
......@@ -35,11 +35,15 @@ module Namespaces
.where(namespaces[:id].eq(ancestors_cte.table[:ancestor_id]))
.order_by_depth(hierarchy_order)
if include_self
records
else
records.where(ancestors_cte.table[:base_id].not_eq(ancestors_cte.table[:ancestor_id]))
unless include_self
records = records.where(ancestors_cte.table[:base_id].not_eq(ancestors_cte.table[:ancestor_id]))
end
if upto
records = records.where.not(id: unscoped.where(id: upto).select('unnest(traversal_ids)'))
end
records
end
def self_and_ancestor_ids(include_self: true)
......
......@@ -17,8 +17,8 @@ module Namespaces
.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)
def self_and_ancestors(include_self: true, upto: nil, hierarchy_order: nil)
records = Gitlab::ObjectHierarchy.new(all).base_and_ancestors(upto: upto, hierarchy_order: hierarchy_order)
if include_self
records
......
......@@ -130,6 +130,12 @@ RSpec.shared_examples 'namespace traversal scopes' do
it { is_expected.to contain_exactly(group_2, nested_group_2, deep_nested_group_2) }
end
context 'with upto' do
subject { described_class.where(id: deep_nested_group_1).self_and_ancestors(upto: nested_group_1.id) }
it { is_expected.to contain_exactly(deep_nested_group_1) }
end
end
describe '.self_and_ancestors' do
......
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