activerecord-import 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/Gemfile +1 -0
- data/LICENSE +21 -56
- data/README.markdown +5 -2
- data/activerecord-import.gemspec +2 -2
- data/lib/activerecord-import/import.rb +22 -4
- data/lib/activerecord-import/version.rb +1 -1
- data/test/schema/postgresql_schema.rb +1 -0
- data/test/support/postgresql/import_examples.rb +9 -0
- data/test/support/shared_examples/recursive_import.rb +9 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 123bf726b813b21067d888221d627a03796f262de7e1b70c318def898b26d3c0
|
4
|
+
data.tar.gz: d6e6a3031944ac841587a8c29c701bc1a15667a888771333ad7198b085b2e8b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4409c4e0f048ea6272329d34de680d6314168784fdd4f244712ea37725254c66e4f091d302c0598e8232c31fbb9b94152197ee5dd660aa36d5df937839bf197d
|
7
|
+
data.tar.gz: 5c0e49836fdf38e4f95c863aa5c0841592d169063b5a455a90d2a16c8a65736b8837e05f371227b9c8e4a23c406e990421e1e3f7a1cb37e1e4d3f9b91b36521e
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
## Changes in 1.0.5
|
2
|
+
|
3
|
+
### Fixes
|
4
|
+
|
5
|
+
* Allow serialized attributes to be returned from import. Thanks to @timanovsky, @jkowens via \#660.
|
6
|
+
* Return ActiveRecord::Connection from
|
7
|
+
ActiveREcord::Base#establish_connection. Thanks to @reverentF via
|
8
|
+
\#663.
|
9
|
+
* Support PostgreSQL array. Thanks to @ujihisa via \#669.
|
10
|
+
* Skip loading association ids when column changed. Thanks to @Aristat
|
11
|
+
via \#673.
|
12
|
+
|
1
13
|
## Changes in 1.0.4
|
2
14
|
|
3
15
|
### Fixes
|
data/Gemfile
CHANGED
data/LICENSE
CHANGED
@@ -1,56 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
d) make other distribution arrangements with the author.
|
24
|
-
|
25
|
-
3. You may distribute the software in object code or binary form,
|
26
|
-
provided that you do at least ONE of the following:
|
27
|
-
|
28
|
-
a) distribute the binaries and library files of the software,
|
29
|
-
together with instructions (in the manual page or equivalent)
|
30
|
-
on where to get the original distribution.
|
31
|
-
|
32
|
-
b) accompany the distribution with the machine-readable source of
|
33
|
-
the software.
|
34
|
-
|
35
|
-
c) give non-standard binaries non-standard names, with
|
36
|
-
instructions on where to get the original software distribution.
|
37
|
-
|
38
|
-
d) make other distribution arrangements with the author.
|
39
|
-
|
40
|
-
4. You may modify and include the part of the software into any other
|
41
|
-
software (possibly commercial). But some files in the distribution
|
42
|
-
are not written by the author, so that they are not under these terms.
|
43
|
-
|
44
|
-
For the list of those files and their copying conditions, see the
|
45
|
-
file LEGAL.
|
46
|
-
|
47
|
-
5. The scripts and library files supplied as input to or produced as
|
48
|
-
output from the software do not automatically fall under the
|
49
|
-
copyright of the software, but belong to whomever generated them,
|
50
|
-
and may be sold commercially, and may be aggregated with this
|
51
|
-
software.
|
52
|
-
|
53
|
-
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
-
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
-
PURPOSE.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Zach Dennis <zach.dennis@gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.markdown
CHANGED
@@ -60,6 +60,8 @@ The gem provides the following high-level features:
|
|
60
60
|
|
61
61
|
#### Introduction
|
62
62
|
|
63
|
+
This gem adds an `import` method (or `bulk_import`, for compatibility with gems like `elasticsearch-model`; see [Conflicts With Other Gems](#conflicts-with-other-gems)) to ActiveRecord classes.
|
64
|
+
|
63
65
|
Without `activerecord-import`, you'd write something like this:
|
64
66
|
|
65
67
|
```ruby
|
@@ -231,7 +233,8 @@ Book.import columns, books, batch_size: 2
|
|
231
233
|
|
232
234
|
#### Recursive
|
233
235
|
|
234
|
-
NOTE: This only works with PostgreSQL.
|
236
|
+
NOTE: This only works with PostgreSQL and ActiveRecord objects. This won't work with
|
237
|
+
hashes or arrays as recursive inputs.
|
235
238
|
|
236
239
|
Assume that Books <code>has_many</code> Reviews.
|
237
240
|
|
@@ -613,7 +616,7 @@ You can triage issues which may include reproducing bug reports or asking for vi
|
|
613
616
|
|
614
617
|
# License
|
615
618
|
|
616
|
-
This is licensed under the
|
619
|
+
This is licensed under the MIT license.
|
617
620
|
|
618
621
|
# Author
|
619
622
|
|
data/activerecord-import.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.summary = "Bulk insert extension for ActiveRecord"
|
8
8
|
gem.description = "A library for bulk inserting data using ActiveRecord."
|
9
9
|
gem.homepage = "http://github.com/zdennis/activerecord-import"
|
10
|
-
gem.license = "
|
10
|
+
gem.license = "MIT"
|
11
11
|
|
12
12
|
gem.files = `git ls-files`.split($\)
|
13
13
|
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.require_paths = ["lib"]
|
17
17
|
gem.version = ActiveRecord::Import::VERSION
|
18
18
|
|
19
|
-
gem.required_ruby_version = ">=
|
19
|
+
gem.required_ruby_version = ">= 2.0.0"
|
20
20
|
|
21
21
|
gem.add_runtime_dependency "activerecord", ">= 3.2"
|
22
22
|
gem.add_development_dependency "rake"
|
@@ -241,8 +241,9 @@ end
|
|
241
241
|
|
242
242
|
module ActiveRecord::Import::Connection
|
243
243
|
def establish_connection(args = nil)
|
244
|
-
super(args)
|
244
|
+
conn = super(args)
|
245
245
|
ActiveRecord::Import.load_from_connection_pool connection_pool
|
246
|
+
conn
|
246
247
|
end
|
247
248
|
end
|
248
249
|
|
@@ -835,6 +836,19 @@ class ActiveRecord::Base
|
|
835
836
|
end
|
836
837
|
end
|
837
838
|
|
839
|
+
deserialize_value = lambda do |column, value|
|
840
|
+
column = columns_hash[column]
|
841
|
+
return value unless column
|
842
|
+
if respond_to?(:type_caster)
|
843
|
+
type = type_for_attribute(column.name)
|
844
|
+
type.deserialize(value)
|
845
|
+
elsif column.respond_to?(:type_cast_from_database)
|
846
|
+
column.type_cast_from_database(value)
|
847
|
+
else
|
848
|
+
value
|
849
|
+
end
|
850
|
+
end
|
851
|
+
|
838
852
|
if models.size == import_result.results.size
|
839
853
|
columns = Array(options[:returning])
|
840
854
|
single_column = "#{columns.first}=" if columns.size == 1
|
@@ -842,10 +856,12 @@ class ActiveRecord::Base
|
|
842
856
|
model = models[index]
|
843
857
|
|
844
858
|
if single_column
|
845
|
-
|
859
|
+
val = deserialize_value.call(columns.first, result)
|
860
|
+
model.send(single_column, val)
|
846
861
|
else
|
847
862
|
columns.each_with_index do |column, col_index|
|
848
|
-
|
863
|
+
val = deserialize_value.call(column, result[col_index])
|
864
|
+
model.send("#{column}=", val)
|
849
865
|
end
|
850
866
|
end
|
851
867
|
end
|
@@ -866,10 +882,12 @@ class ActiveRecord::Base
|
|
866
882
|
|
867
883
|
# Sync belongs_to association ids with foreign key field
|
868
884
|
def load_association_ids(model)
|
885
|
+
changed_columns = model.changed
|
869
886
|
association_reflections = model.class.reflect_on_all_associations(:belongs_to)
|
870
887
|
association_reflections.each do |association_reflection|
|
871
888
|
column_name = association_reflection.foreign_key
|
872
889
|
next if association_reflection.options[:polymorphic]
|
890
|
+
next if changed_columns.include?(column_name)
|
873
891
|
association = model.association(association_reflection.name)
|
874
892
|
association = association.target
|
875
893
|
next if association.blank? || model.public_send(column_name).present?
|
@@ -949,7 +967,7 @@ class ActiveRecord::Base
|
|
949
967
|
elsif column
|
950
968
|
if respond_to?(:type_caster) # Rails 5.0 and higher
|
951
969
|
type = type_for_attribute(column.name)
|
952
|
-
val = type.type == :boolean ? type.cast(val) : type.serialize(val)
|
970
|
+
val = !type.respond_to?(:subtype) && type.type == :boolean ? type.cast(val) : type.serialize(val)
|
953
971
|
connection_memo.quote(val)
|
954
972
|
elsif column.respond_to?(:type_cast_from_user) # Rails 4.2
|
955
973
|
connection_memo.quote(column.type_cast_from_user(val), column)
|
@@ -127,6 +127,15 @@ def should_support_postgresql_import_functionality
|
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
130
|
+
context "when a returning column is a serialized attribute" do
|
131
|
+
let(:vendor) { Vendor.new(hours: { monday: '8-5' }) }
|
132
|
+
let(:result) { Vendor.import([vendor], returning: %w(hours)) }
|
133
|
+
|
134
|
+
it "creates records" do
|
135
|
+
assert_difference("Vendor.count", +1) { result }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
130
139
|
context "when primary key and returning overlap" do
|
131
140
|
let(:result) { Book.import(books, returning: %w(id title)) }
|
132
141
|
|
@@ -138,6 +138,15 @@ def should_support_recursive_import
|
|
138
138
|
books.each do |book|
|
139
139
|
assert_equal book.topic_id, second_new_topic.id
|
140
140
|
end
|
141
|
+
|
142
|
+
books.each { |book| book.topic_id = nil }
|
143
|
+
assert_no_difference "Book.count", books.size do
|
144
|
+
Book.import books, validate: false, on_duplicate_key_update: [:topic_id]
|
145
|
+
end
|
146
|
+
|
147
|
+
books.each do |book|
|
148
|
+
assert_equal book.topic_id, nil
|
149
|
+
end
|
141
150
|
end
|
142
151
|
|
143
152
|
unless ENV["SKIP_COMPOSITE_PK"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Dennis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -168,7 +168,7 @@ files:
|
|
168
168
|
- test/value_sets_records_parser_test.rb
|
169
169
|
homepage: http://github.com/zdennis/activerecord-import
|
170
170
|
licenses:
|
171
|
-
-
|
171
|
+
- MIT
|
172
172
|
metadata: {}
|
173
173
|
post_install_message:
|
174
174
|
rdoc_options: []
|
@@ -178,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
178
178
|
requirements:
|
179
179
|
- - ">="
|
180
180
|
- !ruby/object:Gem::Version
|
181
|
-
version:
|
181
|
+
version: 2.0.0
|
182
182
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
183
|
requirements:
|
184
184
|
- - ">="
|