Commit 2d7b79bf authored by Jan Provaznik's avatar Jan Provaznik

Merge branch 'issue_237977-2' into 'master'

Populate blocking issues count

See merge request gitlab-org/gitlab!45176
parents eb1b194f e573bf1e
---
title: Populate blocking issues count
merge_request: 45176
author:
type: other
# frozen_string_literal: true
require 'set'
class ScheduleSyncBlockingIssuesCount < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
BATCH_SIZE = 50
DELAY_INTERVAL = 120.seconds.to_i
MIGRATION = 'SyncBlockingIssuesCount'.freeze
disable_ddl_transaction!
class TmpIssueLink < ActiveRecord::Base
self.table_name = 'issue_links'
include EachBatch
end
def up
return unless Gitlab.ee?
issue_link_ids = SortedSet.new
TmpIssueLink.distinct.select(:source_id).where(link_type: 1).each_batch(of: 1000, column: :source_id) do |query|
issue_link_ids.merge(query.pluck(:source_id))
end
TmpIssueLink.distinct.select(:target_id).where(link_type: 2).each_batch(of: 1000, column: :target_id) do |query|
issue_link_ids.merge(query.pluck(:target_id))
end
issue_link_ids.each_slice(BATCH_SIZE).with_index do |items, index|
start_id, *, end_id = items
arguments = [start_id, end_id]
final_delay = DELAY_INTERVAL * (index + 1)
migrate_in(final_delay, MIGRATION, arguments)
end
end
def down
# NO OP
end
end
aed103bb25b70eb8f6387d84225a8e51672a83c4586ccc65da3011ef010da4b1
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20201014142521_schedule_sync_blocking_issues_count')
RSpec.describe ScheduleSyncBlockingIssuesCount do
let(:issues) { table(:issues) }
let(:links) { table(:issue_links) }
let(:migration) { described_class::MIGRATION }
# Blocking issues
let!(:issue_1) { issues.create!(description: 'blocking 1', state_id: 1) }
let!(:issue_2) { issues.create!(description: 'blocking 2', state_id: 1) }
let!(:issue_3) { issues.create!(description: 'blocking 3', state_id: 2) }
let!(:issue_4) { issues.create!(description: 'blocking 4', state_id: 1 ) }
# Blocked issues
let!(:issue_5) { issues.create!(description: 'blocked 1', state_id: 1) }
let!(:issue_6) { issues.create!(description: 'blocked 2', state_id: 1) }
let!(:issue_7) { issues.create!(description: 'blocked 3', state_id: 1) }
let!(:issue_8) { issues.create!(description: 'blocked 4', state_id: 1) }
let!(:issue_9) { issues.create!(description: 'blocked 5', state_id: 1) }
let!(:issue_10) { issues.create!(description: 'blocked 6', state_id: 1) }
before do
stub_const("#{described_class.name}::BATCH_SIZE", 2)
# Issue links
# -----------
# issue_1 blocks two issues with link_type BLOCKS
# issue_2 blocks two issues with link_type IS_BLOCKED_BY
# issue_3 blocks one issue with link_type IS_BLOCKED_BY but it is closed
# issue_4 blocks one issue with link_type BLOCKS
links.create!(link_type: 1, source_id: issue_1.id, target_id: issue_5.id)
links.create!(link_type: 1, source_id: issue_1.id, target_id: issue_6.id)
links.create!(link_type: 2, source_id: issue_7.id, target_id: issue_2.id)
links.create!(link_type: 2, source_id: issue_8.id, target_id: issue_2.id)
links.create!(link_type: 1, source_id: issue_4.id, target_id: issue_9.id)
links.create!(link_type: 2, source_id: issue_10.id, target_id: issue_3.id)
end
context 'scheduling migrations' do
before do
Sidekiq::Worker.clear_all
end
it 'correctly schedules issuable sync background migration' do
Sidekiq::Testing.fake! do
freeze_time do
migrate!
expect(migration).to be_scheduled_delayed_migration(120.seconds, issue_1.id, issue_2.id)
expect(migration).to be_scheduled_delayed_migration(240.seconds, issue_3.id, issue_4.id)
expect(BackgroundMigrationWorker.jobs.size).to eq(2)
end
end
end
end
context 'running background migration' do
it 'correctly populates issues blocking_issues_count', :sidekiq_might_not_need_inline do
migrate!
expect(issue_1.reload.blocking_issues_count).to eq(2)
expect(issue_2.reload.blocking_issues_count).to eq(2)
expect(issue_3.reload.blocking_issues_count).to eq(0)
expect(issue_4.reload.blocking_issues_count).to eq(1)
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