diff options
Diffstat (limited to 'lib/gitlab/background_migration/user_mentions/create_resource_user_mention.rb')
-rw-r--r-- | lib/gitlab/background_migration/user_mentions/create_resource_user_mention.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/user_mentions/create_resource_user_mention.rb b/lib/gitlab/background_migration/user_mentions/create_resource_user_mention.rb new file mode 100644 index 00000000000..e951b44b036 --- /dev/null +++ b/lib/gitlab/background_migration/user_mentions/create_resource_user_mention.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true +# rubocop:disable Style/Documentation + +module Gitlab + module BackgroundMigration + module UserMentions + class CreateResourceUserMention + # Resources that have mentions to be migrated: + # issue, merge_request, epic, commit, snippet, design + + BULK_INSERT_SIZE = 5000 + ISOLATION_MODULE = 'Gitlab::BackgroundMigration::UserMentions::Models' + + def perform(resource_model, join, conditions, with_notes, start_id, end_id) + resource_model = "#{ISOLATION_MODULE}::#{resource_model}".constantize if resource_model.is_a?(String) + model = with_notes ? "#{ISOLATION_MODULE}::Note".constantize : resource_model + resource_user_mention_model = resource_model.user_mention_model + + records = model.joins(join).where(conditions).where(id: start_id..end_id) + + records.in_groups_of(BULK_INSERT_SIZE, false).each do |records| + mentions = [] + records.each do |record| + mentions << record.build_mention_values + end + + no_quote_columns = [:note_id] + no_quote_columns << resource_user_mention_model.resource_foreign_key + + Gitlab::Database.bulk_insert( + resource_user_mention_model.table_name, + mentions, + return_ids: true, + disable_quote: no_quote_columns, + on_conflict: :do_nothing + ) + end + end + end + end + end +end |