Module: BatchNullifyDependentAssociations

Extended by:
ActiveSupport::Concern
Included in:
Ci::PipelineSchedule, User
Defined in:
app/models/concerns/batch_nullify_dependent_associations.rb

Overview

Provides a way to execute nullify behaviour in batches to avoid query timeouts for really big tables Assumes that associations have ‘dependent: :nullify` statement

Instance Method Summary collapse

Instance Method Details

#nullify_dependent_associations_in_batches(exclude: [], batch_size: 100) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/concerns/batch_nullify_dependent_associations.rb', line 15

def nullify_dependent_associations_in_batches(exclude: [], batch_size: 100)
  self.class.dependent_associations_to_nullify.each do |association|
    next if association.name.in?(exclude)

    loop do
      # rubocop:disable GitlabSecurity/PublicSend
      update_count = public_send(association.name).limit(batch_size).update_all(association.foreign_key => nil)
      # rubocop:enable GitlabSecurity/PublicSend
      break if update_count < batch_size
    end
  end
end