summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/add_merge_request_diff_commits_count.rb
blob: c912628d0fc21d6b4cf540402517b3ced4d38f36 (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
# frozen_string_literal: true
# rubocop:disable Style/Documentation

module Gitlab
  module BackgroundMigration
    class AddMergeRequestDiffCommitsCount
      class MergeRequestDiff < ActiveRecord::Base
        self.table_name = 'merge_request_diffs'
      end

      def perform(start_id, stop_id)
        Rails.logger.info("Setting commits_count for merge request diffs: #{start_id} - #{stop_id}") # rubocop:disable Gitlab/RailsLogger

        update = '
          commits_count = (
            SELECT count(*)
            FROM merge_request_diff_commits
            WHERE merge_request_diffs.id = merge_request_diff_commits.merge_request_diff_id
          )'.squish

        MergeRequestDiff.where(id: start_id..stop_id).where(commits_count: nil).update_all(update)
      end
    end
  end
end