Commit 92661655 authored by Imre Farkas's avatar Imre Farkas

Avoid using contained by psql operator

parent f0feebb5
......@@ -45,7 +45,6 @@ module Namespaces
after_update :sync_traversal_ids, if: -> { sync_traversal_ids? && saved_change_to_parent_id? }
scope :traversal_ids_contains, ->(ids) { where("traversal_ids @> (?)", ids) }
scope :traversal_ids_contained_by, ->(ids) { where("traversal_ids <@ (?)", ids) }
end
def sync_traversal_ids?
......@@ -102,7 +101,7 @@ module Namespaces
end
if bottom
skope = skope.traversal_ids_contained_by(sql_array(bottom.traversal_ids))
skope = skope.where(id: bottom.traversal_ids[0..-1])
end
# The original `with_depth` attribute in ObjectHierarchy increments as you
......@@ -116,10 +115,6 @@ module Namespaces
skope
end
def sql_array(ids)
"{#{ids.join(',')}}"
end
end
end
end
......@@ -454,7 +454,7 @@ RSpec.describe Group do
end
describe '#ancestors' do
it { expect(group.ancestors.to_sql).to include 'traversal_ids <@' }
it { expect(group.ancestors.to_sql).to include "\"namespaces\".\"id\" = #{group.parent_id}" }
it 'hierarchy order' do
expect(group.ancestors(hierarchy_order: :asc).to_sql).to include 'ORDER BY "depth" ASC'
......
......@@ -39,16 +39,17 @@ RSpec.shared_examples 'namespace traversal' do
end
describe '#ancestors' do
let(:group) { create(:group) }
let(:nested_group) { create(:group, parent: group) }
let(:deep_nested_group) { create(:group, parent: nested_group) }
let(:very_deep_nested_group) { create(:group, parent: deep_nested_group) }
let_it_be(:group) { create(:group) }
let_it_be(:nested_group) { create(:group, parent: group) }
let_it_be(:deep_nested_group) { create(:group, parent: nested_group) }
let_it_be(:very_deep_nested_group) { create(:group, parent: deep_nested_group) }
it 'returns the correct ancestors' do
expect(very_deep_nested_group.ancestors).to include(group, nested_group, deep_nested_group)
expect(deep_nested_group.ancestors).to include(group, nested_group)
expect(nested_group.ancestors).to include(group)
expect(group.ancestors).to eq([])
# #reload is called to make sure traversal_ids are reloaded
expect(very_deep_nested_group.reload.ancestors).to contain_exactly(group, nested_group, deep_nested_group)
expect(deep_nested_group.reload.ancestors).to contain_exactly(group, nested_group)
expect(nested_group.reload.ancestors).to contain_exactly(group)
expect(group.reload.ancestors).to eq([])
end
describe '#recursive_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