diff options
author | Shinya Maeda <shinya@gitlab.com> | 2018-05-30 08:50:09 +0300 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-05-30 08:50:09 +0300 |
commit | 09122f93c34b15cb827aabdbdf35fc33b08f93af (patch) | |
tree | 57c137ef57621a7a2ed4940c56c7f5cbe6ec1c80 /app/models/concerns | |
parent | 1d20679e9c8b1ba16bebaf982255946e7207b4d4 (diff) | |
parent | 5b1416aa74c4fa80e0c324fd2907166af5ca479b (diff) |
Merge branch 'master' into per-project-pipeline-iid
Diffstat (limited to 'app/models/concerns')
-rw-r--r-- | app/models/concerns/batch_destroy_dependent_associations.rb | 28 | ||||
-rw-r--r-- | app/models/concerns/diff_file.rb | 9 |
2 files changed, 37 insertions, 0 deletions
diff --git a/app/models/concerns/batch_destroy_dependent_associations.rb b/app/models/concerns/batch_destroy_dependent_associations.rb new file mode 100644 index 00000000000..353ee2e73d0 --- /dev/null +++ b/app/models/concerns/batch_destroy_dependent_associations.rb @@ -0,0 +1,28 @@ +# Provides a way to work around Rails issue where dependent objects are all +# loaded into memory before destroyed: https://github.com/rails/rails/issues/22510. +# +# This concern allows an ActiveRecord module to destroy all its dependent +# associations in batches. The idea is borrowed from https://github.com/thisismydesign/batch_dependent_associations. +# +# The differences here with that gem: +# +# 1. We allow excluding certain associations. +# 2. We don't need to support delete_all since we can use the EachBatch concern. +module BatchDestroyDependentAssociations + extend ActiveSupport::Concern + + DEPENDENT_ASSOCIATIONS_BATCH_SIZE = 1000 + + def dependent_associations_to_destroy + self.class.reflect_on_all_associations(:has_many).select { |assoc| assoc.options[:dependent] == :destroy } + end + + def destroy_dependent_associations_in_batches(exclude: []) + dependent_associations_to_destroy.each do |association| + next if exclude.include?(association.name) + + # rubocop:disable GitlabSecurity/PublicSend + public_send(association.name).find_each(batch_size: DEPENDENT_ASSOCIATIONS_BATCH_SIZE, &:destroy) + end + end +end diff --git a/app/models/concerns/diff_file.rb b/app/models/concerns/diff_file.rb new file mode 100644 index 00000000000..72332072012 --- /dev/null +++ b/app/models/concerns/diff_file.rb @@ -0,0 +1,9 @@ +module DiffFile + extend ActiveSupport::Concern + + def to_hash + keys = Gitlab::Git::Diff::SERIALIZE_KEYS - [:diff] + + as_json(only: keys).merge(diff: diff).with_indifferent_access + end +end |