summaryrefslogtreecommitdiff
path: root/db/post_migrate/20180521162137_migrate_remaining_mr_metrics_populating_background_migration.rb
blob: 39666a0cd2ae59c43c4653a318899c34b3dea6ca (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
42
43
44
class MigrateRemainingMrMetricsPopulatingBackgroundMigration < ActiveRecord::Migration[4.2]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false
  BATCH_SIZE = 5_000
  MIGRATION = 'PopulateMergeRequestMetricsWithEventsData'
  DELAY_INTERVAL = 10.minutes

  disable_ddl_transaction!

  class MergeRequest < ActiveRecord::Base
    self.table_name = 'merge_requests'

    include ::EachBatch
  end

  def up
    # Perform any ongoing background migration that might still be running. This
    # avoids scheduling way too many of the same jobs on self-hosted instances
    # if they're updating GitLab across multiple versions. The "Take one"
    # migration was executed on 10.4 on
    # SchedulePopulateMergeRequestMetricsWithEventsData.
    Gitlab::BackgroundMigration.steal(MIGRATION)

    metrics_not_exists_clause = <<~SQL
      NOT EXISTS (SELECT 1 FROM merge_request_metrics
                  WHERE merge_request_metrics.merge_request_id = merge_requests.id)
    SQL

    relation = MergeRequest.where(metrics_not_exists_clause)

    # We currently have ~400_000 MR records without metrics on GitLab.com.
    # This means it'll schedule ~80 jobs (5000 MRs each) with a 10 minutes gap,
    # so this should take ~14 hours for all background migrations to complete.
    #
    queue_background_migration_jobs_by_range_at_intervals(relation,
                                                          MIGRATION,
                                                          DELAY_INTERVAL,
                                                          batch_size: BATCH_SIZE)
  end

  def down
  end
end