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 +4 -4
- data/README.md +2 -9
- data/lib/activerecord-updateinbulk/builder.rb +23 -3
- data/lib/activerecord-updateinbulk/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5b333b357426d210ed69dfa18de2f7f77ae8088e9fc47819198118f22f7647fc
|
|
4
|
+
data.tar.gz: 96eb8a466eb413735de1e1ed4e80dbbc18402b356b5870d9b24b92826828b425
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|