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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitlab/background_migration/set_merge_request_diff_files_count.rb')
-rw-r--r--lib/gitlab/background_migration/set_merge_request_diff_files_count.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/set_merge_request_diff_files_count.rb b/lib/gitlab/background_migration/set_merge_request_diff_files_count.rb
new file mode 100644
index 00000000000..9f765d03d62
--- /dev/null
+++ b/lib/gitlab/background_migration/set_merge_request_diff_files_count.rb
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module BackgroundMigration
+ # Sets the MergeRequestDiff#files_count value for old rows
+ class SetMergeRequestDiffFilesCount
+ COUNT_SUBQUERY = <<~SQL
+ files_count = (
+ SELECT count(*)
+ FROM merge_request_diff_files
+ WHERE merge_request_diff_files.merge_request_diff_id = merge_request_diffs.id
+ )
+ SQL
+
+ class MergeRequestDiff < ActiveRecord::Base # rubocop:disable Style/Documentation
+ include EachBatch
+
+ self.table_name = 'merge_request_diffs'
+ end
+
+ def perform(start_id, end_id)
+ MergeRequestDiff.where(id: start_id..end_id).each_batch do |relation|
+ relation.update_all(COUNT_SUBQUERY)
+ end
+ end
+ end
+ end
+end