diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-08-04 16:44:44 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-08-07 13:38:32 +0300 |
commit | 4a915c739d502a8b1d2a019f4352d46a3be3d7e0 (patch) | |
tree | 99c4e8a7d7557f7d128999ea1ae91f913f28c9ce | |
parent | f77fda6437bfeb946510abdf5c56872af392f624 (diff) |
Remove redundant query from User#recent_push
The "events" table has a foreign key on "events.project_id" with a
cascading delete. As such it's impossible for an event to have a
non-existing project ID.
-rw-r--r-- | app/models/user.rb | 18 | ||||
-rw-r--r-- | changelogs/unreleased/remove-redundant-query-when-retrieving-recent-pushes.yml | 4 |
2 files changed, 12 insertions, 10 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 267eebb42ff..afcadfe484e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -646,16 +646,14 @@ class User < ActiveRecord::Base events = events.where(project_id: project_ids) if project_ids # Use the latest event that has not been pushed or merged recently - events.recent.find do |event| - project = Project.find_by_id(event.project_id) - next unless project - - if project.repository.branch_exists?(event.branch_name) - merge_requests = MergeRequest.where("created_at >= ?", event.created_at) - .where(source_project_id: project.id, - source_branch: event.branch_name) - merge_requests.empty? - end + events.includes(:project).recent.find do |event| + next unless event.project.repository.branch_exists?(event.branch_name) + + merge_requests = MergeRequest.where("created_at >= ?", event.created_at) + .where(source_project_id: event.project.id, + source_branch: event.branch_name) + + merge_requests.empty? end end diff --git a/changelogs/unreleased/remove-redundant-query-when-retrieving-recent-pushes.yml b/changelogs/unreleased/remove-redundant-query-when-retrieving-recent-pushes.yml new file mode 100644 index 00000000000..83934217e6a --- /dev/null +++ b/changelogs/unreleased/remove-redundant-query-when-retrieving-recent-pushes.yml @@ -0,0 +1,4 @@ +--- +title: Remove redundant query when retrieving the most recent push of a user +merge_request: +author: |