activerecord-updateinbulk 0.2.1 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1a844813b98d092d304542eb4f8c3ffc6b99a50f7510d430bef229e47c969415
4
- data.tar.gz: e83aa71485ad9255b977b96afe9a8ec55820eda485d489d16e30629c1a79a971
3
+ metadata.gz: 5b333b357426d210ed69dfa18de2f7f77ae8088e9fc47819198118f22f7647fc
4
+ data.tar.gz: 96eb8a466eb413735de1e1ed4e80dbbc18402b356b5870d9b24b92826828b425
5
5
  SHA512:
6
- metadata.gz: 45d04870ea3981cc5bb3533572f8fd412cc442bfbde77163d2d78bb47af3e3234658db990afdea09fc754f6d492ed400bc50f7f23508f01a82614f1803fae05e
7
- data.tar.gz: 33dfb33a7963b357eb8da64afc3353e671fa33cab926e8bf3b9380ccecd47dbae1360f776159e35dc02a44c0c8f98721d7ed58306eebc456bf89673b0fc14900
6
+ metadata.gz: 7a9ff7817a67baf305cd391c855e7607b7adc4c756abadf8d11bba119ad137bd378e83841e80dacaa0abe8265cd3c6d432f58faa4029608fe783c3ab410b7b14
7
+ data.tar.gz: b71a70301fa255f1c52ae9472b6cce357b2d8f10313226398e74294af0a76179b04383d3d2c5fa52b82fa05dc5dd3581fe4da0330414c1394d8ab900dd4863e9
data/README.md CHANGED
@@ -61,17 +61,10 @@ Order.joins(:items).where(items: { status: :shipped }).update_in_bulk({
61
61
 
62
62
  ### Rails configuration
63
63
 
64
- Railtie options are available at `config.active_record_update_in_bulk`:
64
+ Railtie initializer options are available at `config.active_record_update_in_bulk`:
65
65
 
66
66
  - `values_table_alias` (`String`, optional): alias used for generated VALUES tables (default `"t"`).
67
- - `ignore_scope_order` (`Boolean`, default `true`): when true, ORDER BY scopes are ignored by `update_in_bulk`; when false, ordered relations raise `NotImplementedError`.
68
-
69
- Example initializer:
70
-
71
- ```ruby
72
- Rails.application.config.active_record_update_in_bulk.values_table_alias = "vals"
73
- Rails.application.config.active_record_update_in_bulk.ignore_scope_order = true
74
- ```
67
+ - `ignore_scope_order` (`Boolean`, default `true`): when true, ORDER BY scopes are ignored by `update_in_bulk`.
75
68
 
76
69
  ### Record timestamps
77
70
 
@@ -138,9 +138,11 @@ module ActiveRecord::UpdateInBulk
138
138
  @conditions = conditions
139
139
  @assigns = assigns
140
140
  @formulas = normalize_formulas(formulas)
141
+ @auto_locking_column = nil
141
142
 
142
143
  resolve_attribute_aliases!
143
144
  resolve_read_and_write_keys!
145
+ apply_optimistic_locking!
144
146
  verify_read_and_write_keys!
145
147
  serialize_values!
146
148
  detect_constant_columns! unless simple_update?
@@ -222,7 +224,7 @@ module ActiveRecord::UpdateInBulk
222
224
 
223
225
  def build_values_table_rows
224
226
  bitmask_keys = Set.new
225
- non_constant_write_keys = write_keys - constant_assigns.keys
227
+ non_constant_write_keys = write_keys.reject { |key| constant_assigns.key?(key) }
226
228
 
227
229
  rows = @conditions.map.with_index do |row_conditions, row_index|
228
230
  row_assigns = @assigns[row_index]
@@ -264,6 +266,10 @@ module ActiveRecord::UpdateInBulk
264
266
  else
265
267
  build_simple_assignments(table)
266
268
  end
269
+ if @auto_locking_column
270
+ lock = table[@auto_locking_column]
271
+ set_assignments << [lock, table.coalesce(lock, 0) + 1]
272
+ end
267
273
 
268
274
  if timestamp_keys.any?
269
275
  # Timestamp assignments precede data assignments to increase the
@@ -288,7 +294,8 @@ module ActiveRecord::UpdateInBulk
288
294
  if constant_assigns.key?(key)
289
295
  rhs = Arel::Nodes::Quoted.new(constant_assigns[key])
290
296
  else
291
- rhs = values_table[column]
297
+ val = values_table[column]
298
+ rhs = val
292
299
  column += 1
293
300
  rhs = self.class.apply_formula(formula, lhs, rhs, model) if formula
294
301
  end
@@ -296,7 +303,11 @@ module ActiveRecord::UpdateInBulk
296
303
  if function = bitmask_functions[key]
297
304
  rhs = Arel::Nodes::Case.new(function).when("1").then(rhs).else(lhs)
298
305
  elsif optional_keys.include?(key)
299
- rhs = table.coalesce(rhs, lhs)
306
+ if formula
307
+ rhs = Arel::Nodes::Case.new.when(val.eq(nil)).then(lhs).else(rhs)
308
+ else
309
+ rhs = table.coalesce(rhs, lhs)
310
+ end
300
311
  end
301
312
  [lhs, rhs]
302
313
  end
@@ -359,6 +370,15 @@ module ActiveRecord::UpdateInBulk
359
370
  normalized
360
371
  end
361
372
 
373
+ def apply_optimistic_locking!
374
+ return unless model.locking_enabled?
375
+
376
+ locking_column = model.locking_column
377
+ return if write_keys.include?(locking_column)
378
+
379
+ @auto_locking_column = locking_column
380
+ end
381
+
362
382
  def resolve_attribute_aliases!
363
383
  return if model.attribute_aliases.empty?
364
384
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module UpdateInBulk
5
- VERSION = "0.2.1"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-updateinbulk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Carvalho