activerecord-import 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +74 -8
- data/Brewfile +3 -1
- data/CHANGELOG.md +6 -0
- data/Gemfile +3 -1
- data/README.markdown +2 -2
- data/Rakefile +2 -0
- data/activerecord-import.gemspec +2 -1
- data/benchmarks/benchmark.rb +2 -0
- data/benchmarks/lib/base.rb +2 -0
- data/benchmarks/lib/cli_parser.rb +3 -1
- data/benchmarks/lib/float.rb +2 -0
- data/benchmarks/lib/mysql2_benchmark.rb +2 -0
- data/benchmarks/lib/output_to_csv.rb +2 -0
- data/benchmarks/lib/output_to_html.rb +4 -2
- data/benchmarks/models/test_innodb.rb +2 -0
- data/benchmarks/models/test_memory.rb +2 -0
- data/benchmarks/models/test_myisam.rb +2 -0
- data/benchmarks/schema/mysql2_schema.rb +2 -0
- data/gemfiles/4.2.gemfile +2 -0
- data/gemfiles/5.0.gemfile +2 -0
- data/gemfiles/5.1.gemfile +2 -0
- data/gemfiles/5.2.gemfile +2 -0
- data/gemfiles/6.0.gemfile +2 -0
- data/gemfiles/6.1.gemfile +2 -0
- data/gemfiles/7.0.gemfile +3 -0
- data/lib/activerecord-import/active_record/adapters/abstract_adapter.rb +2 -0
- data/lib/activerecord-import/active_record/adapters/jdbcmysql_adapter.rb +2 -0
- data/lib/activerecord-import/active_record/adapters/jdbcpostgresql_adapter.rb +2 -0
- data/lib/activerecord-import/active_record/adapters/jdbcsqlite3_adapter.rb +2 -0
- data/lib/activerecord-import/active_record/adapters/mysql2_adapter.rb +2 -0
- data/lib/activerecord-import/active_record/adapters/postgresql_adapter.rb +2 -0
- data/lib/activerecord-import/active_record/adapters/seamless_database_pool_adapter.rb +2 -0
- data/lib/activerecord-import/active_record/adapters/sqlite3_adapter.rb +2 -0
- data/lib/activerecord-import/adapters/abstract_adapter.rb +2 -0
- data/lib/activerecord-import/adapters/em_mysql2_adapter.rb +2 -0
- data/lib/activerecord-import/adapters/mysql2_adapter.rb +2 -0
- data/lib/activerecord-import/adapters/mysql_adapter.rb +2 -0
- data/lib/activerecord-import/adapters/postgresql_adapter.rb +37 -24
- data/lib/activerecord-import/adapters/sqlite3_adapter.rb +4 -2
- data/lib/activerecord-import/base.rb +3 -1
- data/lib/activerecord-import/import.rb +21 -10
- data/lib/activerecord-import/mysql2.rb +2 -0
- data/lib/activerecord-import/postgresql.rb +2 -0
- data/lib/activerecord-import/sqlite3.rb +2 -0
- data/lib/activerecord-import/synchronize.rb +2 -0
- data/lib/activerecord-import/value_sets_parser.rb +2 -0
- data/lib/activerecord-import/version.rb +3 -1
- data/lib/activerecord-import.rb +3 -1
- data/test/adapters/jdbcmysql.rb +2 -0
- data/test/adapters/jdbcpostgresql.rb +2 -0
- data/test/adapters/jdbcsqlite3.rb +2 -0
- data/test/adapters/makara_postgis.rb +2 -0
- data/test/adapters/mysql2.rb +2 -0
- data/test/adapters/mysql2_makara.rb +2 -0
- data/test/adapters/mysql2spatial.rb +2 -0
- data/test/adapters/postgis.rb +2 -0
- data/test/adapters/postgresql.rb +2 -0
- data/test/adapters/postgresql_makara.rb +2 -0
- data/test/adapters/seamless_database_pool.rb +2 -0
- data/test/adapters/spatialite.rb +2 -0
- data/test/adapters/sqlite3.rb +2 -0
- data/test/import_test.rb +21 -0
- data/test/jdbcmysql/import_test.rb +2 -0
- data/test/jdbcpostgresql/import_test.rb +2 -0
- data/test/jdbcsqlite3/import_test.rb +2 -0
- data/test/makara_postgis/import_test.rb +2 -0
- data/test/models/account.rb +2 -0
- data/test/models/alarm.rb +2 -0
- data/test/models/animal.rb +2 -0
- data/test/models/bike_maker.rb +2 -0
- data/test/models/book.rb +2 -0
- data/test/models/car.rb +2 -0
- data/test/models/card.rb +2 -0
- data/test/models/chapter.rb +2 -0
- data/test/models/customer.rb +2 -0
- data/test/models/deck.rb +2 -0
- data/test/models/dictionary.rb +2 -0
- data/test/models/discount.rb +2 -0
- data/test/models/end_note.rb +2 -0
- data/test/models/group.rb +2 -0
- data/test/models/order.rb +2 -0
- data/test/models/playing_card.rb +2 -0
- data/test/models/promotion.rb +2 -0
- data/test/models/question.rb +2 -0
- data/test/models/rule.rb +2 -0
- data/test/models/tag.rb +3 -0
- data/test/models/tag_alias.rb +5 -0
- data/test/models/topic.rb +2 -0
- data/test/models/user.rb +2 -0
- data/test/models/user_token.rb +2 -0
- data/test/models/vendor.rb +2 -0
- data/test/models/widget.rb +2 -0
- data/test/mysql2/import_test.rb +2 -0
- data/test/mysql2_makara/import_test.rb +2 -0
- data/test/mysqlspatial2/import_test.rb +2 -0
- data/test/postgis/import_test.rb +2 -0
- data/test/postgresql/import_test.rb +2 -0
- data/test/schema/generic_schema.rb +8 -0
- data/test/schema/jdbcpostgresql_schema.rb +2 -0
- data/test/schema/mysql2_schema.rb +2 -0
- data/test/schema/postgis_schema.rb +2 -0
- data/test/schema/postgresql_schema.rb +2 -0
- data/test/schema/sqlite3_schema.rb +2 -0
- data/test/schema/version.rb +2 -0
- data/test/sqlite3/import_test.rb +2 -0
- data/test/support/active_support/test_case_extensions.rb +2 -0
- data/test/support/assertions.rb +2 -0
- data/test/support/factories.rb +2 -0
- data/test/support/generate.rb +4 -2
- data/test/support/mysql/import_examples.rb +2 -1
- data/test/support/postgresql/import_examples.rb +40 -1
- data/test/support/shared_examples/on_duplicate_key_ignore.rb +2 -0
- data/test/support/shared_examples/on_duplicate_key_update.rb +2 -0
- data/test/support/shared_examples/recursive_import.rb +2 -0
- data/test/support/sqlite3/import_examples.rb +2 -1
- data/test/synchronize_test.rb +2 -0
- data/test/test_helper.rb +8 -0
- data/test/value_sets_bytes_parser_test.rb +2 -0
- data/test/value_sets_records_parser_test.rb +2 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b9c717d31cf5a4012568c9d8497947466408be7fef5610fba9143f7a911ed7c
|
4
|
+
data.tar.gz: 925bc5f645152eeb09f9583e9d50b9be67c4bfa8049c303571f82649262e4d5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 409d66e4d4e6e9ac31940cea097837d76c93cbeb4b95461323afc6a5f2ace736f945705e5ef33f2a851bb92c13c27e19ffbc14c7e776a0366f1d76091cafd51f
|
7
|
+
data.tar.gz: 6a0eaa48b85f7b1863dfb2b04c3c193a5e7e1108084556ba1b25fa303bccb40f62dd706ec8d4b5c3394239f167eb1807dfff4fe292edc1e4ad550719a9a63446
|
data/.rubocop.yml
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
inherit_from: .rubocop_todo.yml
|
2
2
|
|
3
|
-
Lint/EndAlignment:
|
4
|
-
AlignWith: variable
|
5
|
-
|
6
3
|
Metrics/AbcSize:
|
7
4
|
Enabled: false
|
8
5
|
|
6
|
+
Metrics/BlockLength:
|
7
|
+
Enabled: false
|
8
|
+
|
9
9
|
Metrics/ClassLength:
|
10
10
|
Enabled: false
|
11
11
|
|
@@ -24,8 +24,8 @@ Metrics/ModuleLength:
|
|
24
24
|
Metrics/PerceivedComplexity:
|
25
25
|
Enabled: false
|
26
26
|
|
27
|
-
Style/
|
28
|
-
|
27
|
+
Style/CommentedKeyword:
|
28
|
+
Enabled: false
|
29
29
|
|
30
30
|
Style/ClassAndModuleChildren:
|
31
31
|
Enabled: false
|
@@ -33,10 +33,25 @@ Style/ClassAndModuleChildren:
|
|
33
33
|
Style/Documentation:
|
34
34
|
Enabled: false
|
35
35
|
|
36
|
-
Style/
|
36
|
+
Style/EvalWithLocation:
|
37
|
+
Enabled: false
|
38
|
+
|
39
|
+
Style/ExpandPathArguments:
|
40
|
+
Enabled: false
|
41
|
+
|
42
|
+
Style/GuardClause:
|
43
|
+
Enabled: false
|
44
|
+
|
45
|
+
Style/IfUnlessModifier:
|
46
|
+
Enabled: false
|
47
|
+
|
48
|
+
Style/NumericPredicate:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/PercentLiteralDelimiters:
|
37
52
|
Enabled: false
|
38
53
|
|
39
|
-
Style/
|
54
|
+
Style/RedundantBegin:
|
40
55
|
Enabled: false
|
41
56
|
|
42
57
|
Style/SpecialGlobalVars:
|
@@ -45,5 +60,56 @@ Style/SpecialGlobalVars:
|
|
45
60
|
Style/StringLiterals:
|
46
61
|
Enabled: false
|
47
62
|
|
48
|
-
Style/
|
63
|
+
Style/SymbolArray:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
Style/TrailingCommaInArrayLiteral:
|
67
|
+
Enabled: false
|
68
|
+
|
69
|
+
Layout/AlignArguments:
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
Layout/AlignParameters:
|
73
|
+
EnforcedStyle: with_fixed_indentation
|
74
|
+
|
75
|
+
Layout/EndAlignment:
|
76
|
+
EnforcedStyleAlignWith: variable
|
77
|
+
|
78
|
+
Layout/ElseAlignment:
|
79
|
+
Enabled: false
|
80
|
+
|
81
|
+
Layout/EmptyLineAfterGuardClause:
|
82
|
+
Enabled: false
|
83
|
+
|
84
|
+
Layout/IndentHeredoc:
|
85
|
+
Enabled: false
|
86
|
+
|
87
|
+
Layout/SpaceInsideParens:
|
88
|
+
Enabled: false
|
89
|
+
|
90
|
+
Layout/SpaceInsidePercentLiteralDelimiters:
|
91
|
+
Enabled: false
|
92
|
+
|
93
|
+
Lint/ErbNewArguments:
|
94
|
+
Enabled: false
|
95
|
+
|
96
|
+
Lint/MissingCopEnableDirective:
|
97
|
+
Enabled: false
|
98
|
+
|
99
|
+
Lint/PercentStringArray:
|
100
|
+
Enabled: false
|
101
|
+
|
102
|
+
Naming/HeredocDelimiterNaming:
|
103
|
+
Enabled: false
|
104
|
+
|
105
|
+
Naming/UncommunicativeMethodParamName:
|
106
|
+
Enabled: false
|
107
|
+
|
108
|
+
Security/YAMLLoad:
|
109
|
+
Enabled: false
|
110
|
+
|
111
|
+
Gemspec/RequiredRubyVersion:
|
112
|
+
Enabled: false
|
113
|
+
|
114
|
+
Bundler/OrderedGems:
|
49
115
|
Enabled: false
|
data/Brewfile
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
source 'https://rubygems.org'
|
2
4
|
|
3
5
|
gemspec
|
@@ -13,7 +15,7 @@ pg_version = '0.9'
|
|
13
15
|
pg_version = '1.1' if version >= 6.1
|
14
16
|
|
15
17
|
group :development, :test do
|
16
|
-
gem 'rubocop', '~> 0.
|
18
|
+
gem 'rubocop', '~> 0.71.0'
|
17
19
|
gem 'rake'
|
18
20
|
end
|
19
21
|
|
data/README.markdown
CHANGED
@@ -268,9 +268,9 @@ Key | Options | Default | Descrip
|
|
268
268
|
:validate_uniqueness | `true`/`false` | `false` | Whether or not to run uniqueness validations, has potential pitfalls, use with caution (requires `>= v0.27.0`).
|
269
269
|
:validate_with_context | `Symbol` |`:create`/`:update` | Allows passing an ActiveModel validation context for each model. Default is `:create` for new records and `:update` for existing ones.
|
270
270
|
:track_validation_failures| `true`/`false` | `false` | When this is set to true, `failed_instances` will be an array of arrays, with each inner array having the form `[:index_in_dataset, :object_with_errors]`
|
271
|
-
:on_duplicate_key_ignore | `true`/`false` | `false` | Allows skipping records with duplicate keys. See [here](
|
271
|
+
:on_duplicate_key_ignore | `true`/`false` | `false` | Allows skipping records with duplicate keys. See [here](#duplicate-key-ignore) for more details.
|
272
272
|
:ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
|
273
|
-
:on_duplicate_key_update | :all, `Array`, `Hash` | N/A | Allows upsert logic to be used. See [here](
|
273
|
+
:on_duplicate_key_update | :all, `Array`, `Hash` | N/A | Allows upsert logic to be used. See [here](#duplicate-key-update) for more details.
|
274
274
|
:synchronize | `Array` | N/A | An array of ActiveRecord instances. This synchronizes existing instances in memory with updates from the import.
|
275
275
|
:timestamps | `true`/`false` | `true` | Enables/disables timestamps on imported records.
|
276
276
|
:recursive | `true`/`false` | `false` | Imports has_many/has_one associations (PostgreSQL only).
|
data/Rakefile
CHANGED
data/activerecord-import.gemspec
CHANGED
data/benchmarks/benchmark.rb
CHANGED
data/benchmarks/lib/base.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'optparse'
|
2
4
|
require 'ostruct'
|
3
5
|
|
@@ -8,7 +10,7 @@ require 'ostruct'
|
|
8
10
|
# * t - the table types to test. ie: myisam, innodb, memory, temporary, etc.
|
9
11
|
#
|
10
12
|
module BenchmarkOptionParser
|
11
|
-
BANNER = "Usage: ruby #{$0} [options]\nSee ruby #{$0} -h for more options."
|
13
|
+
BANNER = "Usage: ruby #{$0} [options]\nSee ruby #{$0} -h for more options."
|
12
14
|
|
13
15
|
def self.print_banner
|
14
16
|
puts BANNER
|
data/benchmarks/lib/float.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'erb'
|
2
4
|
|
3
5
|
module OutputToHTML
|
4
|
-
TEMPLATE_HEADER = <<"EOT"
|
6
|
+
TEMPLATE_HEADER = <<"EOT"
|
5
7
|
<div>
|
6
8
|
All times are rounded to the nearest thousandth for display purposes. Speedups next to each time are computed
|
7
9
|
before any rounding occurs. Also, all speedup calculations are computed by comparing a given time against
|
@@ -9,7 +11,7 @@ module OutputToHTML
|
|
9
11
|
</div>
|
10
12
|
EOT
|
11
13
|
|
12
|
-
TEMPLATE = <<"EOT"
|
14
|
+
TEMPLATE = <<"EOT"
|
13
15
|
<style>
|
14
16
|
td#benchmarkTitle {
|
15
17
|
border: 1px solid black;
|
data/gemfiles/4.2.gemfile
CHANGED
data/gemfiles/5.0.gemfile
CHANGED
data/gemfiles/5.1.gemfile
CHANGED
data/gemfiles/5.2.gemfile
CHANGED
data/gemfiles/6.0.gemfile
CHANGED
data/gemfiles/6.1.gemfile
CHANGED
data/gemfiles/7.0.gemfile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord::Import::PostgreSQLAdapter
|
2
4
|
include ActiveRecord::Import::ImportSupport
|
3
5
|
include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
|
@@ -6,7 +8,7 @@ module ActiveRecord::Import::PostgreSQLAdapter
|
|
6
8
|
|
7
9
|
def insert_many( sql, values, options = {}, *args ) # :nodoc:
|
8
10
|
number_of_inserts = 1
|
9
|
-
returned_values =
|
11
|
+
returned_values = {}
|
10
12
|
ids = []
|
11
13
|
results = []
|
12
14
|
|
@@ -18,47 +20,53 @@ module ActiveRecord::Import::PostgreSQLAdapter
|
|
18
20
|
|
19
21
|
sql2insert = base_sql + values.join( ',' ) + post_sql
|
20
22
|
|
21
|
-
|
22
|
-
if
|
23
|
+
selections = returning_selections(options)
|
24
|
+
if selections.blank? || (options[:no_returning] && !options[:recursive])
|
23
25
|
insert( sql2insert, *args )
|
24
26
|
else
|
25
|
-
returned_values = if
|
27
|
+
returned_values = if selections.size > 1
|
26
28
|
# Select composite columns
|
27
|
-
|
29
|
+
db_result = select_all( sql2insert, *args )
|
30
|
+
{ values: db_result.rows, columns: db_result.columns }
|
28
31
|
else
|
29
|
-
select_values( sql2insert, *args )
|
32
|
+
{ values: select_values( sql2insert, *args ) }
|
30
33
|
end
|
31
34
|
clear_query_cache if query_cache_enabled
|
32
35
|
end
|
33
36
|
|
34
37
|
if options[:returning].blank?
|
35
|
-
ids = returned_values
|
38
|
+
ids = Array(returned_values[:values])
|
36
39
|
elsif options[:primary_key].blank?
|
37
|
-
|
40
|
+
options[:returning_columns] ||= returned_values[:columns]
|
41
|
+
results = Array(returned_values[:values])
|
38
42
|
else
|
39
43
|
# split primary key and returning columns
|
40
|
-
ids, results = split_ids_and_results(returned_values,
|
44
|
+
ids, results, options[:returning_columns] = split_ids_and_results(returned_values, options)
|
41
45
|
end
|
42
46
|
|
43
47
|
ActiveRecord::Import::Result.new([], number_of_inserts, ids, results)
|
44
48
|
end
|
45
49
|
|
46
|
-
def split_ids_and_results(
|
50
|
+
def split_ids_and_results( selections, options )
|
47
51
|
ids = []
|
48
|
-
|
52
|
+
returning_values = []
|
53
|
+
|
54
|
+
columns = Array(selections[:columns])
|
55
|
+
values = Array(selections[:values])
|
49
56
|
id_indexes = Array(options[:primary_key]).map { |key| columns.index(key) }
|
50
|
-
|
57
|
+
returning_columns = columns.reject.with_index { |_, index| id_indexes.include?(index) }
|
58
|
+
returning_indexes = returning_columns.map { |column| columns.index(column) }
|
51
59
|
|
52
60
|
values.each do |value|
|
53
61
|
value_array = Array(value)
|
54
|
-
ids << id_indexes.map { |
|
55
|
-
|
62
|
+
ids << id_indexes.map { |index| value_array[index] }
|
63
|
+
returning_values << returning_indexes.map { |index| value_array[index] }
|
56
64
|
end
|
57
65
|
|
58
66
|
ids.map!(&:first) if id_indexes.size == 1
|
59
|
-
|
67
|
+
returning_values.map!(&:first) if returning_columns.size == 1
|
60
68
|
|
61
|
-
[ids,
|
69
|
+
[ids, returning_values, returning_columns]
|
62
70
|
end
|
63
71
|
|
64
72
|
def next_value_for_sequence(sequence_name)
|
@@ -79,19 +87,24 @@ module ActiveRecord::Import::PostgreSQLAdapter
|
|
79
87
|
|
80
88
|
sql += super(table_name, options)
|
81
89
|
|
82
|
-
|
83
|
-
unless
|
84
|
-
sql << " RETURNING
|
90
|
+
selections = returning_selections(options)
|
91
|
+
unless selections.blank? || (options[:no_returning] && !options[:recursive])
|
92
|
+
sql << " RETURNING #{selections.join(', ')}"
|
85
93
|
end
|
86
94
|
|
87
95
|
sql
|
88
96
|
end
|
89
97
|
|
90
|
-
def
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
98
|
+
def returning_selections(options)
|
99
|
+
selections = []
|
100
|
+
column_names = Array(options[:model].column_names)
|
101
|
+
|
102
|
+
selections += Array(options[:primary_key]) if options[:primary_key].present?
|
103
|
+
selections += Array(options[:returning]) if options[:returning].present?
|
104
|
+
|
105
|
+
selections.map do |selection|
|
106
|
+
column_names.include?(selection.to_s) ? "\"#{selection}\"" : selection
|
107
|
+
end
|
95
108
|
end
|
96
109
|
|
97
110
|
# Add a column to be updated on duplicate key update
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord::Import::SQLite3Adapter
|
2
4
|
include ActiveRecord::Import::ImportSupport
|
3
5
|
include ActiveRecord::Import::OnDuplicateKeyUpdateSupport
|
4
6
|
|
5
|
-
MIN_VERSION_FOR_IMPORT = "3.7.11"
|
6
|
-
MIN_VERSION_FOR_UPSERT = "3.24.0"
|
7
|
+
MIN_VERSION_FOR_IMPORT = "3.7.11"
|
8
|
+
MIN_VERSION_FOR_UPSERT = "3.24.0"
|
7
9
|
SQLITE_LIMIT_COMPOUND_SELECT = 500
|
8
10
|
|
9
11
|
# Override our conformance to ActiveRecord::Import::ImportSupport interface
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "pathname"
|
2
4
|
require "active_record"
|
3
5
|
require "active_record/version"
|
4
6
|
|
5
7
|
module ActiveRecord::Import
|
6
|
-
ADAPTER_PATH = "activerecord-import/active_record/adapters"
|
8
|
+
ADAPTER_PATH = "activerecord-import/active_record/adapters"
|
7
9
|
|
8
10
|
def self.base_adapter(adapter)
|
9
11
|
case adapter
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require "ostruct"
|
2
4
|
|
3
5
|
module ActiveRecord::Import::ConnectionAdapters; end
|
@@ -55,7 +57,7 @@ module ActiveRecord::Import #:nodoc:
|
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
58
|
-
filter.instance_variable_set(:@attributes, attrs)
|
60
|
+
filter.instance_variable_set(:@attributes, attrs.flatten)
|
59
61
|
|
60
62
|
if @validate_callbacks.respond_to?(:chain, true)
|
61
63
|
@validate_callbacks.send(:chain).tap do |chain|
|
@@ -547,7 +549,7 @@ class ActiveRecord::Base
|
|
547
549
|
alias import! bulk_import! unless ActiveRecord::Base.respond_to? :import!
|
548
550
|
|
549
551
|
def import_helper( *args )
|
550
|
-
options = { validate: true, timestamps: true, track_validation_failures: false }
|
552
|
+
options = { model: self, validate: true, timestamps: true, track_validation_failures: false }
|
551
553
|
options.merge!( args.pop ) if args.last.is_a? Hash
|
552
554
|
# making sure that current model's primary key is used
|
553
555
|
options[:primary_key] = primary_key
|
@@ -875,19 +877,28 @@ class ActiveRecord::Base
|
|
875
877
|
end
|
876
878
|
end
|
877
879
|
|
878
|
-
|
879
|
-
|
880
|
-
|
881
|
-
|
880
|
+
set_value = lambda do |model, column, value|
|
881
|
+
val = deserialize_value.call(column, value)
|
882
|
+
if model.attribute_names.include?(column)
|
883
|
+
model.send("#{column}=", val)
|
884
|
+
else
|
885
|
+
attributes = attributes_builder.build_from_database(model.attributes.merge(column => val))
|
886
|
+
model.instance_variable_set(:@attributes, attributes)
|
887
|
+
end
|
888
|
+
end
|
889
|
+
|
890
|
+
columns = Array(options[:returning_columns])
|
891
|
+
results = Array(import_result.results)
|
892
|
+
if models.size == results.size
|
893
|
+
single_column = columns.first if columns.size == 1
|
894
|
+
results.each_with_index do |result, index|
|
882
895
|
model = models[index]
|
883
896
|
|
884
897
|
if single_column
|
885
|
-
|
886
|
-
model.send(single_column, val)
|
898
|
+
set_value.call(model, single_column, result)
|
887
899
|
else
|
888
900
|
columns.each_with_index do |column, col_index|
|
889
|
-
|
890
|
-
model.send("#{column}=", val)
|
901
|
+
set_value.call(model, column, result[col_index])
|
891
902
|
end
|
892
903
|
end
|
893
904
|
end
|