debian-mirror-gitlab/lib/tasks/gitlab/background_migrations.rake

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.6 KiB
Ruby
Raw Normal View History

2021-09-30 23:02:18 +05:30
# frozen_string_literal: true
2022-05-07 20:08:51 +05:30
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
2021-09-30 23:02:18 +05:30
namespace :gitlab do
namespace :background_migrations do
2022-01-26 12:08:38 +05:30
desc 'Synchronously finish executing a batched background migration'
2021-09-30 23:02:18 +05:30
task :finalize, [:job_class_name, :table_name, :column_name, :job_arguments] => :environment do |_, args|
2022-05-07 20:08:51 +05:30
if Gitlab::Database.db_config_names.size > 1
puts "Please specify the database".color(:red)
exit 1
2021-09-30 23:02:18 +05:30
end
2022-05-07 20:08:51 +05:30
validate_finalization_arguments!(args)
main_model = Gitlab::Database.database_base_models[:main]
finalize_migration(
2021-09-30 23:02:18 +05:30
args[:job_class_name],
args[:table_name],
args[:column_name],
2022-05-07 20:08:51 +05:30
args[:job_arguments],
connection: main_model.connection
2021-09-30 23:02:18 +05:30
)
2022-05-07 20:08:51 +05:30
end
2021-09-30 23:02:18 +05:30
2022-05-07 20:08:51 +05:30
namespace :finalize do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
next if name.to_s == 'geo'
desc "Gitlab | DB | Synchronously finish executing a batched background migration on #{name} database"
task name, [:job_class_name, :table_name, :column_name, :job_arguments] => :environment do |_, args|
validate_finalization_arguments!(args)
model = Gitlab::Database.database_base_models[name]
finalize_migration(
args[:job_class_name],
args[:table_name],
args[:column_name],
args[:job_arguments],
connection: model.connection
)
end
end
2021-09-30 23:02:18 +05:30
end
2022-01-26 12:08:38 +05:30
desc 'Display the status of batched background migrations'
2022-05-07 20:08:51 +05:30
task status: :environment do |_, args|
Gitlab::Database.database_base_models.each do |name, model|
display_migration_status(name, model.connection)
end
end
namespace :status do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
next if name.to_s == 'geo'
desc "Gitlab | DB | Display the status of batched background migrations on #{name} database"
task name => :environment do |_, args|
model = Gitlab::Database.database_base_models[name]
display_migration_status(name, model.connection)
end
end
end
private
def finalize_migration(class_name, table_name, column_name, job_arguments, connection:)
Gitlab::Database::BackgroundMigration::BatchedMigrationRunner.finalize(
class_name,
table_name,
column_name,
Gitlab::Json.parse(job_arguments),
connection: connection
)
puts "Done.".color(:green)
end
def display_migration_status(database_name, connection)
Gitlab::Database::SharedModel.using_connection(connection) do
2022-06-21 17:19:12 +05:30
valid_status = Gitlab::Database::BackgroundMigration::BatchedMigration.valid_status
max_status_length = valid_status.map(&:length).max
2022-05-07 20:08:51 +05:30
format_string = "%-#{max_status_length}s | %s\n"
puts "Database: #{database_name}\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(',')
2022-06-21 17:19:12 +05:30
printf(format_string, migration.status_name, identification_fields)
2022-05-07 20:08:51 +05:30
end
end
end
def validate_finalization_arguments!(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
2022-01-26 12:08:38 +05:30
end
end
2021-09-30 23:02:18 +05:30
end
end