summaryrefslogtreecommitdiff
path: root/db/migrate/20180717125853_remove_restricted_todos.rb
blob: 1d4bbf6571ece001c6c9243d95a9246d0986bc3d (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
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
# frozen_string_literal: true

class RemoveRestrictedTodos < ActiveRecord::Migration[4.2]
  DOWNTIME = false
  disable_ddl_transaction!

  MIGRATION = 'RemoveRestrictedTodos'.freeze
  BATCH_SIZE = 1000
  DELAY_INTERVAL = 5.minutes.to_i

  class Project < ActiveRecord::Base
    include EachBatch

    self.table_name = 'projects'
  end

  def up
    Project.where('EXISTS (SELECT 1 FROM todos WHERE todos.project_id = projects.id)')
      .each_batch(of: BATCH_SIZE) do |batch, index|
      range = batch.pluck('MIN(id)', 'MAX(id)').first

      BackgroundMigrationWorker.perform_in(index * DELAY_INTERVAL, MIGRATION, range)
    end
  end

  def down
    # nothing to do
  end
end