summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2019-03-26 18:00:46 +0000
committerDouglas Barbosa Alexandre <dbalexandre@gmail.com>2019-03-26 18:00:46 +0000
commit19010942652c23d203c856acf454528ef342fafb (patch)
tree4b6f1e5319ef050faf6336d3020291701e787e9f /lib
parent61ffcebd5e577df23b39b289a1d6e203a9073ef0 (diff)
parent929ee4d18da886826e9fcc15c35b4d4024bc8237 (diff)
downloadgitlab-ce-19010942652c23d203c856acf454528ef342fafb.tar.gz
Merge branch 'osw-multi-assignees-merge-requests-migration' into 'master'
Add multiple MR assignees migration and background table population See merge request gitlab-org/gitlab-ce!26496
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/background_migration/populate_merge_request_assignees_table.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/populate_merge_request_assignees_table.rb b/lib/gitlab/background_migration/populate_merge_request_assignees_table.rb
new file mode 100644
index 00000000000..a4c6540c61b
--- /dev/null
+++ b/lib/gitlab/background_migration/populate_merge_request_assignees_table.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module Gitlab
+ module BackgroundMigration
+ # This background migration creates records on merge_request_assignees according
+ # to the given merge request IDs range. A _single_ INSERT is issued for the given range.
+ # This is required for supporting multiple assignees on merge requests.
+ class PopulateMergeRequestAssigneesTable
+ def perform(from_id, to_id)
+ select_sql =
+ MergeRequest
+ .where(merge_request_assignees_not_exists_clause)
+ .where(id: from_id..to_id)
+ .where('assignee_id IS NOT NULL')
+ .select(:id, :assignee_id)
+ .to_sql
+
+ execute("INSERT INTO merge_request_assignees (merge_request_id, user_id) #{select_sql}")
+ end
+
+ private
+
+ def merge_request_assignees_not_exists_clause
+ <<~SQL
+ NOT EXISTS (SELECT 1 FROM merge_request_assignees
+ WHERE merge_request_assignees.merge_request_id = merge_requests.id)
+ SQL
+ end
+
+ def execute(sql)
+ @connection ||= ActiveRecord::Base.connection
+ @connection.execute(sql)
+ end
+ end
+ end
+end