summaryrefslogtreecommitdiff
path: root/db/post_migrate/20211028155449_schedule_fix_merge_request_diff_commit_users_migration.rb
blob: 659cb7b76b2102cd283ac56229d196276c97f1ed (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
# frozen_string_literal: true

class ScheduleFixMergeRequestDiffCommitUsersMigration < Gitlab::Database::Migration[1.0]
  disable_ddl_transaction!

  MIGRATION_CLASS = 'FixMergeRequestDiffCommitUsers'

  class Project < ApplicationRecord
    include EachBatch

    self.table_name = 'projects'
  end

  def up
    # This is the day on which we merged
    # https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63669. Since the
    # deploy of this MR we may have imported projects using the old format, but
    # after their merge_request_diff_id range had been migrated by Sidekiq. As a
    # result, there may be rows without a committer_id or commit_author_id
    # field.
    date = '2021-07-07 00:00:00'

    transaction do
      Project.each_batch(of: 10_000) do |batch|
        time = Time.now.utc
        rows = batch
          .where('created_at >= ?', date)
          .where(import_type: 'gitlab_project')
          .pluck(:id)
          .map do |id|
            Gitlab::Database::BackgroundMigrationJob.new(
              class_name: MIGRATION_CLASS,
              arguments: [id],
              created_at: time,
              updated_at: time
            )
          end

        Gitlab::Database::BackgroundMigrationJob
          .bulk_insert!(rows, validate: false)
      end
    end

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

    migrate_in(2.minutes, MIGRATION_CLASS, job.arguments) if job
  end

  def down
    # no-op
  end
end