Class: Gitlab::BackgroundMigration::PopulateCanonicalEmails

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/background_migration/populate_canonical_emails.rb

Overview

Class to populate new rows of UserCanonicalEmail based on existing email addresses

Instance Method Summary collapse

Instance Method Details

#perform(start_id, stop_id) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitlab/background_migration/populate_canonical_emails.rb', line 7

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