File tree Expand file tree Collapse file tree 4 files changed +39
-3
lines changed
lib/active_record/relation/batches Expand file tree Collapse file tree 4 files changed +39
-3
lines changed Original file line number Diff line number Diff line change
1
+ * Change ` BatchEnumerator#destroy_all ` to return the total number of affected rows.
2
+
3
+ Previously, it always returned ` nil ` .
4
+
5
+ * fatkodima*
1
6
2
7
Please check [ 7-2-stable] ( https://github.com/rails/rails/blob/7-2-stable/activerecord/CHANGELOG.md ) for previous changes.
Original file line number Diff line number Diff line change @@ -88,13 +88,15 @@ def touch_all(...)
88
88
end
89
89
end
90
90
91
- # Destroys records in batches.
91
+ # Destroys records in batches. Returns the total number of rows affected.
92
92
#
93
93
# Person.where("age < 10").in_batches.destroy_all
94
94
#
95
95
# See Relation#destroy_all for details of how each batch is destroyed.
96
96
def destroy_all
97
- each ( &:destroy_all )
97
+ sum do |relation |
98
+ relation . destroy_all . count ( &:destroyed? )
99
+ end
98
100
end
99
101
100
102
# Yields an ActiveRecord::Relation object for each batch of records.
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "cases/helper"
4
- require "models/comment"
5
4
require "models/post"
6
5
require "models/subscriber"
7
6
require "models/developer"
@@ -416,6 +415,22 @@ def test_in_batches_delete_all_returns_zero_when_no_batches
416
415
assert_equal 0 , Post . where ( "1=0" ) . in_batches ( of : 2 ) . delete_all
417
416
end
418
417
418
+ def test_in_batches_destroy_all_should_not_destroy_records_in_other_batches
419
+ not_destroyed_count = Post . where ( "id <= 2" ) . count
420
+ Post . where ( "id > 2" ) . in_batches ( of : 2 ) . destroy_all
421
+ assert_equal 0 , Post . where ( "id > 2" ) . count
422
+ assert_equal not_destroyed_count , Post . count
423
+ end
424
+
425
+ def test_in_batches_destroy_all_returns_rows_affected
426
+ # 1 records is not destroyed because of the callback.
427
+ assert_equal 10 , PostWithDestroyCallback . in_batches ( of : 2 ) . destroy_all
428
+ end
429
+
430
+ def test_in_batches_destroy_all_returns_zero_when_no_batches
431
+ assert_equal 0 , Post . where ( "1=0" ) . in_batches ( of : 2 ) . destroy_all
432
+ end
433
+
419
434
def test_in_batches_should_not_be_loaded
420
435
Post . in_batches ( of : 1 ) do |relation |
421
436
assert_not_predicate relation , :loaded?
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ require "models/tag"
4
+ require "models/tagging"
5
+ require "models/comment"
6
+ require "models/category"
7
+
3
8
class Post < ActiveRecord ::Base
4
9
class CategoryPost < ActiveRecord ::Base
5
10
self . table_name = "categories_posts"
@@ -329,6 +334,15 @@ class ConditionalStiPost < Post
329
334
class SubConditionalStiPost < ConditionalStiPost
330
335
end
331
336
337
+ class PostWithDestroyCallback < ActiveRecord ::Base
338
+ self . inheritance_column = :disabled
339
+ self . table_name = "posts"
340
+
341
+ before_destroy do
342
+ throw :abort if id == 1
343
+ end
344
+ end
345
+
332
346
class FakeKlass
333
347
extend ActiveRecord ::Delegation ::DelegateCache
334
348
You can’t perform that action at this time.
0 commit comments