2021-06-17 14:10:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-03-02 13:16:35 -05:00
|
|
|
databases = ActiveRecord::Tasks::DatabaseTasks.setup_initial_database_yaml
|
|
|
|
|
2021-06-17 14:10:22 -04:00
|
|
|
namespace :gitlab do
|
|
|
|
namespace :background_migrations do
|
2021-12-07 16:14:42 -05:00
|
|
|
desc 'Synchronously finish executing a batched background migration'
|
2021-06-17 14:10:22 -04:00
|
|
|
task :finalize, [:job_class_name, :table_name, :column_name, :job_arguments] => :environment do |_, args|
|
2022-03-02 13:16:35 -05:00
|
|
|
if Gitlab::Database.db_config_names.size > 1
|
|
|
|
puts "Please specify the database".color(:red)
|
|
|
|
exit 1
|
2021-06-17 14:10:22 -04:00
|
|
|
end
|
|
|
|
|
2022-03-02 13:16:35 -05:00
|
|
|
validate_finalization_arguments!(args)
|
|
|
|
|
|
|
|
main_model = Gitlab::Database.database_base_models[:main]
|
|
|
|
|
|
|
|
finalize_migration(
|
2021-06-17 14:10:22 -04:00
|
|
|
args[:job_class_name],
|
|
|
|
args[:table_name],
|
|
|
|
args[:column_name],
|
2022-03-02 13:16:35 -05:00
|
|
|
Gitlab::Json.parse(args[:job_arguments]),
|
|
|
|
connection: main_model.connection
|
2021-06-17 14:10:22 -04:00
|
|
|
)
|
2022-03-02 13:16:35 -05:00
|
|
|
end
|
2021-06-17 14:10:22 -04:00
|
|
|
|
2022-03-02 13:16:35 -05:00
|
|
|
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],
|
|
|
|
Gitlab::Json.parse(args[:job_arguments]),
|
|
|
|
connection: model.connection
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2021-06-17 14:10:22 -04:00
|
|
|
end
|
2021-12-07 16:14:42 -05:00
|
|
|
|
|
|
|
desc 'Display the status of batched background migrations'
|
2022-03-02 13:16:35 -05:00
|
|
|
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
|
|
|
|
statuses = Gitlab::Database::BackgroundMigration::BatchedMigration.statuses
|
|
|
|
max_status_length = statuses.keys.map(&:length).max
|
|
|
|
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(',')
|
|
|
|
|
|
|
|
printf(format_string, migration.status, identification_fields)
|
|
|
|
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
|
2021-12-07 16:14:42 -05:00
|
|
|
end
|
|
|
|
end
|
2021-06-17 14:10:22 -04:00
|
|
|
end
|
|
|
|
end
|