activerecord-import 1.0.1 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +17 -14
- data/CHANGELOG.md +57 -0
- data/Gemfile +4 -1
- data/LICENSE +21 -56
- data/README.markdown +55 -60
- data/activerecord-import.gemspec +2 -2
- data/gemfiles/6.0.gemfile +1 -0
- data/gemfiles/6.1.gemfile +1 -0
- data/lib/activerecord-import/adapters/abstract_adapter.rb +7 -1
- data/lib/activerecord-import/adapters/mysql_adapter.rb +4 -4
- data/lib/activerecord-import/adapters/postgresql_adapter.rb +9 -9
- data/lib/activerecord-import/adapters/sqlite3_adapter.rb +15 -19
- data/lib/activerecord-import/base.rb +8 -1
- data/lib/activerecord-import/import.rb +37 -17
- data/lib/activerecord-import/synchronize.rb +1 -1
- data/lib/activerecord-import/version.rb +1 -1
- data/test/import_test.rb +29 -0
- data/test/models/animal.rb +6 -0
- data/test/schema/postgresql_schema.rb +14 -0
- data/test/support/postgresql/import_examples.rb +31 -0
- data/test/support/shared_examples/on_duplicate_key_update.rb +10 -0
- data/test/support/shared_examples/recursive_import.rb +9 -0
- data/test/support/sqlite3/import_examples.rb +2 -15
- metadata +12 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bedb8323d872ae8ca6daebc691f82fedc918a3f2c01e7e354a8534854c06d80e
|
4
|
+
data.tar.gz: 2df3dfaef596b93d208854557700167a4a8e0e9bb151ff224e4a6255e374086f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c97ba946d5453394831959f384868812ca59833d1c694feccc19d68967d38e3501305fca11fcd39dec32ddc8c88d26982ea2f087d590e34a19da88ff7def1c9c
|
7
|
+
data.tar.gz: 0fcdc64e20ac0ee42584ab97f060e116f2542a353f0dc815a616adbe3810d6a343f8b021c0204adde9eb484aac5befc4e0b4699b1bbdfbddb2d486f8418d1839
|
data/.travis.yml
CHANGED
@@ -1,30 +1,29 @@
|
|
1
1
|
language: ruby
|
2
2
|
cache: bundler
|
3
3
|
rvm:
|
4
|
-
- 2.
|
4
|
+
- 2.5.5
|
5
5
|
|
6
6
|
env:
|
7
7
|
global:
|
8
8
|
# https://github.com/discourse/discourse/blob/master/.travis.yml
|
9
9
|
- RUBY_GC_MALLOC_LIMIT=50000000
|
10
10
|
matrix:
|
11
|
-
- AR_VERSION=3.2
|
12
|
-
- AR_VERSION=4.0
|
13
|
-
- AR_VERSION=4.1
|
14
|
-
- AR_VERSION=4.2
|
15
|
-
- AR_VERSION=5.0
|
16
11
|
- AR_VERSION=5.1
|
17
12
|
- AR_VERSION=5.2
|
13
|
+
- AR_VERSION=6.0
|
18
14
|
|
19
15
|
matrix:
|
20
16
|
include:
|
21
|
-
- rvm:
|
17
|
+
- rvm: 2.3.8
|
18
|
+
env: AR_VERSION=3.2
|
19
|
+
- rvm: 2.3.8
|
20
|
+
env: AR_VERSION=4.0
|
21
|
+
- rvm: 2.3.8
|
22
|
+
env: AR_VERSION=4.1
|
23
|
+
- rvm: 2.3.8
|
22
24
|
env: AR_VERSION=4.2
|
23
|
-
|
24
|
-
|
25
|
-
- bundle exec rake test:jdbcsqlite3
|
26
|
-
- bundle exec rake test:jdbcmysql
|
27
|
-
- bundle exec rake test:jdbcpostgresql
|
25
|
+
- rvm: 2.3.8
|
26
|
+
env: AR_VERSION=5.0
|
28
27
|
|
29
28
|
fast_finish: true
|
30
29
|
|
@@ -38,7 +37,7 @@ addons:
|
|
38
37
|
- sqlite3
|
39
38
|
- mysql-server
|
40
39
|
- mysql-client
|
41
|
-
- postgresql-9.5-postgis-2.
|
40
|
+
- postgresql-9.5-postgis-2.4
|
42
41
|
|
43
42
|
before_install:
|
44
43
|
- gem update --system
|
@@ -66,6 +65,10 @@ script:
|
|
66
65
|
- bundle exec rake test:sqlite3
|
67
66
|
- bundle exec rubocop
|
68
67
|
|
69
|
-
dist:
|
68
|
+
dist: xenial
|
69
|
+
|
70
|
+
services:
|
71
|
+
- mysql
|
72
|
+
- postgresql
|
70
73
|
|
71
74
|
sudo: required
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,60 @@
|
|
1
|
+
## Changes in 1.0.6
|
2
|
+
|
3
|
+
### Fixes
|
4
|
+
|
5
|
+
* Handle after_initialize callbacks. Thanks to @AhMohsen46 via \#691 and
|
6
|
+
\#692.
|
7
|
+
* Fix regression introduced in 1.0.4. Explicity allow adapters to
|
8
|
+
support on duplicate key update. Thanks to @dsobiera, @jkowens via \#698.
|
9
|
+
|
10
|
+
## Changes in 1.0.5
|
11
|
+
|
12
|
+
### Fixes
|
13
|
+
|
14
|
+
* Allow serialized attributes to be returned from import. Thanks to @timanovsky, @jkowens via \#660.
|
15
|
+
* Return ActiveRecord::Connection from
|
16
|
+
ActiveREcord::Base#establish_connection. Thanks to @reverentF via
|
17
|
+
\#663.
|
18
|
+
* Support PostgreSQL array. Thanks to @ujihisa via \#669.
|
19
|
+
* Skip loading association ids when column changed. Thanks to @Aristat
|
20
|
+
via \#673.
|
21
|
+
|
22
|
+
## Changes in 1.0.4
|
23
|
+
|
24
|
+
### Fixes
|
25
|
+
|
26
|
+
* Use prepend pattern for ActiveRecord::Base#establish_connection patching. Thanks to @dombesz via \#648.
|
27
|
+
* Fix NoMethodError when using PostgreSQL ENUM types. Thanks to @sebcoetzee via \#651.
|
28
|
+
* Fix issue updating optimistic lock in Postgres. Thanks to @timanovsky
|
29
|
+
via \#656.
|
30
|
+
|
31
|
+
## Changes in 1.0.3
|
32
|
+
|
33
|
+
### New Features
|
34
|
+
|
35
|
+
* Add support for ActiveRecord 6.1.0.alpha. Thanks to @imtayadeway via
|
36
|
+
\#642.
|
37
|
+
|
38
|
+
### Fixes
|
39
|
+
|
40
|
+
* Return an empty array for results instead of nil when importing empty
|
41
|
+
array. Thanks to @gyfis via \#636.
|
42
|
+
|
43
|
+
## Changes in 1.0.2
|
44
|
+
|
45
|
+
### New Features
|
46
|
+
|
47
|
+
* Add support for CockroachDB adapter. Thanks to @willie via \#605.
|
48
|
+
* Add support for ActiveRecord 6.0.0.rc1. Thanks to @madeindjs, @bill-filler,
|
49
|
+
@jkowens via \#619, \#623.
|
50
|
+
|
51
|
+
### Fixes
|
52
|
+
|
53
|
+
* Fixes NoMethodError when attempting to use nil logger. Thanks to @MattMecel,
|
54
|
+
@khiav22357.
|
55
|
+
* Fix issue validating STI models. Thanks to @thejbsmith, @jkowens via
|
56
|
+
\#626.
|
57
|
+
|
1
58
|
## Changes in 1.0.1
|
2
59
|
|
3
60
|
### Fixes
|
data/Gemfile
CHANGED
@@ -6,6 +6,8 @@ version = ENV['AR_VERSION'].to_f
|
|
6
6
|
|
7
7
|
mysql2_version = '0.3.0'
|
8
8
|
mysql2_version = '0.4.0' if version >= 4.2
|
9
|
+
sqlite3_version = '1.3.0'
|
10
|
+
sqlite3_version = '1.4.0' if version >= 6.0
|
9
11
|
|
10
12
|
group :development, :test do
|
11
13
|
gem 'rubocop', '~> 0.40.0'
|
@@ -16,7 +18,7 @@ end
|
|
16
18
|
platforms :ruby do
|
17
19
|
gem "mysql2", "~> #{mysql2_version}"
|
18
20
|
gem "pg", "~> 0.9"
|
19
|
-
gem "sqlite3", "~>
|
21
|
+
gem "sqlite3", "~> #{sqlite3_version}"
|
20
22
|
gem "seamless_database_pool", "~> 1.0.20"
|
21
23
|
end
|
22
24
|
|
@@ -45,6 +47,7 @@ end
|
|
45
47
|
|
46
48
|
platforms :ruby do
|
47
49
|
gem "pry-byebug"
|
50
|
+
gem "pry", "~> 0.12.0"
|
48
51
|
gem "rb-readline"
|
49
52
|
end
|
50
53
|
|
data/LICENSE
CHANGED
@@ -1,56 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
d) make other distribution arrangements with the author.
|
24
|
-
|
25
|
-
3. You may distribute the software in object code or binary form,
|
26
|
-
provided that you do at least ONE of the following:
|
27
|
-
|
28
|
-
a) distribute the binaries and library files of the software,
|
29
|
-
together with instructions (in the manual page or equivalent)
|
30
|
-
on where to get the original distribution.
|
31
|
-
|
32
|
-
b) accompany the distribution with the machine-readable source of
|
33
|
-
the software.
|
34
|
-
|
35
|
-
c) give non-standard binaries non-standard names, with
|
36
|
-
instructions on where to get the original software distribution.
|
37
|
-
|
38
|
-
d) make other distribution arrangements with the author.
|
39
|
-
|
40
|
-
4. You may modify and include the part of the software into any other
|
41
|
-
software (possibly commercial). But some files in the distribution
|
42
|
-
are not written by the author, so that they are not under these terms.
|
43
|
-
|
44
|
-
For the list of those files and their copying conditions, see the
|
45
|
-
file LEGAL.
|
46
|
-
|
47
|
-
5. The scripts and library files supplied as input to or produced as
|
48
|
-
output from the software do not automatically fall under the
|
49
|
-
copyright of the software, but belong to whomever generated them,
|
50
|
-
and may be sold commercially, and may be aggregated with this
|
51
|
-
software.
|
52
|
-
|
53
|
-
6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
54
|
-
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
55
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
56
|
-
PURPOSE.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Zach Dennis <zach.dennis@gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.markdown
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# Activerecord-Import [![Build Status](https://travis-ci.org/zdennis/activerecord-import.svg?branch=master)](https://travis-ci.org/zdennis/activerecord-import)
|
2
2
|
|
3
|
-
|
3
|
+
Activerecord-Import is a library for bulk inserting data using ActiveRecord.
|
4
4
|
|
5
5
|
One of its major features is following activerecord associations and generating the minimal
|
6
6
|
number of SQL insert statements required, avoiding the N+1 insert problem. An example probably
|
@@ -23,10 +23,10 @@ an 18 hour batch process to <2 hrs.
|
|
23
23
|
|
24
24
|
The gem provides the following high-level features:
|
25
25
|
|
26
|
-
*
|
27
|
-
*
|
28
|
-
*
|
29
|
-
*
|
26
|
+
* Works with raw columns and arrays of values (fastest)
|
27
|
+
* Works with model objects (faster)
|
28
|
+
* Performs validations (fast)
|
29
|
+
* Performs on duplicate key updates (requires MySQL, SQLite 3.24.0+, or Postgres 9.5+)
|
30
30
|
|
31
31
|
## Table of Contents
|
32
32
|
|
@@ -54,16 +54,19 @@ The gem provides the following high-level features:
|
|
54
54
|
* [More Information](#more-information)
|
55
55
|
* [Contributing](#contributing)
|
56
56
|
* [Running Tests](#running-tests)
|
57
|
+
* [Issue Triage](#issue-triage)
|
57
58
|
|
58
59
|
### Examples
|
59
60
|
|
60
61
|
#### Introduction
|
61
62
|
|
63
|
+
This gem adds an `import` method (or `bulk_import`, for compatibility with gems like `elasticsearch-model`; see [Conflicts With Other Gems](#conflicts-with-other-gems)) to ActiveRecord classes.
|
64
|
+
|
62
65
|
Without `activerecord-import`, you'd write something like this:
|
63
66
|
|
64
67
|
```ruby
|
65
68
|
10.times do |i|
|
66
|
-
Book.create! :
|
69
|
+
Book.create! name: "book #{i}"
|
67
70
|
end
|
68
71
|
```
|
69
72
|
|
@@ -72,7 +75,7 @@ This would end up making 10 SQL calls. YUCK! With `activerecord-import`, you ca
|
|
72
75
|
```ruby
|
73
76
|
books = []
|
74
77
|
10.times do |i|
|
75
|
-
books << Book.new(:
|
78
|
+
books << Book.new(name: "book #{i}")
|
76
79
|
end
|
77
80
|
Book.import books # or use import!
|
78
81
|
```
|
@@ -85,13 +88,13 @@ The `import` method can take an array of column names (string or symbols) and an
|
|
85
88
|
|
86
89
|
```ruby
|
87
90
|
columns = [ :title, :author ]
|
88
|
-
values = [ ['Book1', '
|
91
|
+
values = [ ['Book1', 'George Orwell'], ['Book2', 'Bob Jones'] ]
|
89
92
|
|
90
93
|
# Importing without model validations
|
91
|
-
Book.import columns, values, :
|
94
|
+
Book.import columns, values, validate: false
|
92
95
|
|
93
96
|
# Import with model validations
|
94
|
-
Book.import columns, values, :
|
97
|
+
Book.import columns, values, validate: true
|
95
98
|
|
96
99
|
# when not specified :validate defaults to true
|
97
100
|
Book.import columns, values
|
@@ -102,7 +105,7 @@ Book.import columns, values
|
|
102
105
|
The `import` method can take an array of hashes. The keys map to the column names in the database.
|
103
106
|
|
104
107
|
```ruby
|
105
|
-
values = [{ title: 'Book1', author: '
|
108
|
+
values = [{ title: 'Book1', author: 'George Orwell' }, { title: 'Book2', author: 'Bob Jones'}]
|
106
109
|
|
107
110
|
# Importing without model validations
|
108
111
|
Book.import values, validate: false
|
@@ -119,7 +122,7 @@ The `import` method can take an array of column names and an array of hash objec
|
|
119
122
|
|
120
123
|
```ruby
|
121
124
|
books = [
|
122
|
-
{ title: "Book 1", author: "
|
125
|
+
{ title: "Book 1", author: "George Orwell" },
|
123
126
|
{ title: "Book 2", author: "Bob Jones" }
|
124
127
|
]
|
125
128
|
columns = [ :title ]
|
@@ -171,15 +174,15 @@ The `import` method can take an array of models. The attributes will be pulled o
|
|
171
174
|
|
172
175
|
```ruby
|
173
176
|
books = [
|
174
|
-
Book.new(:
|
175
|
-
Book.new(:
|
177
|
+
Book.new(title: "Book 1", author: "George Orwell"),
|
178
|
+
Book.new(title: "Book 2", author: "Bob Jones")
|
176
179
|
]
|
177
180
|
|
178
181
|
# without validations
|
179
|
-
Book.import books, :
|
182
|
+
Book.import books, validate: false
|
180
183
|
|
181
184
|
# with validations
|
182
|
-
Book.import books, :
|
185
|
+
Book.import books, validate: true
|
183
186
|
|
184
187
|
# when not specified :validate defaults to true
|
185
188
|
Book.import books
|
@@ -189,16 +192,16 @@ The `import` method can take an array of column names and an array of models. Th
|
|
189
192
|
|
190
193
|
```ruby
|
191
194
|
books = [
|
192
|
-
Book.new(:
|
193
|
-
Book.new(:
|
195
|
+
Book.new(title: "Book 1", author: "George Orwell"),
|
196
|
+
Book.new(title: "Book 2", author: "Bob Jones")
|
194
197
|
]
|
195
198
|
columns = [ :title ]
|
196
199
|
|
197
200
|
# without validations
|
198
|
-
Book.import columns, books, :
|
201
|
+
Book.import columns, books, validate: false
|
199
202
|
|
200
203
|
# with validations
|
201
|
-
Book.import columns, books, :
|
204
|
+
Book.import columns, books, validate: true
|
202
205
|
|
203
206
|
# when not specified :validate defaults to true
|
204
207
|
Book.import columns, books
|
@@ -217,28 +220,29 @@ The `import` method can take a `batch_size` option to control the number of rows
|
|
217
220
|
|
218
221
|
```ruby
|
219
222
|
books = [
|
220
|
-
Book.new(:
|
221
|
-
Book.new(:
|
222
|
-
Book.new(:
|
223
|
-
Book.new(:
|
223
|
+
Book.new(title: "Book 1", author: "George Orwell"),
|
224
|
+
Book.new(title: "Book 2", author: "Bob Jones"),
|
225
|
+
Book.new(title: "Book 1", author: "John Doe"),
|
226
|
+
Book.new(title: "Book 2", author: "Richard Wright")
|
224
227
|
]
|
225
228
|
columns = [ :title ]
|
226
229
|
|
227
230
|
# 2 INSERT statements for 4 records
|
228
|
-
Book.import columns, books, :
|
231
|
+
Book.import columns, books, batch_size: 2
|
229
232
|
```
|
230
233
|
|
231
234
|
#### Recursive
|
232
235
|
|
233
|
-
NOTE: This only works with PostgreSQL.
|
236
|
+
NOTE: This only works with PostgreSQL and ActiveRecord objects. This won't work with
|
237
|
+
hashes or arrays as recursive inputs.
|
234
238
|
|
235
239
|
Assume that Books <code>has_many</code> Reviews.
|
236
240
|
|
237
241
|
```ruby
|
238
242
|
books = []
|
239
243
|
10.times do |i|
|
240
|
-
book = Book.new(:
|
241
|
-
book.reviews.build(:
|
244
|
+
book = Book.new(name: "book #{i}")
|
245
|
+
book.reviews.build(title: "Excellent")
|
242
246
|
books << book
|
243
247
|
end
|
244
248
|
Book.import books, recursive: true
|
@@ -248,8 +252,9 @@ Book.import books, recursive: true
|
|
248
252
|
|
249
253
|
Key | Options | Default | Description
|
250
254
|
----------------------- | --------------------- | ------------------ | -----------
|
251
|
-
:validate | `true`/`false` | `true` | Whether or not to run `ActiveRecord` validations (uniqueness skipped).
|
255
|
+
:validate | `true`/`false` | `true` | Whether or not to run `ActiveRecord` validations (uniqueness skipped). This option will always be true when using `import!`.
|
252
256
|
:validate_uniqueness | `true`/`false` | `false` | Whether or not to run uniqueness validations, has potential pitfalls, use with caution (requires `>= v0.27.0`).
|
257
|
+
:validate_with_context | `Symbol` |`:create`/`:update` | Allows passing an ActiveModel validation context for each model. Default is `:create` for new records and `:update` for existing ones.
|
253
258
|
:on_duplicate_key_ignore| `true`/`false` | `false` | Allows skipping records with duplicate keys. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-ignore) for more details.
|
254
259
|
:ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
|
255
260
|
:on_duplicate_key_update| :all, `Array`, `Hash` | N/A | Allows upsert logic to be used. See [here](https://github.com/zdennis/activerecord-import/#duplicate-key-update) for more details.
|
@@ -257,8 +262,8 @@ Key | Options | Default | Descripti
|
|
257
262
|
:timestamps | `true`/`false` | `true` | Enables/disables timestamps on imported records.
|
258
263
|
:recursive | `true`/`false` | `false` | Imports has_many/has_one associations (PostgreSQL only).
|
259
264
|
:batch_size | `Integer` | total # of records | Max number of records to insert per import
|
260
|
-
:raise_error | `true`/`false` | `false` |
|
261
|
-
|
265
|
+
:raise_error | `true`/`false` | `false` | Raises an exception at the first invalid record. This means there will not be a result object returned. The `import!` method is a shortcut for this.
|
266
|
+
:all_or_none | `true`/`false` | `false` | Will not import any records if there is a record with validation errors.
|
262
267
|
|
263
268
|
#### Duplicate Key Ignore
|
264
269
|
|
@@ -267,14 +272,14 @@ Key | Options | Default | Descripti
|
|
267
272
|
For Postgres 9.5+ it adds `ON CONFLICT DO NOTHING`, for MySQL it uses `INSERT IGNORE`, and for SQLite it uses `INSERT OR IGNORE`. Cannot be enabled on a recursive import. For database adapters that normally support setting primary keys on imported objects, this option prevents that from occurring.
|
268
273
|
|
269
274
|
```ruby
|
270
|
-
book = Book.create! title: "Book1", author: "
|
275
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
271
276
|
book.title = "Updated Book Title"
|
272
277
|
book.author = "Bob Barker"
|
273
278
|
|
274
279
|
Book.import [book], on_duplicate_key_ignore: true
|
275
280
|
|
276
281
|
book.reload.title # => "Book1" (stayed the same)
|
277
|
-
book.reload.author # => "
|
282
|
+
book.reload.author # => "George Orwell" (stayed the same)
|
278
283
|
```
|
279
284
|
|
280
285
|
The option `:on_duplicate_key_ignore` is bypassed when `:recursive` is enabled for [PostgreSQL imports](https://github.com/zdennis/activerecord-import/wiki#recursive-example-postgresql-only).
|
@@ -290,7 +295,7 @@ This will use MySQL's `ON DUPLICATE KEY UPDATE` or Postgres/SQLite `ON CONFLICT
|
|
290
295
|
Basic Update
|
291
296
|
|
292
297
|
```ruby
|
293
|
-
book = Book.create! title: "Book1", author: "
|
298
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
294
299
|
book.title = "Updated Book Title"
|
295
300
|
book.author = "Bob Barker"
|
296
301
|
|
@@ -304,13 +309,13 @@ Book.import [book], on_duplicate_key_update: {conflict_target: [:id], columns: [
|
|
304
309
|
Book.import [book], on_duplicate_key_update: [:title]
|
305
310
|
|
306
311
|
book.reload.title # => "Updated Book Title" (changed)
|
307
|
-
book.reload.author # => "
|
312
|
+
book.reload.author # => "George Orwell" (stayed the same)
|
308
313
|
```
|
309
314
|
|
310
315
|
Using the value from another column
|
311
316
|
|
312
317
|
```ruby
|
313
|
-
book = Book.create! title: "Book1", author: "
|
318
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
314
319
|
book.title = "Updated Book Title"
|
315
320
|
|
316
321
|
# MySQL version
|
@@ -328,7 +333,7 @@ book.reload.author # => "Updated Book Title" (changed)
|
|
328
333
|
Using Custom SQL
|
329
334
|
|
330
335
|
```ruby
|
331
|
-
book = Book.create! title: "Book1", author: "
|
336
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
332
337
|
book.author = "Bob Barker"
|
333
338
|
|
334
339
|
# MySQL version
|
@@ -349,7 +354,7 @@ book.reload.author # => "Bob Barker" (changed)
|
|
349
354
|
PostgreSQL Using constraints
|
350
355
|
|
351
356
|
```ruby
|
352
|
-
book = Book.create! title: "Book1", author: "
|
357
|
+
book = Book.create! title: "Book1", author: "George Orwell", edition: 3, published_at: nil
|
353
358
|
book.published_at = Time.now
|
354
359
|
|
355
360
|
# in migration
|
@@ -363,7 +368,7 @@ Book.import [book], on_duplicate_key_update: {constraint_name: :for_upsert, colu
|
|
363
368
|
|
364
369
|
|
365
370
|
book.reload.title # => "Book1" (stayed the same)
|
366
|
-
book.reload.author # => "
|
371
|
+
book.reload.author # => "George Orwell" (stayed the same)
|
367
372
|
book.reload.edition # => 3 (stayed the same)
|
368
373
|
book.reload.published_at # => 2017-10-09 (changed)
|
369
374
|
```
|
@@ -383,7 +388,7 @@ articles = [
|
|
383
388
|
Article.new(author_id: 3, content: '')
|
384
389
|
]
|
385
390
|
|
386
|
-
demo = Article.import(articles
|
391
|
+
demo = Article.import(articles, returning: :title) # => #<struct ActiveRecord::Import::Result
|
387
392
|
|
388
393
|
demo.failed_instances
|
389
394
|
=> [#<Article id: 3, author_id: 3, title: nil, content: "", created_at: nil, updated_at: nil>]
|
@@ -397,7 +402,7 @@ demo.ids
|
|
397
402
|
|
398
403
|
demo.results
|
399
404
|
=> ["First Article", "Second Article"] # for Postgres
|
400
|
-
=> [] for other DBs
|
405
|
+
=> [] # for other DBs
|
401
406
|
```
|
402
407
|
|
403
408
|
### Counter Cache
|
@@ -524,7 +529,7 @@ require 'activerecord-import'
|
|
524
529
|
### Load Path Setup
|
525
530
|
To understand how rubygems loads code you can reference the following:
|
526
531
|
|
527
|
-
http://guides.rubygems.org/patterns/#
|
532
|
+
http://guides.rubygems.org/patterns/#loading-code
|
528
533
|
|
529
534
|
And an example of how active_record dynamically load adapters:
|
530
535
|
|
@@ -552,7 +557,7 @@ When rubygems pushes the `lib` folder onto the load path a `require` will now fi
|
|
552
557
|
|
553
558
|
### Conflicts With Other Gems
|
554
559
|
|
555
|
-
|
560
|
+
Activerecord-Import adds the `.import` method onto `ActiveRecord::Base`. There are other gems, such as `elasticsearch-rails`, that do the same thing. In conflicts such as this, there is an aliased method named `.bulk_import` that can be used interchangeably.
|
556
561
|
|
557
562
|
If you are using the `apartment` gem, there is a weird triple interaction between that gem, `activerecord-import`, and `activerecord` involving caching of the `sequence_name` of a model. This can be worked around by explcitly setting this value within the model. For example:
|
558
563
|
|
@@ -579,7 +584,7 @@ See https://github.com/zdennis/activerecord-import/issues/233 for further discus
|
|
579
584
|
|
580
585
|
### More Information
|
581
586
|
|
582
|
-
For more information on
|
587
|
+
For more information on Activerecord-Import please see its wiki: https://github.com/zdennis/activerecord-import/wiki
|
583
588
|
|
584
589
|
To document new information, please add to the README instead of the wiki. See https://github.com/zdennis/activerecord-import/issues/397 for discussion.
|
585
590
|
|
@@ -605,24 +610,14 @@ AR_VERSION=4.2 bundle exec rake test:postgresql test:sqlite3 test:mysql2
|
|
605
610
|
|
606
611
|
Once you have pushed up your changes, you can find your CI results [here](https://travis-ci.org/zdennis/activerecord-import/).
|
607
612
|
|
613
|
+
## Issue Triage [![Open Source Helpers](https://www.codetriage.com/zdennis/activerecord-import/badges/users.svg)](https://www.codetriage.com/zdennis/activerecord-import)
|
614
|
+
|
615
|
+
You can triage issues which may include reproducing bug reports or asking for vital information, such as version numbers or reproduction instructions. If you would like to start triaging issues, one easy way to get started is to [subscribe to activerecord-import on CodeTriage](https://www.codetriage.com/zdennis/activerecord-import).
|
616
|
+
|
608
617
|
# License
|
609
618
|
|
610
|
-
This is licensed under the
|
619
|
+
This is licensed under the MIT license.
|
611
620
|
|
612
621
|
# Author
|
613
622
|
|
614
623
|
Zach Dennis (zach.dennis@gmail.com)
|
615
|
-
|
616
|
-
# Contributors
|
617
|
-
|
618
|
-
* Jordan Owens (@jkowens)
|
619
|
-
* Erik Michaels-Ober (@sferik)
|
620
|
-
* Blythe Dunham
|
621
|
-
* Gabe da Silveira
|
622
|
-
* Henry Work
|
623
|
-
* James Herdman
|
624
|
-
* Marcus Crafter
|
625
|
-
* Thibaud Guillaume-Gentil
|
626
|
-
* Mark Van Holstyn
|
627
|
-
* Victor Costan
|
628
|
-
* Dillon Welch
|
data/activerecord-import.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
|
|
7
7
|
gem.summary = "Bulk insert extension for ActiveRecord"
|
8
8
|
gem.description = "A library for bulk inserting data using ActiveRecord."
|
9
9
|
gem.homepage = "http://github.com/zdennis/activerecord-import"
|
10
|
-
gem.license = "
|
10
|
+
gem.license = "MIT"
|
11
11
|
|
12
12
|
gem.files = `git ls-files`.split($\)
|
13
13
|
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
@@ -16,7 +16,7 @@ Gem::Specification.new do |gem|
|
|
16
16
|
gem.require_paths = ["lib"]
|
17
17
|
gem.version = ActiveRecord::Import::VERSION
|
18
18
|
|
19
|
-
gem.required_ruby_version = ">=
|
19
|
+
gem.required_ruby_version = ">= 2.0.0"
|
20
20
|
|
21
21
|
gem.add_runtime_dependency "activerecord", ">= 3.2"
|
22
22
|
gem.add_development_dependency "rake"
|
@@ -0,0 +1 @@
|
|
1
|
+
gem 'activerecord', '~> 6.0.0'
|
@@ -0,0 +1 @@
|
|
1
|
+
gem 'activerecord', '~> 6.1.0.alpha', github: "rails/rails"
|
@@ -46,7 +46,7 @@ module ActiveRecord::Import::AbstractAdapter
|
|
46
46
|
|
47
47
|
if supports_on_duplicate_key_update? && options[:on_duplicate_key_update]
|
48
48
|
post_sql_statements << sql_for_on_duplicate_key_update( table_name, options[:on_duplicate_key_update], options[:primary_key], options[:locking_column] )
|
49
|
-
elsif options[:on_duplicate_key_update]
|
49
|
+
elsif logger && options[:on_duplicate_key_update]
|
50
50
|
logger.warn "Ignoring on_duplicate_key_update because it is not supported by the database."
|
51
51
|
end
|
52
52
|
|
@@ -59,6 +59,12 @@ module ActiveRecord::Import::AbstractAdapter
|
|
59
59
|
post_sql_statements
|
60
60
|
end
|
61
61
|
|
62
|
+
def increment_locking_column!(table_name, results, locking_column)
|
63
|
+
if locking_column.present?
|
64
|
+
results << "\"#{locking_column}\"=#{table_name}.\"#{locking_column}\"+1"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
62
68
|
def supports_on_duplicate_key_update?
|
63
69
|
false
|
64
70
|
end
|
@@ -102,7 +102,7 @@ module ActiveRecord::Import::MysqlAdapter
|
|
102
102
|
qc = quote_column_name( column )
|
103
103
|
"#{table_name}.#{qc}=VALUES(#{qc})"
|
104
104
|
end
|
105
|
-
increment_locking_column!(
|
105
|
+
increment_locking_column!(table_name, results, locking_column)
|
106
106
|
results.join( ',' )
|
107
107
|
end
|
108
108
|
|
@@ -112,7 +112,7 @@ module ActiveRecord::Import::MysqlAdapter
|
|
112
112
|
qc2 = quote_column_name( column2 )
|
113
113
|
"#{table_name}.#{qc1}=VALUES( #{qc2} )"
|
114
114
|
end
|
115
|
-
increment_locking_column!(
|
115
|
+
increment_locking_column!(table_name, results, locking_column)
|
116
116
|
results.join( ',')
|
117
117
|
end
|
118
118
|
|
@@ -121,9 +121,9 @@ module ActiveRecord::Import::MysqlAdapter
|
|
121
121
|
exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('Duplicate entry')
|
122
122
|
end
|
123
123
|
|
124
|
-
def increment_locking_column!(
|
124
|
+
def increment_locking_column!(table_name, results, locking_column)
|
125
125
|
if locking_column.present?
|
126
|
-
results << "
|
126
|
+
results << "`#{locking_column}`=#{table_name}.`#{locking_column}`+1"
|
127
127
|
end
|
128
128
|
end
|
129
129
|
end
|
@@ -73,7 +73,7 @@ module ActiveRecord::Import::PostgreSQLAdapter
|
|
73
73
|
if (options[:ignore] || options[:on_duplicate_key_ignore]) && !options[:on_duplicate_key_update] && !options[:recursive]
|
74
74
|
sql << sql_for_on_duplicate_key_ignore( table_name, options[:on_duplicate_key_ignore] )
|
75
75
|
end
|
76
|
-
elsif options[:on_duplicate_key_ignore] && !options[:on_duplicate_key_update]
|
76
|
+
elsif logger && options[:on_duplicate_key_ignore] && !options[:on_duplicate_key_update]
|
77
77
|
logger.warn "Ignoring on_duplicate_key_ignore because it is not supported by the database."
|
78
78
|
end
|
79
79
|
|
@@ -158,7 +158,7 @@ module ActiveRecord::Import::PostgreSQLAdapter
|
|
158
158
|
qc = quote_column_name( column )
|
159
159
|
"#{qc}=EXCLUDED.#{qc}"
|
160
160
|
end
|
161
|
-
increment_locking_column!(results, locking_column)
|
161
|
+
increment_locking_column!(table_name, results, locking_column)
|
162
162
|
results.join( ',' )
|
163
163
|
end
|
164
164
|
|
@@ -168,7 +168,7 @@ module ActiveRecord::Import::PostgreSQLAdapter
|
|
168
168
|
qc2 = quote_column_name( column2 )
|
169
169
|
"#{qc1}=EXCLUDED.#{qc2}"
|
170
170
|
end
|
171
|
-
increment_locking_column!(results, locking_column)
|
171
|
+
increment_locking_column!(table_name, results, locking_column)
|
172
172
|
results.join( ',' )
|
173
173
|
end
|
174
174
|
|
@@ -195,17 +195,17 @@ module ActiveRecord::Import::PostgreSQLAdapter
|
|
195
195
|
exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('duplicate key')
|
196
196
|
end
|
197
197
|
|
198
|
-
def supports_on_duplicate_key_update?
|
199
|
-
|
198
|
+
def supports_on_duplicate_key_update?
|
199
|
+
database_version >= MIN_VERSION_FOR_UPSERT
|
200
200
|
end
|
201
201
|
|
202
202
|
def supports_setting_primary_key_of_imported_objects?
|
203
203
|
true
|
204
204
|
end
|
205
205
|
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
206
|
+
private
|
207
|
+
|
208
|
+
def database_version
|
209
|
+
defined?(postgresql_version) ? postgresql_version : super
|
210
210
|
end
|
211
211
|
end
|
@@ -9,16 +9,12 @@ module ActiveRecord::Import::SQLite3Adapter
|
|
9
9
|
# Override our conformance to ActiveRecord::Import::ImportSupport interface
|
10
10
|
# to ensure that we only support import in supported version of SQLite.
|
11
11
|
# Which INSERT statements with multiple value sets was introduced in 3.7.11.
|
12
|
-
def supports_import?
|
13
|
-
|
14
|
-
true
|
15
|
-
else
|
16
|
-
false
|
17
|
-
end
|
12
|
+
def supports_import?
|
13
|
+
database_version >= MIN_VERSION_FOR_IMPORT
|
18
14
|
end
|
19
15
|
|
20
|
-
def supports_on_duplicate_key_update?
|
21
|
-
|
16
|
+
def supports_on_duplicate_key_update?
|
17
|
+
database_version >= MIN_VERSION_FOR_UPSERT
|
22
18
|
end
|
23
19
|
|
24
20
|
# +sql+ can be a single string or an array. If it is an array all
|
@@ -96,7 +92,7 @@ module ActiveRecord::Import::SQLite3Adapter
|
|
96
92
|
|
97
93
|
# Returns a generated ON CONFLICT DO UPDATE statement given the passed
|
98
94
|
# in +args+.
|
99
|
-
def sql_for_on_duplicate_key_update(
|
95
|
+
def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc:
|
100
96
|
arg, primary_key, locking_column = args
|
101
97
|
arg = { columns: arg } if arg.is_a?( Array ) || arg.is_a?( String )
|
102
98
|
return unless arg.is_a?( Hash )
|
@@ -117,9 +113,9 @@ module ActiveRecord::Import::SQLite3Adapter
|
|
117
113
|
|
118
114
|
sql << "#{conflict_target}DO UPDATE SET "
|
119
115
|
if columns.is_a?( Array )
|
120
|
-
sql << sql_for_on_duplicate_key_update_as_array( locking_column, columns )
|
116
|
+
sql << sql_for_on_duplicate_key_update_as_array( table_name, locking_column, columns )
|
121
117
|
elsif columns.is_a?( Hash )
|
122
|
-
sql << sql_for_on_duplicate_key_update_as_hash( locking_column, columns )
|
118
|
+
sql << sql_for_on_duplicate_key_update_as_hash( table_name, locking_column, columns )
|
123
119
|
elsif columns.is_a?( String )
|
124
120
|
sql << columns
|
125
121
|
else
|
@@ -131,22 +127,22 @@ module ActiveRecord::Import::SQLite3Adapter
|
|
131
127
|
sql
|
132
128
|
end
|
133
129
|
|
134
|
-
def sql_for_on_duplicate_key_update_as_array( locking_column, arr ) # :nodoc:
|
130
|
+
def sql_for_on_duplicate_key_update_as_array( table_name, locking_column, arr ) # :nodoc:
|
135
131
|
results = arr.map do |column|
|
136
132
|
qc = quote_column_name( column )
|
137
133
|
"#{qc}=EXCLUDED.#{qc}"
|
138
134
|
end
|
139
|
-
increment_locking_column!(results, locking_column)
|
135
|
+
increment_locking_column!(table_name, results, locking_column)
|
140
136
|
results.join( ',' )
|
141
137
|
end
|
142
138
|
|
143
|
-
def sql_for_on_duplicate_key_update_as_hash( locking_column, hsh ) # :nodoc:
|
139
|
+
def sql_for_on_duplicate_key_update_as_hash( table_name, locking_column, hsh ) # :nodoc:
|
144
140
|
results = hsh.map do |column1, column2|
|
145
141
|
qc1 = quote_column_name( column1 )
|
146
142
|
qc2 = quote_column_name( column2 )
|
147
143
|
"#{qc1}=EXCLUDED.#{qc2}"
|
148
144
|
end
|
149
|
-
increment_locking_column!(results, locking_column)
|
145
|
+
increment_locking_column!(table_name, results, locking_column)
|
150
146
|
results.join( ',' )
|
151
147
|
end
|
152
148
|
|
@@ -170,9 +166,9 @@ module ActiveRecord::Import::SQLite3Adapter
|
|
170
166
|
exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('duplicate key')
|
171
167
|
end
|
172
168
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
169
|
+
private
|
170
|
+
|
171
|
+
def database_version
|
172
|
+
defined?(sqlite_version) ? sqlite_version : super
|
177
173
|
end
|
178
174
|
end
|
@@ -13,6 +13,7 @@ module ActiveRecord::Import
|
|
13
13
|
when 'postgresql_makara' then 'postgresql'
|
14
14
|
when 'makara_postgis' then 'postgresql'
|
15
15
|
when 'postgis' then 'postgresql'
|
16
|
+
when 'cockroachdb' then 'postgresql'
|
16
17
|
else adapter
|
17
18
|
end
|
18
19
|
end
|
@@ -26,7 +27,13 @@ module ActiveRecord::Import
|
|
26
27
|
|
27
28
|
# Loads the import functionality for the passed in ActiveRecord connection
|
28
29
|
def self.load_from_connection_pool(connection_pool)
|
29
|
-
|
30
|
+
adapter =
|
31
|
+
if connection_pool.respond_to?(:db_config) # ActiveRecord >= 6.1
|
32
|
+
connection_pool.db_config.adapter
|
33
|
+
else
|
34
|
+
connection_pool.spec.config[:adapter]
|
35
|
+
end
|
36
|
+
require_adapter adapter
|
30
37
|
end
|
31
38
|
end
|
32
39
|
|
@@ -26,6 +26,7 @@ module ActiveRecord::Import #:nodoc:
|
|
26
26
|
class Validator
|
27
27
|
def initialize(klass, options = {})
|
28
28
|
@options = options
|
29
|
+
@validator_class = klass
|
29
30
|
init_validations(klass)
|
30
31
|
end
|
31
32
|
|
@@ -70,6 +71,8 @@ module ActiveRecord::Import #:nodoc:
|
|
70
71
|
end
|
71
72
|
|
72
73
|
def valid_model?(model)
|
74
|
+
init_validations(model.class) unless model.class == @validator_class
|
75
|
+
|
73
76
|
validation_context = @options[:validate_with_context]
|
74
77
|
validation_context ||= (model.new_record? ? :create : :update)
|
75
78
|
current_context = model.send(:validation_context)
|
@@ -242,16 +245,17 @@ class ActiveRecord::Associations::CollectionAssociation
|
|
242
245
|
alias import bulk_import unless respond_to? :import
|
243
246
|
end
|
244
247
|
|
248
|
+
module ActiveRecord::Import::Connection
|
249
|
+
def establish_connection(args = nil)
|
250
|
+
conn = super(args)
|
251
|
+
ActiveRecord::Import.load_from_connection_pool connection_pool
|
252
|
+
conn
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
245
256
|
class ActiveRecord::Base
|
246
257
|
class << self
|
247
|
-
|
248
|
-
conn = establish_connection_without_activerecord_import(*args)
|
249
|
-
ActiveRecord::Import.load_from_connection_pool connection_pool
|
250
|
-
conn
|
251
|
-
end
|
252
|
-
|
253
|
-
alias establish_connection_without_activerecord_import establish_connection
|
254
|
-
alias establish_connection establish_connection_with_activerecord_import
|
258
|
+
prepend ActiveRecord::Import::Connection
|
255
259
|
|
256
260
|
# Returns true if the current database connection adapter
|
257
261
|
# supports import functionality, otherwise returns false.
|
@@ -578,7 +582,7 @@ class ActiveRecord::Base
|
|
578
582
|
if respond_to?(:timestamp_attributes_for_update, true)
|
579
583
|
send(:timestamp_attributes_for_update).map(&:to_sym)
|
580
584
|
else
|
581
|
-
|
585
|
+
allocate.send(:timestamp_attributes_for_update_in_model)
|
582
586
|
end
|
583
587
|
end
|
584
588
|
|
@@ -627,7 +631,7 @@ class ActiveRecord::Base
|
|
627
631
|
end
|
628
632
|
# supports empty array
|
629
633
|
elsif args.last.is_a?( Array ) && args.last.empty?
|
630
|
-
return ActiveRecord::Import::Result.new([], 0, [])
|
634
|
+
return ActiveRecord::Import::Result.new([], 0, [], [])
|
631
635
|
# supports 2-element array and array
|
632
636
|
elsif args.size == 2 && args.first.is_a?( Array ) && args.last.is_a?( Array )
|
633
637
|
|
@@ -699,8 +703,7 @@ class ActiveRecord::Base
|
|
699
703
|
# keep track of the instance and the position it is currently at. if this fails
|
700
704
|
# validation we'll use the index to remove it from the array_of_attributes
|
701
705
|
arr.each_with_index do |hsh, i|
|
702
|
-
model = new
|
703
|
-
hsh.each_pair { |k, v| model[k] = v }
|
706
|
+
model = new(hsh)
|
704
707
|
next if validator.valid_model?(model)
|
705
708
|
raise(ActiveRecord::RecordInvalid, model) if options[:raise_error]
|
706
709
|
array_of_attributes[i] = nil
|
@@ -838,6 +841,19 @@ class ActiveRecord::Base
|
|
838
841
|
end
|
839
842
|
end
|
840
843
|
|
844
|
+
deserialize_value = lambda do |column, value|
|
845
|
+
column = columns_hash[column]
|
846
|
+
return value unless column
|
847
|
+
if respond_to?(:type_caster)
|
848
|
+
type = type_for_attribute(column.name)
|
849
|
+
type.deserialize(value)
|
850
|
+
elsif column.respond_to?(:type_cast_from_database)
|
851
|
+
column.type_cast_from_database(value)
|
852
|
+
else
|
853
|
+
value
|
854
|
+
end
|
855
|
+
end
|
856
|
+
|
841
857
|
if models.size == import_result.results.size
|
842
858
|
columns = Array(options[:returning])
|
843
859
|
single_column = "#{columns.first}=" if columns.size == 1
|
@@ -845,10 +861,12 @@ class ActiveRecord::Base
|
|
845
861
|
model = models[index]
|
846
862
|
|
847
863
|
if single_column
|
848
|
-
|
864
|
+
val = deserialize_value.call(columns.first, result)
|
865
|
+
model.send(single_column, val)
|
849
866
|
else
|
850
867
|
columns.each_with_index do |column, col_index|
|
851
|
-
|
868
|
+
val = deserialize_value.call(column, result[col_index])
|
869
|
+
model.send("#{column}=", val)
|
852
870
|
end
|
853
871
|
end
|
854
872
|
end
|
@@ -869,10 +887,12 @@ class ActiveRecord::Base
|
|
869
887
|
|
870
888
|
# Sync belongs_to association ids with foreign key field
|
871
889
|
def load_association_ids(model)
|
890
|
+
changed_columns = model.changed
|
872
891
|
association_reflections = model.class.reflect_on_all_associations(:belongs_to)
|
873
892
|
association_reflections.each do |association_reflection|
|
874
893
|
column_name = association_reflection.foreign_key
|
875
894
|
next if association_reflection.options[:polymorphic]
|
895
|
+
next if changed_columns.include?(column_name)
|
876
896
|
association = model.association(association_reflection.name)
|
877
897
|
association = association.target
|
878
898
|
next if association.blank? || model.public_send(column_name).present?
|
@@ -952,7 +972,7 @@ class ActiveRecord::Base
|
|
952
972
|
elsif column
|
953
973
|
if respond_to?(:type_caster) # Rails 5.0 and higher
|
954
974
|
type = type_for_attribute(column.name)
|
955
|
-
val = type.type == :boolean ? type.cast(val) : type.serialize(val)
|
975
|
+
val = !type.respond_to?(:subtype) && type.type == :boolean ? type.cast(val) : type.serialize(val)
|
956
976
|
connection_memo.quote(val)
|
957
977
|
elsif column.respond_to?(:type_cast_from_user) # Rails 4.2
|
958
978
|
connection_memo.quote(column.type_cast_from_user(val), column)
|
@@ -961,7 +981,7 @@ class ActiveRecord::Base
|
|
961
981
|
val = serialized_attributes[column.name].dump(val)
|
962
982
|
end
|
963
983
|
# Fixes #443 to support binary (i.e. bytea) columns on PG
|
964
|
-
val = column.type_cast(val) unless column.type.to_sym == :binary
|
984
|
+
val = column.type_cast(val) unless column.type && column.type.to_sym == :binary
|
965
985
|
connection_memo.quote(val, column)
|
966
986
|
end
|
967
987
|
else
|
@@ -980,7 +1000,7 @@ class ActiveRecord::Base
|
|
980
1000
|
timestamp_columns[:create] = timestamp_attributes_for_create_in_model
|
981
1001
|
timestamp_columns[:update] = timestamp_attributes_for_update_in_model
|
982
1002
|
else
|
983
|
-
instance =
|
1003
|
+
instance = allocate
|
984
1004
|
timestamp_columns[:create] = instance.send(:timestamp_attributes_for_create_in_model)
|
985
1005
|
timestamp_columns[:update] = instance.send(:timestamp_attributes_for_update_in_model)
|
986
1006
|
end
|
@@ -39,8 +39,8 @@ module ActiveRecord # :nodoc:
|
|
39
39
|
|
40
40
|
next unless matched_instance
|
41
41
|
|
42
|
-
instance.send :clear_aggregation_cache
|
43
42
|
instance.send :clear_association_cache
|
43
|
+
instance.send :clear_aggregation_cache if instance.respond_to?(:clear_aggregation_cache, true)
|
44
44
|
instance.instance_variable_set :@attributes, matched_instance.instance_variable_get(:@attributes)
|
45
45
|
|
46
46
|
if instance.respond_to?(:clear_changes_information)
|
data/test/import_test.rb
CHANGED
@@ -900,4 +900,33 @@ describe "#import" do
|
|
900
900
|
end
|
901
901
|
end
|
902
902
|
end
|
903
|
+
describe "importing model with after_initialize callback" do
|
904
|
+
let(:columns) { %w(name size) }
|
905
|
+
let(:valid_values) { [%w("Deer", "Small"), %w("Monkey", "Medium")] }
|
906
|
+
let(:invalid_values) do
|
907
|
+
[
|
908
|
+
{ name: "giraffe", size: "Large" },
|
909
|
+
{ size: "Medium" } # name is missing
|
910
|
+
]
|
911
|
+
end
|
912
|
+
context "with validation checks turned off" do
|
913
|
+
it "should import valid data" do
|
914
|
+
Animal.import(columns, valid_values, validate: false)
|
915
|
+
assert_equal 2, Animal.count
|
916
|
+
end
|
917
|
+
it "should raise ArgumentError" do
|
918
|
+
assert_raise(ArgumentError) { Animal.import(invalid_values, validate: false) }
|
919
|
+
end
|
920
|
+
end
|
921
|
+
|
922
|
+
context "with validation checks turned on" do
|
923
|
+
it "should import valid data" do
|
924
|
+
Animal.import(columns, valid_values, validate: true)
|
925
|
+
assert_equal 2, Animal.count
|
926
|
+
end
|
927
|
+
it "should raise ArgumentError" do
|
928
|
+
assert_raise(ArgumentError) { Animal.import(invalid_values, validate: true) }
|
929
|
+
end
|
930
|
+
end
|
931
|
+
end
|
903
932
|
end
|
@@ -3,8 +3,20 @@ ActiveRecord::Schema.define do
|
|
3
3
|
execute('CREATE extension IF NOT EXISTS "pgcrypto";')
|
4
4
|
execute('CREATE extension IF NOT EXISTS "uuid-ossp";')
|
5
5
|
|
6
|
+
# create ENUM if it does not exist yet
|
7
|
+
begin
|
8
|
+
execute('CREATE TYPE vendor_type AS ENUM (\'wholesaler\', \'retailer\');')
|
9
|
+
rescue ActiveRecord::StatementInvalid => e
|
10
|
+
# since PostgreSQL does not support IF NOT EXISTS when creating a TYPE,
|
11
|
+
# rescue the error and check the error class
|
12
|
+
raise unless e.cause.is_a? PG::DuplicateObject
|
13
|
+
execute('ALTER TYPE vendor_type ADD VALUE IF NOT EXISTS \'wholesaler\';')
|
14
|
+
execute('ALTER TYPE vendor_type ADD VALUE IF NOT EXISTS \'retailer\';')
|
15
|
+
end
|
16
|
+
|
6
17
|
create_table :vendors, id: :uuid, force: :cascade do |t|
|
7
18
|
t.string :name, null: true
|
19
|
+
t.text :hours
|
8
20
|
t.text :preferences
|
9
21
|
|
10
22
|
if t.respond_to?(:json)
|
@@ -29,6 +41,8 @@ ActiveRecord::Schema.define do
|
|
29
41
|
t.text :json_data
|
30
42
|
end
|
31
43
|
|
44
|
+
t.column :vendor_type, :vendor_type
|
45
|
+
|
32
46
|
t.datetime :created_at
|
33
47
|
t.datetime :updated_at
|
34
48
|
end
|
@@ -116,6 +116,26 @@ def should_support_postgresql_import_functionality
|
|
116
116
|
assert_equal [%w(King It)], result.results
|
117
117
|
end
|
118
118
|
|
119
|
+
context "when given an empty array" do
|
120
|
+
let(:result) { Book.import([], returning: %w(title)) }
|
121
|
+
|
122
|
+
setup { result }
|
123
|
+
|
124
|
+
it "returns empty arrays for ids and results" do
|
125
|
+
assert_equal [], result.ids
|
126
|
+
assert_equal [], result.results
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
context "when a returning column is a serialized attribute" do
|
131
|
+
let(:vendor) { Vendor.new(hours: { monday: '8-5' }) }
|
132
|
+
let(:result) { Vendor.import([vendor], returning: %w(hours)) }
|
133
|
+
|
134
|
+
it "creates records" do
|
135
|
+
assert_difference("Vendor.count", +1) { result }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
119
139
|
context "when primary key and returning overlap" do
|
120
140
|
let(:result) { Book.import(books, returning: %w(id title)) }
|
121
141
|
|
@@ -249,6 +269,17 @@ def should_support_postgresql_import_functionality
|
|
249
269
|
end
|
250
270
|
end
|
251
271
|
|
272
|
+
describe "with enum field" do
|
273
|
+
let(:vendor_type) { "retailer" }
|
274
|
+
it "imports the correct values for enum fields" do
|
275
|
+
vendor = Vendor.new(name: 'Vendor 1', vendor_type: vendor_type)
|
276
|
+
assert_difference "Vendor.count", +1 do
|
277
|
+
Vendor.import [vendor]
|
278
|
+
end
|
279
|
+
assert_equal(vendor_type, Vendor.first.vendor_type)
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
252
283
|
describe "with binary field" do
|
253
284
|
let(:binary_value) { "\xE0'c\xB2\xB0\xB3Bh\\\xC2M\xB1m\\I\xC4r".force_encoding('ASCII-8BIT') }
|
254
285
|
it "imports the correct values for binary fields" do
|
@@ -73,6 +73,16 @@ def should_support_basic_on_duplicate_key_update
|
|
73
73
|
assert_equal user.name, users[i].name + ' Rothschild'
|
74
74
|
assert_equal 1, user.lock_version
|
75
75
|
end
|
76
|
+
updated_values2 = User.all.map do |user|
|
77
|
+
user.name += ' jr.'
|
78
|
+
{ id: user.id, name: user.name }
|
79
|
+
end
|
80
|
+
User.import(updated_values2, on_duplicate_key_update: [:name])
|
81
|
+
assert User.count == updated_values2.length
|
82
|
+
User.all.each_with_index do |user, i|
|
83
|
+
assert_equal user.name, users[i].name + ' Rothschild jr.'
|
84
|
+
assert_equal 2, user.lock_version
|
85
|
+
end
|
76
86
|
end
|
77
87
|
|
78
88
|
it 'upsert optimistic lock columns other than lock_version by model' do
|
@@ -138,6 +138,15 @@ def should_support_recursive_import
|
|
138
138
|
books.each do |book|
|
139
139
|
assert_equal book.topic_id, second_new_topic.id
|
140
140
|
end
|
141
|
+
|
142
|
+
books.each { |book| book.topic_id = nil }
|
143
|
+
assert_no_difference "Book.count", books.size do
|
144
|
+
Book.import books, validate: false, on_duplicate_key_update: [:topic_id]
|
145
|
+
end
|
146
|
+
|
147
|
+
books.each do |book|
|
148
|
+
assert_equal book.topic_id, nil
|
149
|
+
end
|
141
150
|
end
|
142
151
|
|
143
152
|
unless ENV["SKIP_COMPOSITE_PK"]
|
@@ -5,21 +5,8 @@ def should_support_sqlite3_import_functionality
|
|
5
5
|
end
|
6
6
|
|
7
7
|
describe "#supports_imports?" do
|
8
|
-
|
9
|
-
|
10
|
-
version = ActiveRecord::ConnectionAdapters::SQLite3Adapter::Version.new("3.7.11")
|
11
|
-
assert ActiveRecord::Base.supports_import?(version)
|
12
|
-
|
13
|
-
version = ActiveRecord::ConnectionAdapters::SQLite3Adapter::Version.new("3.7.12")
|
14
|
-
assert ActiveRecord::Base.supports_import?(version)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context "and SQLite less than 3.7.11" do
|
19
|
-
it "doesn't support import" do
|
20
|
-
version = ActiveRecord::ConnectionAdapters::SQLite3Adapter::Version.new("3.7.10")
|
21
|
-
assert !ActiveRecord::Base.supports_import?(version)
|
22
|
-
end
|
8
|
+
it "should support import" do
|
9
|
+
assert ActiveRecord::Base.supports_import?
|
23
10
|
end
|
24
11
|
end
|
25
12
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Dennis
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -75,6 +75,8 @@ files:
|
|
75
75
|
- gemfiles/5.0.gemfile
|
76
76
|
- gemfiles/5.1.gemfile
|
77
77
|
- gemfiles/5.2.gemfile
|
78
|
+
- gemfiles/6.0.gemfile
|
79
|
+
- gemfiles/6.1.gemfile
|
78
80
|
- lib/activerecord-import.rb
|
79
81
|
- lib/activerecord-import/active_record/adapters/abstract_adapter.rb
|
80
82
|
- lib/activerecord-import/active_record/adapters/jdbcmysql_adapter.rb
|
@@ -119,6 +121,7 @@ files:
|
|
119
121
|
- test/makara_postgis/import_test.rb
|
120
122
|
- test/models/account.rb
|
121
123
|
- test/models/alarm.rb
|
124
|
+
- test/models/animal.rb
|
122
125
|
- test/models/bike_maker.rb
|
123
126
|
- test/models/book.rb
|
124
127
|
- test/models/car.rb
|
@@ -166,9 +169,9 @@ files:
|
|
166
169
|
- test/value_sets_records_parser_test.rb
|
167
170
|
homepage: http://github.com/zdennis/activerecord-import
|
168
171
|
licenses:
|
169
|
-
-
|
172
|
+
- MIT
|
170
173
|
metadata: {}
|
171
|
-
post_install_message:
|
174
|
+
post_install_message:
|
172
175
|
rdoc_options: []
|
173
176
|
require_paths:
|
174
177
|
- lib
|
@@ -176,16 +179,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
176
179
|
requirements:
|
177
180
|
- - ">="
|
178
181
|
- !ruby/object:Gem::Version
|
179
|
-
version:
|
182
|
+
version: 2.0.0
|
180
183
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
181
184
|
requirements:
|
182
185
|
- - ">="
|
183
186
|
- !ruby/object:Gem::Version
|
184
187
|
version: '0'
|
185
188
|
requirements: []
|
186
|
-
|
187
|
-
|
188
|
-
signing_key:
|
189
|
+
rubygems_version: 3.0.6
|
190
|
+
signing_key:
|
189
191
|
specification_version: 4
|
190
192
|
summary: Bulk insert extension for ActiveRecord
|
191
193
|
test_files:
|
@@ -210,6 +212,7 @@ test_files:
|
|
210
212
|
- test/makara_postgis/import_test.rb
|
211
213
|
- test/models/account.rb
|
212
214
|
- test/models/alarm.rb
|
215
|
+
- test/models/animal.rb
|
213
216
|
- test/models/bike_maker.rb
|
214
217
|
- test/models/book.rb
|
215
218
|
- test/models/car.rb
|