mass_insert 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 26d14bfd22069edf29737a7eb97321e3292d558a
4
- data.tar.gz: 3185e4f1caae8fda79464859507f9448c2fdb572
3
+ metadata.gz: 18392b0e959d9ca325b062eebdb89f481b94d654
4
+ data.tar.gz: b1b910f77458d48fbcdd6db93793eb44d358900f
5
5
  SHA512:
6
- metadata.gz: 8b9163e8d0941d1ffa6a206ba26a8c5da737e1db9f0664770fc7cc2411c580cf576e36196c8bb9b2068f87222acac65d084bdde4fba0df174e164a4fbbaafe38
7
- data.tar.gz: b6c807db5bee2b7b504bbd12f2645640a6cfc96a0a1ea1e5385f53a789bac61fc19e4ab83699db323469b2e814e9b499e2a44beb7e7202a0d5c719fa016bf146
6
+ metadata.gz: 210e4f0303a3ed783414754c11097f1d0709dbe8800f40af813d19747cf6996372a6580d9933f9a7a20d751e35e874d8721bcac3cf77dbb9c868eac12393e834
7
+ data.tar.gz: 874eb4f0c1d3a57d9c1167637f598018629d1b0b35650ba962c4cff877dd8a427f860e908eb8ea168430924811f98e4fe14355bbc8b6598110435b2cb37771e6
data/README.md CHANGED
@@ -44,6 +44,24 @@ User.mass_insert(values)
44
44
  ```
45
45
 
46
46
 
47
+
48
+ ### Allow primary key
49
+ Sometimes you can need to insert records forcing primary keys.
50
+ Just pass the `primary_key` option with true. Example...
51
+ ```ruby
52
+ values = [
53
+ {
54
+ id: 1000, # Force primary key.
55
+ name: 'Jay',
56
+ email: 'tremendous_gamer@gmail.com',
57
+ age: 15
58
+ }
59
+ ]
60
+
61
+ User.mass_insert(values, primary_key: true)
62
+ ```
63
+
64
+
47
65
  ### Insertion per batches
48
66
  Due you can get a database timeout error you can specify that the insertion will be in batches.
49
67
  Just pass the `per_batch` option with the records per batch. Example...
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- mass_insert (0.2.3)
4
+ mass_insert (0.2.5)
5
5
  activerecord (>= 4.0)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- mass_insert (0.2.3)
4
+ mass_insert (0.2.5)
5
5
  activerecord (>= 4.0)
6
6
 
7
7
  GEM
@@ -3,10 +3,10 @@ module MassInsert
3
3
  class AbstractAdapter < SimpleDelegator
4
4
  attr_accessor :values, :options
5
5
 
6
- def initialize(class_name, values, options)
6
+ def initialize(values, options)
7
+ super(options.fetch(:class_name))
7
8
  @values = values
8
9
  @options = options
9
- super(class_name)
10
10
  end
11
11
 
12
12
  def to_sql
@@ -18,7 +18,7 @@ module MassInsert
18
18
  def columns
19
19
  @columns ||= begin
20
20
  columns = column_names
21
- columns.delete(primary_key)
21
+ columns.delete(primary_key) unless options[:primary_key]
22
22
  columns.map(&:to_sym)
23
23
  end
24
24
  end
@@ -1,7 +1,8 @@
1
1
  module MassInsert
2
2
  module Base
3
3
  def mass_insert(values, options = {})
4
- options[:class_name] ||= self
4
+ options[:class_name] ||= self
5
+ options[:primary_key] ||= false
5
6
  Process.new(values, options).start
6
7
  end
7
8
  end
@@ -1,8 +1,7 @@
1
1
  module MassInsert
2
2
  class Builder
3
3
  def build(values, options)
4
- class_name = options[:class_name]
5
- Utilities.adapter_class.new(class_name, values, options).to_sql
4
+ Utilities.adapter_class.new(values, options).to_sql
6
5
  end
7
6
  end
8
7
  end
@@ -1,3 +1,3 @@
1
1
  module MassInsert
2
- VERSION = '0.2.4'
2
+ VERSION = '0.2.5'
3
3
  end
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
data/test/schema.rb CHANGED
@@ -1,15 +1,20 @@
1
1
  ActiveRecord::Schema.define do
2
2
  create_table :users, force: true do |t|
3
- t.string :name
3
+ t.string :name
4
4
  t.boolean :active
5
5
  t.integer :age
6
6
  t.decimal :money, precision: 10, scale: 2
7
- t.date :birth_date
7
+ t.date :birth_date
8
8
  t.timestamps null: false
9
9
  end
10
10
 
11
11
  create_table :kinds, force: true do |t|
12
- t.string :name
12
+ t.string :name
13
13
  t.index ['name'], name: 'index_kinds_on_name', unique: true, using: :btree
14
14
  end
15
+
16
+ create_table :posts, force: true, id: false do |t|
17
+ t.integer :id, primary_key: true
18
+ t.string :name
19
+ end
15
20
  end
@@ -14,6 +14,7 @@ def shared_examples
14
14
 
15
15
  after :each do
16
16
  User.delete_all
17
+ Post.delete_all
17
18
  end
18
19
 
19
20
  describe 'Column types' do
@@ -26,31 +27,63 @@ def shared_examples
26
27
  end
27
28
 
28
29
  it 'saves values correctly' do
29
- assert_equal user.name, 'User Name'
30
- assert_equal user.active, true
31
- assert_equal user.age, 25
32
- assert_equal user.money, 20.50
33
- assert_equal user.birth_date, Date.today
30
+ assert_equal 'User Name', user.name
31
+ assert_equal true, user.active
32
+ assert_equal 25, user.age
33
+ assert_equal 20.50, user.money
34
+ assert_equal Date.today, user.birth_date
34
35
  end
35
36
  end
36
37
 
37
38
  describe 'Bulk inseting' do
38
39
  def array_of_values_with(size)
39
- (0...size).map do |i|
40
- attributes
41
- end
40
+ Array.new(size) { attributes }
42
41
  end
43
42
 
44
43
  it 'saves 500 records correctly' do
45
44
  values = array_of_values_with(500)
46
45
  User.mass_insert(values)
47
- assert_equal User.count, 500
46
+ assert_equal 500, User.count
48
47
  end
49
48
 
50
49
  it 'saves 1000 records correctly' do
51
50
  values = array_of_values_with(1000)
52
51
  User.mass_insert(values)
53
- assert_equal User.count, 1000
52
+ assert_equal 1000, User.count
53
+ end
54
+ end
55
+
56
+ describe 'Primary key' do
57
+ describe 'when primary key is true' do
58
+ def setup
59
+ Post.mass_insert([
60
+ { id: 10_000, name: 'Post' }
61
+ ], primary_key: true)
62
+ end
63
+
64
+ def post
65
+ @post ||= Post.last
66
+ end
67
+
68
+ it 'saves values correctly' do
69
+ assert_equal 10_000, post.id
70
+ end
71
+ end
72
+
73
+ describe 'when primary key is false' do
74
+ def setup
75
+ User.mass_insert([
76
+ attributes.merge(id: 10_000)
77
+ ], primary_key: false)
78
+ end
79
+
80
+ def user
81
+ @user ||= User.last
82
+ end
83
+
84
+ it 'saves values incorrectly' do
85
+ refute_equal 10_000, user.id
86
+ end
54
87
  end
55
88
  end
56
89
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mass_insert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Gutiérrez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-17 00:00:00.000000000 Z
11
+ date: 2017-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -117,6 +117,7 @@ files:
117
117
  - test/adapters/sqlite3/example_test.rb
118
118
  - test/database.yml.example
119
119
  - test/models/kind.rb
120
+ - test/models/post.rb
120
121
  - test/models/user.rb
121
122
  - test/schema.rb
122
123
  - test/support/adapters/mysql2.rb
@@ -144,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
145
  version: '0'
145
146
  requirements: []
146
147
  rubyforge_project:
147
- rubygems_version: 2.5.1
148
+ rubygems_version: 2.6.8
148
149
  signing_key:
149
150
  specification_version: 4
150
151
  summary: Mass database insertion in Rails
@@ -154,6 +155,7 @@ test_files:
154
155
  - test/adapters/sqlite3/example_test.rb
155
156
  - test/database.yml.example
156
157
  - test/models/kind.rb
158
+ - test/models/post.rb
157
159
  - test/models/user.rb
158
160
  - test/schema.rb
159
161
  - test/support/adapters/mysql2.rb