Commit 5185e928 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'if-327224-avoid_latest_traversal_ids' into 'master'

Simple and optimized self_and_descendants query [RUN ALL RSPEC]

See merge request gitlab-org/gitlab!59762
parents e3a22f2d 7ea96af5
......@@ -52,7 +52,9 @@ module Namespaces
end
def use_traversal_ids?
Feature.enabled?(:use_traversal_ids, root_ancestor, default_enabled: :yaml)
return false unless Feature.enabled?(:use_traversal_ids, root_ancestor, default_enabled: :yaml)
traversal_ids.present?
end
def self_and_descendants
......@@ -86,25 +88,7 @@ module Namespaces
raise UnboundedSearch.new('Must bound search by a top') unless top
without_sti_condition
.traversal_ids_contains(latest_traversal_ids(top))
end
# traversal_ids are a cached value.
#
# The traversal_ids value in a loaded object can become stale when compared
# to the database value. For example, if you load a hierarchy and then move
# a group, any previously loaded descendant objects will have out of date
# traversal_ids.
#
# To solve this problem, we never depend on the object's traversal_ids
# value. We always query the database first with a sub-select for the
# latest traversal_ids.
#
# Note that ActiveRecord will cache query results. You can avoid this by
# using `Model.uncached { ... }`
def latest_traversal_ids(namespace = self)
without_sti_condition.where('id = (?)', namespace)
.select('traversal_ids as latest_traversal_ids')
.traversal_ids_contains("{#{top.id}}")
end
end
end
......
......@@ -3,7 +3,7 @@
require 'spec_helper'
RSpec.describe Gitlab::ObjectHierarchy do
let_it_be(:parent) { create(:group) }
let_it_be(:parent, reload: true) { create(:group) }
let_it_be(:child1) { create(:group, parent: parent) }
let_it_be(:child2) { create(:group, parent: child1) }
......
......@@ -880,7 +880,7 @@ RSpec.describe Namespace do
end
describe '#use_traversal_ids?' do
let_it_be(:namespace) { build(:namespace) }
let_it_be(:namespace, reload: true) { create(:namespace) }
subject { namespace.use_traversal_ids? }
......@@ -902,6 +902,8 @@ RSpec.describe Namespace do
end
context 'when use_traversal_ids feature flag is true' do
let_it_be(:namespace, reload: true) { create(:namespace) }
it_behaves_like 'namespace traversal'
describe '#self_and_descendants' 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