Commit 88fb3219 authored by Catalin Irimie's avatar Catalin Irimie

Fix releases API N+1 in sorted_links usage

The preload for N+1 doesn't seem to work with the scoped
sorted_links. This adds inverse_of to sorted_links, to properly
preload Release::Link queries that check if a link is external.

Changelog: performance
parent 1336aedb
......@@ -13,7 +13,7 @@ class Release < ApplicationRecord
belongs_to :author, class_name: 'User'
has_many :links, class_name: 'Releases::Link'
has_many :sorted_links, -> { sorted }, class_name: 'Releases::Link'
has_many :sorted_links, -> { sorted }, class_name: 'Releases::Link', inverse_of: :release
has_many :milestone_releases
has_many :milestones, through: :milestone_releases
......
---
title: Fix releases API N+1 in sorted_links usage
merge_request: 60561
author:
type: performance
......@@ -129,19 +129,22 @@ RSpec.describe API::Releases do
expect(json_response.first['upcoming_release']).to eq(false)
end
it 'avoids N+1 queries' do
it 'avoids N+1 queries', :use_sql_query_cache do
create(:release, :with_evidence, project: project, tag: 'v0.1', author: maintainer)
create(:release_link, release: project.releases.first)
control_count = ActiveRecord::QueryRecorder.new do
control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do
get api("/projects/#{project.id}/releases", maintainer)
end.count
create_list(:release, 2, :with_evidence, project: project, tag: 'v0.1', author: maintainer)
create_list(:release, 2, project: project)
create_list(:release_link, 2, release: project.releases.first)
create_list(:release_link, 2, release: project.releases.last)
expect do
get api("/projects/#{project.id}/releases", maintainer)
end.not_to exceed_query_limit(control_count)
end.not_to exceed_all_query_limit(control_count)
end
context 'when tag does not exist in git repository' 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