summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration
diff options
context:
space:
mode:
authorJan Provaznik <jprovaznik@gitlab.com>2017-12-29 17:15:50 +0100
committerJan Provaznik <jprovaznik@gitlab.com>2018-01-10 20:40:02 +0100
commite6a1db6d9e3036ae0c9dc677f9029f5acf37f9f6 (patch)
tree2ba714a0042a675af0cb0b604f46b927409a628c /lib/gitlab/background_migration
parent047cde243ed0fb0e71ddf85735342bec66c92eae (diff)
downloadgitlab-ce-e6a1db6d9e3036ae0c9dc677f9029f5acf37f9f6.tar.gz
Denormalize commits count for merge request diffs38068-commits-count
For each MR diff an extra 'SELECT COUNT()' is executed to get number of commits for the diff. Overall time to get counts for all MR diffs may be quite expensive. To speed up loading of MR info, information about number of commits is stored in a MR diff's extra column. Closes #38068
Diffstat (limited to 'lib/gitlab/background_migration')
-rw-r--r--lib/gitlab/background_migration/add_merge_request_diff_commits_count.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/add_merge_request_diff_commits_count.rb b/lib/gitlab/background_migration/add_merge_request_diff_commits_count.rb
new file mode 100644
index 00000000000..7bffffec94d
--- /dev/null
+++ b/lib/gitlab/background_migration/add_merge_request_diff_commits_count.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+# rubocop:disable Style/Documentation
+# rubocop:disable Metrics/LineLength
+
+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}")
+
+ 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).update_all(update)
+ end
+ end
+ end
+end