Merge branch 'dm-discussions-n-plus-1' into 'master'
Resolve N+1 query issue with discussions Closes #33013 See merge request !11775
This commit is contained in:
commit
69e31003be
6 changed files with 28 additions and 3 deletions
|
@ -43,7 +43,12 @@ module Noteable
|
|||
end
|
||||
|
||||
def resolvable_discussions
|
||||
@resolvable_discussions ||= discussion_notes.resolvable.discussions(self)
|
||||
@resolvable_discussions ||=
|
||||
if defined?(@discussions)
|
||||
@discussions.select(&:resolvable?)
|
||||
else
|
||||
discussion_notes.resolvable.discussions(self)
|
||||
end
|
||||
end
|
||||
|
||||
def discussions_resolvable?
|
||||
|
|
|
@ -21,7 +21,8 @@ class Discussion
|
|||
end
|
||||
|
||||
def self.build_collection(notes, context_noteable = nil)
|
||||
notes.group_by { |n| n.discussion_id(context_noteable) }.values.map { |notes| build(notes, context_noteable) }
|
||||
grouped_notes = notes.group_by { |n| n.discussion_id(context_noteable) }
|
||||
grouped_notes.values.map { |notes| build(notes, context_noteable) }
|
||||
end
|
||||
|
||||
# Returns an alphanumeric discussion ID based on `build_discussion_id`
|
||||
|
|
|
@ -110,7 +110,7 @@ class Note < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def discussions(context_noteable = nil)
|
||||
Discussion.build_collection(fresh, context_noteable)
|
||||
Discussion.build_collection(all.includes(:noteable).fresh, context_noteable)
|
||||
end
|
||||
|
||||
def find_discussion(discussion_id)
|
||||
|
|
4
changelogs/unreleased/dm-discussions-n-plus-1.yml
Normal file
4
changelogs/unreleased/dm-discussions-n-plus-1.yml
Normal file
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
title: Resolve N+1 query issue with discussions
|
||||
merge_request:
|
||||
author:
|
15
config/initializers/active_record_preloader.rb
Normal file
15
config/initializers/active_record_preloader.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module ActiveRecord
|
||||
module Associations
|
||||
class Preloader
|
||||
module NoCommitPreloader
|
||||
def preloader_for(reflection, owners, rhs_klass)
|
||||
return NullPreloader if rhs_klass == ::Commit
|
||||
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
prepend NoCommitPreloader
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue