Commit d72bcea1 authored by pbair's avatar pbair

Add rake task for batched bg migration status

Add a rake task which prints the status of all batched background
migrations to the console, to simplify instructions for finding
migrations which haven't completed.
parent 7acd6ef4
......@@ -2,6 +2,7 @@
namespace :gitlab do
namespace :background_migrations do
desc 'Synchronously finish executing a batched background migration'
task :finalize, [:job_class_name, :table_name, :column_name, :job_arguments] => :environment do |_, args|
[:job_class_name, :table_name, :column_name, :job_arguments].each do |argument|
unless args[argument]
......@@ -19,5 +20,23 @@ namespace :gitlab do
puts "Done.".color(:green)
end
desc 'Display the status of batched background migrations'
task status: :environment do
statuses = Gitlab::Database::BackgroundMigration::BatchedMigration.statuses
max_status_length = statuses.keys.map(&:length).max
format_string = "%-#{max_status_length}s | %s\n"
Gitlab::Database::BackgroundMigration::BatchedMigration.find_each(batch_size: 100) do |migration|
identification_fields = [
migration.job_class_name,
migration.table_name,
migration.column_name,
migration.job_arguments.to_json
].join(',')
printf(format_string, migration.status, identification_fields)
end
end
end
end
......@@ -12,5 +12,13 @@ FactoryBot.define do
sequence(:job_arguments) { |n| [["column_#{n}"], ["column_#{n}_convert_to_bigint"]] }
total_tuple_count { 10_000 }
pause_ms { 100 }
trait :finished do
status { :finished }
end
trait :failed do
status { :failed }
end
end
end
# frozen_string_literal: true
require 'rake_helper'
RSpec.describe 'gitlab:background_migrations namespace rake tasks' do
before do
Rake.application.rake_require 'tasks/gitlab/background_migrations'
end
describe 'finalize' do
subject(:finalize_task) { run_rake_task('gitlab:background_migrations:finalize', *arguments) }
context 'without the proper arguments' do
let(:arguments) { %w[CopyColumnUsingBackgroundMigrationJob events id] }
it 'exits without finalizing the migration' do
expect(Gitlab::Database::BackgroundMigration::BatchedMigrationRunner).not_to receive(:finalize)
expect { finalize_task }.to output(/Must specify job_arguments as an argument/).to_stdout
.and raise_error(SystemExit) { |error| expect(error.status).to eq(1) }
end
end
context 'with the proper arguments' do
let(:arguments) { %w[CopyColumnUsingBackgroundMigrationJob events id [["id1"\,"id2"]]] }
it 'finalizes the matching migration' do
expect(Gitlab::Database::BackgroundMigration::BatchedMigrationRunner).to receive(:finalize)
.with('CopyColumnUsingBackgroundMigrationJob', 'events', 'id', [%w[id1 id2]])
expect { finalize_task }.to output(/Done/).to_stdout
end
end
end
describe 'status' do
subject(:status_task) { run_rake_task('gitlab:background_migrations:status') }
it 'outputs the status of background migrations' do
migration1 = create(:batched_background_migration, :finished, job_arguments: [%w[id1 id2]])
migration2 = create(:batched_background_migration, :failed, job_arguments: [])
expect { status_task }.to output(<<~OUTPUT).to_stdout
finished | #{migration1.job_class_name},#{migration1.table_name},#{migration1.column_name},[["id1","id2"]]
failed | #{migration2.job_class_name},#{migration2.table_name},#{migration2.column_name},[]
OUTPUT
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