Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20210901153324_slice_merge_request_diff_commit_migrations.rb « post_migrate « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ee7feae1a6eafd0e01c44887aed1671f4a32bce (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true

class SliceMergeRequestDiffCommitMigrations < ActiveRecord::Migration[6.1]
  include Gitlab::Database::MigrationHelpers

  disable_ddl_transaction!

  BATCH_SIZE = 5_000
  MIGRATION_CLASS = 'MigrateMergeRequestDiffCommitUsers'
  STEAL_MIGRATION_CLASS = 'StealMigrateMergeRequestDiffCommitUsers'

  def up
    old_jobs = Gitlab::Database::BackgroundMigrationJob
      .for_migration_class(MIGRATION_CLASS)
      .pending
      .to_a

    return if old_jobs.empty?

    transaction do
      # This ensures we stop processing the old ranges, as the background
      # migrations skip already processed jobs.
      Gitlab::Database::BackgroundMigrationJob
        .for_migration_class(MIGRATION_CLASS)
        .pending
        .update_all(status: :succeeded)

      rows = []

      old_jobs.each do |job|
        min, max = job.arguments

        while min < max
          rows << {
            class_name: MIGRATION_CLASS,
            arguments: [min, min + BATCH_SIZE],
            created_at: Time.now.utc,
            updated_at: Time.now.utc
          }

          min += BATCH_SIZE
        end
      end

      Gitlab::Database::BackgroundMigrationJob.insert_all!(rows)
    end

    job = Gitlab::Database::BackgroundMigrationJob
      .for_migration_class(MIGRATION_CLASS)
      .pending
      .first

    migrate_in(1.hour, STEAL_MIGRATION_CLASS, job.arguments)
  end

  def down
    # no-op
  end
end