Commit 0a544f14 authored by Alex Pooley's avatar Alex Pooley

Namespace#self_and_hierarchy linear query

Integrates linear queries for both descendants and ancestors.
parent 42cd0687
...@@ -57,6 +57,13 @@ module Namespaces ...@@ -57,6 +57,13 @@ module Namespaces
traversal_ids.present? traversal_ids.present?
end end
def use_traversal_ids_for_self_and_hierarchy?
return false unless use_traversal_ids?
return false unless Feature.enabled?(:use_traversal_ids_for_self_and_hierarchy, root_ancestor, default_enabled: :yaml)
traversal_ids.present?
end
def use_traversal_ids_for_ancestors? def use_traversal_ids_for_ancestors?
return false unless use_traversal_ids? return false unless use_traversal_ids?
return false unless Feature.enabled?(:use_traversal_ids_for_ancestors, root_ancestor, default_enabled: :yaml) return false unless Feature.enabled?(:use_traversal_ids_for_ancestors, root_ancestor, default_enabled: :yaml)
...@@ -107,6 +114,12 @@ module Namespaces ...@@ -107,6 +114,12 @@ module Namespaces
self_and_descendants.where.not(id: id) self_and_descendants.where.not(id: id)
end end
def self_and_hierarchy
return super unless use_traversal_ids_for_self_and_hierarchy?
self_and_descendants.or(ancestors)
end
def ancestors(hierarchy_order: nil) def ancestors(hierarchy_order: nil)
return super unless use_traversal_ids_for_ancestors? return super unless use_traversal_ids_for_ancestors?
......
---
name: use_traversal_ids_for_self_and_hierarchy
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/76814
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/348527
milestone: '14.7'
type: development
group: group::workspace
default_enabled: false
...@@ -502,6 +502,10 @@ RSpec.describe Group do ...@@ -502,6 +502,10 @@ RSpec.describe Group do
it { expect(group.descendants.to_sql).not_to include 'traversal_ids @>' } it { expect(group.descendants.to_sql).not_to include 'traversal_ids @>' }
end end
describe '#self_and_hierarchy' do
it { expect(group.self_and_hierarchy.to_sql).not_to include 'traversal_ids @>' }
end
describe '#ancestors' do describe '#ancestors' do
it { expect(group.ancestors.to_sql).not_to include 'traversal_ids <@' } it { expect(group.ancestors.to_sql).not_to include 'traversal_ids <@' }
end end
...@@ -526,6 +530,10 @@ RSpec.describe Group do ...@@ -526,6 +530,10 @@ RSpec.describe Group do
it { expect(group.descendants.to_sql).to include 'traversal_ids @>' } it { expect(group.descendants.to_sql).to include 'traversal_ids @>' }
end end
describe '#self_and_hierarchy' do
it { expect(group.self_and_hierarchy.to_sql).to include 'traversal_ids @>' }
end
describe '#ancestors' do describe '#ancestors' do
it { expect(group.ancestors.to_sql).to include "\"namespaces\".\"id\" = #{group.parent_id}" } it { expect(group.ancestors.to_sql).to include "\"namespaces\".\"id\" = #{group.parent_id}" }
......
...@@ -1263,6 +1263,32 @@ RSpec.describe Namespace do ...@@ -1263,6 +1263,32 @@ RSpec.describe Namespace do
end end
end end
describe '#use_traversal_ids_for_self_and_hierarchy?' do
let_it_be(:namespace, reload: true) { create(:namespace) }
subject { namespace.use_traversal_ids_for_self_and_hierarchy? }
it { is_expected.to eq true }
it_behaves_like 'disabled feature flag when traversal_ids is blank'
context 'when use_traversal_ids_for_self_and_hierarchy feature flag is false' do
before do
stub_feature_flags(use_traversal_ids_for_self_and_hierarchy: false)
end
it { is_expected.to eq false }
end
context 'when use_traversal_ids? feature flag is false' do
before do
stub_feature_flags(use_traversal_ids: false)
end
it { is_expected.to eq false }
end
end
describe '#users_with_descendants' do describe '#users_with_descendants' do
let(:user_a) { create(:user) } let(:user_a) { create(:user) }
let(:user_b) { create(:user) } let(:user_b) { create(:user) }
......
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