Commit 25b0543c authored by Bob Van Landuyt's avatar Bob Van Landuyt

Optimize number of queries for approvals

This gets the number of queries down using some simple memoization and
eager loading queries.
parent 21a8bf41
---
title: Speed up approvals calculations
merge_request: 4492
author:
type: performance
...@@ -3,6 +3,8 @@ module EE ...@@ -3,6 +3,8 @@ module EE
module MergeRequestsController module MergeRequestsController
extend ActiveSupport::Concern extend ActiveSupport::Concern
APPROVAL_RENDERING_ACTIONS = [:approve, :approvals, :unapprove].freeze
def approve def approve
unless merge_request.can_approve?(current_user) unless merge_request.can_approve?(current_user)
return render_404 return render_404
...@@ -31,6 +33,21 @@ module EE ...@@ -31,6 +33,21 @@ module EE
protected protected
# rubocop:disable Gitlab/ModuleWithInstanceVariables
# Assigning both @merge_request and @issuable like in
# `Projects::MergeRequests::ApplicationController`, and calling super if
# we don't need the extra includes requires us to disable this cop.
def merge_request
return super unless APPROVAL_RENDERING_ACTIONS.include?(action_name.to_sym)
@issuable = @merge_request ||= project.merge_requests
.includes(approvals: :user,
approvers: :user,
approver_groups: :group)
.find_by!(iid: params[:id])
end
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def define_edit_vars def define_edit_vars
super super
......
...@@ -13,13 +13,13 @@ module Approvable ...@@ -13,13 +13,13 @@ module Approvable
# #
def approvals_left def approvals_left
[ [
[approvals_required - approvals.count, number_of_potential_approvers].min, [approvals_required - approvals.size, number_of_potential_approvers].min,
0 0
].max ].max
end end
def approvals_required def approvals_required
approvals_before_merge || target_project.approvals_before_merge @approvals_required ||= approvals_before_merge || target_project.approvals_before_merge
end end
def approvals_before_merge def approvals_before_merge
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment