activerecord-import 0.27.0 → 0.28.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.markdown +498 -3
- data/lib/activerecord-import/import.rb +29 -8
- data/lib/activerecord-import/version.rb +1 -1
- data/test/import_test.rb +33 -2
- data/test/models/user_token.rb +1 -0
- data/test/support/shared_examples/on_duplicate_key_update.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b3c71766c00d489ea17001a179599e9bfe643cc
|
4
|
+
data.tar.gz: cce704816a91718a1e81fee5c1e799447655bf3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9880191ac0df7086dc8b5be319262655cd43d365ae9df6cde654bc8374d57d75a2aafe3018d5f63e1af1fbd64f9041bb15fb5a967d5ece0e0d5702dd739c0123
|
7
|
+
data.tar.gz: 989fde16bae64ff5789e481053e6a596b6f172b0d9085846d7bb91c108fb1ebb01c8d8ea20c70794476b3b7ddbb4d15efe985f54780642fddcc98991061406cb
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
## Changes in 0.28.0
|
2
|
+
|
3
|
+
### New Features
|
4
|
+
|
5
|
+
* Allow updated timestamps to be manually set.Thanks to @Rob117, @jkowens via \#570.
|
6
|
+
|
7
|
+
### Fixes
|
8
|
+
|
9
|
+
* Fix validating presence of belongs_to associations. Existence
|
10
|
+
of the parent record is not validated, but the foreign key field
|
11
|
+
cannot be empty. Thanks to @Rob117, @jkowens via \#575.
|
12
|
+
|
1
13
|
## Changes in 0.27.0
|
2
14
|
|
3
15
|
### New Features
|
data/README.markdown
CHANGED
@@ -21,12 +21,401 @@ and then the reviews:
|
|
21
21
|
That would be about 4M SQL insert statements vs 3, which results in vastly improved performance. In our case, it converted
|
22
22
|
an 18 hour batch process to <2 hrs.
|
23
23
|
|
24
|
+
The gem provides the following high-level features:
|
25
|
+
|
26
|
+
* activerecord-import can work with raw columns and arrays of values (fastest)
|
27
|
+
* activerecord-import works with model objects (faster)
|
28
|
+
* activerecord-import can perform validations (fast)
|
29
|
+
* activerecord-import can perform on duplicate key updates (requires MySQL or Postgres 9.5+)
|
30
|
+
|
24
31
|
## Table of Contents
|
25
32
|
|
33
|
+
* [Examples](#examples)
|
34
|
+
* [Introduction](#introduction)
|
35
|
+
* [Columns and Arrays](#columns-and-arrays)
|
36
|
+
* [Hashes](#hashes)
|
37
|
+
* [ActiveRecord Models](#activerecord-models)
|
38
|
+
* [Batching](#batching)
|
39
|
+
* [Recursive](#recursive)
|
40
|
+
* [Options](#options)
|
41
|
+
* [Duplicate Key Ignore](#duplicate-key-ignore)
|
42
|
+
* [Duplicate Key Update](#duplicate-key-update)
|
43
|
+
* [Return Info](#return-info)
|
44
|
+
* [Counter Cache](#counter-cache)
|
45
|
+
* [ActiveRecord Timestamps](#activerecord-timestamps)
|
26
46
|
* [Callbacks](#callbacks)
|
47
|
+
* [Supported Adapters](#supported-adapters)
|
27
48
|
* [Additional Adapters](#additional-adapters)
|
49
|
+
* [Requiring](#requiring)
|
50
|
+
* [Autoloading via Bundler](#autoloading-via-bundler)
|
51
|
+
* [Manually Loading](#manually-loading)
|
28
52
|
* [Load Path Setup](#load-path-setup)
|
53
|
+
* [Conflicts With Other Gems](#conflicts-with-other-gems)
|
29
54
|
* [More Information](#more-information)
|
55
|
+
* [Contributing](#contributing)
|
56
|
+
* [Running Tests](#running-tests)
|
57
|
+
|
58
|
+
### Examples
|
59
|
+
|
60
|
+
#### Introduction
|
61
|
+
|
62
|
+
Without `activerecord-import`, you'd write something like this:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
10.times do |i|
|
66
|
+
Book.create! :name => "book #{i}"
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
This would end up making 10 SQL calls. YUCK! With `activerecord-import`, you can instead do this:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
books = []
|
74
|
+
10.times do |i|
|
75
|
+
books << Book.new(:name => "book #{i}")
|
76
|
+
end
|
77
|
+
Book.import books # or use import!
|
78
|
+
```
|
79
|
+
|
80
|
+
and only have 1 SQL call. Much better!
|
81
|
+
|
82
|
+
#### Columns and Arrays
|
83
|
+
|
84
|
+
The `import` method can take an array of column names (string or symbols) and an array of arrays. Each child array represents an individual record and its list of values in the same order as the columns. This is the fastest import mechanism and also the most primitive.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
columns = [ :title, :author ]
|
88
|
+
values = [ ['Book1', 'FooManChu'], ['Book2', 'Bob Jones'] ]
|
89
|
+
|
90
|
+
# Importing without model validations
|
91
|
+
Book.import columns, values, :validate => false
|
92
|
+
|
93
|
+
# Import with model validations
|
94
|
+
Book.import columns, values, :validate => true
|
95
|
+
|
96
|
+
# when not specified :validate defaults to true
|
97
|
+
Book.import columns, values
|
98
|
+
```
|
99
|
+
|
100
|
+
#### Hashes
|
101
|
+
|
102
|
+
The `import` method can take an array of hashes. The keys map to the column names in the database.
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
values = [{ title: 'Book1', author: 'FooManChu' }, { title: 'Book2', author: 'Bob Jones'}]
|
106
|
+
|
107
|
+
# Importing without model validations
|
108
|
+
Book.import values, validate: false
|
109
|
+
|
110
|
+
# Import with model validations
|
111
|
+
Book.import values, validate: true
|
112
|
+
|
113
|
+
# when not specified :validate defaults to true
|
114
|
+
Book.import values
|
115
|
+
```
|
116
|
+
h2. Import Using Hashes and Explicit Column Names
|
117
|
+
|
118
|
+
The `import` method can take an array of column names and an array of hash objects. The column names are used to determine what fields of data should be imported. The following example will only import books with the `title` field:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
books = [
|
122
|
+
{ title: "Book 1", author: "FooManChu" },
|
123
|
+
{ title: "Book 2", author: "Bob Jones" }
|
124
|
+
]
|
125
|
+
columns = [ :title ]
|
126
|
+
|
127
|
+
# without validations
|
128
|
+
Book.import columns, books, validate: false
|
129
|
+
|
130
|
+
# with validations
|
131
|
+
Book.import columns, books, validate: true
|
132
|
+
|
133
|
+
# when not specified :validate defaults to true
|
134
|
+
Book.import columns, books
|
135
|
+
|
136
|
+
# result in table books
|
137
|
+
# title | author
|
138
|
+
#--------|--------
|
139
|
+
# Book 1 | NULL
|
140
|
+
# Book 2 | NULL
|
141
|
+
|
142
|
+
```
|
143
|
+
|
144
|
+
Using hashes will only work if the columns are consistent in every hash of the array. If this does not hold, an exception will be raised. There are two workarounds: use the array to instantiate an array of ActiveRecord objects and then pass that into `import` or divide the array into multiple ones with consistent columns and import each one separately.
|
145
|
+
|
146
|
+
See https://github.com/zdennis/activerecord-import/issues/507 for discussion.
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
arr = [
|
150
|
+
{ bar: 'abc' },
|
151
|
+
{ baz: 'xyz' },
|
152
|
+
{ bar: '123', baz: '456' }
|
153
|
+
]
|
154
|
+
|
155
|
+
# An exception will be raised
|
156
|
+
Foo.import arr
|
157
|
+
|
158
|
+
# better
|
159
|
+
arr.map! { |args| Foo.new(args) }
|
160
|
+
Foo.import arr
|
161
|
+
|
162
|
+
# better
|
163
|
+
arr.group_by(&:keys).each_value do |v|
|
164
|
+
Foo.import v
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
#### ActiveRecord Models
|
169
|
+
|
170
|
+
The `import` method can take an array of models. The attributes will be pulled off from each model by looking at the columns available on the model.
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
books = [
|
174
|
+
Book.new(:title => "Book 1", :author => "FooManChu"),
|
175
|
+
Book.new(:title => "Book 2", :author => "Bob Jones")
|
176
|
+
]
|
177
|
+
|
178
|
+
# without validations
|
179
|
+
Book.import books, :validate => false
|
180
|
+
|
181
|
+
# with validations
|
182
|
+
Book.import books, :validate => true
|
183
|
+
|
184
|
+
# when not specified :validate defaults to true
|
185
|
+
Book.import books
|
186
|
+
```
|
187
|
+
|
188
|
+
The `import` method can take an array of column names and an array of models. The column names are used to determine what fields of data should be imported. The following example will only import books with the `title` field:
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
books = [
|
192
|
+
Book.new(:title => "Book 1", :author => "FooManChu"),
|
193
|
+
Book.new(:title => "Book 2", :author => "Bob Jones")
|
194
|
+
]
|
195
|
+
columns = [ :title ]
|
196
|
+
|
197
|
+
# without validations
|
198
|
+
Book.import columns, books, :validate => false
|
199
|
+
|
200
|
+
# with validations
|
201
|
+
Book.import columns, books, :validate => true
|
202
|
+
|
203
|
+
# when not specified :validate defaults to true
|
204
|
+
Book.import columns, books
|
205
|
+
|
206
|
+
# result in table books
|
207
|
+
# title | author
|
208
|
+
#--------|--------
|
209
|
+
# Book 1 | NULL
|
210
|
+
# Book 2 | NULL
|
211
|
+
|
212
|
+
```
|
213
|
+
|
214
|
+
#### Batching
|
215
|
+
|
216
|
+
The `import` method can take a `batch_size` option to control the number of rows to insert per INSERT statement. The default is the total number of records being inserted so there is a single INSERT statement.
|
217
|
+
|
218
|
+
```ruby
|
219
|
+
books = [
|
220
|
+
Book.new(:title => "Book 1", :author => "FooManChu"),
|
221
|
+
Book.new(:title => "Book 2", :author => "Bob Jones"),
|
222
|
+
Book.new(:title => "Book 1", :author => "John Doe"),
|
223
|
+
Book.new(:title => "Book 2", :author => "Richard Wright")
|
224
|
+
]
|
225
|
+
columns = [ :title ]
|
226
|
+
|
227
|
+
# 2 INSERT statements for 4 records
|
228
|
+
Book.import columns, books, :batch_size => 2
|
229
|
+
```
|
230
|
+
|
231
|
+
#### Recursive
|
232
|
+
|
233
|
+
NOTE: This only works with PostgreSQL.
|
234
|
+
|
235
|
+
Assume that Books <code>has_many</code> Reviews.
|
236
|
+
|
237
|
+
```ruby
|
238
|
+
books = []
|
239
|
+
10.times do |i|
|
240
|
+
book = Book.new(:name => "book #{i}")
|
241
|
+
book.reviews.build(:title => "Excellent")
|
242
|
+
books << book
|
243
|
+
end
|
244
|
+
Book.import books, recursive: true
|
245
|
+
```
|
246
|
+
|
247
|
+
### Options
|
248
|
+
|
249
|
+
Key | Options | Default | Description
|
250
|
+
----------------------- | --------------------- | ------------------ | -----------
|
251
|
+
:validate | `true`/`false` | `true` | Whether or not to run `ActiveRecord` validations (uniqueness skipped).
|
252
|
+
:validate_uniqueness | `true`/`false` | `false` | Whether or not to run uniqueness validations, has potential pitfalls, use with caution (requires `>= v0.27.0`).
|
253
|
+
: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
|
+
:ignore | `true`/`false` | `false` | Alias for :on_duplicate_key_ignore.
|
255
|
+
: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.
|
256
|
+
:synchronize | `Array` | N/A | An array of ActiveRecord instances. This synchronizes existing instances in memory with updates from the import.
|
257
|
+
:timestamps | `true`/`false` | `true` | Enables/disables timestamps on imported records.
|
258
|
+
:recursive | `true`/`false` | `false` | Imports has_many/has_one associations (PostgreSQL only).
|
259
|
+
:batch_size | `Integer` | total # of records | Max number of records to insert per import
|
260
|
+
:raise_error | `true`/`false` | `false` | Throws an exception if there are invalid records. `import!` is a shortcut for this.
|
261
|
+
|
262
|
+
|
263
|
+
#### Duplicate Key Ignore
|
264
|
+
|
265
|
+
[MySQL](http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html), [SQLite](https://www.sqlite.org/lang_insert.html), and [PostgreSQL](https://www.postgresql.org/docs/current/static/sql-insert.html#SQL-ON-CONFLICT) (9.5+) support `on_duplicate_key_ignore` which allows you to skip records if a primary or unique key constraint is violated.
|
266
|
+
|
267
|
+
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
|
+
|
269
|
+
```ruby
|
270
|
+
book = Book.create! title: "Book1", author: "FooManChu"
|
271
|
+
book.title = "Updated Book Title"
|
272
|
+
book.author = "Bob Barker"
|
273
|
+
|
274
|
+
Book.import [book], on_duplicate_key_ignore: true
|
275
|
+
|
276
|
+
book.reload.title # => "Book1" (stayed the same)
|
277
|
+
book.reload.author # => "FooManChu" (stayed the same)
|
278
|
+
```
|
279
|
+
|
280
|
+
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).
|
281
|
+
|
282
|
+
#### Duplicate Key Update
|
283
|
+
|
284
|
+
MySQL, PostgreSQL (9.5+), and SQLite (3.24.0+) support `on duplicate key update` (also known as "upsert") which allows you to specify fields whose values should be updated if a primary or unique key constraint is violated.
|
285
|
+
|
286
|
+
One big difference between MySQL and PostgreSQL support is that MySQL will handle any conflict that happens, but PostgreSQL requires that you specify which columns the conflict would occur over. SQLite models its upsert support after PostgreSQL.
|
287
|
+
|
288
|
+
This will use MySQL's `ON DUPLICATE KEY UPDATE` or Postgres/SQLite `ON CONFLICT DO UPDATE` to do upsert.
|
289
|
+
|
290
|
+
Basic Update
|
291
|
+
|
292
|
+
```ruby
|
293
|
+
book = Book.create! title: "Book1", author: "FooManChu"
|
294
|
+
book.title = "Updated Book Title"
|
295
|
+
book.author = "Bob Barker"
|
296
|
+
|
297
|
+
# MySQL version
|
298
|
+
Book.import [book], on_duplicate_key_update: [:title]
|
299
|
+
|
300
|
+
# PostgreSQL version
|
301
|
+
Book.import [book], on_duplicate_key_update: {conflict_target: [:id], columns: [:title]}
|
302
|
+
|
303
|
+
# PostgreSQL shorthand version (conflict target must be primary key)
|
304
|
+
Book.import [book], on_duplicate_key_update: [:title]
|
305
|
+
|
306
|
+
book.reload.title # => "Updated Book Title" (changed)
|
307
|
+
book.reload.author # => "FooManChu" (stayed the same)
|
308
|
+
```
|
309
|
+
|
310
|
+
Using the value from another column
|
311
|
+
|
312
|
+
```ruby
|
313
|
+
book = Book.create! title: "Book1", author: "FooManChu"
|
314
|
+
book.title = "Updated Book Title"
|
315
|
+
|
316
|
+
# MySQL version
|
317
|
+
Book.import [book], on_duplicate_key_update: {author: :title}
|
318
|
+
|
319
|
+
# PostgreSQL version (no shorthand version)
|
320
|
+
Book.import [book], on_duplicate_key_update: {
|
321
|
+
conflict_target: [:id], columns: {author: :title}
|
322
|
+
}
|
323
|
+
|
324
|
+
book.reload.title # => "Book1" (stayed the same)
|
325
|
+
book.reload.author # => "Updated Book Title" (changed)
|
326
|
+
```
|
327
|
+
|
328
|
+
Using Custom SQL
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
book = Book.create! title: "Book1", author: "FooManChu"
|
332
|
+
book.author = "Bob Barker"
|
333
|
+
|
334
|
+
# MySQL version
|
335
|
+
Book.import [book], on_duplicate_key_update: "author = values(author)"
|
336
|
+
|
337
|
+
# PostgreSQL version
|
338
|
+
Book.import [book], on_duplicate_key_update: {
|
339
|
+
conflict_target: [:id], columns: "author = excluded.author"
|
340
|
+
}
|
341
|
+
|
342
|
+
# PostgreSQL shorthand version (conflict target must be primary key)
|
343
|
+
Book.import [book], on_duplicate_key_update: "author = excluded.author"
|
344
|
+
|
345
|
+
book.reload.title # => "Book1" (stayed the same)
|
346
|
+
book.reload.author # => "Bob Barker" (changed)
|
347
|
+
```
|
348
|
+
|
349
|
+
PostgreSQL Using constraints
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
book = Book.create! title: "Book1", author: "FooManChu", edition: 3, published_at: nil
|
353
|
+
book.published_at = Time.now
|
354
|
+
|
355
|
+
# in migration
|
356
|
+
execute <<-SQL
|
357
|
+
ALTER TABLE books
|
358
|
+
ADD CONSTRAINT for_upsert UNIQUE (title, author, edition);
|
359
|
+
SQL
|
360
|
+
|
361
|
+
# PostgreSQL version
|
362
|
+
Book.import [book], on_duplicate_key_update: {constraint_name: :for_upsert, columns: [:published_at]}
|
363
|
+
|
364
|
+
|
365
|
+
book.reload.title # => "Book1" (stayed the same)
|
366
|
+
book.reload.author # => "FooManChu" (stayed the same)
|
367
|
+
book.reload.edition # => 3 (stayed the same)
|
368
|
+
book.reload.published_at # => 2017-10-09 (changed)
|
369
|
+
```
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
Book.import books, validate_uniqueness: true
|
373
|
+
```
|
374
|
+
|
375
|
+
### Return Info
|
376
|
+
|
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`.
|
378
|
+
|
379
|
+
```ruby
|
380
|
+
articles = [
|
381
|
+
Article.new(author_id: 1, title: 'First Article', content: 'This is the first article'),
|
382
|
+
Article.new(author_id: 2, title: 'Second Article', content: ''),
|
383
|
+
Article.new(author_id: 3, content: '')
|
384
|
+
]
|
385
|
+
|
386
|
+
demo = Article.import(articles), returning: :title # => #<struct ActiveRecord::Import::Result
|
387
|
+
|
388
|
+
demo.failed_instances
|
389
|
+
=> [#<Article id: 3, author_id: 3, title: nil, content: "", created_at: nil, updated_at: nil>]
|
390
|
+
|
391
|
+
demo.num_inserts
|
392
|
+
=> 1,
|
393
|
+
|
394
|
+
demo.ids
|
395
|
+
=> ["1", "2"] # for Postgres
|
396
|
+
=> [] # for other DBs
|
397
|
+
|
398
|
+
demo.results
|
399
|
+
=> ["First Article", "Second Article"] # for Postgres
|
400
|
+
=> [] for other DBs
|
401
|
+
```
|
402
|
+
|
403
|
+
### Counter Cache
|
404
|
+
|
405
|
+
When running `import`, `activerecord-import` does not automatically update counter cache columns. To update these columns, you will need to do one of the following:
|
406
|
+
|
407
|
+
* Provide values to the column as an argument on your object that is passed in.
|
408
|
+
* Manually update the column after the record has been imported.
|
409
|
+
|
410
|
+
### ActiveRecord Timestamps
|
411
|
+
|
412
|
+
If you're familiar with ActiveRecord you're probably familiar with its timestamp columns: created_at, created_on, updated_at, updated_on, etc. When importing data the timestamp fields will continue to work as expected and each timestamp column will be set.
|
413
|
+
|
414
|
+
Should you wish to specify those columns, you may use the option `timestamps: false`.
|
415
|
+
|
416
|
+
However, it is also possible to set just `:created_at` in specific records. In this case despite using `timestamps: true`, `:created_at` will be updated only in records where that field is `nil`. Same rule applies for record associations when enabling the option `recursive: true`.
|
417
|
+
|
418
|
+
If you are using custom time zones, these will be respected when performing imports as well as long as `ActiveRecord::Base.default_timezone` is set, which for practically all Rails apps it is
|
30
419
|
|
31
420
|
### Callbacks
|
32
421
|
|
@@ -34,7 +423,7 @@ ActiveRecord callbacks related to [creating](http://guides.rubyonrails.org/activ
|
|
34
423
|
|
35
424
|
If you do have a collection of in-memory ActiveRecord objects you can do something like this:
|
36
425
|
|
37
|
-
```
|
426
|
+
```ruby
|
38
427
|
books.each do |book|
|
39
428
|
book.run_callbacks(:save) { false }
|
40
429
|
book.run_callbacks(:create) { false }
|
@@ -46,7 +435,7 @@ This will run before_create and before_save callbacks on each item. The `false`
|
|
46
435
|
|
47
436
|
If that is an issue, another possible approach is to loop through your models first to do validations and then only run callbacks on and import the valid models.
|
48
437
|
|
49
|
-
```
|
438
|
+
```ruby
|
50
439
|
valid_books = []
|
51
440
|
invalid_books = []
|
52
441
|
|
@@ -66,6 +455,24 @@ end
|
|
66
455
|
Book.import valid_books, validate: false
|
67
456
|
```
|
68
457
|
|
458
|
+
### Supported Adapters
|
459
|
+
|
460
|
+
The following database adapters are currently supported:
|
461
|
+
|
462
|
+
* MySQL - supports core import functionality plus on duplicate key update support (included in activerecord-import 0.1.0 and higher)
|
463
|
+
* MySQL2 - supports core import functionality plus on duplicate key update support (included in activerecord-import 0.2.0 and higher)
|
464
|
+
* PostgreSQL - supports core import functionality (included in activerecord-import 0.1.0 and higher)
|
465
|
+
* SQLite3 - supports core import functionality (included in activerecord-import 0.1.0 and higher)
|
466
|
+
* Oracle - supports core import functionality through DML trigger (available as an external gem: [activerecord-import-oracle_enhanced](https://github.com/keeguon/activerecord-import-oracle_enhanced)
|
467
|
+
* SQL Server - supports core import functionality (available as an external gem: [activerecord-import-sqlserver](https://github.com/keeguon/activerecord-import-sqlserver)
|
468
|
+
|
469
|
+
If your adapter isn't listed here, please consider creating an external gem as described in the README to provide support. If you do, feel free to update this wiki to include a link to the new adapter's repository!
|
470
|
+
|
471
|
+
To test which features are supported by your adapter, use the following methods on a model class:
|
472
|
+
* `supports_import?(*args)`
|
473
|
+
* `supports_on_duplicate_key_update?`
|
474
|
+
* `supports_setting_primary_key_of_imported_objects?`
|
475
|
+
|
69
476
|
### Additional Adapters
|
70
477
|
|
71
478
|
Additional adapters can be provided by gems external to activerecord-import by providing an adapter that matches the naming convention setup by activerecord-import (and subsequently activerecord) for dynamically loading adapters. This involves also providing a folder on the load path that follows the activerecord-import naming convention to allow activerecord-import to dynamically load the file.
|
@@ -73,17 +480,54 @@ Additional adapters can be provided by gems external to activerecord-import by p
|
|
73
480
|
When `ActiveRecord::Import.require_adapter("fake_name")` is called the require will be:
|
74
481
|
|
75
482
|
```ruby
|
76
|
-
|
483
|
+
require 'activerecord-import/active_record/adapters/fake_name_adapter'
|
77
484
|
```
|
78
485
|
|
79
486
|
This allows an external gem to dynamically add an adapter without the need to add any file/code to the core activerecord-import gem.
|
80
487
|
|
488
|
+
### Requiring
|
489
|
+
|
490
|
+
Note: These instructions will only work if you are using version 0.2.0 or higher.
|
491
|
+
|
492
|
+
#### Autoloading via Bundler
|
493
|
+
|
494
|
+
If you are using Rails or otherwise autoload your dependencies via Bundler, all you need to do add the gem to your `Gemfile` like so:
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
gem 'activerecord-import'
|
498
|
+
```
|
499
|
+
|
500
|
+
#### Manually Loading
|
501
|
+
|
502
|
+
You may want to manually load activerecord-import for one reason or another. First, add the `require: false` argument like so:
|
503
|
+
|
504
|
+
```ruby
|
505
|
+
gem 'activerecord-import', require: false
|
506
|
+
```
|
507
|
+
|
508
|
+
This will allow you to load up activerecord-import in the file or files where you are using it and only load the parts you need.
|
509
|
+
If you are doing this within Rails and ActiveRecord has established a database connection (such as within a controller), you will need to do extra initialization work:
|
510
|
+
|
511
|
+
```ruby
|
512
|
+
require 'activerecord-import/base'
|
513
|
+
# load the appropriate database adapter (postgresql, mysql2, sqlite3, etc)
|
514
|
+
require 'activerecord-import/active_record/adapters/postgresql_adapter'
|
515
|
+
```
|
516
|
+
|
517
|
+
If your gem dependencies aren’t autoloaded, and your script will be establishing a database connection, then simply require activerecord-import after ActiveRecord has been loaded, i.e.:
|
518
|
+
|
519
|
+
```ruby
|
520
|
+
require 'active_record'
|
521
|
+
require 'activerecord-import'
|
522
|
+
```
|
523
|
+
|
81
524
|
### Load Path Setup
|
82
525
|
To understand how rubygems loads code you can reference the following:
|
83
526
|
|
84
527
|
http://guides.rubygems.org/patterns/#loading_code
|
85
528
|
|
86
529
|
And an example of how active_record dynamically load adapters:
|
530
|
+
|
87
531
|
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/connection_specification.rb
|
88
532
|
|
89
533
|
In summary, when a gem is loaded rubygems adds the `lib` folder of the gem to the global load path `$LOAD_PATH` so that all `require` lookups will not propagate through all of the folders on the load path. When a `require` is issued each folder on the `$LOAD_PATH` is checked for the file and/or folder referenced. This allows a gem (like activerecord-import) to define push the activerecord-import folder (or namespace) on the `$LOAD_PATH` and any adapters provided by activerecord-import will be found by rubygems when the require is issued.
|
@@ -105,12 +549,62 @@ activerecord-import-fake_name/
|
|
105
549
|
|
106
550
|
When rubygems pushes the `lib` folder onto the load path a `require` will now find `activerecord-import/active_record/adapters/fake_name_adapter` as it runs through the lookup process for a ruby file under that path in `$LOAD_PATH`
|
107
551
|
|
552
|
+
|
553
|
+
### Conflicts With Other Gems
|
554
|
+
|
555
|
+
`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
|
+
|
557
|
+
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
|
+
|
559
|
+
```ruby
|
560
|
+
class Post < ActiveRecord::Base
|
561
|
+
self.sequence_name = "posts_seq"
|
562
|
+
end
|
563
|
+
```
|
564
|
+
|
565
|
+
Another way to work around the issue is to call `.reset_sequence_name` on the model. For example:
|
566
|
+
|
567
|
+
```ruby
|
568
|
+
schemas.all.each do |schema|
|
569
|
+
Apartment::Tenant.switch! schema.name
|
570
|
+
ActiveRecord::Base.transaction do
|
571
|
+
Post.reset_sequence_name
|
572
|
+
|
573
|
+
Post.import posts
|
574
|
+
end
|
575
|
+
end
|
576
|
+
```
|
577
|
+
|
578
|
+
See https://github.com/zdennis/activerecord-import/issues/233 for further discussion.
|
579
|
+
|
108
580
|
### More Information
|
109
581
|
|
110
582
|
For more information on activerecord-import please see its wiki: https://github.com/zdennis/activerecord-import/wiki
|
111
583
|
|
112
584
|
To document new information, please add to the README instead of the wiki. See https://github.com/zdennis/activerecord-import/issues/397 for discussion.
|
113
585
|
|
586
|
+
### Contributing
|
587
|
+
|
588
|
+
#### Running Tests
|
589
|
+
|
590
|
+
The first thing you need to do is set up your database(s):
|
591
|
+
|
592
|
+
* copy `test/database.yml.sample` to `test/database.yml`
|
593
|
+
* modify `test/database.yml` for your database settings
|
594
|
+
* create databases as needed
|
595
|
+
|
596
|
+
After that, you can run the tests. They run against multiple tests and ActiveRecord versions.
|
597
|
+
|
598
|
+
This is one example of how to run the tests:
|
599
|
+
|
600
|
+
```ruby
|
601
|
+
rm Gemfile.lock
|
602
|
+
AR_VERSION=4.2 bundle install
|
603
|
+
AR_VERSION=4.2 bundle exec rake test:postgresql test:sqlite3 test:mysql2
|
604
|
+
```
|
605
|
+
|
606
|
+
Once you have pushed up your changes, you can find your CI results [here](https://travis-ci.org/zdennis/activerecord-import/).
|
607
|
+
|
114
608
|
# License
|
115
609
|
|
116
610
|
This is licensed under the ruby license.
|
@@ -131,3 +625,4 @@ Zach Dennis (zach.dennis@gmail.com)
|
|
131
625
|
* Thibaud Guillaume-Gentil
|
132
626
|
* Mark Van Holstyn
|
133
627
|
* Victor Costan
|
628
|
+
* Dillon Welch
|
@@ -38,12 +38,21 @@ module ActiveRecord::Import #:nodoc:
|
|
38
38
|
model.errors.clear
|
39
39
|
|
40
40
|
validate_callbacks = model._validate_callbacks.dup
|
41
|
-
associations = model.class.reflect_on_all_associations(:belongs_to).map(&:name)
|
42
41
|
|
43
42
|
model._validate_callbacks.each do |callback|
|
44
43
|
filter = callback.raw_filter
|
45
|
-
|
46
|
-
|
44
|
+
|
45
|
+
if filter.class.name =~ /Validations::PresenceValidator/
|
46
|
+
associations = model.class.reflect_on_all_associations(:belongs_to)
|
47
|
+
attrs = filter.instance_variable_get(:@attributes)
|
48
|
+
associations.each do |assoc|
|
49
|
+
if (index = attrs.index(assoc.name))
|
50
|
+
key = assoc.foreign_key.to_sym
|
51
|
+
attrs[index] = key unless attrs.include?(key)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
filter.instance_variable_set(:@attributes, attrs)
|
55
|
+
elsif !@options[:validate_uniqueness] && filter.is_a?(ActiveRecord::Validations::UniquenessValidator)
|
47
56
|
validate_callbacks.delete(callback)
|
48
57
|
end
|
49
58
|
end
|
@@ -553,6 +562,14 @@ class ActiveRecord::Base
|
|
553
562
|
serialized_attributes
|
554
563
|
end
|
555
564
|
|
565
|
+
update_attrs = if record_timestamps && options[:timestamps]
|
566
|
+
if respond_to?(:timestamp_attributes_for_update, true)
|
567
|
+
send(:timestamp_attributes_for_update).map(&:to_sym)
|
568
|
+
else
|
569
|
+
new.send(:timestamp_attributes_for_update_in_model)
|
570
|
+
end
|
571
|
+
end
|
572
|
+
|
556
573
|
array_of_attributes = []
|
557
574
|
|
558
575
|
models.each do |model|
|
@@ -566,9 +583,13 @@ class ActiveRecord::Base
|
|
566
583
|
end
|
567
584
|
|
568
585
|
array_of_attributes << column_names.map do |name|
|
569
|
-
if
|
570
|
-
|
571
|
-
|
586
|
+
if model.persisted? &&
|
587
|
+
update_attrs && update_attrs.include?(name.to_sym) &&
|
588
|
+
!model.send("#{name}_changed?")
|
589
|
+
nil
|
590
|
+
elsif stored_attrs.key?(name.to_sym) ||
|
591
|
+
serialized_attrs.key?(name.to_s) ||
|
592
|
+
default_values[name.to_s]
|
572
593
|
model.read_attribute(name.to_s)
|
573
594
|
else
|
574
595
|
model.read_attribute_before_type_cast(name.to_s)
|
@@ -655,7 +676,7 @@ class ActiveRecord::Base
|
|
655
676
|
timestamps = {}
|
656
677
|
|
657
678
|
# record timestamps unless disabled in ActiveRecord::Base
|
658
|
-
if record_timestamps && options
|
679
|
+
if record_timestamps && options[:timestamps]
|
659
680
|
timestamps = add_special_rails_stamps column_names, array_of_attributes, options
|
660
681
|
end
|
661
682
|
|
@@ -961,7 +982,7 @@ class ActiveRecord::Base
|
|
961
982
|
index = column_names.index(column) || column_names.index(column.to_sym)
|
962
983
|
if index
|
963
984
|
# replace every instance of the array of attributes with our value
|
964
|
-
array_of_attributes.each { |arr| arr[index] = timestamp if arr[index].nil?
|
985
|
+
array_of_attributes.each { |arr| arr[index] = timestamp if arr[index].nil? }
|
965
986
|
else
|
966
987
|
column_names << column
|
967
988
|
array_of_attributes.each { |arr| arr << timestamp }
|
data/test/import_test.rb
CHANGED
@@ -303,6 +303,20 @@ describe "#import" do
|
|
303
303
|
end
|
304
304
|
end
|
305
305
|
end
|
306
|
+
|
307
|
+
context "when validatoring presence of belongs_to association" do
|
308
|
+
it "should not import records without foreign key" do
|
309
|
+
assert_no_difference "UserToken.count" do
|
310
|
+
UserToken.import [:token], [['12345abcdef67890']]
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
it "should import records with foreign key" do
|
315
|
+
assert_difference "UserToken.count", +1 do
|
316
|
+
UserToken.import [:user_name, :token], [%w("Bob", "12345abcdef67890")]
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
306
320
|
end
|
307
321
|
|
308
322
|
context "without :validation option" do
|
@@ -499,11 +513,11 @@ describe "#import" do
|
|
499
513
|
|
500
514
|
context "when the timestamps columns are present" do
|
501
515
|
setup do
|
502
|
-
@existing_book = Book.create(title: "Fell", author_name: "Curry", publisher: "Bayer", created_at: 2.years.ago.utc, created_on: 2.years.ago.utc)
|
516
|
+
@existing_book = Book.create(title: "Fell", author_name: "Curry", publisher: "Bayer", created_at: 2.years.ago.utc, created_on: 2.years.ago.utc, updated_at: 2.years.ago.utc, updated_on: 2.years.ago.utc)
|
503
517
|
ActiveRecord::Base.default_timezone = :utc
|
504
518
|
Timecop.freeze(time) do
|
505
519
|
assert_difference "Book.count", +2 do
|
506
|
-
Book.import %w(title author_name publisher created_at created_on), [["LDAP", "Big Bird", "Del Rey", nil, nil], [@existing_book.title, @existing_book.author_name, @existing_book.publisher, @existing_book.created_at, @existing_book.created_on]]
|
520
|
+
Book.import %w(title author_name publisher created_at created_on updated_at updated_on), [["LDAP", "Big Bird", "Del Rey", nil, nil, nil, nil], [@existing_book.title, @existing_book.author_name, @existing_book.publisher, @existing_book.created_at, @existing_book.created_on, @existing_book.updated_at, @existing_book.updated_on]]
|
507
521
|
end
|
508
522
|
end
|
509
523
|
@new_book, @existing_book = Book.last 2
|
@@ -532,6 +546,23 @@ describe "#import" do
|
|
532
546
|
it "should set the updated_on column for new records" do
|
533
547
|
assert_in_delta time.to_i, @new_book.updated_on.to_i, 1.second
|
534
548
|
end
|
549
|
+
|
550
|
+
it "should not set the updated_at column for existing records" do
|
551
|
+
assert_equal 2.years.ago.utc.strftime("%Y:%d"), @existing_book.updated_at.strftime("%Y:%d")
|
552
|
+
end
|
553
|
+
|
554
|
+
it "should not set the updated_on column for existing records" do
|
555
|
+
assert_equal 2.years.ago.utc.strftime("%Y:%d"), @existing_book.updated_on.strftime("%Y:%d")
|
556
|
+
end
|
557
|
+
|
558
|
+
it "should not set the updated_at column on models if changed" do
|
559
|
+
timestamp = Time.now.utc
|
560
|
+
books = [
|
561
|
+
Book.new(author_name: "Foo", title: "Baz", created_at: timestamp, updated_at: timestamp)
|
562
|
+
]
|
563
|
+
Book.import books
|
564
|
+
assert_equal timestamp.strftime("%Y:%d"), Book.last.updated_at.strftime("%Y:%d")
|
565
|
+
end
|
535
566
|
end
|
536
567
|
|
537
568
|
context "when a custom time zone is set" do
|
data/test/models/user_token.rb
CHANGED
@@ -246,6 +246,26 @@ def should_support_basic_on_duplicate_key_update
|
|
246
246
|
end
|
247
247
|
end
|
248
248
|
|
249
|
+
context "with timestamps enabled" do
|
250
|
+
let(:time) { Chronic.parse("5 minutes from now") }
|
251
|
+
|
252
|
+
it 'should not overwrite changed updated_at with current timestamp' do
|
253
|
+
topic = Topic.create(author_name: "Jane Doe", title: "Book")
|
254
|
+
timestamp = Time.now.utc
|
255
|
+
topic.updated_at = timestamp
|
256
|
+
Topic.import [topic], on_duplicate_key_update: :all, validate: false
|
257
|
+
assert_equal timestamp.to_s, Topic.last.updated_at.to_s
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'should update updated_at with current timestamp' do
|
261
|
+
topic = Topic.create(author_name: "Jane Doe", title: "Book")
|
262
|
+
Timecop.freeze(time) do
|
263
|
+
Topic.import [topic], on_duplicate_key_update: [:updated_at], validate: false
|
264
|
+
assert_in_delta time.to_i, topic.reload.updated_at.to_i, 1.second
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
249
269
|
context "with validation checks turned off" do
|
250
270
|
asssertion_group(:should_support_on_duplicate_key_update) do
|
251
271
|
should_not_update_fields_not_mentioned
|
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: 0.
|
4
|
+
version: 0.28.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Dennis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|