Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow batch disconnect for the batch strategy #15699

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,21 @@ def save!(inventory_collection, association)
end

def destroy_records(records)
# TODO(lsmola) we need at least batch disconnect. Batch destroy won't be probably possible because of the
# :dependent => :destroy.
ActiveRecord::Base.transaction do
records.each do |record|
delete_record!(inventory_collection, record)
return false unless inventory_collection.delete_allowed?
return if records.blank?

# Is the delete_method rails standard deleting method?
rails_delete = %i(destroy delete).include?(inventory_collection.delete_method)
if !rails_delete && inventory_collection.model_class.respond_to?(inventory_collection.delete_method)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look like they could be cleaned up a bit, maybe move the logic into the InventoryCollection but that can be a follow-up PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps InventoryCollection having :batch_delete_method param, instead of magic implication of having class method with same name?

# We have custom delete method defined on a class, that means it supports batch destroy
inventory_collection.model_class.public_send(inventory_collection.delete_method, records.map(&:id))
else
# We have either standard :destroy and :delete rails method, or custom instance level delete method
# Note: The standard :destroy and :delete rails method can't be batched because of the hooks and cascade destroy
Copy link
Contributor

@cben cben Aug 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My impression is that delete doesn't do any cascading/callback and can be batched without instantiation using model.delete([ids...]) or where(...).delete_all.

The row is simply removed with an SQL DELETE statement on the record's primary key, and no callbacks are executed.
...
To enforce the object's before_destroy and after_destroy callbacks or any :dependent association options, use #destroy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm right, I will remove the delete from there entirely, since we do not really use it anyway.

ActiveRecord::Base.transaction do
records.each do |record|
delete_record!(inventory_collection, record)
end
end
end
end
Expand Down