Module: BulkInsertSafe

Extended by:
ActiveSupport::Concern
Included in:
AuditEvent, Ci::BuildNeed, Ci::JobVariable, CiPlatformMetric, LabelLink, MergeRequestDiffCommit, MergeRequestDiffFile
Defined in:
app/models/concerns/bulk_insert_safe.rb

Overview

A mixin for ActiveRecord models that enables callers to insert instances of the target class into the database en-bloc via the [bulk_insert] method.

Upon inclusion in the target class, the mixin will perform a number of checks to ensure that the target is eligible for bulk insertions. For instance, it must not use ActiveRecord callbacks that fire between [save]s, since these would not run properly when instances are inserted in bulk.

The mixin uses ActiveRecord 6's [InsertAll] type internally for bulk insertions. Unlike [InsertAll], however, it requires you to pass instances of the target type rather than row hashes, since it will run validations prior to insertion.

Examples:


class MyRecord < ApplicationRecord
  include BulkInsertSafe # must be included _last_ i.e. after any other concerns
end

# simple
MyRecord.bulk_insert!(items)

# with custom batch size
MyRecord.bulk_insert!(items, batch_size: 100)

# without validations
MyRecord.bulk_insert!(items, validate: false)

# with attribute hash modification
MyRecord.bulk_insert!(items) { |item_attrs| item_attrs['col'] = 42 }

Constant Summary collapse

ALLOWED_CALLBACKS =

These are the callbacks we think safe when used on models that are written to the database in bulk

Set[
  :initialize,
  :validate,
  :validation,
  :find,
  :destroy
].freeze
DEFAULT_BATCH_SIZE =
500
MethodNotAllowedError =
Class.new(StandardError)
PrimaryKeySetError =
Class.new(StandardError)