Commit 77c4fec6 authored by Jan Provaznik's avatar Jan Provaznik

Use gitlab_subscription in group preloader

`plan_id` attribute on Group model is outdated and may
not be always set. Instead we should get plan id from
namespace's gitlab_subscription.
parent 56d2fcfd
---
title: Fix loading of sub-epics caused by wrong subscription check.
merge_request: 23184
author:
type: fixed
...@@ -35,6 +35,7 @@ module EE ...@@ -35,6 +35,7 @@ module EE
accepts_nested_attributes_for :gitlab_subscription accepts_nested_attributes_for :gitlab_subscription
scope :include_gitlab_subscription, -> { includes(:gitlab_subscription) } scope :include_gitlab_subscription, -> { includes(:gitlab_subscription) }
scope :join_gitlab_subscription, -> { joins("LEFT OUTER JOIN gitlab_subscriptions ON gitlab_subscriptions.namespace_id=namespaces.id") }
scope :with_plan, -> { where.not(plan_id: nil) } scope :with_plan, -> { where.not(plan_id: nil) }
scope :with_shared_runners_minutes_limit, -> { where("namespaces.shared_runners_minutes_limit > 0") } scope :with_shared_runners_minutes_limit, -> { where("namespaces.shared_runners_minutes_limit > 0") }
scope :with_extra_shared_runners_minutes_limit, -> { where("namespaces.extra_shared_runners_minutes_limit > 0") } scope :with_extra_shared_runners_minutes_limit, -> { where("namespaces.extra_shared_runners_minutes_limit > 0") }
......
...@@ -32,7 +32,7 @@ module Gitlab ...@@ -32,7 +32,7 @@ module Gitlab
while current while current
if (plan_id = current.plan_id) if (plan_id = current.hosted_plan_id)
hash[group.id] << plan_id hash[group.id] << plan_id
all_plan_ids << plan_id all_plan_ids << plan_id
end end
...@@ -61,7 +61,8 @@ module Gitlab ...@@ -61,7 +61,8 @@ module Gitlab
Gitlab::ObjectHierarchy Gitlab::ObjectHierarchy
.new(groups) .new(groups)
.base_and_ancestors .base_and_ancestors
.select(:id, :parent_id, :plan_id) .join_gitlab_subscription
.select('namespaces.id', 'namespaces.parent_id', 'gitlab_subscriptions.hosted_plan_id')
end end
end end
end end
...@@ -14,9 +14,12 @@ describe Gitlab::GroupPlansPreloader do ...@@ -14,9 +14,12 @@ describe Gitlab::GroupPlansPreloader do
end end
before do before do
group1 = create(:group, name: 'group-1', plan_id: plan1.id) group1 = create(:group, name: 'group-1')
create(:gitlab_subscription, namespace: group1, hosted_plan_id: plan1.id)
group2 = create(:group, name: 'group-2', plan_id: plan2.id)
create(:gitlab_subscription, namespace: group2, hosted_plan_id: plan2.id)
create(:group, name: 'group-2', plan_id: plan2.id)
create(:group, name: 'group-3', parent: group1) create(:group, name: 'group-3', parent: group1)
end end
......
...@@ -77,6 +77,24 @@ describe Namespace do ...@@ -77,6 +77,24 @@ describe Namespace do
end end
end end
end end
describe '.join_gitlab_subscription' do
subject { described_class.join_gitlab_subscription.select('gitlab_subscriptions.hosted_plan_id').first.hosted_plan_id }
context 'when there is no subscription' do
it 'returns namespace with nil subscription' do
is_expected.to be_nil
end
end
context 'when there is a subscription' do
let!(:subscription) { create(:gitlab_subscription, namespace: namespace, hosted_plan_id: gold_plan.id) }
it 'returns namespace with subscription set' do
is_expected.to eq(gold_plan.id)
end
end
end
end end
context 'validation' do context 'validation' 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