Module: ActiveRecord::OnDemand::DeleteAllByPk::ClassMethods
- Defined in:
- lib/ar-ondemand/delete_all_by_pk.rb
Instance Method Summary collapse
-
#delete_all_by_pk(options = {}) ⇒ Object
Use this instead of delete_all to perform a delete using the PK of the table, which prevents a complete table scan that locks it Based on find_in_batches function.
Instance Method Details
#delete_all_by_pk(options = {}) ⇒ Object
Use this instead of delete_all to perform a delete using the PK of the table, which prevents a complete table scan that locks it Based on find_in_batches function
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ar-ondemand/delete_all_by_pk.rb', line 11 def delete_all_by_pk( = {}) relation = self # TODO Clean this up after dropping support for Rails 3 if ActiveRecord::VERSION::MAJOR == 3 relation = self.scoped unless respond_to? :arel else relation = self.all unless respond_to? :arel end unless relation.arel.orders.blank? && relation.arel.taken.blank? ::ActiveRecord::Base.logger.warn("Scoped order and limit are ignored, it's forced to be batch order and batch size") end start = .delete(:start) batch_size = .delete(:batch_size) relation = relation.reorder(batch_order_delete_all_by_pk).limit(batch_size) if batch_size records = query_delete_all_by_pk(start ? relation.where(relation.table[primary_key].gteq(start)) : relation) deleted = 0 while records.any? records_size = records.size primary_key_offset = records.last deleted += self.unscoped.where(id: records).delete_all break if batch_size.nil? || records_size < batch_size records = query_delete_all_by_pk relation.where(relation.table[primary_key].gt(primary_key_offset)) end deleted end |