active_record_bulk_insert 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 480b59f2af018604382897c14b7429039abb6e92
4
- data.tar.gz: c9ac794a5474b28fd6475536aa9dae2f1ee67af4
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MjEyNGY3MzlkZTc3ZDkyOTRkYjdkMGFiZjUwYzVjNTFmN2M0NDVmYQ==
5
+ data.tar.gz: !binary |-
6
+ NGFmMWVkN2U1ZWFmNTZmMDJiOTgxYzRjM2M4OWI1M2RkOThiYTNhMQ==
5
7
  SHA512:
6
- metadata.gz: 326b9b67bfa9c11aff90d89a7056335a94b95c5d707a6b0780ac37aa4a0ae425d81e90f992445ba16fb8b48fdd8fe85070377299a1c1f098bcf5d10ebdefc167
7
- data.tar.gz: c880802bcb693a22f981818fc6e0f7fffcca71be2530e6e68f296373b64e3c46be866c67e3d9315d639923123ec373780ad2ad14a6bc7a771c92f3893da0fb95
8
+ metadata.gz: !binary |-
9
+ YWQ5MDA0ZmY0ZjllNDc0ZGI4ZjY5OTFjYmYxNjFhNzdhZWRiZGExYTEzMTdm
10
+ Mzk4Zjc3MzBjZDhjZWY5ZDZjMjlmMWVlYzUzOWY3ZTFhN2FmZTg2ZjgzMDFk
11
+ ODU0YTNiZWM1MGQ3ZTFkZTZlNzdlOGZjN2JkZDIwMmVkNTU2OGM=
12
+ data.tar.gz: !binary |-
13
+ MmU5MmRjMWUzMDQ0MzRmMmE3ZjJjNGJmZjQ0OTI4NDRmOTUyMGY2ODg2MDA4
14
+ NWNlMThjZGUwNzVlMGI2Y2ZiNmFiZGFmNTZjNTM2NjVkOTc0N2RhMDY4NmFl
15
+ YWJiOTU2MDg4ZDBmMDIwMDZhMTliNjQwNTgzN2EyYWQ0NDJkY2U=
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/active_record_bulk_insert.svg)](http://badge.fury.io/rb/active_record_bulk_insert)
2
+ [![Build Status](https://api.travis-ci.org/bjhaid/active_record_bulk_insert.png)](https://travis-ci.org/bjhaid/active_record_bulk_insert)
2
3
 
3
4
  # BULK INSERT
4
5
 
@@ -23,7 +23,10 @@ ActiveRecord::Base.class_eval do
23
23
  end
24
24
 
25
25
  values_sql = attrs.map do |record|
26
- "(#{_resolve_record(record, options).map {|k, v| connection.quote(v, try(:column_for_attribute, k)) }.join(', ')})"
26
+ quoted = _resolve_record(record, options).map {|k, v|
27
+ _bulk_insert_quote(k, v)
28
+ }
29
+ "(#{quoted.join(', ')})"
27
30
  end.join(",")
28
31
 
29
32
  sql = <<-SQL
@@ -36,6 +39,20 @@ ActiveRecord::Base.class_eval do
36
39
  invalid
37
40
  end
38
41
 
42
+ def self._bulk_insert_quote(key, value)
43
+ case ActiveRecord::VERSION::MAJOR
44
+ when 5
45
+ column = try(:column_for_attribute, value)
46
+ value = connection.type_cast_from_column(column, value) if column
47
+ connection.quote(value)
48
+ when 4
49
+ column = try(:column_for_attribute, key)
50
+ connection.quote(value, column)
51
+ else
52
+ sanitize(value)
53
+ end
54
+ end
55
+
39
56
  def self._resolve_record(record, options)
40
57
  time = ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now
41
58
  _record = record.is_a?(ActiveRecord::Base) ? record.attributes : record
@@ -31,27 +31,6 @@ describe SampleRecord do
31
31
  SampleRecord.bulk_insert(records)
32
32
  end
33
33
 
34
- it "relies on the DB to provide primary_key if :use_provided_primary_key is false or nil" do
35
- records = 10.times.map { |i| SampleRecord.new(:id => 10000 + i, :age => 4, :name => "Foo#{i}") }
36
-
37
- ActiveRecord::ConnectionAdapters::SQLite3Adapter.any_instance.should_receive(:execute).with do |params|
38
- records.each do |record|
39
- params.should_not include(record.id.to_s)
40
- end
41
- end
42
-
43
- SampleRecord.bulk_insert(records)
44
- end
45
-
46
- it "uses provided primary_key if :use_provided_primary_key is true" do
47
- records = 10.times.map { |i| SampleRecord.new(:id => 10000 + i, :age => 4, :name => "Foo#{i}") }
48
-
49
- SampleRecord.bulk_insert(records, :use_provided_primary_key => true)
50
- records.each do |record|
51
- SampleRecord.exists?(:id => record.id).should be_true
52
- end
53
- end
54
-
55
34
  it "support insertion of ActiveRecord objects" do
56
35
  records = 10.times.map { |i| SampleRecord.new(:age => 4, :name => "Foo#{i}") }
57
36
 
@@ -73,6 +52,31 @@ describe SampleRecord do
73
52
  end.to_not raise_error
74
53
  end
75
54
 
55
+ if ActiveRecord::VERSION::MAJOR >= 4
56
+ context "use_provided_primary_key" do
57
+ it "relies on the DB to provide primary_key if :use_provided_primary_key is false or nil" do
58
+ records = 10.times.map { |i| SampleRecord.new(:id => 10000 + i, :age => 4, :name => "Foo#{i}") }
59
+
60
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.any_instance.should_receive(:execute).with do |params|
61
+ records.each do |record|
62
+ params.should_not include(record.id.to_s)
63
+ end
64
+ end
65
+
66
+ SampleRecord.bulk_insert(records)
67
+ end
68
+
69
+ it "uses provided primary_key if :use_provided_primary_key is true" do
70
+ records = 10.times.map { |i| SampleRecord.new(:id => 10000 + i, :age => 4, :name => "Foo#{i}") }
71
+
72
+ SampleRecord.bulk_insert(records, :use_provided_primary_key => true)
73
+ records.each do |record|
74
+ SampleRecord.exists?(:id => record.id).should be_true
75
+ end
76
+ end
77
+ end
78
+ end
79
+
76
80
  context "validations" do
77
81
  it "should not persist invalid records if ':validate => true' is specified" do
78
82
  SampleRecord.send(:validates, :name, :presence => true)
data/spec/spec_helper.rb CHANGED
@@ -4,9 +4,17 @@ require "database_cleaner"
4
4
  require "support/sample_record"
5
5
 
6
6
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
7
- ActiveRecord::Migration.verbose = false
7
+ if ActiveRecord::VERSION::MAJOR == 5
8
+ MIGRATION_CLASS = ActiveRecord::Migration[5.0]
9
+ else
10
+ MIGRATION_CLASS = ActiveRecord::Migration
11
+ end
12
+
13
+ MIGRATION_CLASS.verbose = false
8
14
  ActiveRecord::Migrator.migrate("spec/support/migrations")
9
15
 
16
+ I18n.config.enforce_available_locales = false
17
+
10
18
  RSpec.configure do |config|
11
19
  config.treat_symbols_as_metadata_keys_with_true_values = true
12
20
  config.order = "random"
@@ -1,9 +1,9 @@
1
- class SampleRecordMigration < ActiveRecord::Migration
1
+ class SampleRecordMigration < MIGRATION_CLASS
2
2
  def change
3
3
  create_table :sample_records do |t|
4
4
  t.text "name"
5
5
  t.integer "age"
6
- t.timestamps
6
+ t.timestamps null: true
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_bulk_insert
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abejide Ayodele
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-06 00:00:00.000000000 Z
11
+ date: 2016-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - ! '>='
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.0
19
+ version: 3.2.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - ! '>='
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.0
26
+ version: 3.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: database_cleaner
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '='
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
- version: 1.0.1
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '='
38
+ - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
- version: 1.0.1
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: sqlite3
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '='
59
+ - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: 1.3.7
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '='
66
+ - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
- version: 1.3.7
68
+ version: '0'
69
69
  description: Exposes a bulk insert API to AR subclasses
70
70
  email:
71
71
  - abejideayodele@gmail.com
@@ -89,17 +89,17 @@ require_paths:
89
89
  - lib
90
90
  required_ruby_version: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - ">="
92
+ - - ! '>='
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  requirements:
97
- - - ">="
97
+ - - ! '>='
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
101
  rubyforge_project:
102
- rubygems_version: 2.4.5.1
102
+ rubygems_version: 2.4.8
103
103
  signing_key:
104
104
  specification_version: 4
105
105
  summary: bulk insert records into the DB