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

populate_canonical_emails.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 052e75c565561da7b53267aa37331c8af72c54ab (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
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Class to populate new rows of UserCanonicalEmail based on existing email addresses
    class PopulateCanonicalEmails
      def perform(start_id, stop_id)
        ActiveRecord::Base.connection.execute <<~SQL
          INSERT INTO
            user_canonical_emails (
                user_id,
                canonical_email,
                created_at,
                updated_at
            )
            SELECT users.id AS user_id,
                   concat(translate(split_part(split_part(users.email, '@', 1), '+', 1), '.', ''), '@gmail.com') AS canonical_email,
                   NOW() AS created_at,
                   NOW() AS updated_at
             FROM users
            WHERE users.email ILIKE '%@gmail.com'
              AND users.id BETWEEN #{start_id} AND #{stop_id}
            ON CONFLICT DO NOTHING;
        SQL
      end
    end
  end
end