Commit b2e8bd91 authored by Dmitry Gruzd's avatar Dmitry Gruzd

Merge branch '339458-fj-use-group-plans-preloader-linear-ancestor-scopes' into 'master'

Use GroupPlansPreloader linear ancestor scopes

See merge request gitlab-org/gitlab!70685
parents 6fa4c0bb 5bffbad5
---
name: linear_group_plans_preloaded_ancestor_scopes
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/70685
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/341349
milestone: '14.4'
type: development
group: group::access
default_enabled: false
...@@ -57,9 +57,15 @@ module Gitlab ...@@ -57,9 +57,15 @@ module Gitlab
# Returns an ActiveRecord::Relation that includes the given groups, and all # Returns an ActiveRecord::Relation that includes the given groups, and all
# their (recursive) ancestors. # their (recursive) ancestors.
def groups_and_ancestors_for(groups) def groups_and_ancestors_for(groups)
Gitlab::ObjectHierarchy groups_and_ancestors = if ::Feature.enabled?(:linear_group_plans_preloaded_ancestor_scopes, default_enabled: :yaml)
.new(groups) groups.self_and_ancestors
.base_and_ancestors else
Gitlab::ObjectHierarchy
.new(groups)
.base_and_ancestors
end
groups_and_ancestors
.join_gitlab_subscription .join_gitlab_subscription
.select('namespaces.id', 'namespaces.parent_id', 'gitlab_subscriptions.hosted_plan_id') .select('namespaces.id', 'namespaces.parent_id', 'gitlab_subscriptions.hosted_plan_id')
end end
......
...@@ -23,30 +23,42 @@ RSpec.describe Gitlab::GroupPlansPreloader do ...@@ -23,30 +23,42 @@ RSpec.describe Gitlab::GroupPlansPreloader do
create(:group, name: 'group-3', parent: group1) create(:group, name: 'group-3', parent: group1)
end end
it 'only executes three SQL queries to preload the data' do shared_examples 'preloading cases' do
amount = ActiveRecord::QueryRecorder it 'only executes three SQL queries to preload the data' do
.new { preloaded_groups } amount = ActiveRecord::QueryRecorder
.count .new { preloaded_groups }
.count
# One query to get the groups and their ancestors, one query to get their
# plans, and one query to _just_ get the groups. # One query to get the groups and their ancestors, one query to get their
expect(amount).to eq(3) # plans, and one query to _just_ get the groups.
end expect(amount).to eq(3)
end
it 'associates the correct plans with the correct groups' do
expect(preloaded_groups[0].plans).to match_array([plan1])
expect(preloaded_groups[1].plans).to match_array([plan2])
expect(preloaded_groups[2].plans).to match_array([plan1])
end
it 'does not execute any queries for preloaded plans' do
preloaded_groups
amount = ActiveRecord::QueryRecorder
.new { preloaded_groups.each(&:plans) }
.count
it 'associates the correct plans with the correct groups' do expect(amount).to be_zero
expect(preloaded_groups[0].plans).to eq([plan1]) end
expect(preloaded_groups[1].plans).to eq([plan2])
expect(preloaded_groups[2].plans).to eq([plan1])
end end
it 'does not execute any queries for preloaded plans' do it_behaves_like 'preloading cases'
preloaded_groups
amount = ActiveRecord::QueryRecorder context 'when feature flag :linear_group_plans_preloaded_ancestor_scopes is disabled' do
.new { preloaded_groups.each(&:plans) } before do
.count stub_feature_flags(linear_group_plans_preloaded_ancestor_scopes: false)
end
expect(amount).to be_zero it_behaves_like 'preloading cases'
end end
end end
end end
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