debian-mirror-gitlab/lib/gitlab/background_migration/user_mentions/create_resource_user_mention.rb

37 lines
1.4 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# 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
2020-04-08 14:13:33 +05:30
BULK_INSERT_SIZE = 1_000
2020-03-13 15:44:24 +05:30
ISOLATION_MODULE = 'Gitlab::BackgroundMigration::UserMentions::Models'
def perform(resource_model, join, conditions, with_notes, start_id, end_id)
2020-10-24 23:57:45 +05:30
return unless Feature.enabled?(:migrate_user_mentions, default_enabled: true)
2020-03-13 15:44:24 +05:30
resource_model = "#{ISOLATION_MODULE}::#{resource_model}".constantize if resource_model.is_a?(String)
model = with_notes ? Gitlab::BackgroundMigration::UserMentions::Models::Note : resource_model
resource_user_mention_model = resource_model.user_mention_model
records = model.joins(join).where(conditions).where(id: start_id..end_id)
2020-10-24 23:57:45 +05:30
records.each_batch(of: BULK_INSERT_SIZE) do |records|
2020-03-13 15:44:24 +05:30
mentions = []
records.each do |record|
2020-04-08 14:13:33 +05:30
mention_record = record.build_mention_values(resource_user_mention_model.resource_foreign_key)
mentions << mention_record unless mention_record.blank?
2020-03-13 15:44:24 +05:30
end
2020-10-24 23:57:45 +05:30
resource_user_mention_model.insert_all(mentions) unless mentions.empty?
2020-03-13 15:44:24 +05:30
end
end
end
end
end
end