1# frozen_string_literal: true
2
3# Provides a way to work around Rails issue where dependent objects are all
4# loaded into memory before destroyed: https://github.com/rails/rails/issues/22510.
5#
6# This concern allows an ActiveRecord module to destroy all its dependent
7# associations in batches. The idea is borrowed from https://github.com/thisismydesign/batch_dependent_associations.
8#
9# The differences here with that gem:
10#
11# 1. We allow excluding certain associations.
12# 2. We don't need to support delete_all since we can use the EachBatch concern.
13module BatchDestroyDependentAssociations
14  extend ActiveSupport::Concern
15
16  DEPENDENT_ASSOCIATIONS_BATCH_SIZE = 1000
17
18  def dependent_associations_to_destroy
19    self.class.reflect_on_all_associations(:has_many).select { |assoc| assoc.options[:dependent] == :destroy }
20  end
21
22  def destroy_dependent_associations_in_batches(exclude: [])
23    dependent_associations_to_destroy.each do |association|
24      next if exclude.include?(association.name)
25
26      # rubocop:disable GitlabSecurity/PublicSend
27      public_send(association.name).find_each(batch_size: DEPENDENT_ASSOCIATIONS_BATCH_SIZE, &:destroy)
28    end
29  end
30end
31