Commit 86267203 authored by David Fernandez's avatar David Fernandez

Merge branch 'cat-releases-sortedlinks-np1' into 'master'

Fix releases API N+1 in sorted_links usage

See merge request gitlab-org/gitlab!60561
parents f63766b3 88fb3219
...@@ -13,7 +13,7 @@ class Release < ApplicationRecord ...@@ -13,7 +13,7 @@ class Release < ApplicationRecord
belongs_to :author, class_name: 'User' belongs_to :author, class_name: 'User'
has_many :links, class_name: 'Releases::Link' 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 :milestone_releases
has_many :milestones, through: :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 ...@@ -129,19 +129,22 @@ RSpec.describe API::Releases do
expect(json_response.first['upcoming_release']).to eq(false) expect(json_response.first['upcoming_release']).to eq(false)
end 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, :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) get api("/projects/#{project.id}/releases", maintainer)
end.count end.count
create_list(:release, 2, :with_evidence, project: project, tag: 'v0.1', author: maintainer) create_list(:release, 2, :with_evidence, project: project, tag: 'v0.1', author: maintainer)
create_list(:release, 2, project: project) 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 expect do
get api("/projects/#{project.id}/releases", maintainer) 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 end
context 'when tag does not exist in git repository' do 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