active_record_upsert 0.7.3 → 0.7.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +4 -0
- data/lib/active_record_upsert/active_record/connection_adapters/postgresql/database_statements.rb +5 -10
- data/lib/active_record_upsert/active_record/persistence.rb +16 -3
- data/lib/active_record_upsert/active_record.rb +0 -3
- data/lib/active_record_upsert/arel/crud.rb +13 -3
- data/lib/active_record_upsert/compatibility/rails50.rb +1 -0
- data/lib/active_record_upsert/{active_record/relation.rb → compatibility/rails51.rb} +24 -8
- data/lib/active_record_upsert/version.rb +1 -1
- data/lib/active_record_upsert.rb +3 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e867ac92d0f9da516a6845e3ebd2fc03da038ae5451e6ff0ce217cbf3e999423
|
4
|
+
data.tar.gz: 0b559e347df791f257144798d12e03d882584af0035606ceba2777b1aa13c6da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72680038d1f62c296972e56cd788fd01ddc17aec56590eda76b32d3d541543c3acd74e12b1a1ee716df7cbc4d7e148ef5afb34943824e68e55fc1115247621a7
|
7
|
+
data.tar.gz: 0f4270bb398c42726fdec1494e6da82e08ccd73e51e384267cf109ba597ed9e36981922ac013cce7aad90b704222c11576cb07214f401161d4f5432fbdeee5a6
|
data/Rakefile
CHANGED
@@ -13,6 +13,10 @@ task :setup_and_run_spec do |rake_task|
|
|
13
13
|
|
14
14
|
require File.expand_path('../spec/dummy/config/environment.rb', __FILE__)
|
15
15
|
|
16
|
+
if Rails.version >= '5.2.0.rc1'
|
17
|
+
ActiveRecord::Base.connection.migrations_paths << 'spec/dummy/db/migrate'
|
18
|
+
end
|
19
|
+
|
16
20
|
include ActiveRecord::Tasks
|
17
21
|
DatabaseTasks.db_dir = 'spec/dummy/db'
|
18
22
|
DatabaseTasks.drop_current
|
data/lib/active_record_upsert/active_record/connection_adapters/postgresql/database_statements.rb
CHANGED
@@ -3,18 +3,13 @@ module ActiveRecordUpsert
|
|
3
3
|
module ConnectionAdapters
|
4
4
|
module Postgresql
|
5
5
|
module DatabaseStatementsExtensions
|
6
|
-
def upsert(arel, name = nil,
|
7
|
-
sql, binds
|
8
|
-
exec_upsert(sql, name, binds
|
6
|
+
def upsert(arel, name = nil, binds = [])
|
7
|
+
sql, binds = to_sql_and_binds(arel, binds)
|
8
|
+
exec_upsert(sql, name, binds)
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
13
|
-
[sql, binds, pk, sequence_name]
|
14
|
-
end
|
15
|
-
|
16
|
-
def exec_upsert(sql, name, binds, pk)
|
17
|
-
exec_query(sql, name, binds)
|
11
|
+
def exec_upsert(sql, name, binds)
|
12
|
+
exec_query("#{sql} RETURNING *", name, binds)
|
18
13
|
end
|
19
14
|
end
|
20
15
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module ActiveRecordUpsert
|
2
2
|
module ActiveRecord
|
3
3
|
module PersistenceExtensions
|
4
|
-
|
5
4
|
def upsert!(attributes: nil, arel_condition: nil, validate: true)
|
6
5
|
raise ::ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
|
7
6
|
raise ::ActiveRecord::RecordSavedError, "Can't upsert a record that has already been saved" if persisted?
|
@@ -31,10 +30,9 @@ module ActiveRecordUpsert
|
|
31
30
|
false
|
32
31
|
end
|
33
32
|
|
34
|
-
|
35
33
|
def _upsert_record(upsert_attribute_names = changed, arel_condition = nil)
|
36
34
|
existing_attributes = arel_attributes_with_values_for_create(self.attributes.keys)
|
37
|
-
values = self.class.
|
35
|
+
values = self.class._upsert_record(existing_attributes, upsert_attribute_names, [arel_condition].compact)
|
38
36
|
@new_record = false
|
39
37
|
values
|
40
38
|
end
|
@@ -56,6 +54,21 @@ module ActiveRecordUpsert
|
|
56
54
|
false
|
57
55
|
end
|
58
56
|
|
57
|
+
def _upsert_record(existing_attributes, upsert_attributes_names, wheres) # :nodoc:
|
58
|
+
upsert_keys = self.upsert_keys || [primary_key]
|
59
|
+
upsert_attributes_names = upsert_attributes_names - [*upsert_keys, 'created_at']
|
60
|
+
values_for_upsert = existing_attributes.select { |a| upsert_attributes_names.include?(a.name) }
|
61
|
+
|
62
|
+
insert_manager = arel_table.compile_upsert(
|
63
|
+
upsert_keys,
|
64
|
+
_substitute_values(values_for_upsert),
|
65
|
+
_substitute_values(existing_attributes),
|
66
|
+
wheres
|
67
|
+
)
|
68
|
+
|
69
|
+
connection.upsert(insert_manager, "#{self} Upsert")
|
70
|
+
end
|
71
|
+
|
59
72
|
def upsert_keys(*keys)
|
60
73
|
return @_upsert_keys if keys.empty?
|
61
74
|
keys = keys.first if keys.size == 1 # support single string/symbol, multiple string/symbols, and array
|
@@ -14,8 +14,5 @@ end
|
|
14
14
|
::ActiveRecord::Base.prepend(ActiveRecordUpsert::ActiveRecord::TimestampExtensions)
|
15
15
|
::ActiveRecord::Base.prepend(ActiveRecordUpsert::ActiveRecord::TransactionsExtensions)
|
16
16
|
|
17
|
-
#::ActiveRecord::ConnectionAdapters::DatabaseStatements.include(ActiveRecordUpsert::ActiveRecord::ConnectionAdapters::Abstract::DatabaseStatementsExtensions)
|
18
17
|
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(ActiveRecordUpsert::ActiveRecord::ConnectionAdapters::Abstract::DatabaseStatementsExtensions)
|
19
18
|
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(ActiveRecordUpsert::ActiveRecord::ConnectionAdapters::Postgresql::DatabaseStatementsExtensions)
|
20
|
-
|
21
|
-
::ActiveRecord::Relation.include(ActiveRecordUpsert::ActiveRecord::RelationExtensions)
|
@@ -10,9 +10,19 @@
|
|
10
10
|
# end
|
11
11
|
# end
|
12
12
|
module Arel
|
13
|
-
|
14
|
-
def
|
15
|
-
OnConflictDoUpdateManager.new
|
13
|
+
module Crud
|
14
|
+
def compile_upsert(upsert_keys, upsert_values, insert_values, wheres)
|
15
|
+
on_conflict_do_update = OnConflictDoUpdateManager.new
|
16
|
+
|
17
|
+
on_conflict_do_update.target = self[upsert_keys.join(',')]
|
18
|
+
on_conflict_do_update.wheres = wheres
|
19
|
+
on_conflict_do_update.set(upsert_values)
|
20
|
+
|
21
|
+
insert_manager = create_insert
|
22
|
+
insert_manager.on_conflict = on_conflict_do_update.to_node
|
23
|
+
insert_manager.into insert_values.first.first.relation
|
24
|
+
insert_manager.insert(insert_values)
|
25
|
+
insert_manager
|
16
26
|
end
|
17
27
|
end
|
18
28
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_relative 'rails51'
|
@@ -1,5 +1,14 @@
|
|
1
1
|
module ActiveRecordUpsert
|
2
2
|
module ActiveRecord
|
3
|
+
module PersistenceExtensions
|
4
|
+
def _upsert_record(upsert_attribute_names = changed, arel_condition = nil)
|
5
|
+
existing_attributes = arel_attributes_with_values_for_create(self.attributes.keys)
|
6
|
+
values = self.class.unscoped.upsert(existing_attributes, upsert_attribute_names, [arel_condition].compact)
|
7
|
+
@new_record = false
|
8
|
+
values
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
3
12
|
module RelationExtensions
|
4
13
|
def upsert(existing_attributes, upsert_attributes, wheres) # :nodoc:
|
5
14
|
substitutes, binds = substitute_values(existing_attributes)
|
@@ -11,7 +20,7 @@ module ActiveRecordUpsert
|
|
11
20
|
on_conflict_binds = binds.select(&upsert_keys_filter)
|
12
21
|
vals_for_upsert = substitutes.select { |s| upsert_keys_filter.call(s.first) }
|
13
22
|
|
14
|
-
on_conflict_do_update =
|
23
|
+
on_conflict_do_update = ::Arel::OnConflictDoUpdateManager.new
|
15
24
|
on_conflict_do_update.target = arel_table[upsert_keys.join(',')]
|
16
25
|
on_conflict_do_update.wheres = wheres
|
17
26
|
on_conflict_do_update.set(vals_for_upsert)
|
@@ -21,13 +30,20 @@ module ActiveRecordUpsert
|
|
21
30
|
insert_manager.on_conflict = on_conflict_do_update.to_node
|
22
31
|
insert_manager.insert substitutes
|
23
32
|
|
24
|
-
@klass.connection.upsert(
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
33
|
+
@klass.connection.upsert(insert_manager, "#{self} Upsert", binds + on_conflict_binds)
|
34
|
+
end
|
35
|
+
|
36
|
+
::ActiveRecord::Relation.include(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
module ConnectionAdapters
|
40
|
+
module Postgresql
|
41
|
+
module DatabaseStatementsExtensions
|
42
|
+
def upsert(arel, name = nil, binds = [])
|
43
|
+
sql = to_sql(arel, binds)
|
44
|
+
exec_upsert(sql, name, binds)
|
45
|
+
end
|
46
|
+
end
|
31
47
|
end
|
32
48
|
end
|
33
49
|
end
|
data/lib/active_record_upsert.rb
CHANGED
@@ -11,6 +11,9 @@ end
|
|
11
11
|
require 'active_record_upsert/arel'
|
12
12
|
require 'active_record_upsert/active_record'
|
13
13
|
|
14
|
+
require 'active_record_upsert/compatibility/rails51.rb' if Rails.version >= '5.1.0' && Rails.version < '5.2.0.rc1'
|
15
|
+
require 'active_record_upsert/compatibility/rails50.rb' if Rails.version >= '5.0.0' && Rails.version < '5.1.0'
|
16
|
+
|
14
17
|
module ActiveRecordUpsert
|
15
18
|
# Your code goes here...
|
16
19
|
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.7.
|
4
|
+
version: 0.7.4
|
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: 2018-02-
|
12
|
+
date: 2018-02-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -88,7 +88,6 @@ files:
|
|
88
88
|
- lib/active_record_upsert/active_record/connection_adapters/abstract/database_statements.rb
|
89
89
|
- lib/active_record_upsert/active_record/connection_adapters/postgresql/database_statements.rb
|
90
90
|
- lib/active_record_upsert/active_record/persistence.rb
|
91
|
-
- lib/active_record_upsert/active_record/relation.rb
|
92
91
|
- lib/active_record_upsert/active_record/timestamp.rb
|
93
92
|
- lib/active_record_upsert/active_record/transactions.rb
|
94
93
|
- lib/active_record_upsert/arel.rb
|
@@ -103,6 +102,8 @@ files:
|
|
103
102
|
- lib/active_record_upsert/arel/nodes/on_conflict_action.rb
|
104
103
|
- lib/active_record_upsert/arel/on_conflict_do_update_manager.rb
|
105
104
|
- lib/active_record_upsert/arel/visitors/to_sql.rb
|
105
|
+
- lib/active_record_upsert/compatibility/rails50.rb
|
106
|
+
- lib/active_record_upsert/compatibility/rails51.rb
|
106
107
|
- lib/active_record_upsert/version.rb
|
107
108
|
homepage: https://github.com/jesjos/active_record_upsert/
|
108
109
|
licenses:
|
@@ -124,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
125
|
version: '0'
|
125
126
|
requirements: []
|
126
127
|
rubyforge_project:
|
127
|
-
rubygems_version: 2.7.
|
128
|
+
rubygems_version: 2.7.5
|
128
129
|
signing_key:
|
129
130
|
specification_version: 4
|
130
131
|
summary: Real PostgreSQL 9.5+ upserts using ON CONFLICT for ActiveRecord
|