active_record_upsert 0.11.1 → 0.11.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.rails-5-2 +1 -1
- data/Gemfile.rails-6-0 +1 -1
- data/Gemfile.rails-6-1 +1 -1
- data/Gemfile.rails-7-0 +1 -1
- data/Gemfile.rails-7-0-ruby-3-1 +5 -0
- data/Gemfile.rails-7-1 +5 -0
- data/README.md +49 -26
- data/active_record_upsert.gemspec +1 -1
- data/lib/active_record_upsert/version.rb +1 -1
- metadata +8 -6
- /data/{Gemfile.rails-master → Gemfile.rails-main} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b647a02ad6d3383a5180bbe96ee50ec232434dabcbafc6ed58f37cfb4ebe9700
|
4
|
+
data.tar.gz: 761cd2ce8bceaf3ce856932aff29662d5b9dd861106bb723a8a3ef2ab79b018a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c61f58fcf034b25c7e8f3e505ddd355decb422d827d2e1bebd6c5915c59b942c550853750e27c3248a9a222b75631ad33fed605de12d2ae33f6c95b30c1e766e
|
7
|
+
data.tar.gz: 82b587aea2863622cc87e7f443954f9087a1c2508a9b93c9a53681effcaec93d07aa933396337cae291261edfa813fda72874210b08b14d9b41b7870d36f7717
|
data/Gemfile.rails-5-2
CHANGED
data/Gemfile.rails-6-0
CHANGED
data/Gemfile.rails-6-1
CHANGED
data/Gemfile.rails-7-0
CHANGED
data/Gemfile.rails-7-1
ADDED
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
[](https://badge.fury.io/rb/active_record_upsert)
|
2
|
-
[](https://codeclimate.com/github/jesjos/active_record_upsert)
|
4
|
-
|
2
|
+
[](https://github.com/jesjos/active_record_upsert/actions/workflows/ci.yml)
|
5
3
|
# ActiveRecordUpsert
|
6
4
|
|
7
5
|
Real upsert for PostgreSQL 9.5+ and Rails 5.2+ / ActiveRecord 5.2+. Uses [ON CONFLICT DO UPDATE](http://www.postgresql.org/docs/9.5/static/sql-insert.html).
|
@@ -19,6 +17,12 @@ Real upsert for PostgreSQL 9.5+ and Rails 5.2+ / ActiveRecord 5.2+. Uses [ON CON
|
|
19
17
|
- Ruby MRI, with the `pg` gem
|
20
18
|
- _JRuby is currently not supported_
|
21
19
|
|
20
|
+
## Alternatives
|
21
|
+
|
22
|
+
This library was written at a time in history when Rails did not support any `#upsert` method.
|
23
|
+
|
24
|
+
Instead of using this library, if you are using a current version of Rails, you may want to [use its `#upsert`](https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert). You may want to investigate how [newer PostgreSQL versions support `MERGE` statement](https://www.postgresql.org/docs/current/sql-merge.html).
|
25
|
+
|
22
26
|
### NB: Releases to avoid
|
23
27
|
|
24
28
|
Due to a broken build matrix, v0.9.2 and v0.9.3 are incompatible with Rails
|
@@ -26,17 +30,11 @@ Due to a broken build matrix, v0.9.2 and v0.9.3 are incompatible with Rails
|
|
26
30
|
|
27
31
|
### Supported Rails versions
|
28
32
|
|
29
|
-
This library is compatible with all major Rails versions covered by the Rails
|
30
|
-
["Severe Security Issues" maintenance policy](https://guides.rubyonrails.org/maintenance_policy.html).
|
33
|
+
This library is compatible with all major Rails versions covered by the Rails ["Severe Security Issues" maintenance policy](https://guides.rubyonrails.org/maintenance_policy.html).
|
31
34
|
|
32
35
|
### Supported Ruby versions
|
33
36
|
|
34
|
-
This library may be compatible with older versions of Ruby, however we only run automated
|
35
|
-
tests using the
|
36
|
-
[officially supported Ruby versions](https://www.ruby-lang.org/en/downloads/branches/).
|
37
|
-
|
38
|
-
Please note that Ruby 3.1 is not currently tested because it is incompatible with Rails
|
39
|
-
`7.0.0`, `6.1.4.4`, `6.0.4.4` and `5.2.6` ([issue](https://github.com/rails/rails/issues/43998)).
|
37
|
+
This library may be compatible with older versions of Ruby, however we only run automated tests using the [officially supported Ruby versions](https://www.ruby-lang.org/en/downloads/branches/).
|
40
38
|
|
41
39
|
## Installation
|
42
40
|
|
@@ -48,17 +46,21 @@ gem 'active_record_upsert'
|
|
48
46
|
|
49
47
|
And then execute:
|
50
48
|
|
51
|
-
|
49
|
+
```console
|
50
|
+
bundle
|
51
|
+
```
|
52
52
|
|
53
53
|
Or install it yourself as:
|
54
54
|
|
55
|
-
|
55
|
+
```console
|
56
|
+
gem install active_record_upsert
|
57
|
+
```
|
56
58
|
|
57
59
|
## Usage
|
58
60
|
|
59
61
|
### Create
|
60
62
|
|
61
|
-
Use `ActiveRecord.upsert` or `ActiveRecord#upsert`.
|
63
|
+
Use `ActiveRecord.upsert` or `ActiveRecord#upsert`. _ActiveRecordUpsert_ respects timestamps.
|
62
64
|
|
63
65
|
```ruby
|
64
66
|
class MyRecord < ActiveRecord::Base
|
@@ -84,8 +86,7 @@ If you need to specify a condition for the update, pass it as an Arel query:
|
|
84
86
|
MyRecord.upsert({id: 1, wisdom: 3}, arel_condition: MyRecord.arel_table[:updated_at].lt(1.day.ago))
|
85
87
|
```
|
86
88
|
|
87
|
-
The instance method `#upsert` can also take keyword arguments to specify a condition, or to limit which attributes to upsert
|
88
|
-
(by default, all `changed` attributes will be passed to the upsert):
|
89
|
+
The instance method `#upsert` can also take keyword arguments to specify a condition, or to limit which attributes to upsert (by default, all `changed` attributes will be passed to the upsert):
|
89
90
|
|
90
91
|
```ruby
|
91
92
|
r = MyRecord.new(id: 1)
|
@@ -98,8 +99,7 @@ r.upsert(attributes: [:name], arel_condition: MyRecord.arel_table[:updated_at].l
|
|
98
99
|
|
99
100
|
### Create with specific Attributes
|
100
101
|
|
101
|
-
If you want to create a record with the specific attributes, but update only a limited set of attributes,
|
102
|
-
similar to how `ActiveRecord::Base.create_with` works, you can do the following:
|
102
|
+
If you want to create a record with the specific attributes, but update only a limited set of attributes, similar to how `ActiveRecord::Base.create_with` works, you can do the following:
|
103
103
|
|
104
104
|
```ruby
|
105
105
|
existing_record = MyRecord.create(id: 1, name: 'lemon', color: 'green')
|
@@ -121,16 +121,19 @@ MyRecord.create_with(name: 'banana').find_or_initialize_by(id: 2).update(color:
|
|
121
121
|
### Validations
|
122
122
|
|
123
123
|
Upsert will perform validation on the object, and return false if it is not valid. To skip validation, pass `validate: false`:
|
124
|
+
|
124
125
|
```ruby
|
125
126
|
MyRecord.upsert({id: 1, wisdom: 3}, validate: false)
|
126
127
|
```
|
127
128
|
|
128
129
|
If you want validations to raise `ActiveRecord::RecordInvalid`, use `upsert!`:
|
130
|
+
|
129
131
|
```ruby
|
130
132
|
MyRecord.upsert!(id: 1, wisdom: 3)
|
131
133
|
```
|
132
134
|
|
133
135
|
Or using the instance method:
|
136
|
+
|
134
137
|
```ruby
|
135
138
|
r = MyRecord.new(id: 1, name: 'bar')
|
136
139
|
r.upsert!
|
@@ -142,11 +145,13 @@ When a table is defined with a database default for a field, this gotcha can occ
|
|
142
145
|
|
143
146
|
**Example**: a table called `hardwares` has a `prio` column with a default value.
|
144
147
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
148
|
+
```text
|
149
|
+
┌─────────┬─────────┬─────────┬
|
150
|
+
│ Column │ Type │ Default │
|
151
|
+
├─────────┼─────────┼─────────┼
|
152
|
+
│ id │ integer │ ... |
|
153
|
+
│ prio │ integer │ 999 |
|
154
|
+
```
|
150
155
|
|
151
156
|
And `hardwares` has a record with a non-default value for `prio`. Say, the record with `id` 1 has a `prio` of `998`.
|
152
157
|
|
@@ -219,9 +224,26 @@ Overriding the models' `upsert_options` (partial index) when calling `#upsert` o
|
|
219
224
|
Account.upsert(attrs, opts: { upsert_options: { where: 'foo IS NOT NULL' } })
|
220
225
|
# Or, on an instance:
|
221
226
|
account = Account.new(attrs)
|
222
|
-
account.upsert(opts: { upsert_options: { where: 'foo IS NOT
|
227
|
+
account.upsert(opts: { upsert_options: { where: 'foo IS NOT NULL' } })
|
223
228
|
```
|
224
229
|
|
230
|
+
## Comparing to native Rails 6 Upsert
|
231
|
+
|
232
|
+
Rails 6 (via the ["Add insert_many to ActiveRecord models" PR #35077](https://github.com/rails/rails/pull/35077)) added the ability to create or update individual records through `#insert` and `#upsert` and similarly the ability to create or update multiple records through `#insert_all` and `#upsert_all`.
|
233
|
+
|
234
|
+
Here is a quick comparison of how the Rails native `ActiveRecord::Persistence#upsert` feature compares to what's offered in this gem:
|
235
|
+
|
236
|
+
| Feature | `active_record_upsert` | Rails native `ActiveRecord::Persistence#upsert` |
|
237
|
+
| ------------------------------------------------------- | ------------------------------- | -------------------------------------------------------- |
|
238
|
+
| Set model level conflict clause | Yes, through `#upsert_keys` | No, but can be passed in through the `:unique_by` option |
|
239
|
+
| Ability to invoke validations and callbacks | Yes | No |
|
240
|
+
| Automatically sets `created_at`/`updated_at` timestamps | Yes | Yes (Rails 7.0+) |
|
241
|
+
| Checks for unique index on the database | No[^1] | Yes |
|
242
|
+
| Use associations in upsert calls | Yes | No |
|
243
|
+
| Return object type | Instantiated ActiveRecord model | `ActiveRecord::Result` |
|
244
|
+
|
245
|
+
[^1]: Though the gem does not check for the index first, the upsert will still fail due to the database constraint.
|
246
|
+
|
225
247
|
## Tests
|
226
248
|
|
227
249
|
Make sure to have an upsert_test database:
|
@@ -234,12 +256,12 @@ Then run `rspec`.
|
|
234
256
|
|
235
257
|
## Contributing
|
236
258
|
|
237
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/jesjos/active_record_upsert
|
259
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/jesjos/active_record_upsert>.
|
238
260
|
|
239
261
|
## Contributors
|
240
262
|
|
241
263
|
- Jesper Josefsson
|
242
|
-
-
|
264
|
+
- Aurora Nockert
|
243
265
|
- Olle Jonsson
|
244
266
|
- Simon Dahlbacka
|
245
267
|
- Paul Hoffer
|
@@ -256,3 +278,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/jesjos
|
|
256
278
|
- Kirill Zaitsev ([@Bugagazavr](https://github.com/Bugagazavr))
|
257
279
|
- Nick Campbell ([@nickcampbell18](https://github.com/nickcampbell18))
|
258
280
|
- Mikhail Doronin ([@misdoro](https://github.com/misdoro))
|
281
|
+
- Jan Graichen ([@jgraichen](https://github.com/jgraichen))
|
@@ -21,6 +21,6 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.platform = Gem::Platform::RUBY
|
23
23
|
|
24
|
-
spec.add_runtime_dependency 'activerecord', '>= 5.2', '< 7.
|
24
|
+
spec.add_runtime_dependency 'activerecord', '>= 5.2', '< 7.2'
|
25
25
|
spec.add_runtime_dependency 'pg', '>= 0.18', '< 2.0'
|
26
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_upsert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.11.
|
4
|
+
version: 0.11.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesper Josefsson
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-11-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
version: '5.2'
|
21
21
|
- - "<"
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: '7.
|
23
|
+
version: '7.2'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
26
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
version: '5.2'
|
31
31
|
- - "<"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '7.
|
33
|
+
version: '7.2'
|
34
34
|
- !ruby/object:Gem::Dependency
|
35
35
|
name: pg
|
36
36
|
requirement: !ruby/object:Gem::Requirement
|
@@ -64,7 +64,9 @@ files:
|
|
64
64
|
- Gemfile.rails-6-0
|
65
65
|
- Gemfile.rails-6-1
|
66
66
|
- Gemfile.rails-7-0
|
67
|
-
- Gemfile.rails-
|
67
|
+
- Gemfile.rails-7-0-ruby-3-1
|
68
|
+
- Gemfile.rails-7-1
|
69
|
+
- Gemfile.rails-main
|
68
70
|
- LICENSE
|
69
71
|
- README.md
|
70
72
|
- Rakefile
|
@@ -110,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
112
|
- !ruby/object:Gem::Version
|
111
113
|
version: '0'
|
112
114
|
requirements: []
|
113
|
-
rubygems_version: 3.
|
115
|
+
rubygems_version: 3.5.0.dev
|
114
116
|
signing_key:
|
115
117
|
specification_version: 4
|
116
118
|
summary: Real PostgreSQL 9.5+ upserts using ON CONFLICT for ActiveRecord
|
File without changes
|