Skip to content
代码片段 群组 项目
未验证 提交 3b3db273 编辑于 作者: Thong Kuah's avatar Thong Kuah 提交者: GitLab
浏览文件

Merge branch...

Merge branch '444592-investigate-and-reduce-n-1-sql-queries-when-publishing-multiple-draft-notes-2' into 'master' 

Reduce N+1 queries when publishing draft notes

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/152323



Merged-by: default avatarThong Kuah <tkuah@gitlab.com>
Approved-by: default avatarHeinrich Lee Yu <heinrich@gitlab.com>
Approved-by: default avatarThong Kuah <tkuah@gitlab.com>
Reviewed-by: default avatarSincheol (David) Kim <dkim@gitlab.com>
Co-authored-by: default avatarDavid Kim <dkim@gitlab.com>
No related branches found
No related tags found
无相关合并请求
...@@ -5,7 +5,7 @@ class DraftNote < ApplicationRecord ...@@ -5,7 +5,7 @@ class DraftNote < ApplicationRecord
include Sortable include Sortable
include ShaAttribute 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 DIFF_ATTRS = %i[position original_position change_position commit_id].freeze
sha_attribute :commit_id sha_attribute :commit_id
......
...@@ -20,7 +20,11 @@ def execute ...@@ -20,7 +20,11 @@ def execute
discussion = discussion.convert_to_discussion! if discussion.can_convert_to_discussion? 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 end
# The `confidential` param for notes is deprecated with 15.3 # The `confidential` param for notes is deprecated with 15.3
......
...@@ -224,19 +224,17 @@ def publish(draft: nil) ...@@ -224,19 +224,17 @@ def publish(draft: nil)
end end
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) } let(:merge_request) { create(:merge_request) }
it 'reduce N+1 queries' do it 'reduce N+1 queries' do
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 1') 5.times do
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 2') create(:draft_note_on_discussion, merge_request: merge_request, author: user, note: 'some note')
create(:draft_note_on_text_diff, merge_request: merge_request, author: user, note: 'note 3') end
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')
recorder = ActiveRecord::QueryRecorder.new(skip_cached: false) { publish } recorder = ActiveRecord::QueryRecorder.new(skip_cached: false) { publish }
expect(recorder.count).not_to be > 186 expect(recorder.count).not_to be > 105
end end
end end
...@@ -303,6 +301,7 @@ def update_file(file_path, new_content) ...@@ -303,6 +301,7 @@ def update_file(file_path, new_content)
refresh = MergeRequests::RefreshService.new(project: project, current_user: user) refresh = MergeRequests::RefreshService.new(project: project, current_user: user)
refresh.execute(oldrev, newrev, merge_request.source_branch_ref) refresh.execute(oldrev, newrev, merge_request.source_branch_ref)
merge_request.reload
expect { publish(draft: draft) }.to change { Suggestion.count }.by(1) expect { publish(draft: draft) }.to change { Suggestion.count }.by(1)
.and change { DiffNote.count }.from(0).to(1) .and change { DiffNote.count }.from(0).to(1)
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册