Class: Gitlab::BackgroundMigration::BackfillMergeRequestFileDiffsPartitionedTable

Inherits:
BackfillPartitionedTable show all
Extended by:
Utils::Override
Defined in:
lib/gitlab/background_migration/backfill_merge_request_file_diffs_partitioned_table.rb

Overview

rubocop: disable Migration/BatchedMigrationBaseClass – This is indirectly deriving from the correct base class

Constant Summary

Constants inherited from BatchedMigrationJob

Gitlab::BackgroundMigration::BatchedMigrationJob::DEFAULT_FEATURE_CATEGORY, Gitlab::BackgroundMigration::BatchedMigrationJob::MINIMUM_PAUSE_MS

Constants included from Database::DynamicModelHelpers

Database::DynamicModelHelpers::BATCH_SIZE

Instance Method Summary collapse

Methods included from Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods inherited from BatchedMigrationJob

#batch_metrics, cursor, cursor?, cursor_columns, feature_category, #filter_batch, generic_instance, health_context_tables, #initialize, job_arguments, job_arguments_count, operation_name, scope_to, tables_to_check_for_vacuum

Methods included from Database::DynamicModelHelpers

define_batchable_model, #each_batch, #each_batch_range

Constructor Details

This class inherits a constructor from Gitlab::BackgroundMigration::BatchedMigrationJob

Instance Method Details

#performObject



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/background_migration/backfill_merge_request_file_diffs_partitioned_table.rb', line 16

def perform
  column_values = connection.columns(batch_table).map do |column|
    case column.name
    when 'new_path'
      'NULLIF("merge_request_diff_files"."new_path", "merge_request_diff_files"."old_path")'
    when 'project_id'
      'COALESCE("merge_request_diff_files"."project_id", "merge_request_diffs"."project_id")'
    else
      connection.quote_column_name(column.name)
    end
  end.join(', ')

  each_sub_batch do |relation|
    connection.execute(<<~SQL)
      INSERT INTO #{partitioned_table} (#{connection.columns(batch_table).map do |c|
        connection.quote_column_name(c.name)
      end.join(', ')})
      #{relation.joins('INNER JOIN "merge_request_diffs" ON "merge_request_diffs"."id" = "merge_request_diff_files"."merge_request_diff_id"').select(column_values).to_sql}
      ON CONFLICT (#{connection.primary_keys(partitioned_table).join(', ')}) DO NOTHING
    SQL
  end
end