activerecord-import 0.19.0 → 0.19.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/lib/activerecord-import/import.rb +10 -9
- data/lib/activerecord-import/value_sets_parser.rb +14 -0
- data/lib/activerecord-import/version.rb +1 -1
- data/test/support/postgresql/import_examples.rb +24 -0
- data/test/value_sets_bytes_parser_test.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ccaefe9611ff80d350abf4beca0e741a23c0167
|
4
|
+
data.tar.gz: 374da8395b259808c29d102df17bde738046d428
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 527652ab74bcf4a44014ba016b846f403e0d94a1b57450d36ea35a65702fcd15a9c273ab82de42278c0fc1c7dd84baee16425d0afab17d425ba36cebbb500d56
|
7
|
+
data.tar.gz: 82f9e31dfe9c002eddedb59e34b4a6f3d3763504f5932d8fbd15a8c39630ae385a5fdbe6006d5aaef5841a6d158eedc19f8158b090f9e1fd7fd43ef219d6aa8d
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## Changes in 0.19.1
|
2
|
+
|
3
|
+
### Fixes
|
4
|
+
|
5
|
+
* Fix a regression where models weren't properly being marked clean. Thanks to @tjwp via \#434.
|
6
|
+
* Raise ActiveRecord::Import::ValueSetTooLargeError when a record being inserted exceeds the
|
7
|
+
`max_allowed_packet` for MySQL. Thanks to @saizai, @jkowens via \#437.
|
8
|
+
* Fix issue concatenating column names array with primary key. Thanks to @keeguon via \#440.
|
9
|
+
|
1
10
|
## Changes in 0.19.0
|
2
11
|
|
3
12
|
### New Features
|
@@ -497,7 +497,7 @@ class ActiveRecord::Base
|
|
497
497
|
|
498
498
|
if !symbolized_primary_key.to_set.subset?(symbolized_column_names.to_set) && connection.prefetch_primary_key? && sequence_name
|
499
499
|
column_count = column_names.size
|
500
|
-
column_names.concat(primary_key).uniq!
|
500
|
+
column_names.concat(Array(primary_key)).uniq!
|
501
501
|
columns_added = column_names.size - column_count
|
502
502
|
new_fields = Array.new(columns_added)
|
503
503
|
array_of_attributes.each { |a| a.concat(new_fields) }
|
@@ -657,14 +657,6 @@ class ActiveRecord::Base
|
|
657
657
|
def set_attributes_and_mark_clean(models, import_result, timestamps)
|
658
658
|
return if models.nil?
|
659
659
|
models -= import_result.failed_instances
|
660
|
-
models.each do |model|
|
661
|
-
if model.respond_to?(:clear_changes_information) # Rails 4.0 and higher
|
662
|
-
model.clear_changes_information
|
663
|
-
else # Rails 3.2
|
664
|
-
model.instance_variable_get(:@changed_attributes).clear
|
665
|
-
end
|
666
|
-
model.instance_variable_set(:@new_record, false)
|
667
|
-
end
|
668
660
|
|
669
661
|
# if ids were returned for all models we know all were updated
|
670
662
|
if models.size == import_result.ids.size
|
@@ -677,6 +669,15 @@ class ActiveRecord::Base
|
|
677
669
|
end
|
678
670
|
end
|
679
671
|
end
|
672
|
+
|
673
|
+
models.each do |model|
|
674
|
+
if model.respond_to?(:clear_changes_information) # Rails 4.0 and higher
|
675
|
+
model.clear_changes_information
|
676
|
+
else # Rails 3.2
|
677
|
+
model.instance_variable_get(:@changed_attributes).clear
|
678
|
+
end
|
679
|
+
model.instance_variable_set(:@new_record, false)
|
680
|
+
end
|
680
681
|
end
|
681
682
|
|
682
683
|
def import_associations(models, options)
|
@@ -1,4 +1,12 @@
|
|
1
1
|
module ActiveRecord::Import
|
2
|
+
class ValueSetTooLargeError < StandardError
|
3
|
+
attr_reader :size
|
4
|
+
def initialize(msg = "Value set exceeds max size", size = 0)
|
5
|
+
@size = size
|
6
|
+
super(msg)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
2
10
|
class ValueSetsBytesParser
|
3
11
|
attr_reader :reserved_bytes, :max_bytes, :values
|
4
12
|
|
@@ -18,6 +26,12 @@ module ActiveRecord::Import
|
|
18
26
|
current_size = 0
|
19
27
|
values.each_with_index do |val, i|
|
20
28
|
comma_bytes = arr.size
|
29
|
+
insert_size = reserved_bytes + val.bytesize
|
30
|
+
|
31
|
+
if insert_size > max_bytes
|
32
|
+
raise ValueSetTooLargeError.new("#{insert_size} bytes exceeds the max allowed for an insert [#{@max_bytes}]", insert_size)
|
33
|
+
end
|
34
|
+
|
21
35
|
bytes_thus_far = reserved_bytes + current_size + val.bytesize + comma_bytes
|
22
36
|
if bytes_thus_far <= max_bytes
|
23
37
|
current_size += val.bytesize
|
@@ -24,6 +24,30 @@ def should_support_postgresql_import_functionality
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
context "setting attributes and marking clean" do
|
28
|
+
let(:topic) { Build(:topics) }
|
29
|
+
|
30
|
+
setup { Topic.import([topic]) }
|
31
|
+
|
32
|
+
it "assigns ids" do
|
33
|
+
assert topic.id.present?
|
34
|
+
end
|
35
|
+
|
36
|
+
it "marks models as clean" do
|
37
|
+
assert !topic.changed?
|
38
|
+
end
|
39
|
+
|
40
|
+
it "marks models as persisted" do
|
41
|
+
assert !topic.new_record?
|
42
|
+
assert topic.persisted?
|
43
|
+
end
|
44
|
+
|
45
|
+
it "assigns timestamps" do
|
46
|
+
assert topic.created_at.present?
|
47
|
+
assert topic.updated_at.present?
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
27
51
|
describe "with query cache enabled" do
|
28
52
|
setup do
|
29
53
|
unless ActiveRecord::Base.connection.query_cache_enabled
|
@@ -8,6 +8,15 @@ describe ActiveRecord::Import::ValueSetsBytesParser do
|
|
8
8
|
let(:base_sql) { "INSERT INTO atable (a,b,c)" }
|
9
9
|
let(:values) { ["(1,2,3)", "(2,3,4)", "(3,4,5)"] }
|
10
10
|
|
11
|
+
context "when the max allowed bytes is 30 and the base SQL is 26 bytes" do
|
12
|
+
it "should raise ActiveRecord::Import::ValueSetTooLargeError" do
|
13
|
+
error = assert_raises ActiveRecord::Import::ValueSetTooLargeError do
|
14
|
+
parser.parse values, reserved_bytes: base_sql.size, max_bytes: 30
|
15
|
+
end
|
16
|
+
assert_match(/33 bytes exceeds the max allowed for an insert \[30\]/, error.message)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
11
20
|
context "when the max allowed bytes is 33 and the base SQL is 26 bytes" do
|
12
21
|
it "should return 3 value sets when given 3 value sets of 7 bytes a piece" do
|
13
22
|
value_sets = parser.parse values, reserved_bytes: base_sql.size, max_bytes: 33
|
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: 0.19.
|
4
|
+
version: 0.19.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Dennis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|