database_helpers.rb 745 Bytes
Newer Older
1 2 3 4 5 6 7 8
# frozen_string_literal: true

module Gitlab
  module Import
    module DatabaseHelpers
      # Inserts a raw row and returns the ID of the inserted row.
      #
      # attributes - The attributes/columns to set.
Nick Thomas's avatar
Nick Thomas committed
9
      # relation - An ActiveRecord::Relation to use for finding the table name
10 11 12 13
      def insert_and_return_id(attributes, relation)
        # We use bulk_insert here so we can bypass any queries executed by
        # callbacks or validation rules, as doing this wouldn't scale when
        # importing very large projects.
14
        result = Gitlab::Database.main # rubocop:disable Gitlab/BulkInsert
15 16
                 .bulk_insert(relation.table_name, [attributes], return_ids: true)

Nick Thomas's avatar
Nick Thomas committed
17
        result.first
18 19 20 21
      end
    end
  end
end