summaryrefslogtreecommitdiff
path: root/db/post_migrate/20170703130158_schedule_merge_request_diff_migrations.rb
blob: 17a9dc293f1f111204d0a3339c59135f76a977f2 (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
class ScheduleMergeRequestDiffMigrations < ActiveRecord::Migration
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  BATCH_SIZE = 2500
  MIGRATION = 'DeserializeMergeRequestDiffsAndCommits'

  disable_ddl_transaction!

  class MergeRequestDiff < ActiveRecord::Base
    self.table_name = 'merge_request_diffs'

    include ::EachBatch
  end

  # Assuming that there are 5 million rows affected (which is more than on
  # GitLab.com), and that each batch of 2,500 rows takes up to 5 minutes, then
  # we can migrate all the rows in 7 days.
  #
  # On staging, plucking the IDs themselves takes 5 seconds.
  def up
    non_empty = 'st_commits IS NOT NULL OR st_diffs IS NOT NULL'

    MergeRequestDiff.where(non_empty).each_batch(of: BATCH_SIZE) do |relation, index|
      range = relation.pluck('MIN(id)', 'MAX(id)').first

      BackgroundMigrationWorker.perform_in(index * 5.minutes, MIGRATION, range)
    end
  end

  def down
  end
end