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
module MergeRequestsController
extend ActiveSupport::Concern
APPROVAL_RENDERING_ACTIONS = [:approve, :approvals, :unapprove].freeze
def approve
unless merge_request.can_approve?(current_user)
return render_404
......@@ -31,6 +33,21 @@ module EE
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
super
......
......@@ -13,13 +13,13 @@ module Approvable
#
def approvals_left
[
[approvals_required - approvals.count, number_of_potential_approvers].min,
[approvals_required - approvals.size, number_of_potential_approvers].min,
0
].max
end
def approvals_required
approvals_before_merge || target_project.approvals_before_merge
@approvals_required ||= approvals_before_merge || target_project.approvals_before_merge
end
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