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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9c813a1d355a3033bd79ea76d6e2680dc50e84f
4
- data.tar.gz: ce141093d4663e23ebef135d22fcce78d6c3e41f
3
+ metadata.gz: 776d7684b8b9dbc9d4232b18baa16db73c2e30e2
4
+ data.tar.gz: 5f3abd0e966d88cbf1038a5591ace5fba11170b5
5
5
  SHA512:
6
- metadata.gz: 010f53e134a7c33e9dc7cbd4796b10b9a5caec4cfd3fd3db9303d315da5cade8b60298a03910b641d949156ff01c42bd6223feccddcb52ac98c1648df1c3cab9
7
- data.tar.gz: 1e573ff99bda7e7653a6c96e708893c2524584d960ad54a04cc1a9ffdcc50a0d236532629a9a48c25cbfe2df449ee9bed82c234352f28054ce9a275a067b8546
6
+ metadata.gz: 8574e134b4e067efba5ab705e88c5ebffa6426dc4148f2849b43aceb1ce48cc154645f19fdb82dcb7c2b9136283a7de425011138edfe1e3e55180a92dcd26b59
7
+ data.tar.gz: 1ba5fedf3377bb938a872545f9a4c0980ddf4bc88d386e489b3189720ac208b4a1ecab3bdb409dbda3caf7b478d4afeb67c9452e9458df3b9f4c5badd5e532fb
data/.travis.yml CHANGED
@@ -1,9 +1,8 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0.0
4
- - 2.1.8
5
- - 2.2.4
3
+ - 2.2.2
6
4
  - 2.3.0
5
+ - 2.3.1
7
6
 
8
7
  before_script:
9
8
  - cp test/database.yml.example test/database.yml
data/README.md CHANGED
@@ -44,7 +44,7 @@ User.mass_insert(values)
44
44
  ```
45
45
 
46
46
 
47
- ## Insertion per batches
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, columns_hash[name.to_s])
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; end
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
@@ -1,3 +1,3 @@
1
1
  module MassInsert
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -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
@@ -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: &mysql2
5
+ mysql2:
8
6
  <<: *common
9
7
  adapter: mysql2
8
+ username: #your_mysql_username
9
+ password: #your_mysql_password
10
10
 
11
- postgresql: &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: &sqlite3
17
+ sqlite3:
18
+ <<: *common
17
19
  adapter: sqlite3
18
- database: tmp/test.db
20
+ database: tmp/mass_insert_test.db
@@ -0,0 +1,2 @@
1
+ class Kind < ActiveRecord::Base
2
+ end
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
- ActiveRecord::Base.configurations['test'] = YAML.load_file(File.dirname(__FILE__) + '/database.yml')[adapter]
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.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-03-30 00:00:00.000000000 Z
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