Skip to content
Snippets Groups Projects
Commit 0e186a05 authored by Mark Chao's avatar Mark Chao :island:
Browse files

Avoid query and model initialization

Finding id is enough for update
parent fb7c62b8
No related branches found
No related tags found
1 merge request!8669[Data migration] Sync Approver/ApproverGroup with ApprovalRule/ApprovalRuleMember
...@@ -19,8 +19,6 @@ class ApproverGroup < ActiveRecord::Base ...@@ -19,8 +19,6 @@ class ApproverGroup < ActiveRecord::Base
class ApprovalMergeRequestRule < ActiveRecord::Base class ApprovalMergeRequestRule < ActiveRecord::Base
self.table_name = 'approval_merge_request_rules' self.table_name = 'approval_merge_request_rules'
include Gitlab::Utils::StrongMemoize
belongs_to :merge_request belongs_to :merge_request
scope :code_owner, -> { where(code_owner: true) } scope :code_owner, -> { where(code_owner: true) }
scope :regular, -> { where(code_owner: false) } # Non code owner rule scope :regular, -> { where(code_owner: false) } # Non code owner rule
...@@ -57,15 +55,17 @@ class MergeRequest < ActiveRecord::Base ...@@ -57,15 +55,17 @@ class MergeRequest < ActiveRecord::Base
belongs_to :target_project, class_name: "Project" belongs_to :target_project, class_name: "Project"
has_many :approval_rules, class_name: 'ApprovalMergeRequestRule' has_many :approval_rules, class_name: 'ApprovalMergeRequestRule'
def approvers def approver_ids
Approver.where(target_type: 'MergeRequest', target_id: id) @approver_ids ||= Approver.where(target_type: 'MergeRequest', target_id: id).pluck(:user_id)
end end
def approver_groups def approver_group_ids
ApproverGroup.where(target_type: 'MergeRequest', target_id: id) @approver_group_ids ||= ApproverGroup.where(target_type: 'MergeRequest', target_id: id).pluck(:group_id)
end end
def sync_code_owners_with_approvers def sync_code_owners_with_approvers
return if state == 'merged' || state == 'closed'
Gitlab::GitalyClient.allow_n_plus_1_calls do Gitlab::GitalyClient.allow_n_plus_1_calls do
::MergeRequest.find(id).sync_code_owners_with_approvers ::MergeRequest.find(id).sync_code_owners_with_approvers
end end
...@@ -77,12 +77,12 @@ class Project < ActiveRecord::Base ...@@ -77,12 +77,12 @@ class Project < ActiveRecord::Base
has_many :approval_rules, class_name: 'ApprovalProjectRule' has_many :approval_rules, class_name: 'ApprovalProjectRule'
def approvers def approver_ids
Approver.where(target_type: 'Project', target_id: id) @approver_ids ||= Approver.where(target_type: 'Project', target_id: id).pluck(:user_id)
end end
def approver_groups def approver_group_ids
ApproverGroup.where(target_type: 'Project', target_id: id) @approver_group_ids ||= ApproverGroup.where(target_type: 'Project', target_id: id).pluck(:group_id)
end end
end end
...@@ -131,8 +131,8 @@ def sync_rule ...@@ -131,8 +131,8 @@ def sync_rule
end end
rule = find_or_create_rule rule = find_or_create_rule
rule.user_ids = target.approvers.pluck(:user_id) rule.user_ids = target.approver_ids
rule.group_ids = target.approver_groups.pluck(:group_id) rule.group_ids = target.approver_group_ids
rule rule
end end
...@@ -159,7 +159,7 @@ def find_or_create_rule ...@@ -159,7 +159,7 @@ def find_or_create_rule
end end
def approvers_exists? def approvers_exists?
target.approvers.to_a.any? || target.approver_groups.to_a.any? target.approver_ids.any? || target.approver_group_ids.any?
end end
end end
end end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment