Class: Gitlab::BackgroundMigration::BackfillUserDetails

Inherits:
BatchedMigrationJob show all
Defined in:
lib/gitlab/background_migration/backfill_user_details.rb

Defined Under Namespace

Classes: UserDetail

Constant Summary

Constants inherited from BatchedMigrationJob

Gitlab::BackgroundMigration::BatchedMigrationJob::DEFAULT_FEATURE_CATEGORY

Constants included from Database::DynamicModelHelpers

Database::DynamicModelHelpers::BATCH_SIZE

Instance Method Summary collapse

Methods inherited from BatchedMigrationJob

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

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



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
38
39
40
41
42
43
# File 'lib/gitlab/background_migration/backfill_user_details.rb', line 13

def perform
  each_sub_batch do |sub_batch|
    records_need_created_for_user_ids = sub_batch
                                          .joins("LEFT JOIN user_details ON (users.id = user_details.user_id)")
                                          .where(user_details: { user_id: nil })
                                          .ids

    next if records_need_created_for_user_ids.empty?

    user_details_attributes = records_need_created_for_user_ids.map do |user_id|
      {
        user_id: user_id
      }
    end

    # This should be safe so we do not hit the fk_rails_12e0b3043d constraint
    # since we already prequalified in the query above that no user_details
    # record exists for that user.
    # However, to be sure, we'll rescue here in case there is some extreme
    # edge case.
    UserDetail.upsert_all(user_details_attributes, returning: false)
  rescue Exception => e # rubocop:disable Lint/RescueException -- following https://docs.gitlab.com/ee/development/database/batched_background_migrations.html#best-practices re-raise
    logger.error(
      class: e.class,
      message: "BackfillUserDetails Migration: error inserting. Reason: #{e.message}",
      user_ids: records_need_created_for_user_ids
    )

    raise
  end
end