summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200311130802_schedule_populate_user_highest_roles_table.rb
blob: 36f0d42a8557176d09d46db77f4432638dc3c3a8 (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
# frozen_string_literal: true

class SchedulePopulateUserHighestRolesTable < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  BATCH_SIZE = 10_000
  DELAY = 5.minutes.to_i
  DOWNTIME = false
  MIGRATION = 'PopulateUserHighestRolesTable'

  disable_ddl_transaction!

  class User < ActiveRecord::Base
    include EachBatch

    scope :active, -> {
      where(state: 'active', user_type: nil, bot_type: nil)
        .where('ghost IS NOT TRUE')
    }
  end

  def up
    # We currently have ~5_300_000 users with the state active on GitLab.com.
    # This means it'll schedule ~530 jobs (10k Users each) with a 5 minutes gap,
    # so this should take ~44 hours for all background migrations to complete.
    User.active.each_batch(of: BATCH_SIZE) do |batch, index|
      range = batch.pluck(Arel.sql('MIN(id)'), Arel.sql('MAX(id)')).first
      delay = index * DELAY

      migrate_in(delay.seconds, MIGRATION, [*range])
    end
  end

  def down
    # nothing
  end
end