active_record_upsert 0.1.0 → 0.2.0
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/.rspec +1 -0
- data/README.md +17 -9
- data/active_record_upsert.gemspec +4 -4
- data/bin/console +2 -0
- data/lib/active_record_upsert/active_record/persistence.rb +5 -5
- data/lib/active_record_upsert/active_record/timestamp.rb +1 -1
- data/lib/active_record_upsert/version.rb +1 -1
- metadata +31 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7882a124e75ba90b73063ee0972dbe78478a56f0
|
4
|
+
data.tar.gz: e2de676c14dc4ef582756549ddbcc357da6395e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ea854f8b213733904066171fac163e2524d9584a967510f93bf58797457d3227a72495e9b71789b40ec9d8a8cd5652d6cbda1374ccdd6641835cbb8b04f1bda
|
7
|
+
data.tar.gz: aceeeee29b6d44f2a91b5e5b91527994efe04ceec42931ea8d95031ab23f00902f71a8e973fe3b6fe220cdf8afd9a720044684f5976bf048638fefabb6dfa69d
|
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# ActiveRecordUpsert
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Real upsert for PostgreSQL 9.5+ and ActiveRecord. Uses ON CONFLICT DO UPDATE.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -21,16 +19,26 @@ Or install it yourself as:
|
|
21
19
|
$ gem install active_record_upsert
|
22
20
|
|
23
21
|
## Usage
|
22
|
+
Just use `ActiveRecord.upsert` or `ActiveRecord#upsert`.
|
23
|
+
*ActiveRecordUpsert* respects timestamps.
|
24
|
+
|
25
|
+
```
|
26
|
+
class MyRecord < ActiveRecord::Base
|
27
|
+
end
|
24
28
|
|
25
|
-
|
26
|
-
|
27
|
-
## Development
|
29
|
+
MyRecord.create(name: 'foo', wisdom: 1)
|
30
|
+
=> #<MyRecord id: 1, name: "foo", created_at: "2016-02-20 14:15:55", updated_at: "2016-02-20 14:15:55", wisdom: 1>
|
28
31
|
|
29
|
-
|
32
|
+
MyRecord.upsert(id: 1, wisdom: 3)
|
33
|
+
=> #<MyRecord id: 2, name: "foo", created_at: "2016-02-20 14:15:55", updated_at: "2016-02-20 14:18:15", wisdom: 3>
|
30
34
|
|
31
|
-
|
35
|
+
r = MyRecord.new(id: 1)
|
36
|
+
r.name = 'bar'
|
37
|
+
r.upsert
|
38
|
+
=> #<MyRecord id: 2, name: "bar", created_at: "2016-02-20 14:17:50", updated_at: "2016-02-20 14:18:49", wisdom: 3>
|
39
|
+
```
|
32
40
|
|
33
41
|
## Contributing
|
34
42
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
43
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jesjos/active_record_upsert.
|
36
44
|
|
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Jesper Josefsson"]
|
10
10
|
spec.email = ["jesper.josefsson@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
13
|
-
|
12
|
+
spec.summary = %q{Real PostgreSQL upserts using ON CONFLICT for ActiveRecord}
|
13
|
+
|
14
14
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
15
15
|
spec.bindir = "exe"
|
16
16
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
@@ -27,9 +27,9 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_runtime_dependency 'pg', '> 0'
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
30
|
spec.add_development_dependency "bundler", "~> 1.11"
|
33
31
|
spec.add_development_dependency "rake", "~> 10.0"
|
34
32
|
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
spec.add_development_dependency "pry", "> 0"
|
34
|
+
spec.add_development_dependency "database_cleaner", "~> 1.5.1"
|
35
35
|
end
|
data/bin/console
CHANGED
@@ -4,6 +4,8 @@ require "bundler/setup"
|
|
4
4
|
require 'active_record'
|
5
5
|
require 'active_record/connection_adapters/postgresql_adapter'
|
6
6
|
require "active_record_upsert"
|
7
|
+
require File.join(__dir__, '../spec/setup')
|
8
|
+
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
7
9
|
|
8
10
|
# You can add fixtures and/or initialization code here to make experimenting
|
9
11
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -1,11 +1,12 @@
|
|
1
1
|
module ActiveRecordUpsert
|
2
2
|
module ActiveRecord
|
3
3
|
module PersistenceExtensions
|
4
|
-
|
4
|
+
|
5
|
+
def upsert
|
5
6
|
raise ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
|
6
7
|
values = run_callbacks(:save) {
|
7
8
|
run_callbacks(:create) {
|
8
|
-
_upsert_record
|
9
|
+
_upsert_record
|
9
10
|
}
|
10
11
|
}
|
11
12
|
assign_attributes(values.first.to_h)
|
@@ -14,7 +15,7 @@ module ActiveRecordUpsert
|
|
14
15
|
false
|
15
16
|
end
|
16
17
|
|
17
|
-
def _upsert_record(attribute_names =
|
18
|
+
def _upsert_record(attribute_names = changed)
|
18
19
|
attributes_values = arel_attributes_with_values_for_create(attribute_names)
|
19
20
|
values = self.class.unscoped.upsert attributes_values
|
20
21
|
@new_record = false
|
@@ -24,7 +25,7 @@ module ActiveRecordUpsert
|
|
24
25
|
module ClassMethods
|
25
26
|
def upsert(attributes, &block)
|
26
27
|
if attributes.is_a?(Array)
|
27
|
-
attributes.collect { |
|
28
|
+
attributes.collect { |hash| upsert(hash, &block) }
|
28
29
|
else
|
29
30
|
new(attributes, &block).upsert
|
30
31
|
end
|
@@ -32,7 +33,6 @@ module ActiveRecordUpsert
|
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
|
-
puts 'JOJOJOJOJOJO'
|
36
36
|
::ActiveRecord::Base.prepend(PersistenceExtensions)
|
37
37
|
::ActiveRecord::Base.extend(PersistenceExtensions::ClassMethods)
|
38
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record_upsert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jesper Josefsson
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -94,6 +94,34 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '3.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: database_cleaner
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.5.1
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.5.1
|
97
125
|
description:
|
98
126
|
email:
|
99
127
|
- jesper.josefsson@gmail.com
|
@@ -152,5 +180,5 @@ rubyforge_project:
|
|
152
180
|
rubygems_version: 2.5.1
|
153
181
|
signing_key:
|
154
182
|
specification_version: 4
|
155
|
-
summary:
|
183
|
+
summary: Real PostgreSQL upserts using ON CONFLICT for ActiveRecord
|
156
184
|
test_files: []
|