Commit 5ea899d3 authored by Andreas Brandl's avatar Andreas Brandl

Merge branch 'id-clean-up-mr-assignees-migration' into 'master'

Add cleanup migration for MR's multiple assignees

See merge request gitlab-org/gitlab-ce!30269
parents 90715495 184807b2
---
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 ...@@ -18,6 +18,14 @@ module Gitlab
execute("INSERT INTO merge_request_assignees (merge_request_id, user_id) #{select_sql}") execute("INSERT INTO merge_request_assignees (merge_request_id, user_id) #{select_sql}")
end 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 private
def merge_request_assignees_not_exists_clause def merge_request_assignees_not_exists_clause
......
...@@ -27,14 +27,19 @@ describe Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable, :migra ...@@ -27,14 +27,19 @@ describe Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable, :migra
merge_requests.create(params) merge_requests.create(params)
end 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 describe '#perform' do
it 'creates merge_request_assignees rows according to merge_requests' 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) subject.perform(1, 4)
rows = merge_request_assignees.order(:id).map { |row| row.attributes.slice('merge_request_id', 'user_id') } 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 ...@@ -53,4 +58,13 @@ describe Gitlab::BackgroundMigration::PopulateMergeRequestAssigneesTable, :migra
end end
end 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 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