Commit 184807b2 authored by Igor Drozdov's avatar Igor Drozdov

Add cleanup migration for MR's mutliple assignees

The migration steals the remaining background jobs
of populating MRs with assignees, executes them
synchronously and then makes sure that all the
assignees are migrated
parent 775910d3
---
title: Add cleanup migration for MR's multiple assignees
merge_request: 30261
author:
type: changed
# frozen_string_literal: true
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class PopulateRemainingMergeRequestAssignees < ActiveRecord::Migration[5.1]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
BATCH_SIZE = 10_000
MIGRATION = 'PopulateMergeRequestAssigneesTable'
disable_ddl_transaction!
def up
Gitlab::BackgroundMigration.steal(MIGRATION)
Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable.new.perform_all_sync(batch_size: BATCH_SIZE)
end
end
......@@ -18,6 +18,14 @@ module Gitlab
execute("INSERT INTO merge_request_assignees (merge_request_id, user_id) #{select_sql}")
end
def perform_all_sync(batch_size:)
MergeRequest.each_batch(of: batch_size) do |batch|
range = batch.pluck('MIN(id)', 'MAX(id)').first
perform(*range)
end
end
private
def merge_request_assignees_not_exists_clause
......
......@@ -27,14 +27,19 @@ describe Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable, :migra
merge_requests.create(params)
end
before do
create_merge_request(2, assignee_id: user.id)
create_merge_request(3, assignee_id: user_2.id)
create_merge_request(4, assignee_id: user_3.id)
# Test filtering MRs without assignees
create_merge_request(5, assignee_id: nil)
# Test filtering already migrated row
merge_request_assignees.create!(merge_request_id: 2, user_id: user_3.id)
end
describe '#perform' do
it 'creates merge_request_assignees rows according to merge_requests' do
create_merge_request(2, assignee_id: user.id)
create_merge_request(3, assignee_id: user_2.id)
create_merge_request(4, assignee_id: user_3.id)
# Test filtering already migrated row
merge_request_assignees.create!(merge_request_id: 2, user_id: user_3.id)
subject.perform(1, 4)
rows = merge_request_assignees.order(:id).map { |row| row.attributes.slice('merge_request_id', 'user_id') }
......@@ -53,4 +58,13 @@ describe Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable, :migra
end
end
end
describe '#perform_all_sync' do
it 'executes peform for all merge requests in batches' do
expect(subject).to receive(:perform).with(2, 4).ordered
expect(subject).to receive(:perform).with(5, 5).ordered
subject.perform_all_sync(batch_size: 3)
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