diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-01-03 19:44:29 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-01-04 16:32:38 +0300 |
commit | dac51ace521d7b2b2a5a5bb19167a8690ead242e (patch) | |
tree | 2e6d20697bdfe4cc5d71d4f62075a186867236dd /spec/models/event_spec.rb | |
parent | 1dac4271798a3b9ad36c3d985a3f7740cd1c60b3 (diff) |
Eager load event target authors whenever possible
This ensures that the "author" association of an event's "target"
association is eager loaded whenever the "target" association defines an
"author" association. This in turn solves the N+1 query problem we first
tried to solve in
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/15788 but caused
problems when displaying milestones as those don't define an "author"
association.
The approach in this commit does mean that the authors are _always_
eager loaded since this takes place in the "belongs_to" block. This
however shouldn't pose too much of a problem, and as far as I can tell
there's no real way around this unfortunately.
Diffstat (limited to 'spec/models/event_spec.rb')
-rw-r--r-- | spec/models/event_spec.rb | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb index e999192940c..67f49348acb 100644 --- a/spec/models/event_spec.rb +++ b/spec/models/event_spec.rb @@ -347,6 +347,22 @@ describe Event do end end + describe '#target' do + it 'eager loads the author of an event target' do + create(:closed_issue_event) + + events = described_class.preload(:target).all.to_a + count = ActiveRecord::QueryRecorder + .new { events.first.target.author }.count + + # This expectation exists to make sure the test doesn't pass when the + # author is for some reason not loaded at all. + expect(events.first.target.author).to be_an_instance_of(User) + + expect(count).to be_zero + end + end + def create_push_event(project, user) event = create(:push_event, project: project, author: user) |