summaryrefslogtreecommitdiff
path: root/db/migrate/20170828135939_migrate_user_external_mail_data.rb
blob: 592e141b7e653245c9d3fbfcff0a2e2b351fde32 (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
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.

class MigrateUserExternalMailData < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  class User < ActiveRecord::Base
    self.table_name = 'users'

    include EachBatch
  end

  class UserSyncedAttributesMetadata < ActiveRecord::Base
    self.table_name = 'user_synced_attributes_metadata'

    include EachBatch
  end

  def up
    User.each_batch do |batch|
      start_id, end_id = batch.pluck('MIN(id), MAX(id)').first

      execute <<-EOF
        INSERT INTO user_synced_attributes_metadata (user_id, provider, email_synced)
        SELECT id, email_provider, external_email
        FROM users
        WHERE external_email = TRUE
        AND NOT EXISTS (
          SELECT true
          FROM user_synced_attributes_metadata
          WHERE user_id = users.id
          AND provider = users.email_provider
        )
        AND id BETWEEN #{start_id} AND #{end_id}
      EOF
    end
  end

  def down
    UserSyncedAttributesMetadata.each_batch do |batch|
      start_id, end_id = batch.pluck('MIN(id), MAX(id)').first

      execute <<-EOF
        UPDATE users
        SET users.email_provider = metadata.provider, users.external_email = metadata.email_synced
        FROM user_synced_attributes_metadata as metadata, users
        WHERE metadata.email_synced = TRUE
        AND metadata.user_id = users.id
        AND id BETWEEN #{start_id} AND #{end_id}
      EOF
    end
  end
end