Class: Gitlab::Database::PartitioningMigrationHelpers::BackfillPartitionedTable

Inherits:
Object
  • Object
show all
Includes:
DynamicModelHelpers
Defined in:
lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb

Overview

Class that will generically copy data from a given table into its corresponding partitioned table

Defined Under Namespace

Classes: BulkCopy

Constant Summary collapse

SUB_BATCH_SIZE =
2_500
PAUSE_SECONDS =
0.25

Instance Method Summary collapse

Methods included from DynamicModelHelpers

#define_batchable_model

Instance Method Details

#perform(start_id, stop_id, source_table, partitioned_table, source_column) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb', line 13

def perform(start_id, stop_id, source_table, partitioned_table, source_column)
  return unless Feature.enabled?(:backfill_partitioned_audit_events, default_enabled: true)

  if transaction_open?
    raise "Aborting job to backfill partitioned #{source_table} table! Do not run this job in a transaction block!"
  end

  unless table_exists?(partitioned_table)
    logger.warn "exiting backfill migration because partitioned table #{partitioned_table} does not exist. " \
      "This could be due to the migration being rolled back after migration jobs were enqueued in sidekiq"
    return
  end

  bulk_copy = BulkCopy.new(source_table, partitioned_table, source_column)
  parent_batch_relation = relation_scoped_to_range(source_table, source_column, start_id, stop_id)

  parent_batch_relation.each_batch(of: SUB_BATCH_SIZE) do |sub_batch|
    sub_start_id, sub_stop_id = sub_batch.pluck(Arel.sql("MIN(#{source_column}), MAX(#{source_column})")).first

    bulk_copy.copy_between(sub_start_id, sub_stop_id)
    sleep(PAUSE_SECONDS)
  end

  mark_jobs_as_succeeded(start_id, stop_id, source_table, partitioned_table, source_column)
end