Commit 6d666b21 authored by Krasimir Angelov's avatar Krasimir Angelov

Add rake task to finalize batched background migration inline

https://gitlab.com/gitlab-org/gitlab/-/issues/292874
parent aef36f40
...@@ -54,17 +54,18 @@ module Gitlab ...@@ -54,17 +54,18 @@ module Gitlab
def finalize(job_class_name, table_name, column_name, job_arguments) def finalize(job_class_name, table_name, column_name, job_arguments)
migration = BatchedMigration.find_for_configuration(job_class_name, table_name, column_name, job_arguments) migration = BatchedMigration.find_for_configuration(job_class_name, table_name, column_name, job_arguments)
if migration.nil?
configuration = { configuration = {
job_class_name: job_class_name, job_class_name: job_class_name,
table_name: table_name, table_name: table_name,
column_name: column_name, column_name: column_name,
job_arguments: job_arguments job_arguments: job_arguments
} }
if migration.nil?
Gitlab::AppLogger.warn "Could not find batched background migration for the given configuration: #{configuration}" Gitlab::AppLogger.warn "Could not find batched background migration for the given configuration: #{configuration}"
elsif migration.finished?
Gitlab::AppLogger.warn "Batched background migration for the given configuration is already finished: #{configuration}"
else else
return if migration.finished?
migration.finalizing! migration.finalizing!
migration.batched_jobs.pending.each { |job| migration_wrapper.perform(job) } migration.batched_jobs.pending.each { |job| migration_wrapper.perform(job) }
......
...@@ -1106,7 +1106,11 @@ module Gitlab ...@@ -1106,7 +1106,11 @@ module Gitlab
Gitlab::AppLogger.warn "Could not find batched background migration for the given configuration: #{configuration}" Gitlab::AppLogger.warn "Could not find batched background migration for the given configuration: #{configuration}"
elsif !migration.finished? elsif !migration.finished?
raise "Expected batched background migration for the given configuration to be marked as 'finished', " \ raise "Expected batched background migration for the given configuration to be marked as 'finished', " \
"but it is '#{migration.status}': #{configuration}" "but it is '#{migration.status}': #{configuration}" \
"\n\n" \
"Finalize it manualy by running" \
"\n\n" \
"\tgitlab-rake gitlab:background_migrations:finalize[#{job_class_name},#{table_name},#{column_name},'#{job_arguments.inspect.gsub(',', '\,')}']"
end end
end end
......
# frozen_string_literal: true
namespace :gitlab do
namespace :background_migrations do
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]
puts "Must specify #{argument} as an argument".color(:red)
exit 1
end
end
Gitlab::Database::BackgroundMigration::BatchedMigrationRunner.finalize(
args[:job_class_name],
args[:table_name],
args[:column_name],
Gitlab::Json.parse(args[:job_arguments])
)
puts "Done.".color(:green)
end
end
end
...@@ -381,6 +381,16 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigrationRunner do ...@@ -381,6 +381,16 @@ RSpec.describe Gitlab::Database::BackgroundMigration::BatchedMigrationRunner do
.with('CopyColumnUsingBackgroundMigrationJob', table_name, column_name, job_arguments) .with('CopyColumnUsingBackgroundMigrationJob', table_name, column_name, job_arguments)
.and_return(batched_migration) .and_return(batched_migration)
configuration = {
job_class_name: batched_migration.job_class_name,
table_name: table_name.to_sym,
column_name: column_name.to_sym,
job_arguments: job_arguments
}
expect(Gitlab::AppLogger).to receive(:warn)
.with("Batched background migration for the given configuration is already finished: #{configuration}")
expect(batched_migration).not_to receive(:finalizing!) expect(batched_migration).not_to receive(:finalizing!)
runner.finalize( runner.finalize(
......
...@@ -2007,7 +2007,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -2007,7 +2007,7 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
job_class_name: 'CopyColumnUsingBackgroundMigrationJob', job_class_name: 'CopyColumnUsingBackgroundMigrationJob',
table_name: :events, table_name: :events,
column_name: :id, column_name: :id,
job_arguments: [[:id], [:id_convert_to_bigint]] job_arguments: [["id"], ["id_convert_to_bigint"]]
} }
end end
...@@ -2017,7 +2017,11 @@ RSpec.describe Gitlab::Database::MigrationHelpers do ...@@ -2017,7 +2017,11 @@ RSpec.describe Gitlab::Database::MigrationHelpers do
create(:batched_background_migration, configuration.merge(status: :active)) create(:batched_background_migration, configuration.merge(status: :active))
expect { ensure_batched_background_migration_is_finished } expect { ensure_batched_background_migration_is_finished }
.to raise_error "Expected batched background migration for the given configuration to be marked as 'finished', but it is 'active': #{configuration}" .to raise_error "Expected batched background migration for the given configuration to be marked as 'finished', but it is 'active': #{configuration}" \
"\n\n" \
"Finalize it manualy by running" \
"\n\n" \
"\tgitlab-rake gitlab:background_migrations:finalize[CopyColumnUsingBackgroundMigrationJob,events,id,'[[\"id\"]\\, [\"id_convert_to_bigint\"]]']"
end end
it 'does not raise error when migration exists and is marked as finished' do it 'does not raise error when migration exists and is marked as finished' do
......
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