activerecord-import 1.0.2 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yaml +78 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +91 -3
- data/Gemfile +6 -2
- data/LICENSE +21 -56
- data/README.markdown +61 -53
- data/activerecord-import.gemspec +4 -4
- data/benchmarks/schema/{mysql_schema.rb → mysql2_schema.rb} +0 -0
- data/gemfiles/6.0.gemfile +2 -1
- data/gemfiles/6.1.gemfile +2 -1
- data/gemfiles/7.0.gemfile +1 -0
- data/lib/activerecord-import/active_record/adapters/jdbcmysql_adapter.rb +4 -4
- data/lib/activerecord-import/adapters/abstract_adapter.rb +6 -0
- data/lib/activerecord-import/adapters/mysql_adapter.rb +6 -6
- data/lib/activerecord-import/adapters/postgresql_adapter.rb +3 -11
- data/lib/activerecord-import/adapters/sqlite3_adapter.rb +7 -15
- data/lib/activerecord-import/base.rb +7 -1
- data/lib/activerecord-import/import.rb +95 -42
- data/lib/activerecord-import/synchronize.rb +1 -1
- data/lib/activerecord-import/value_sets_parser.rb +2 -0
- data/lib/activerecord-import/version.rb +1 -1
- data/test/{travis → github}/database.yml +3 -1
- data/test/import_test.rb +67 -1
- data/test/models/animal.rb +6 -0
- data/test/models/card.rb +3 -0
- data/test/models/customer.rb +6 -0
- data/test/models/deck.rb +6 -0
- data/test/models/order.rb +6 -0
- data/test/models/playing_card.rb +2 -0
- data/test/schema/generic_schema.rb +25 -0
- data/test/schema/postgresql_schema.rb +14 -0
- data/test/support/postgresql/import_examples.rb +55 -0
- data/test/support/shared_examples/on_duplicate_key_update.rb +10 -0
- data/test/support/shared_examples/recursive_import.rb +30 -1
- data/test/test_helper.rb +10 -1
- metadata +25 -16
- data/.travis.yml +0 -70
- data/gemfiles/3.2.gemfile +0 -2
- data/gemfiles/4.0.gemfile +0 -2
- data/gemfiles/4.1.gemfile +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 815280c3b17a8ee84e54defd450a68fa532719dc8760ac001883f3df0156d689
|
4
|
+
data.tar.gz: d34ee8c4c186b172259ea4f3427a75379d08b7ae41ee67128fd870e489d2f78d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49c6d78cbcdc342db44497ec66f0dfe466b08124dc233996688c9443b0c455328e6d7ebc80d011c8732f7ae5b5468bd3c4f57f4abe2bb20bc26a118183cea402
|
7
|
+
data.tar.gz: 9f602bb4423fb3cc5169727d150df7eef5396954f0576f1ecf8cb168179adc625f5ef5927a4655eb8271e37d845d9e5c6b6a6a597a83cb1c268958b4f2f65375
|
@@ -0,0 +1,78 @@
|
|
1
|
+
name: Test
|
2
|
+
on: [push, pull_request]
|
3
|
+
jobs:
|
4
|
+
test:
|
5
|
+
services:
|
6
|
+
postgres:
|
7
|
+
image: postgis/postgis:10-2.5
|
8
|
+
env:
|
9
|
+
POSTGRES_USER: postgres
|
10
|
+
POSTGRES_PASSWORD: postgres
|
11
|
+
ports:
|
12
|
+
- 5432:5432
|
13
|
+
# Set health checks to wait until postgres has started
|
14
|
+
options: >-
|
15
|
+
--health-cmd pg_isready
|
16
|
+
--health-interval 10s
|
17
|
+
--health-timeout 5s
|
18
|
+
--health-retries 5
|
19
|
+
strategy:
|
20
|
+
fail-fast: false
|
21
|
+
matrix:
|
22
|
+
ruby:
|
23
|
+
- 2.7
|
24
|
+
env:
|
25
|
+
- AR_VERSION: 7.0
|
26
|
+
- AR_VERSION: 6.1
|
27
|
+
- AR_VERSION: 6.0
|
28
|
+
include:
|
29
|
+
- ruby: 2.6
|
30
|
+
env:
|
31
|
+
AR_VERSION: 5.2
|
32
|
+
- ruby: 2.6
|
33
|
+
env:
|
34
|
+
AR_VERSION: 5.1
|
35
|
+
- ruby: 2.4
|
36
|
+
env:
|
37
|
+
AR_VERSION: 5.0
|
38
|
+
- ruby: 2.4
|
39
|
+
env:
|
40
|
+
AR_VERSION: 4.2
|
41
|
+
runs-on: ubuntu-latest
|
42
|
+
env:
|
43
|
+
AR_VERSION: ${{ matrix.env.AR_VERSION }}
|
44
|
+
DB_DATABASE: activerecord_import_test
|
45
|
+
steps:
|
46
|
+
- uses: actions/checkout@v2
|
47
|
+
- uses: ruby/setup-ruby@v1
|
48
|
+
with:
|
49
|
+
ruby-version: ${{ matrix.ruby }}
|
50
|
+
- name: Setup Bundler 1.x for Ruby 2.3
|
51
|
+
if: ${{ matrix.ruby == '2.3' }}
|
52
|
+
run: echo "BUNDLER_VERSION=1.17.3" >> $GITHUB_ENV
|
53
|
+
- name: Set up databases
|
54
|
+
run: |
|
55
|
+
sudo /etc/init.d/mysql start
|
56
|
+
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }} CHARACTER SET utf8 COLLATE utf8_general_ci;' -u root -proot
|
57
|
+
psql -h localhost -U postgres -c 'create database ${{ env.DB_DATABASE }};'
|
58
|
+
psql -h localhost -U postgres -d ${{ env.DB_DATABASE }} -c 'create extension if not exists hstore;'
|
59
|
+
psql -h localhost -U postgres -c 'create extension if not exists postgis;'
|
60
|
+
psql -h localhost -U postgres -c 'create extension if not exists "uuid-ossp";'
|
61
|
+
cp test/github/database.yml test/database.yml
|
62
|
+
env:
|
63
|
+
PGPASSWORD: postgres
|
64
|
+
- name: Install dependencies
|
65
|
+
run : AR_VERSION=${{ env.AR_VERSION }} bundle install
|
66
|
+
- name: Run tests
|
67
|
+
run: |
|
68
|
+
bundle exec rake test:mysql2
|
69
|
+
bundle exec rake test:mysql2_makara
|
70
|
+
bundle exec rake test:mysql2spatial
|
71
|
+
bundle exec rake test:postgis
|
72
|
+
bundle exec rake test:postgresql
|
73
|
+
bundle exec rake test:postgresql_makara
|
74
|
+
bundle exec rake test:seamless_database_pool
|
75
|
+
bundle exec rake test:spatialite
|
76
|
+
bundle exec rake test:sqlite3
|
77
|
+
- name: Run Rubocop
|
78
|
+
run: bundle exec rubocop
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,91 @@
|
|
1
|
+
## Changes in 1.3.0
|
2
|
+
|
3
|
+
### Fixes
|
4
|
+
|
5
|
+
* Ensure correct timestamp values are returned for models after insert. Thanks to @kos1kov via \##756.
|
6
|
+
* Restore database_version method to public scope. Thanks to @beauraF via \##753.
|
7
|
+
|
8
|
+
### New Features
|
9
|
+
|
10
|
+
* Add support for ActiveRecord 7.0. Thanks to @nickhammond, @ryanwood, @jkowens via \##749 and \##752.
|
11
|
+
* Add support for compound foreign keys. Thanks to @Uladzimiro via \##750.
|
12
|
+
* Add support for :recursive combined with on_duplicate_key_update: :all. Thanks to @deathwish via \##746.
|
13
|
+
|
14
|
+
## Changes in 1.2.0
|
15
|
+
|
16
|
+
### Fixes
|
17
|
+
|
18
|
+
* Update JDBC MySQL adapter to use mysql2 connection adapter. Thanks to @terencechow via \##744.
|
19
|
+
* Fix importing STI models with ActiveRecord 6. Thanks to @clemens1483 via \##743.
|
20
|
+
* Use polymorphic_name instead of base_class.name for imports. Thanks to @kmhajjar via \##741.
|
21
|
+
* Fix compatibility issue with composite primary keys. Thanks to @dlanileonardo via \##737.
|
22
|
+
* Prevent double validation of associations on recursive import.
|
23
|
+
|
24
|
+
## Changes in 1.1.0
|
25
|
+
|
26
|
+
### New Features
|
27
|
+
|
28
|
+
* Add batch progress reporting. Thanks to @gee-forr via \##729.
|
29
|
+
|
30
|
+
## Changes in 1.0.8
|
31
|
+
|
32
|
+
### Fixes
|
33
|
+
|
34
|
+
* Use correct method for clearing query cache. Thanks to @EtienneDepaulis via \##719.
|
35
|
+
|
36
|
+
## Changes in 1.0.7
|
37
|
+
|
38
|
+
### New Features
|
39
|
+
|
40
|
+
* Use @@max_allowed_packet session variable instead of querying SHOW VARIABLES. Thanks to @diclophis via \#706.
|
41
|
+
* Add option :track_validation_failures. When this is set to true, failed_instances will be an array of arrays, with each inner array having the form [:index_in_dataset, :object_with_errors]. Thanks to @rorymckinley via \#684.
|
42
|
+
|
43
|
+
### Fixes
|
44
|
+
|
45
|
+
* Prevent mass-assignment errors in Rails strict mode. Thanks to @diclophis via \##709.
|
46
|
+
|
47
|
+
## Changes in 1.0.6
|
48
|
+
|
49
|
+
### Fixes
|
50
|
+
|
51
|
+
* Handle after_initialize callbacks. Thanks to @AhMohsen46 via \#691 and
|
52
|
+
\#692.
|
53
|
+
* Fix regression introduced in 1.0.4. Explicitly allow adapters to
|
54
|
+
support on duplicate key update. Thanks to @dsobiera, @jkowens via \#696.
|
55
|
+
|
56
|
+
## Changes in 1.0.5
|
57
|
+
|
58
|
+
### Fixes
|
59
|
+
|
60
|
+
* Allow serialized attributes to be returned from import. Thanks to @timanovsky, @jkowens via \#660.
|
61
|
+
* Return ActiveRecord::Connection from
|
62
|
+
ActiveRecord::Base#establish_connection. Thanks to @reverentF via
|
63
|
+
\#663.
|
64
|
+
* Support PostgreSQL array. Thanks to @ujihisa via \#669.
|
65
|
+
* Skip loading association ids when column changed. Thanks to @Aristat
|
66
|
+
via \#673.
|
67
|
+
|
68
|
+
## Changes in 1.0.4
|
69
|
+
|
70
|
+
### Fixes
|
71
|
+
|
72
|
+
* Use prepend pattern for ActiveRecord::Base#establish_connection patching. Thanks to @dombesz via \#648.
|
73
|
+
* Fix NoMethodError when using PostgreSQL ENUM types. Thanks to @sebcoetzee via \#651.
|
74
|
+
* Fix issue updating optimistic lock in Postgres. Thanks to @timanovsky
|
75
|
+
via \#656.
|
76
|
+
|
77
|
+
## Changes in 1.0.3
|
78
|
+
|
79
|
+
### New Features
|
80
|
+
|
81
|
+
* Add support for ActiveRecord 6.1.0.alpha. Thanks to @imtayadeway via
|
82
|
+
\#642.
|
83
|
+
|
84
|
+
### Fixes
|
85
|
+
|
86
|
+
* Return an empty array for results instead of nil when importing empty
|
87
|
+
array. Thanks to @gyfis via \#636.
|
88
|
+
|
1
89
|
## Changes in 1.0.2
|
2
90
|
|
3
91
|
### New Features
|
@@ -26,7 +114,7 @@
|
|
26
114
|
* Fix import issue for models with Postgresql json/jsonb fields. Thanks
|
27
115
|
to @stokarenko via \#594.
|
28
116
|
* Fix issue importing models with timestamps that contain timezone
|
29
|
-
information.
|
117
|
+
information. Thanks to @dekaikiwi, @jkowens via \#598.
|
30
118
|
* Ignore :no_returning when using :recursive option. Thanks to @dgollahon, @jkowens
|
31
119
|
via \#599.
|
32
120
|
|
@@ -241,7 +329,7 @@
|
|
241
329
|
Thanks to @jkowens via \#301.
|
242
330
|
* Allow for custom timestamp columns. Thanks to @mojidabckuu, @jkowens
|
243
331
|
via \#401.
|
244
|
-
|
332
|
+
|
245
333
|
### Fixes
|
246
334
|
|
247
335
|
* Fix ActiveRecord 5 issue coercing boolean values when serializing
|
@@ -253,7 +341,7 @@
|
|
253
341
|
|
254
342
|
* Fix issue where PostgreSQL cannot recognize columns if names
|
255
343
|
include mixed case characters. Thanks to @hugobgranja via \#379.
|
256
|
-
* Fix an issue for ActiveRecord 5 where serialized fields with
|
344
|
+
* Fix an issue for ActiveRecord 5 where serialized fields with
|
257
345
|
default values were not being typecast. Thanks to @whistlerbrk,
|
258
346
|
@jkowens via \#386.
|
259
347
|
* Add option :force_single_insert for MySQL to make sure a single
|
data/Gemfile
CHANGED
@@ -6,8 +6,11 @@ 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
|
+
mysql2_version = '0.5.0' if version >= 6.1
|
9
10
|
sqlite3_version = '1.3.0'
|
10
|
-
sqlite3_version = '1.4.0' if version >=
|
11
|
+
sqlite3_version = '1.4.0' if version >= 6.0
|
12
|
+
pg_version = '0.9'
|
13
|
+
pg_version = '1.1' if version >= 6.1
|
11
14
|
|
12
15
|
group :development, :test do
|
13
16
|
gem 'rubocop', '~> 0.40.0'
|
@@ -17,7 +20,7 @@ end
|
|
17
20
|
# Database Adapters
|
18
21
|
platforms :ruby do
|
19
22
|
gem "mysql2", "~> #{mysql2_version}"
|
20
|
-
gem "pg", "~>
|
23
|
+
gem "pg", "~> #{pg_version}"
|
21
24
|
gem "sqlite3", "~> #{sqlite3_version}"
|
22
25
|
gem "seamless_database_pool", "~> 1.0.20"
|
23
26
|
end
|
@@ -47,6 +50,7 @@ end
|
|
47
50
|
|
48
51
|
platforms :ruby do
|
49
52
|
gem "pry-byebug"
|
53
|
+
gem "pry", "~> 0.12.0"
|
50
54
|
gem "rb-readline"
|
51
55
|
end
|
52
56
|
|
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://github.com/zdennis/activerecord-import/actions/workflows/test.yaml/badge.svg)
|
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,11 +54,14 @@ 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
|
@@ -85,7 +88,7 @@ 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
94
|
Book.import columns, values, validate: false
|
@@ -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,7 +174,7 @@ 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(title: "Book 1", author: "
|
177
|
+
Book.new(title: "Book 1", author: "George Orwell"),
|
175
178
|
Book.new(title: "Book 2", author: "Bob Jones")
|
176
179
|
]
|
177
180
|
|
@@ -189,7 +192,7 @@ 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(title: "Book 1", author: "
|
195
|
+
Book.new(title: "Book 1", author: "George Orwell"),
|
193
196
|
Book.new(title: "Book 2", author: "Bob Jones")
|
194
197
|
]
|
195
198
|
columns = [ :title ]
|
@@ -217,7 +220,7 @@ 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(title: "Book 1", author: "
|
223
|
+
Book.new(title: "Book 1", author: "George Orwell"),
|
221
224
|
Book.new(title: "Book 2", author: "Bob Jones"),
|
222
225
|
Book.new(title: "Book 1", author: "John Doe"),
|
223
226
|
Book.new(title: "Book 2", author: "Richard Wright")
|
@@ -228,9 +231,22 @@ columns = [ :title ]
|
|
228
231
|
Book.import columns, books, batch_size: 2
|
229
232
|
```
|
230
233
|
|
234
|
+
If your import is particularly large or slow (possibly due to [callbacks](#callbacks)) whilst batch importing, you might want a way to report back on progress. This is supported by passing a callable as the `batch_progress` option. e.g:
|
235
|
+
|
236
|
+
```ruby
|
237
|
+
my_proc = ->(rows_size, num_batches, current_batch_number, batch_duration_in_secs) {
|
238
|
+
# Using the arguments provided to the callable, you can
|
239
|
+
# send an email, post to a websocket,
|
240
|
+
# update slack, alert if import is taking too long, etc.
|
241
|
+
}
|
242
|
+
|
243
|
+
Book.import columns, books, batch_size: 2, batch_progress: my_proc
|
244
|
+
```
|
245
|
+
|
231
246
|
#### Recursive
|
232
247
|
|
233
|
-
NOTE: This only works with PostgreSQL.
|
248
|
+
NOTE: This only works with PostgreSQL and ActiveRecord objects. This won't work with
|
249
|
+
hashes or arrays as recursive inputs.
|
234
250
|
|
235
251
|
Assume that Books <code>has_many</code> Reviews.
|
236
252
|
|
@@ -246,19 +262,21 @@ Book.import books, recursive: true
|
|
246
262
|
|
247
263
|
### Options
|
248
264
|
|
249
|
-
Key
|
250
|
-
|
251
|
-
:validate
|
252
|
-
:validate_uniqueness
|
253
|
-
:
|
254
|
-
:
|
255
|
-
:
|
256
|
-
:
|
257
|
-
:
|
258
|
-
:
|
259
|
-
:
|
260
|
-
:
|
261
|
-
|
265
|
+
Key | Options | Default | Description
|
266
|
+
------------------------- | --------------------- | ------------------ | -----------
|
267
|
+
:validate | `true`/`false` | `true` | Whether or not to run `ActiveRecord` validations (uniqueness skipped). This option will always be true when using `import!`.
|
268
|
+
:validate_uniqueness | `true`/`false` | `false` | Whether or not to run uniqueness validations, has potential pitfalls, use with caution (requires `>= v0.27.0`).
|
269
|
+
: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.
|
270
|
+
:track_validation_failures| `true`/`false` | `false` | When this is set to true, `failed_instances` will be an array of arrays, with each inner array having the form `[:index_in_dataset, :object_with_errors]`
|
271
|
+
: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.
|
272
|
+
:ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
|
273
|
+
: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.
|
274
|
+
:synchronize | `Array` | N/A | An array of ActiveRecord instances. This synchronizes existing instances in memory with updates from the import.
|
275
|
+
:timestamps | `true`/`false` | `true` | Enables/disables timestamps on imported records.
|
276
|
+
:recursive | `true`/`false` | `false` | Imports has_many/has_one associations (PostgreSQL only).
|
277
|
+
:batch_size | `Integer` | total # of records | Max number of records to insert per import
|
278
|
+
: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.
|
279
|
+
:all_or_none | `true`/`false` | `false` | Will not import any records if there is a record with validation errors.
|
262
280
|
|
263
281
|
#### Duplicate Key Ignore
|
264
282
|
|
@@ -267,14 +285,14 @@ Key | Options | Default | Descripti
|
|
267
285
|
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
286
|
|
269
287
|
```ruby
|
270
|
-
book = Book.create! title: "Book1", author: "
|
288
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
271
289
|
book.title = "Updated Book Title"
|
272
290
|
book.author = "Bob Barker"
|
273
291
|
|
274
292
|
Book.import [book], on_duplicate_key_ignore: true
|
275
293
|
|
276
294
|
book.reload.title # => "Book1" (stayed the same)
|
277
|
-
book.reload.author # => "
|
295
|
+
book.reload.author # => "George Orwell" (stayed the same)
|
278
296
|
```
|
279
297
|
|
280
298
|
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 +308,7 @@ This will use MySQL's `ON DUPLICATE KEY UPDATE` or Postgres/SQLite `ON CONFLICT
|
|
290
308
|
Basic Update
|
291
309
|
|
292
310
|
```ruby
|
293
|
-
book = Book.create! title: "Book1", author: "
|
311
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
294
312
|
book.title = "Updated Book Title"
|
295
313
|
book.author = "Bob Barker"
|
296
314
|
|
@@ -304,13 +322,13 @@ Book.import [book], on_duplicate_key_update: {conflict_target: [:id], columns: [
|
|
304
322
|
Book.import [book], on_duplicate_key_update: [:title]
|
305
323
|
|
306
324
|
book.reload.title # => "Updated Book Title" (changed)
|
307
|
-
book.reload.author # => "
|
325
|
+
book.reload.author # => "George Orwell" (stayed the same)
|
308
326
|
```
|
309
327
|
|
310
328
|
Using the value from another column
|
311
329
|
|
312
330
|
```ruby
|
313
|
-
book = Book.create! title: "Book1", author: "
|
331
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
314
332
|
book.title = "Updated Book Title"
|
315
333
|
|
316
334
|
# MySQL version
|
@@ -328,7 +346,7 @@ book.reload.author # => "Updated Book Title" (changed)
|
|
328
346
|
Using Custom SQL
|
329
347
|
|
330
348
|
```ruby
|
331
|
-
book = Book.create! title: "Book1", author: "
|
349
|
+
book = Book.create! title: "Book1", author: "George Orwell"
|
332
350
|
book.author = "Bob Barker"
|
333
351
|
|
334
352
|
# MySQL version
|
@@ -349,7 +367,7 @@ book.reload.author # => "Bob Barker" (changed)
|
|
349
367
|
PostgreSQL Using constraints
|
350
368
|
|
351
369
|
```ruby
|
352
|
-
book = Book.create! title: "Book1", author: "
|
370
|
+
book = Book.create! title: "Book1", author: "George Orwell", edition: 3, published_at: nil
|
353
371
|
book.published_at = Time.now
|
354
372
|
|
355
373
|
# in migration
|
@@ -363,7 +381,7 @@ Book.import [book], on_duplicate_key_update: {constraint_name: :for_upsert, colu
|
|
363
381
|
|
364
382
|
|
365
383
|
book.reload.title # => "Book1" (stayed the same)
|
366
|
-
book.reload.author # => "
|
384
|
+
book.reload.author # => "George Orwell" (stayed the same)
|
367
385
|
book.reload.edition # => 3 (stayed the same)
|
368
386
|
book.reload.published_at # => 2017-10-09 (changed)
|
369
387
|
```
|
@@ -374,7 +392,7 @@ Book.import books, validate_uniqueness: true
|
|
374
392
|
|
375
393
|
### Return Info
|
376
394
|
|
377
|
-
The `import` method returns a `Result` object that responds to `failed_instances` and `num_inserts`. Additionally, for users of Postgres, there will be two arrays `ids` and `results` that can be accessed
|
395
|
+
The `import` method returns a `Result` object that responds to `failed_instances` and `num_inserts`. Additionally, for users of Postgres, there will be two arrays `ids` and `results` that can be accessed.
|
378
396
|
|
379
397
|
```ruby
|
380
398
|
articles = [
|
@@ -524,7 +542,7 @@ require 'activerecord-import'
|
|
524
542
|
### Load Path Setup
|
525
543
|
To understand how rubygems loads code you can reference the following:
|
526
544
|
|
527
|
-
http://guides.rubygems.org/patterns/#
|
545
|
+
http://guides.rubygems.org/patterns/#loading-code
|
528
546
|
|
529
547
|
And an example of how active_record dynamically load adapters:
|
530
548
|
|
@@ -552,7 +570,7 @@ When rubygems pushes the `lib` folder onto the load path a `require` will now fi
|
|
552
570
|
|
553
571
|
### Conflicts With Other Gems
|
554
572
|
|
555
|
-
|
573
|
+
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
574
|
|
557
575
|
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
576
|
|
@@ -579,7 +597,7 @@ See https://github.com/zdennis/activerecord-import/issues/233 for further discus
|
|
579
597
|
|
580
598
|
### More Information
|
581
599
|
|
582
|
-
For more information on
|
600
|
+
For more information on Activerecord-Import please see its wiki: https://github.com/zdennis/activerecord-import/wiki
|
583
601
|
|
584
602
|
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
603
|
|
@@ -605,24 +623,14 @@ AR_VERSION=4.2 bundle exec rake test:postgresql test:sqlite3 test:mysql2
|
|
605
623
|
|
606
624
|
Once you have pushed up your changes, you can find your CI results [here](https://travis-ci.org/zdennis/activerecord-import/).
|
607
625
|
|
626
|
+
## Issue Triage [![Open Source Helpers](https://www.codetriage.com/zdennis/activerecord-import/badges/users.svg)](https://www.codetriage.com/zdennis/activerecord-import)
|
627
|
+
|
628
|
+
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).
|
629
|
+
|
608
630
|
# License
|
609
631
|
|
610
|
-
This is licensed under the
|
632
|
+
This is licensed under the MIT license.
|
611
633
|
|
612
634
|
# Author
|
613
635
|
|
614
636
|
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
@@ -6,8 +6,8 @@ Gem::Specification.new do |gem|
|
|
6
6
|
gem.email = ["zach.dennis@gmail.com"]
|
7
7
|
gem.summary = "Bulk insert extension for ActiveRecord"
|
8
8
|
gem.description = "A library for bulk inserting data using ActiveRecord."
|
9
|
-
gem.homepage = "
|
10
|
-
gem.license = "
|
9
|
+
gem.homepage = "https://github.com/zdennis/activerecord-import"
|
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,8 +16,8 @@ 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.4.0"
|
20
20
|
|
21
|
-
gem.add_runtime_dependency "activerecord", ">=
|
21
|
+
gem.add_runtime_dependency "activerecord", ">= 4.2"
|
22
22
|
gem.add_development_dependency "rake"
|
23
23
|
end
|
File without changes
|
data/gemfiles/6.0.gemfile
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
gem 'activerecord', '~> 6.0.0
|
1
|
+
gem 'activerecord', '~> 6.0.0'
|
2
|
+
gem 'composite_primary_keys', '~> 12.0'
|
data/gemfiles/6.1.gemfile
CHANGED
@@ -1 +1,2 @@
|
|
1
|
-
gem 'activerecord', '~> 6.1.0
|
1
|
+
gem 'activerecord', '~> 6.1.0'
|
2
|
+
gem 'composite_primary_keys', '~> 13.0'
|
@@ -0,0 +1 @@
|
|
1
|
+
gem 'activerecord', '~> 7.0.0.alpha2'
|