Class: Gitlab::BackgroundMigration::SetMergeRequestDiffFilesCount

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/background_migration/set_merge_request_diff_files_count.rb

Overview

Sets the MergeRequestDiff#files_count value for old rows

Defined Under Namespace

Classes: MergeRequestDiff

Constant Summary collapse

FILES_COUNT_SENTINEL =

Some historic data has a lot of files. Apply a sentinel to these cases

2**15 - 1

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.count_subqueryObject


10
11
12
13
14
15
16
17
18
# File 'lib/gitlab/background_migration/set_merge_request_diff_files_count.rb', line 10

def self.count_subquery
  <<~SQL
    files_count = (
      SELECT LEAST(#{FILES_COUNT_SENTINEL}, count(*))
      FROM merge_request_diff_files
      WHERE merge_request_diff_files.merge_request_diff_id = merge_request_diffs.id
    )
  SQL
end

Instance Method Details

#perform(start_id, end_id) ⇒ Object


26
27
28
29
30
# File 'lib/gitlab/background_migration/set_merge_request_diff_files_count.rb', line 26

def perform(start_id, end_id)
  MergeRequestDiff.where(id: start_id..end_id).each_batch do |relation|
    relation.update_all(self.class.count_subquery)
  end
end