Module: TypedEAV::BulkUpsert

Defined in:
lib/typed_eav/bulk_upsert.rb

Overview

Reduced-semantics, SQL-oriented bulk value writer.

This API runs casting/domain/entity/partition validation and validation callbacks, but does not run host callbacks/validations or host saves, Value persistence callbacks, versioning, or per-record error isolation. Callers must acknowledge those semantics explicitly. It pre-casts and validates every row in a transaction unit before issuing one upsert batch on the host model's connection. Scope resolution and field casting remain authoritative.

Constant Summary collapse

TYPE_COLUMNS =
%i[
  string_value text_value boolean_value integer_value decimal_value date_value datetime_value json_value
].freeze

Class Method Summary collapse

Class Method Details

.execute(host_class:, records:, values_by_field_name:, acknowledge_reduced_semantics:, transaction: :all, chunk_size: nil) ⇒ Object

rubocop:disable Metrics/ParameterLists -- transaction controls are explicit API contract.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/typed_eav/bulk_upsert.rb', line 19

def execute(
  host_class:, records:, values_by_field_name:, acknowledge_reduced_semantics:, transaction: :all, chunk_size: nil
)
  validate_options!(records, values_by_field_name, acknowledge_reduced_semantics, transaction, chunk_size)
  records = records.to_a
  return 0 if records.empty?

  validate_record_classes!(host_class, records)
  reject_duplicate_or_unpersisted!(records)
  ensure_connection_owner!(host_class)
  values_by_field_name = normalize_values!(values_by_field_name)
  units = transaction == :all ? [records] : records.each_slice(chunk_size)
  units.sum { |unit| write_unit(host_class, unit, values_by_field_name) }
end