activerecord-bulkwrite 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +71 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 369fdcf6f6d5955174b39d47708116a848fccbfd
|
4
|
+
data.tar.gz: 85dc264492bd79912e3f5261da2cf21656fd0350
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7a44baf90e1ee46a3bb9a7ec8bb7164b7ad0ceb660e32a464ebf95afa97fbc71420d363f2ae9591e809a6bfae7b2a8c26d3cd52085c757e3d55f1eb01713274
|
7
|
+
data.tar.gz: fd02168bc0bc8ec34726f31d76c763b7522e830866f7d9965959469179d0cb9ea8dce02d5f6c45e4a205e62948b33a6b39d2884162be40579dc4e82aad6060df
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2016 Robert(louirobert@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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
中文文档请看这里:http://ask.githuber.cn/t/activerecord-bulk-write/1723
|
2
|
+
---
|
3
|
+
# activerecord-bulkwrite
|
4
|
+
Bulk write/upsert for ActiveRecord!
|
5
|
+
|
6
|
+
# Requirement
|
7
|
+
PostgreSQL 9.5 and higher
|
8
|
+
|
9
|
+
# Installation
|
10
|
+
|
11
|
+
```
|
12
|
+
gem install activerecord-bulkwrite
|
13
|
+
```
|
14
|
+
|
15
|
+
# Usage
|
16
|
+
|
17
|
+
Suppose a table definition:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
create table users (
|
21
|
+
id integer primary key,
|
22
|
+
name text,
|
23
|
+
hireable bool,
|
24
|
+
created_at date_time,
|
25
|
+
foo text,
|
26
|
+
bar text,
|
27
|
+
)
|
28
|
+
```
|
29
|
+
|
30
|
+
## Bulk Insert
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require "activerecord/bulkwrite"
|
34
|
+
|
35
|
+
fields = %w(id name hireable created_at)
|
36
|
+
rows = [
|
37
|
+
[1, "Bob's", true, Time.now.utc],
|
38
|
+
[2, nil, "false", Time.now.utc.iso8601],
|
39
|
+
# ...
|
40
|
+
]
|
41
|
+
|
42
|
+
# The result is the effected(inserted) rows.
|
43
|
+
result = User.bulk_write(fields, rows)
|
44
|
+
```
|
45
|
+
|
46
|
+
The values of rows are sent to database as-is, and their to_s method is called when necessary. So here you need to *pay attention to datetime values: it must be in UTC time, not in local time*, since there's no time zone conversion.
|
47
|
+
|
48
|
+
## Bulk Upsert
|
49
|
+
|
50
|
+
You do upsert like:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
result = User.bulk_write(fields, rows, :conflict => [:id])
|
54
|
+
```
|
55
|
+
|
56
|
+
The statement above reinserted `rows` and thus failed on unique violation. But this time we specified an `:conflict` option meaning when the given fields conflict then do update with the rest fields. We can also explicitly specify the fields to update:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
result = User.bulk_write(fields, rows, :conflict => [:id], :update => %w(name created_at))
|
60
|
+
```
|
61
|
+
|
62
|
+
We can even specify conditions under which to do update:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
result = User.bulk_write(fields, rows, :conflict => [:id], :where => "users.hireable = TRUE"))
|
66
|
+
```
|
67
|
+
|
68
|
+
The upsert function depends on PostgreSQL 9.5's upsert. See here for more: https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT
|
69
|
+
|
70
|
+
# Compatible Rails Versions and Test
|
71
|
+
It's tested against ActiveRecord 4.2, but should also work on 4.x as well as 5.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-bulkwrite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Zhang
|
@@ -14,14 +14,14 @@ dependencies:
|
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '4.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '4.0'
|
27
27
|
description:
|
@@ -30,6 +30,8 @@ executables: []
|
|
30
30
|
extensions: []
|
31
31
|
extra_rdoc_files: []
|
32
32
|
files:
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
33
35
|
- lib/activerecord/bulkwrite.rb
|
34
36
|
homepage: https://github.com/coin8086/activerecord-bulkwrite
|
35
37
|
licenses:
|
@@ -51,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
53
|
version: '0'
|
52
54
|
requirements: []
|
53
55
|
rubyforge_project:
|
54
|
-
rubygems_version: 2.
|
56
|
+
rubygems_version: 2.4.8
|
55
57
|
signing_key:
|
56
58
|
specification_version: 4
|
57
59
|
summary: Bulk write/upsert for ActiveRecord
|