Class: Gitlab::BackgroundMigration::BackfillUserDetailsFields

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

Overview

Class that will backfill the following fields from user to user_details

  • linkedin

  • twitter

  • skype

  • website_url

  • location

  • organization

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, 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



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
# File 'lib/gitlab/background_migration/backfill_user_details_fields.rb', line 16

def perform
  query = <<~SQL
    (COALESCE(linkedin, '') IS DISTINCT FROM '')
    OR (COALESCE(twitter, '') IS DISTINCT FROM '')
    OR (COALESCE(skype, '') IS DISTINCT FROM '')
    OR (COALESCE(website_url, '') IS DISTINCT FROM '')
    OR (COALESCE(location, '') IS DISTINCT FROM '')
    OR (COALESCE(organization, '') IS DISTINCT FROM '')
  SQL
  field_limit = UserDetail::DEFAULT_FIELD_LENGTH

  each_sub_batch(
    batching_scope: ->(relation) {
                      relation.where(query).select(
                        'id AS user_id',
                        "substring(COALESCE(linkedin, '') from 1 for #{field_limit}) AS linkedin",
                        "substring(COALESCE(twitter, '') from 1 for #{field_limit}) AS twitter",
                        "substring(COALESCE(skype, '') from 1 for #{field_limit}) AS skype",
                        "substring(COALESCE(website_url, '') from 1 for #{field_limit}) AS website_url",
                        "substring(COALESCE(location, '') from 1 for #{field_limit}) AS location",
                        "substring(COALESCE(organization, '') from 1 for #{field_limit}) AS organization"
                      )
                    }
  ) do |sub_batch|
    upsert_user_details_fields(sub_batch)
  end
end

#upsert_user_details_fields(relation) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gitlab/background_migration/backfill_user_details_fields.rb', line 44

def upsert_user_details_fields(relation)
  connection.execute(
    <<~SQL
      INSERT INTO user_details (user_id, linkedin, twitter, skype, website_url, location, organization)
      #{relation.to_sql}
      ON CONFLICT (user_id)
      DO UPDATE SET
      "linkedin" = EXCLUDED."linkedin",
      "twitter" = EXCLUDED."twitter",
      "skype" = EXCLUDED."skype",
      "website_url" = EXCLUDED."website_url",
      "location" = EXCLUDED."location",
      "organization" = EXCLUDED."organization"
    SQL
  )
end