summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/copy_column_using_background_migration_job.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/background_migration/copy_column_using_background_migration_job.rb')
-rw-r--r--lib/gitlab/background_migration/copy_column_using_background_migration_job.rb49
1 files changed, 12 insertions, 37 deletions
diff --git a/lib/gitlab/background_migration/copy_column_using_background_migration_job.rb b/lib/gitlab/background_migration/copy_column_using_background_migration_job.rb
index 137b4d4bc4e..826845935b8 100644
--- a/lib/gitlab/background_migration/copy_column_using_background_migration_job.rb
+++ b/lib/gitlab/background_migration/copy_column_using_background_migration_job.rb
@@ -13,50 +13,25 @@ module Gitlab
# - We skip the NULL checks as they may result in not using an index scan
# - The table that is migrated does _not_ need `id` as the primary key
# We use the provided primary_key column to perform the update.
- class CopyColumnUsingBackgroundMigrationJob < BaseJob
- include Gitlab::Database::DynamicModelHelpers
+ class CopyColumnUsingBackgroundMigrationJob < BatchedMigrationJob
+ def perform(copy_from, copy_to)
+ assignment_clauses = build_assignment_clauses(copy_from, copy_to)
- # start_id - The start ID of the range of rows to update.
- # end_id - The end ID of the range of rows to update.
- # batch_table - The name of the table that contains the columns.
- # batch_column - The name of the column we use to batch over the table.
- # sub_batch_size - We don't want updates to take more than ~100ms
- # This allows us to run multiple smaller batches during
- # the minimum 2.minute interval that we can schedule jobs
- # pause_ms - The number of milliseconds to sleep between each subbatch execution.
- # copy_from - List of columns containing the data to copy.
- # copy_to - List of columns to copy the data to. Order must match the order in `copy_from`.
- def perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_ms, copy_from, copy_to)
- copy_from = Array.wrap(copy_from)
- copy_to = Array.wrap(copy_to)
-
- raise ArgumentError, 'number of source and destination columns must match' unless copy_from.count == copy_to.count
-
- assignment_clauses = column_assignment_clauses(copy_from, copy_to)
-
- parent_batch_relation = relation_scoped_to_range(batch_table, batch_column, start_id, end_id)
-
- parent_batch_relation.each_batch(column: batch_column, of: sub_batch_size) do |sub_batch|
- batch_metrics.time_operation(:update_all) do
- sub_batch.update_all(assignment_clauses)
- end
-
- pause_ms = 0 if pause_ms < 0
- sleep(pause_ms * 0.001)
+ each_sub_batch(operation_name: :update_all) do |relation|
+ relation.update_all(assignment_clauses)
end
end
- def batch_metrics
- @batch_metrics ||= Gitlab::Database::BackgroundMigration::BatchMetrics.new
- end
-
private
- def relation_scoped_to_range(source_table, source_key_column, start_id, stop_id)
- define_batchable_model(source_table, connection: connection).where(source_key_column => start_id..stop_id)
- end
+ def build_assignment_clauses(copy_from, copy_to)
+ copy_from = Array.wrap(copy_from)
+ copy_to = Array.wrap(copy_to)
+
+ unless copy_from.count == copy_to.count
+ raise ArgumentError, 'number of source and destination columns must match'
+ end
- def column_assignment_clauses(copy_from, copy_to)
assignments = copy_from.zip(copy_to).map do |from_column, to_column|
from_column = connection.quote_column_name(from_column)
to_column = connection.quote_column_name(to_column)