Welcome to mirror list, hosted at ThFree Co, Russian Federation.

add_primary_email_to_emails_if_user_confirmed.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b39c0953fb1ebf6addaa53a1361750f89e09f3ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Add user primary email to emails table if confirmed
    class AddPrimaryEmailToEmailsIfUserConfirmed
      INNER_BATCH_SIZE = 1_000

      # Stubbed class to access the User table
      class User < ActiveRecord::Base
        include ::EachBatch

        self.table_name = 'users'
        self.inheritance_column = :_type_disabled

        scope :confirmed, -> { where.not(confirmed_at: nil) }

        has_many :emails
      end

      # Stubbed class to access the Emails table
      class Email < ActiveRecord::Base
        self.table_name = 'emails'
        self.inheritance_column = :_type_disabled

        belongs_to :user
      end

      def perform(start_id, end_id)
        User.confirmed.where(id: start_id..end_id).select(:id, :email, :confirmed_at).each_batch(of: INNER_BATCH_SIZE) do |users|
          current_time = Time.now.utc

          attributes = users.map do |user|
            {
              user_id: user.id,
              email: user.email,
              confirmed_at: user.confirmed_at,
              created_at: current_time,
              updated_at: current_time
            }
          end

          Email.insert_all(attributes)
        end
        mark_job_as_succeeded(start_id, end_id)
      end

      private

      def mark_job_as_succeeded(*arguments)
        Gitlab::Database::BackgroundMigrationJob.mark_all_as_succeeded(
          'AddPrimaryEmailToEmailsIfUserConfirmed',
          arguments
        )
      end
    end
  end
end