Commit 7cd5e01a authored by Maxime Orefice's avatar Maxime Orefice Committed by Mayra Cabrera

Remove cross joins for ProjectMonthlyUsage.for_namespace_monthly_usage

parent 24c48530
---
name: ci_decompose_for_namespace_monthly_usage_query
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/77952
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/350146
milestone: '14.7'
type: development
group: group::pipeline execution
default_enabled: false
...@@ -12,11 +12,18 @@ module Ci ...@@ -12,11 +12,18 @@ module Ci
scope :current_month, -> { where(date: beginning_of_month) } scope :current_month, -> { where(date: beginning_of_month) }
scope :for_namespace_monthly_usage, -> (namespace_monthly_usage) do scope :for_namespace_monthly_usage, -> (namespace_monthly_usage) do
if Feature.enabled?(:ci_decompose_for_namespace_monthly_usage_query, namespace_monthly_usage.namespace, default_enabled: :yaml)
where(
date: namespace_monthly_usage.date,
project: Ci::ProjectMirror.by_namespace_id(namespace_monthly_usage.namespace_id).select(:project_id)
)
else
where( where(
date: namespace_monthly_usage.date, date: namespace_monthly_usage.date,
project: namespace_monthly_usage.namespace.projects project: namespace_monthly_usage.namespace.projects
).allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/343301') ).allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/343301')
end end
end
def self.beginning_of_month(time = Time.current) def self.beginning_of_month(time = Time.current)
time.utc.beginning_of_month time.utc.beginning_of_month
......
...@@ -77,10 +77,12 @@ RSpec.describe Ci::Minutes::ProjectMonthlyUsage do ...@@ -77,10 +77,12 @@ RSpec.describe Ci::Minutes::ProjectMonthlyUsage do
end end
describe '.for_namespace_monthly_usage' do describe '.for_namespace_monthly_usage' do
shared_examples 'namespace monthly usage' do
let(:date_for_usage) { Date.new(2021, 5, 1) }
let(:namespace_usage) { create(:ci_namespace_monthly_usage, namespace: project.namespace, amount_used: 50, date: date_for_usage) }
it "fetches project monthly usages matching the namespace monthly usage's date and namespace" do it "fetches project monthly usages matching the namespace monthly usage's date and namespace" do
date_for_usage = Date.new(2021, 5, 1)
date_not_for_usage = date_for_usage + 1.month date_not_for_usage = date_for_usage + 1.month
namespace_usage = create(:ci_namespace_monthly_usage, namespace: project.namespace, amount_used: 50, date: date_for_usage)
matching_project_usage = create(:ci_project_monthly_usage, project: project, amount_used: 50, date: date_for_usage) matching_project_usage = create(:ci_project_monthly_usage, project: project, amount_used: 50, date: date_for_usage)
create(:ci_project_monthly_usage, project: project, amount_used: 50, date: date_not_for_usage) create(:ci_project_monthly_usage, project: project, amount_used: 50, date: date_not_for_usage)
create(:ci_project_monthly_usage, project: create(:project), amount_used: 50, date: date_for_usage) create(:ci_project_monthly_usage, project: create(:project), amount_used: 50, date: date_for_usage)
...@@ -89,5 +91,24 @@ RSpec.describe Ci::Minutes::ProjectMonthlyUsage do ...@@ -89,5 +91,24 @@ RSpec.describe Ci::Minutes::ProjectMonthlyUsage do
expect(project_usages).to contain_exactly(matching_project_usage) expect(project_usages).to contain_exactly(matching_project_usage)
end end
it 'does not join across databases' do
with_cross_joins_prevented do
described_class.for_namespace_monthly_usage(namespace_usage)
end
end
end
context 'when ci_decompose_for_namespace_monthly_usage_query is enabled' do
it_behaves_like 'namespace monthly usage'
end
context 'when ci_decompose_for_namespace_monthly_usage_query is disabled' do
before do
stub_feature_flags(ci_decompose_for_namespace_monthly_usage_query: false)
end
it_behaves_like 'namespace monthly usage'
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