Commit d79b7d3f authored by Mark Chao's avatar Mark Chao

Bulk migrate multiple project's MR

This speeds up migration, as previously each project has its own job,
now they can be in a batch, avoiding 2 minutes wait interval per job.
parent 489c337b
...@@ -53,7 +53,6 @@ module Gitlab ...@@ -53,7 +53,6 @@ module Gitlab
class MergeRequest < ActiveRecord::Base class MergeRequest < ActiveRecord::Base
self.table_name = 'merge_requests' self.table_name = 'merge_requests'
include ::EachBatch
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'
...@@ -74,7 +73,6 @@ module Gitlab ...@@ -74,7 +73,6 @@ module Gitlab
class Project < ActiveRecord::Base class Project < ActiveRecord::Base
self.table_name = 'projects' self.table_name = 'projects'
has_many :merge_requests, foreign_key: 'target_project_id', inverse_of: :target_project
has_many :approval_rules, class_name: 'ApprovalProjectRule' has_many :approval_rules, class_name: 'ApprovalProjectRule'
def approvers def approvers
...@@ -124,8 +122,6 @@ module Gitlab ...@@ -124,8 +122,6 @@ module Gitlab
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
sync_rule sync_rule
end end
schedule_to_migrate_merge_requests(target)
end end
def sync_rule def sync_rule
...@@ -140,14 +136,6 @@ module Gitlab ...@@ -140,14 +136,6 @@ module Gitlab
rule rule
end end
def schedule_to_migrate_merge_requests(project)
jobs = []
project.merge_requests.each_batch do |scope, _|
jobs << ['MigrateApproverToApprovalRulesInBatch', ['MergeRequest', scope.pluck(:id)]]
end
BackgroundMigrationWorker.bulk_perform_async(jobs)
end
def target def target
strong_memoize(:target) do strong_memoize(:target) do
case @target_type case @target_type
......
...@@ -3,10 +3,27 @@ ...@@ -3,10 +3,27 @@
module Gitlab module Gitlab
module BackgroundMigration module BackgroundMigration
class MigrateApproverToApprovalRulesInBatch class MigrateApproverToApprovalRulesInBatch
class MergeRequest < ActiveRecord::Base
self.table_name = 'merge_requests'
include ::EachBatch
end
def perform(target_type, target_ids) def perform(target_type, target_ids)
target_ids.each do |target_id| target_ids.each do |target_id|
MigrateApproverToApprovalRules.new.perform(target_type, target_id) MigrateApproverToApprovalRules.new.perform(target_type, target_id)
end end
schedule_to_migrate_merge_requests(target_ids) if target_type == 'Project'
end
private
def schedule_to_migrate_merge_requests(project_ids)
jobs = []
MergeRequest.where(target_project_id: project_ids).each_batch do |scope, _|
jobs << [self.class.name, ['MergeRequest', scope.pluck(:id)]]
end
BackgroundMigrationWorker.bulk_perform_async(jobs) unless jobs.empty?
end end
end end
end end
......
...@@ -6,12 +6,36 @@ describe Gitlab::BackgroundMigration::MigrateApproverToApprovalRulesInBatch do ...@@ -6,12 +6,36 @@ describe Gitlab::BackgroundMigration::MigrateApproverToApprovalRulesInBatch do
context 'when there is no more MigrateApproverToApprovalRules jobs' do context 'when there is no more MigrateApproverToApprovalRules jobs' do
let(:job) { double(:job) } let(:job) { double(:job) }
it 'enables feature' do it 'migrates individual target' do
allow(Gitlab::BackgroundMigration::MigrateApproverToApprovalRules).to receive(:new).and_return(job) allow(Gitlab::BackgroundMigration::MigrateApproverToApprovalRules).to receive(:new).and_return(job)
expect(job).to receive(:perform).exactly(3).times expect(job).to receive(:perform).exactly(3).times
described_class.new.perform('Foo', [1, 2, 3]) described_class.new.perform('Foo', [1, 2, 3])
end end
context 'when targets are projects' do
let(:projects) { create_list(:project, 3) }
context 'when projects contain merge requests' do
it 'schedules migrations for merge requests' do
merge_requests = projects.flat_map do |project|
create(:merge_request, source_project: project, target_project: project)
end
expect(BackgroundMigrationWorker).to receive(:bulk_perform_async).with([[described_class.name, ["MergeRequest", merge_requests.map(&:id)]]])
described_class.new.perform('Project', projects.map(&:id))
end
end
context 'when merge request do not exist' do
it 'does nothing' do
expect(BackgroundMigrationWorker).not_to receive(:bulk_perform_async)
described_class.new.perform('Project', projects.map(&:id))
end
end
end
end end
end end
...@@ -178,16 +178,6 @@ describe Gitlab::BackgroundMigration::MigrateApproverToApprovalRules do ...@@ -178,16 +178,6 @@ describe Gitlab::BackgroundMigration::MigrateApproverToApprovalRules do
let(:approval_rule) { create(:approval_project_rule, project: target) } let(:approval_rule) { create(:approval_project_rule, project: target) }
it_behaves_like 'sync approval member' it_behaves_like 'sync approval member'
context 'when project contains some merge requests' do
let!(:merge_request) { create(:merge_request, source_project: target, target_project: target) }
it 'schedules migrations for all its merge requests' do
expect(BackgroundMigrationWorker).to receive(:bulk_perform_async).with([['MigrateApproverToApprovalRulesInBatch', ["MergeRequest", [merge_request.id]]]])
described_class.new.perform(target.class.name, target.id)
end
end
end end
end end
end end
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