mass_insert 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -3
- data/README.md +25 -1
- data/lib/mass_insert/adapters/abstract_adapter.rb +3 -3
- data/lib/mass_insert/adapters/mysql2_adapter.rb +17 -1
- data/lib/mass_insert/version.rb +1 -1
- data/test/adapters/mysql2/example_test.rb +26 -0
- data/test/database.yml.example +9 -7
- data/test/models/kind.rb +2 -0
- data/test/schema.rb +5 -1
- data/test/test_helper.rb +11 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 776d7684b8b9dbc9d4232b18baa16db73c2e30e2
|
4
|
+
data.tar.gz: 5f3abd0e966d88cbf1038a5591ace5fba11170b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8574e134b4e067efba5ab705e88c5ebffa6426dc4148f2849b43aceb1ce48cc154645f19fdb82dcb7c2b9136283a7de425011138edfe1e3e55180a92dcd26b59
|
7
|
+
data.tar.gz: 1ba5fedf3377bb938a872545f9a4c0980ddf4bc88d386e489b3189720ac208b4a1ecab3bdb409dbda3caf7b478d4afeb67c9452e9458df3b9f4c5badd5e532fb
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -44,7 +44,7 @@ User.mass_insert(values)
|
|
44
44
|
```
|
45
45
|
|
46
46
|
|
47
|
-
|
47
|
+
### Insertion per batches
|
48
48
|
Due you can get a database timeout error you can specify that the insertion will be in batches.
|
49
49
|
Just pass the `per_batch` option with the records per batch. Example...
|
50
50
|
```ruby
|
@@ -52,6 +52,30 @@ User.mass_insert(values, per_batch: 1000)
|
|
52
52
|
```
|
53
53
|
|
54
54
|
|
55
|
+
### Handle unique index on MySQL
|
56
|
+
Sometimes we want to ignore errors when adding duplicated records. MySQL has
|
57
|
+
the ability to do that with `ON DUPLICATE KEY UPDATE`. By using the option
|
58
|
+
`handle_duplication` we will ignore the new values by doing:
|
59
|
+
```ruby
|
60
|
+
User.mass_insert(values, handle_duplication: true)
|
61
|
+
```
|
62
|
+
|
63
|
+
```sql
|
64
|
+
INSERT INTO table (a,b,c) VALUES (1,2,3)
|
65
|
+
ON DUPLICATE KEY UPDATE a=a,b=b,c=c;
|
66
|
+
```
|
67
|
+
|
68
|
+
[Read more about MySQL ON DUPLICATE KEY UPDATE...](http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html)
|
69
|
+
|
70
|
+
|
71
|
+
## Running tests
|
72
|
+
First at all copy `test/database.yml.example` to `test/database.yml` and update username and password
|
73
|
+
for every database adapters. Then, run the following to test the gem against all adapters.
|
74
|
+
```
|
75
|
+
bundle exec rake test:all
|
76
|
+
```
|
77
|
+
|
78
|
+
|
55
79
|
## Contributing
|
56
80
|
1. Fork it
|
57
81
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
@@ -10,7 +10,7 @@ module MassInsert
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def to_sql
|
13
|
-
"#{insert_sql} #{values_sql}"
|
13
|
+
"#{insert_sql} #{values_sql};"
|
14
14
|
end
|
15
15
|
|
16
16
|
private
|
@@ -38,14 +38,14 @@ module MassInsert
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def values_sql
|
41
|
-
"(#{array_of_attributes_sql.join('),(')})
|
41
|
+
"(#{array_of_attributes_sql.join('),(')})"
|
42
42
|
end
|
43
43
|
|
44
44
|
def array_of_attributes_sql
|
45
45
|
values.map do |attrs|
|
46
46
|
columns.map do |name|
|
47
47
|
value = attrs[name.to_sym] || attrs[name.to_s]
|
48
|
-
connection.quote(value
|
48
|
+
connection.quote(value)
|
49
49
|
end.join(',')
|
50
50
|
end
|
51
51
|
end
|
@@ -1,5 +1,21 @@
|
|
1
1
|
module MassInsert
|
2
2
|
module Adapters
|
3
|
-
class Mysql2Adapter < AbstractAdapter
|
3
|
+
class Mysql2Adapter < AbstractAdapter
|
4
|
+
def to_sql
|
5
|
+
"#{insert_sql} #{values_sql} #{on_duplicate_key_update};"
|
6
|
+
end
|
7
|
+
|
8
|
+
def on_duplicate_key_update
|
9
|
+
if @options[:handle_duplication]
|
10
|
+
"ON DUPLICATE KEY UPDATE #{on_duplicate_key_update_values}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_duplicate_key_update_values
|
15
|
+
quoted_columns.map do |quoted_column|
|
16
|
+
"#{quoted_column}=#{quoted_column}"
|
17
|
+
end.join(',')
|
18
|
+
end
|
19
|
+
end
|
4
20
|
end
|
5
21
|
end
|
data/lib/mass_insert/version.rb
CHANGED
@@ -1,3 +1,29 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
shared_examples
|
4
|
+
|
5
|
+
describe 'Database Adapters' do
|
6
|
+
let(:attributes) do
|
7
|
+
{ name: 'Ruby' }
|
8
|
+
end
|
9
|
+
|
10
|
+
after :each do
|
11
|
+
Kind.delete_all
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'with duplicated record' do
|
15
|
+
def setup
|
16
|
+
Kind.mass_insert([attributes])
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'does not raises error if is duplicated and has the duplication handler on' do
|
20
|
+
Kind.mass_insert([attributes], handle_duplication: true)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'raises error if is duplicated and does not have the duplication handler on' do
|
24
|
+
assert_raises ActiveRecord::RecordNotUnique do
|
25
|
+
Kind.mass_insert([attributes])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/test/database.yml.example
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
common: &common
|
2
|
-
username: root
|
3
|
-
password:
|
4
2
|
encoding: utf8
|
5
3
|
database: mass_insert_test
|
6
4
|
|
7
|
-
mysql2:
|
5
|
+
mysql2:
|
8
6
|
<<: *common
|
9
7
|
adapter: mysql2
|
8
|
+
username: #your_mysql_username
|
9
|
+
password: #your_mysql_password
|
10
10
|
|
11
|
-
postgresql:
|
11
|
+
postgresql:
|
12
12
|
<<: *common
|
13
|
-
username: postgres
|
14
13
|
adapter: postgresql
|
14
|
+
username: #your_postgresql_username
|
15
|
+
password: #your_postgresql_password
|
15
16
|
|
16
|
-
sqlite3:
|
17
|
+
sqlite3:
|
18
|
+
<<: *common
|
17
19
|
adapter: sqlite3
|
18
|
-
database: tmp/
|
20
|
+
database: tmp/mass_insert_test.db
|
data/test/models/kind.rb
ADDED
data/test/schema.rb
CHANGED
@@ -5,7 +5,11 @@ ActiveRecord::Schema.define do
|
|
5
5
|
t.integer :age
|
6
6
|
t.decimal :money, precision: 10, scale: 2
|
7
7
|
t.date :birth_date
|
8
|
-
|
9
8
|
t.timestamps null: false
|
10
9
|
end
|
10
|
+
|
11
|
+
create_table :kinds, force: true do |t|
|
12
|
+
t.string :name
|
13
|
+
t.index ['name'], name: 'index_kinds_on_name', unique: true, using: :btree
|
14
|
+
end
|
11
15
|
end
|
data/test/test_helper.rb
CHANGED
@@ -17,9 +17,19 @@ FileUtils.mkdir_p('log')
|
|
17
17
|
|
18
18
|
ActiveRecord::Base.logger = Logger.new('log/test.log')
|
19
19
|
ActiveRecord::Base.logger.level = Logger::DEBUG
|
20
|
-
|
20
|
+
|
21
|
+
database_configuration = YAML.load_file(File.dirname(__FILE__) + '/database.yml')[adapter]
|
22
|
+
ActiveRecord::Base.configurations['test'] = database_configuration
|
21
23
|
ActiveRecord::Base.establish_connection(:test)
|
22
24
|
|
25
|
+
begin
|
26
|
+
ActiveRecord::Base.connection
|
27
|
+
rescue
|
28
|
+
# Ensures database exists.
|
29
|
+
ActiveRecord::Tasks::DatabaseTasks.database_configuration = database_configuration
|
30
|
+
ActiveRecord::Tasks::DatabaseTasks.create_current('test')
|
31
|
+
end
|
32
|
+
|
23
33
|
require File.dirname(__FILE__) + '/schema.rb'
|
24
34
|
|
25
35
|
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each{ |file| require file }
|
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.3
|
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
|
+
date: 2016-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- test/adapters/postgresql/example_test.rb
|
112
112
|
- test/adapters/sqlite3/example_test.rb
|
113
113
|
- test/database.yml.example
|
114
|
+
- test/models/kind.rb
|
114
115
|
- test/models/user.rb
|
115
116
|
- test/schema.rb
|
116
117
|
- test/support/adapters/mysql2.rb
|
@@ -147,6 +148,7 @@ test_files:
|
|
147
148
|
- test/adapters/postgresql/example_test.rb
|
148
149
|
- test/adapters/sqlite3/example_test.rb
|
149
150
|
- test/database.yml.example
|
151
|
+
- test/models/kind.rb
|
150
152
|
- test/models/user.rb
|
151
153
|
- test/schema.rb
|
152
154
|
- test/support/adapters/mysql2.rb
|