active_record_upsert 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b01678cb29f0661418c62699e1278e500a9704d174f114a63118823798361721
4
- data.tar.gz: d850a9160cc1b7fe9c6fa2c57843cd983734747689cb2b956d2a357714c1944d
3
+ metadata.gz: 98aadc2aa0c0144f2e76c0ccfffa7102a93e3ef1ea1b073df4bc89f1f162f9cb
4
+ data.tar.gz: 9066f7f3cc3b0eedc43778de03ab2d9599d645992f4466f8aa987dfff3eaeb06
5
5
  SHA512:
6
- metadata.gz: 66ea79bd414e4d03f0a2ef54d309fb65092a2e40170ee399d28df47fd5fbbe3a0c657a4c16d7615d03dd5d235444c72f8478542249ea66279a36f57076d2b8d4
7
- data.tar.gz: c35cc444d6abac21f43e49d0c588b65347b45d6692d0f2528846f02d05a28cd2241486f9496312ccaa2621966afc5a2b4babb84c305463f02d223644a9336fcb
6
+ metadata.gz: eec007a7ae73f4ccb52fda438a9c73182b2f87f8154a5bdfebd1ba49f7a4ea67289e224eafa9bfa1d571a028f37ddc12d8b795c1ef75772a8bde2c70369e11b7
7
+ data.tar.gz: e6d6f827e67c2c77c22eba302a9be7d3bf4fc435320e0725aa501c618f5ead4d592640f958bf316a866861549adc2bd293daeee816e448d954cc8a66c28fb9f6
@@ -9,7 +9,7 @@ module ActiveRecordUpsert
9
9
  end
10
10
 
11
11
  def exec_upsert(sql, name, binds)
12
- exec_query("#{sql} RETURNING *", name, binds)
12
+ exec_query("#{sql} RETURNING *, (xmax::text::int = 0) AS _upsert_created_record", name, binds)
13
13
  end
14
14
  end
15
15
  end
@@ -5,7 +5,7 @@ module ActiveRecordUpsert
5
5
  raise ::ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
6
6
  raise ::ActiveRecord::RecordSavedError, "Can't upsert a record that has already been saved" if persisted?
7
7
  validate == false || perform_validations || raise_validation_error
8
- values = run_callbacks(:save) {
8
+ run_callbacks(:save) {
9
9
  run_callbacks(:create) {
10
10
  attributes ||= changed
11
11
  attributes = attributes +
@@ -15,12 +15,6 @@ module ActiveRecordUpsert
15
15
  }
16
16
  }
17
17
 
18
- # When a migration adds a column to a table, the upsert will start
19
- # returning the new attribute, and assign_attributes will fail,
20
- # because Rails doesn't know about it yet (until the app is restarted).
21
- #
22
- # This checks that only known attributes are being assigned.
23
- assign_attributes(values.first.to_h.slice(*self.attributes.keys))
24
18
  self
25
19
  end
26
20
 
@@ -33,10 +27,17 @@ module ActiveRecordUpsert
33
27
  def _upsert_record(upsert_attribute_names = changed, arel_condition = nil)
34
28
  existing_attributes = attributes_with_values_for_create(self.attributes.keys)
35
29
  values = self.class._upsert_record(existing_attributes, upsert_attribute_names, [arel_condition].compact)
30
+ @attributes = self.class.attributes_builder.build_from_database(values.first.to_h)
36
31
  @new_record = false
37
32
  values
38
33
  end
39
34
 
35
+ def upsert_operation
36
+ created_record = self['_upsert_created_record']
37
+ return if created_record.nil?
38
+ created_record ? :create : :update
39
+ end
40
+
40
41
  module ClassMethods
41
42
  def upsert!(attributes, arel_condition: nil, validate: true, &block)
42
43
  if attributes.is_a?(Array)
@@ -57,6 +58,15 @@ module ActiveRecordUpsert
57
58
  def _upsert_record(existing_attributes, upsert_attributes_names, wheres) # :nodoc:
58
59
  upsert_keys = self.upsert_keys || [primary_key]
59
60
  upsert_attributes_names = upsert_attributes_names - [*upsert_keys, 'created_at']
61
+
62
+ existing_attributes = existing_attributes
63
+ .transform_keys { |name| _prepare_column(name) }
64
+ .reject { |key, _| key.nil? }
65
+
66
+ upsert_attributes_names = upsert_attributes_names
67
+ .map { |name| _prepare_column(name) }
68
+ .compact
69
+
60
70
  values_for_upsert = existing_attributes.select { |(name, _value)| upsert_attributes_names.include?(name) }
61
71
 
62
72
  insert_manager = arel_table.compile_upsert(
@@ -70,6 +80,16 @@ module ActiveRecordUpsert
70
80
  connection.upsert(insert_manager, "#{self} Upsert")
71
81
  end
72
82
 
83
+ def _prepare_column(column)
84
+ column = attribute_alias(column) if attribute_alias?(column)
85
+
86
+ if columns_hash.key?(column)
87
+ column
88
+ elsif reflections.key?(column)
89
+ reflections[column].foreign_key
90
+ end
91
+ end
92
+
73
93
  def upsert_keys(*keys)
74
94
  return @_upsert_keys if keys.empty?
75
95
  options = keys.extract_options!
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordUpsert
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  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.9.1
4
+ version: 0.9.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: 2018-05-24 00:00:00.000000000 Z
12
+ date: 2018-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord