summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180216121020_fill_pages_domain_verification_code.rb
blob: d423673d2a57bcc8b240bbcef3c28f79af855cc3 (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
class FillPagesDomainVerificationCode < ActiveRecord::Migration
  DOWNTIME = false

  class PagesDomain < ActiveRecord::Base
    include EachBatch
  end

  # Allow this migration to resume if it fails partway through
  disable_ddl_transaction!

  def up
    PagesDomain.where(verification_code: [nil, '']).each_batch do |relation|
      connection.execute(set_codes_sql(relation))

      # Sleep 2 minutes between batches to not overload the DB with dead tuples
      sleep(2.minutes) unless relation.reorder(:id).last == PagesDomain.reorder(:id).last
    end

    change_column_null(:pages_domains, :verification_code, false)
  end

  def down
    change_column_null(:pages_domains, :verification_code, true)
  end

  private

  def set_codes_sql(relation)
    ids = relation.pluck(:id)
    whens = ids.map { |id| "WHEN #{id} THEN '#{SecureRandom.hex(16)}'" }

    <<~SQL
      UPDATE pages_domains
      SET verification_code =
        CASE id
        #{whens.join("\n")}
        END
      WHERE id IN(#{ids.join(',')})
    SQL
  end
end