summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2017-11-16 16:14:24 -0800
committerSean McGivern <sean@gitlab.com>2017-11-17 10:23:48 +0000
commit5cecff893db3188ff5ec779e07e9c598d3ed2e21 (patch)
tree33b5957717da5d0add5356c4db8e65650f9fb1ae /lib
parentd41e66cb632cf4a51428c87a07cbdd182e3e0697 (diff)
downloadgitlab-ce-5cecff893db3188ff5ec779e07e9c598d3ed2e21.tar.gz
Convert migration to populate latest merge request ID into a background migration
This is to smear updates over a few hours to avoid causing excessive replication lag as seen in https://gitlab.com/gitlab-com/infrastructure/issues/3235.
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/background_migration/populate_merge_requests_latest_merge_request_diff_id.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/populate_merge_requests_latest_merge_request_diff_id.rb b/lib/gitlab/background_migration/populate_merge_requests_latest_merge_request_diff_id.rb
new file mode 100644
index 00000000000..7e109e96e73
--- /dev/null
+++ b/lib/gitlab/background_migration/populate_merge_requests_latest_merge_request_diff_id.rb
@@ -0,0 +1,30 @@
+module Gitlab
+ module BackgroundMigration
+ class PopulateMergeRequestsLatestMergeRequestDiffId
+ BATCH_SIZE = 1_000
+
+ class MergeRequest < ActiveRecord::Base
+ self.table_name = 'merge_requests'
+
+ include ::EachBatch
+ end
+
+ def perform(start_id, stop_id)
+ update = '
+ latest_merge_request_diff_id = (
+ SELECT MAX(id)
+ FROM merge_request_diffs
+ WHERE merge_requests.id = merge_request_diffs.merge_request_id
+ )'.squish
+
+ MergeRequest
+ .where(id: start_id..stop_id)
+ .where(latest_merge_request_diff_id: nil)
+ .each_batch(of: BATCH_SIZE) do |relation|
+
+ relation.update_all(update)
+ end
+ end
+ end
+ end
+end