Commit aa7a3d48 authored by Tetiana Chupryna's avatar Tetiana Chupryna Committed by Jan Provaznik

Fix another repeating queries

parent 0289efc2
......@@ -6,7 +6,7 @@ module Labels
@current_user = current_user
@parent = parent
@available_labels = params.delete(:available_labels)
@existing_labels_by_title = params.delete(:existing_labels_by_title) || {}
@existing_labels_by_title = params.delete(:existing_labels_by_title)
@params = params.dup.with_indifferent_access
end
......@@ -45,7 +45,9 @@ module Labels
# rubocop: disable CodeReuse/ActiveRecord
def find_existing_label(title)
existing_labels_by_title[title] || available_labels.find_by(title: title)
return existing_labels_by_title[title] if existing_labels_by_title
available_labels.find_by(title: title)
end
# rubocop: enable CodeReuse/ActiveRecord
......
---
title: Fix repeating SQL queries when changing labels for a resource.
merge_request: 60718
author:
type: performance
......@@ -115,8 +115,6 @@ module API
at_least_one_of :title, :description, :start_date_fixed, :start_date_is_fixed, :due_date_fixed, :due_date_is_fixed, :labels, :add_labels, :remove_labels, :state_event, :confidential
end
put ':id/(-/)epics/:epic_iid' do
Gitlab::QueryLimiting.disable!('https://gitlab.com/gitlab-org/gitlab/issues/194104')
authorize_can_admin_epic!
# Setting updated_at is allowed only for admins and owners
......
......@@ -25,6 +25,35 @@ RSpec.describe Labels::FindOrCreateService do
project.add_developer(user)
end
context 'when existing_labels_by_title is provided' do
let(:preloaded_label) { build(:label, title: 'Security') }
before do
params.merge!(
existing_labels_by_title: {
'Security' => preloaded_label
})
end
context 'when label exists' do
it 'returns preloaded label' do
expect(service.execute).to eq preloaded_label
end
end
context 'when label does not exists' do
before do
params[:title] = 'Audit'
end
it 'does not generates additional label search' do
service.execute
expect(LabelsFinder).not_to receive(:new)
end
end
end
context 'when label does not exist at group level' do
it 'creates a new label at project level' do
expect { service.execute }.to change(project.labels, :count).by(1)
......
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