Welcome to mirror list, hosted at ThFree Co, Russian Federation.

active_record_fix_insert_all.rb « initializers « config - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ae208dd0e5d852c32637c37d7824b7df243cdd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# frozen_string_literal: true

# This fix is needed to properly support
# columns that perform data mutation to a SQL datatype
# ex. would be `jsonb` and `enum`
#
# This is covered by tests in `BulkInsertSafe`
# that validates handling of different data types

if Rails.gem_version > Gem::Version.new("6.0.2")
  raise Gem::DependencyError,
    "Remove patch once the https://github.com/rails/rails/pull/38763 is included"
end

module ActiveRecordInsertAllBuilderMixin
  def extract_types_from_columns_on(table_name, keys:)
    columns = connection.schema_cache.columns_hash(table_name)

    unknown_column = (keys - columns.keys).first
    raise UnknownAttributeError.new(model.new, unknown_column) if unknown_column

    keys.index_with { |key| model.type_for_attribute(key) }
  end
end

ActiveRecord::InsertAll::Builder.prepend(ActiveRecordInsertAllBuilderMixin)