Commit 239dd6a1 authored by Dylan Griffith's avatar Dylan Griffith

Use _subscriptions join table to count upstream/downstream projects

Prior to this change we were joining through `upstream_subscriptions`
and `downstream_subscriptions` relations to count `upstream_projects`
and `downstream_projects`. This uses the `ci_subscriptions_projects`
table. Since we've added `disable_joins` to these
`has_many ... through: ...` relations these queries will now be less
efficient. So instead we've simplified the query to not join at all as
the `ci_subscriptions_projects.upstream_project_id` and
`ci_subscriptions_projects.downstream_project_id` are already both
`NOT NULL` foreign keys which means we already know for sure that
counting the `ci_subscriptions_projects` rows is equivalent to counting
the `projects` rows.
parent a12f5d74
......@@ -191,7 +191,7 @@ module EE
def project_has_subscriptions?
project.feature_available?(:ci_project_subscriptions) &&
project.downstream_projects.any?
project.downstream_project_subscriptions.any?
end
def merge_train_ref?
......
......@@ -794,6 +794,14 @@ module EE
available_features[feature]
end
def upstream_projects_count
upstream_project_subscriptions.count
end
def downstream_projects_count
downstream_project_subscriptions.count
end
def merge_pipelines_enabled?
return false unless ci_cd_settings
......
......@@ -13,7 +13,7 @@
%h5
= _("Subscriptions")
%span.badge.badge-pill
= @project.upstream_projects.count + @project.downstream_projects.count
= @project.upstream_projects_count + @project.downstream_projects_count
%table.table.gl-mt-3
%thead
......
......@@ -3006,6 +3006,18 @@ RSpec.describe Project do
end
end
describe '#upstream_projects_count' do
it 'returns the upstream projects count' do
primary_project = create(:project, :public)
upstream_projects = create_list(:project, 2, :public)
primary_project.upstream_projects = upstream_projects
with_cross_joins_prevented do
expect(primary_project.upstream_projects_count).to eq(2)
end
end
end
describe '#downstream_projects' do
it 'returns the downstream projects' do
primary_project = create(:project, :public)
......@@ -3018,6 +3030,18 @@ RSpec.describe Project do
end
end
describe '#downstream_projects_count' do
it 'returns the downstream projects count' do
primary_project = create(:project, :public)
downstream_projects = create_list(:project, 2, :public)
primary_project.downstream_projects = downstream_projects
with_cross_joins_prevented do
expect(primary_project.downstream_projects_count).to eq(2)
end
end
end
describe '#vulnerability_report_rule' do
subject { project.vulnerability_report_rule }
......
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