Commit 59f7750d authored by Igor's avatar Igor Committed by Andreas Brandl

Fix any-approver rule migration job failure

Currently it fails due to writing a value
which is out of the smallint range
parent 39b5d506
......@@ -5,13 +5,15 @@ module Gitlab
# This background migration creates any approver rule records according
# to the given merge request IDs range. A _single_ INSERT is issued for the given range.
class PopulateAnyApprovalRuleForMergeRequests
MAX_VALUE = 2**15 - 1
def perform(from_id, to_id)
select_sql =
MergeRequest
.where(merge_request_approval_rules_not_exists_clause)
.where(id: from_id..to_id)
.where('approvals_before_merge <> 0')
.select("id, approvals_before_merge, created_at, updated_at, 4, '#{ApprovalRuleLike::ALL_MEMBERS}'")
.select("id, LEAST(#{MAX_VALUE}, approvals_before_merge), created_at, updated_at, 4, '#{ApprovalRuleLike::ALL_MEMBERS}'")
.to_sql
execute("INSERT INTO approval_merge_request_rules (merge_request_id, approvals_required, created_at, updated_at, rule_type, name) #{select_sql}")
......
......@@ -5,13 +5,15 @@ module Gitlab
# This background migration creates any approver rule records according
# to the given project IDs range. A _single_ INSERT is issued for the given range.
class PopulateAnyApprovalRuleForProjects
MAX_VALUE = 2**15 - 1
def perform(from_id, to_id)
select_sql =
Project
.where(project_approval_rules_not_exists_clause)
.where(id: from_id..to_id)
.where('approvals_before_merge <> 0')
.select("id, approvals_before_merge, created_at, updated_at, 4, '#{ApprovalRuleLike::ALL_MEMBERS}'")
.select("id, LEAST(#{MAX_VALUE}, approvals_before_merge), created_at, updated_at, 4, '#{ApprovalRuleLike::ALL_MEMBERS}'")
.to_sql
execute("INSERT INTO approval_project_rules (project_id, approvals_required, created_at, updated_at, rule_type, name) #{select_sql}")
......
......@@ -29,30 +29,34 @@ describe Gitlab::BackgroundMigration::PopulateAnyApprovalRuleForMergeRequests, :
# Test filtering already migrated rows
create_merge_request(4, approvals_before_merge: 3)
approval_merge_request_rules.create(id: 3,
approval_merge_request_rules.create(id: 4,
merge_request_id: 4, approvals_required: 3, rule_type: 4, name: ApprovalRuleLike::ALL_MEMBERS)
# Test filtering MRs with existing rules
create_merge_request(5, approvals_before_merge: 3)
approval_merge_request_rules.create(id: 4,
approval_merge_request_rules.create(id: 5,
merge_request_id: 5, approvals_required: 3, rule_type: 1, name: 'Regular rules')
create_merge_request(6, approvals_before_merge: 5)
# Test filtering rows with zero approvals_before_merge column
create_merge_request(7, approvals_before_merge: 0)
# Test rows with too big approvals_before_merge value
create_merge_request(8, approvals_before_merge: 2**30)
end
describe '#perform' do
it 'creates approval_merge_request_rules rows according to merge_requests' do
expect { subject.perform(1, 7) }.to change(ApprovalMergeRequestRule, :count).by(2)
expect { subject.perform(1, 8) }.to change(ApprovalMergeRequestRule, :count).by(3)
created_rows = [
{ 'merge_request_id' => 2, 'approvals_required' => 2 },
{ 'merge_request_id' => 6, 'approvals_required' => 5 }
]
existing_rows = [
{ 'merge_request_id' => 4, 'approvals_required' => 3 }
{ 'merge_request_id' => 4, 'approvals_required' => 3 },
{ 'merge_request_id' => 8, 'approvals_required' => 2**15 - 1 }
]
rows = approval_merge_request_rules.where(rule_type: 4).order(:id).map do |row|
......
......@@ -22,27 +22,29 @@ describe Gitlab::BackgroundMigration::PopulateAnyApprovalRuleForProjects, :migra
# Test filtering already migrated rows
create_project(4, approvals_before_merge: 3)
approval_project_rules.create(id: 3,
approval_project_rules.create(id: 4,
project_id: 4, approvals_required: 3, rule_type: 4, name: ApprovalRuleLike::ALL_MEMBERS)
# Test filtering MRs with existing rules
create_project(5, approvals_before_merge: 3)
approval_project_rules.create(id: 4,
approval_project_rules.create(id: 5,
project_id: 5, approvals_required: 3, rule_type: 1, name: 'Regular rules')
create_project(6, approvals_before_merge: 5)
create_project(7, approvals_before_merge: 2**30)
end
describe '#perform' do
it 'creates approval_project_rules rows according to projects' do
expect { subject.perform(1, 6) }.to change(ApprovalProjectRule, :count).by(2)
expect { subject.perform(1, 7) }.to change(ApprovalProjectRule, :count).by(3)
created_rows = [
{ 'project_id' => 2, 'approvals_required' => 2 },
{ 'project_id' => 6, 'approvals_required' => 5 }
]
existing_rows = [
{ 'project_id' => 4, 'approvals_required' => 3 }
{ 'project_id' => 4, 'approvals_required' => 3 },
{ 'project_id' => 7, 'approvals_required' => 2**15 - 1 }
]
rows = approval_project_rules.where(rule_type: 4).order(:id).map do |row|
......
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