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 +4 -4
- data/README.md +18 -0
- data/gemfiles/rails_4.gemfile.lock +1 -1
- data/gemfiles/rails_5.gemfile.lock +1 -1
- data/lib/mass_insert/adapters/abstract_adapter.rb +3 -3
- data/lib/mass_insert/base.rb +2 -1
- data/lib/mass_insert/builder.rb +1 -2
- data/lib/mass_insert/version.rb +1 -1
- data/test/models/post.rb +2 -0
- data/test/schema.rb +8 -3
- data/test/support/shared_examples.rb +43 -10
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 18392b0e959d9ca325b062eebdb89f481b94d654
|
4
|
+
data.tar.gz: b1b910f77458d48fbcdd6db93793eb44d358900f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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...
|
@@ -3,10 +3,10 @@ module MassInsert
|
|
3
3
|
class AbstractAdapter < SimpleDelegator
|
4
4
|
attr_accessor :values, :options
|
5
5
|
|
6
|
-
def initialize(
|
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
|
data/lib/mass_insert/base.rb
CHANGED
data/lib/mass_insert/builder.rb
CHANGED
data/lib/mass_insert/version.rb
CHANGED
data/test/models/post.rb
ADDED
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
|
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
|
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
|
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
|
30
|
-
assert_equal user.active
|
31
|
-
assert_equal user.age
|
32
|
-
assert_equal
|
33
|
-
assert_equal
|
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
|
-
(
|
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
|
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
|
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
|
+
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:
|
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.
|
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
|