summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180913142237_schedule_digest_personal_access_tokens.rb
blob: 36be819b2456d655ac4d30b75a0bed79bd2b719f (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
class ScheduleDigestPersonalAccessTokens < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  BATCH_SIZE = 10_000
  MIGRATION = 'DigestColumn'
  DELAY_INTERVAL = 5.minutes.to_i

  disable_ddl_transaction!

  class PersonalAccessToken < ActiveRecord::Base
    include EachBatch

    self.table_name = 'personal_access_tokens'
  end

  def up
    PersonalAccessToken.where('token is NOT NULL').each_batch(of: BATCH_SIZE) do |batch, index|
      range = batch.pluck('MIN(id)', 'MAX(id)').first
      BackgroundMigrationWorker.perform_in(index * DELAY_INTERVAL, MIGRATION, ['PersonalAccessToken', :token, :token_digest, *range])
    end
  end

  def down
    # raise ActiveRecord::IrreversibleMigration
  end
end