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
# Returns an ActiveRecord::Relation that includes the given groups, and all
# their (recursive) ancestors.
def groups_and_ancestors_for(groups)
Gitlab::ObjectHierarchy
.new(groups)
.base_and_ancestors
groups_and_ancestors = if ::Feature.enabled?(:linear_group_plans_preloaded_ancestor_scopes, default_enabled: :yaml)
groups.self_and_ancestors
else
Gitlab::ObjectHierarchy
.new(groups)
.base_and_ancestors
end
groups_and_ancestors
.join_gitlab_subscription
.select('namespaces.id', 'namespaces.parent_id', 'gitlab_subscriptions.hosted_plan_id')
end
......
......@@ -23,30 +23,42 @@ RSpec.describe Gitlab::GroupPlansPreloader do
create(:group, name: 'group-3', parent: group1)
end
it 'only executes three SQL queries to preload the data' do
amount = ActiveRecord::QueryRecorder
.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.
expect(amount).to eq(3)
end
shared_examples 'preloading cases' do
it 'only executes three SQL queries to preload the data' do
amount = ActiveRecord::QueryRecorder
.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.
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(preloaded_groups[0].plans).to eq([plan1])
expect(preloaded_groups[1].plans).to eq([plan2])
expect(preloaded_groups[2].plans).to eq([plan1])
expect(amount).to be_zero
end
end
it 'does not execute any queries for preloaded plans' do
preloaded_groups
it_behaves_like 'preloading cases'
amount = ActiveRecord::QueryRecorder
.new { preloaded_groups.each(&:plans) }
.count
context 'when feature flag :linear_group_plans_preloaded_ancestor_scopes is disabled' do
before do
stub_feature_flags(linear_group_plans_preloaded_ancestor_scopes: false)
end
expect(amount).to be_zero
it_behaves_like 'preloading cases'
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