Skip to content
代码片段 群组 项目
提交 603e93a4 编辑于 作者: David Kim's avatar David Kim
浏览文件

Reduce N+1 queries when publishing draft notes

Use noteable instead to avoid reloading it multiple times
上级 3a40f345
No related branches found
No related tags found
无相关合并请求
......@@ -5,7 +5,7 @@ class DraftNote < ApplicationRecord
include Sortable
include ShaAttribute
PUBLISH_ATTRS = %i[noteable_id noteable_type type note internal].freeze
PUBLISH_ATTRS = %i[noteable type note internal].freeze
DIFF_ATTRS = %i[position original_position change_position commit_id].freeze
sha_attribute :commit_id
......
......@@ -20,7 +20,11 @@ def execute
discussion = discussion.convert_to_discussion! if discussion.can_convert_to_discussion?
params.merge!(discussion.reply_attributes)
reply_attributes = discussion.reply_attributes
# NOTE: Avoid overriding noteable if it already exists so that we don't have to reload noteable.
reply_attributes = reply_attributes.except(:noteable_id, :noteable_type) if params[:noteable]
params.merge!(reply_attributes)
end
# The `confidential` param for notes is deprecated with 15.3
......
......@@ -224,19 +224,17 @@ def publish(draft: nil)
end
end
context 'with many draft notes', :use_sql_query_cache do
context 'with many draft notes', :use_sql_query_cache, :request_store do
let(:merge_request) { create(:merge_request) }
it 'reduce N+1 queries' do
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 1')
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 2')
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 3')
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 4')
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 5')
5.times do
create(:draft_note_on_discussion, merge_request: merge_request, author: user, note: 'some note')
end
recorder = ActiveRecord::QueryRecorder.new(skip_cached: false) { publish }
expect(recorder.count).not_to be > 186
expect(recorder.count).not_to be > 105
end
end
......@@ -303,6 +301,7 @@ def update_file(file_path, new_content)
refresh = MergeRequests::RefreshService.new(project: project, current_user: user)
refresh.execute(oldrev, newrev, merge_request.source_branch_ref)
merge_request.reload
expect { publish(draft: draft) }.to change { Suggestion.count }.by(1)
.and change { DiffNote.count }.from(0).to(1)
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册