Commit 840cb644 authored by Mark Chao's avatar Mark Chao

Add migration progress checker

parent 2714d05a
...@@ -16,7 +16,10 @@ class MigrateProjectApprovers < ActiveRecord::Migration[5.0] ...@@ -16,7 +16,10 @@ class MigrateProjectApprovers < ActiveRecord::Migration[5.0]
Project.each_batch(of: BATCH_SIZE) do |scope, _| Project.each_batch(of: BATCH_SIZE) do |scope, _|
jobs << ['MigrateApproverToApprovalRulesInBatch', ['Project', scope.pluck(:id)]] jobs << ['MigrateApproverToApprovalRulesInBatch', ['Project', scope.pluck(:id)]]
end end
BackgroundMigration.bulk_perform_async(jobs) BackgroundMigrationWorker.bulk_perform_async(jobs)
check_time = Gitlab::BackgroundMigration::MigrateApproverToApprovalRulesCheckProgress::RESCHEDULE_DELAY
BackgroundMigrationWorker.bulk_perform_in(check_time, [['MigrateApproverToApprovalRulesCheckProgress']])
end end
def down def down
......
# frozen_string_literal: true
module Gitlab
module BackgroundMigration
class MigrateApproverToApprovalRulesCheckProgress
RESCHEDULE_DELAY = 1.day
def perform
if remaining('MergeRequest') == 0 && remaining('Project') == 0
Feature.enable(:approval_rule)
else
BackgroundMigrationWorker.perform_in(RESCHEDULE_DELAY, self.class.name)
end
end
private
def remaining(class_name)
target_type = ActiveRecord::Base.connection.quote(class_name)
sql_old_schema = <<-SQL.strip_heredoc
SELECT count(*) FROM (
SELECT target_id FROM "approvers" WHERE "approvers"."target_type" = #{target_type}
UNION
SELECT target_id FROM "approver_groups" WHERE "approver_groups"."target_type" = #{target_type}
) AS target_count
SQL
sql_new_schema = <<-SQL.strip_heredoc
SELECT count(distinct #{class_name.foreign_key}) from approval_#{class_name.underscore}_rules
SQL
count(sql_old_schema) - count(sql_new_schema)
end
def count(sql)
ActiveRecord::Base.connection.exec_query(sql).first['count']
end
end
end
end
...@@ -13,6 +13,7 @@ describe MigrateProjectApprovers, :migration do ...@@ -13,6 +13,7 @@ describe MigrateProjectApprovers, :migration do
let(:approval_project_rules) { table(:approval_project_rules) } let(:approval_project_rules) { table(:approval_project_rules) }
let(:approval_project_rules_users) { table(:approval_project_rules_users) } let(:approval_project_rules_users) { table(:approval_project_rules_users) }
let(:users) { table(:users) } let(:users) { table(:users) }
let(:features) { table(:features) }
before do before do
namespaces.create(id: 1, name: 'gitlab-org', path: 'gitlab-org') namespaces.create(id: 1, name: 'gitlab-org', path: 'gitlab-org')
...@@ -35,6 +36,7 @@ describe MigrateProjectApprovers, :migration do ...@@ -35,6 +36,7 @@ describe MigrateProjectApprovers, :migration do
expect(approval_project_rules_users.where(approval_project_rule_id: rule_ids.first).pluck(:user_id)).to contain_exactly(1) expect(approval_project_rules_users.where(approval_project_rule_id: rule_ids.first).pluck(:user_id)).to contain_exactly(1)
expect(approval_project_rules_users.where(approval_project_rule_id: rule_ids.last).pluck(:user_id)).to contain_exactly(2) expect(approval_project_rules_users.where(approval_project_rule_id: rule_ids.last).pluck(:user_id)).to contain_exactly(2)
expect(features.where(key: 'approval_rule').exists?).to eq(true)
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