Commit bf6e6f1f authored by Pavel Shutsin's avatar Pavel Shutsin

Merge branch '349178-self_and_ancestors-linear-query-with-upto-param' into 'master'

Add self_and_ancestors linear query upto parameter

See merge request gitlab-org/gitlab!77283
parents 3ff8dd5d 9c7214c7
...@@ -22,7 +22,7 @@ module Namespaces ...@@ -22,7 +22,7 @@ module Namespaces
unscoped.where(id: root_ids) unscoped.where(id: root_ids)
end 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? return super unless use_traversal_ids_for_ancestor_scopes?
ancestors_cte, base_cte = ancestor_ctes ancestors_cte, base_cte = ancestor_ctes
...@@ -35,11 +35,15 @@ module Namespaces ...@@ -35,11 +35,15 @@ module Namespaces
.where(namespaces[:id].eq(ancestors_cte.table[:ancestor_id])) .where(namespaces[:id].eq(ancestors_cte.table[:ancestor_id]))
.order_by_depth(hierarchy_order) .order_by_depth(hierarchy_order)
if include_self unless include_self
records records = records.where(ancestors_cte.table[:base_id].not_eq(ancestors_cte.table[:ancestor_id]))
else
records.where(ancestors_cte.table[:base_id].not_eq(ancestors_cte.table[:ancestor_id]))
end end
if upto
records = records.where.not(id: unscoped.where(id: upto).select('unnest(traversal_ids)'))
end
records
end end
def self_and_ancestor_ids(include_self: true) def self_and_ancestor_ids(include_self: true)
......
...@@ -17,8 +17,8 @@ module Namespaces ...@@ -17,8 +17,8 @@ module Namespaces
.where(namespaces: { parent_id: nil }) .where(namespaces: { parent_id: nil })
end end
def self_and_ancestors(include_self: true, hierarchy_order: nil) def self_and_ancestors(include_self: true, upto: nil, hierarchy_order: nil)
records = Gitlab::ObjectHierarchy.new(all).base_and_ancestors(hierarchy_order: hierarchy_order) records = Gitlab::ObjectHierarchy.new(all).base_and_ancestors(upto: upto, hierarchy_order: hierarchy_order)
if include_self if include_self
records records
......
...@@ -130,6 +130,12 @@ RSpec.shared_examples 'namespace traversal scopes' do ...@@ -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) } it { is_expected.to contain_exactly(group_2, nested_group_2, deep_nested_group_2) }
end 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 end
describe '.self_and_ancestors' do 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