rails_delete_all_and_assocs_without_instantiation 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cef795591d41dcdd1dfbb7c64f25d1e070640dad50d2635e605a7315be0f4bd3
4
- data.tar.gz: 930cbbdee057d7444db66ce1855b8ca9b6e1f42f8ec9e67e88b45c8a2d9ff830
3
+ metadata.gz: e9acdcdfc546fa1f49d03d2f52f785e8a012211cb2fead2ff5dad11767d125cc
4
+ data.tar.gz: e46db80ef699f7ad8a8ffbd4f9689f15c019826ad65acdbbd1d982cdaa423a52
5
5
  SHA512:
6
- metadata.gz: d4a7553953c99a07638e27294dd28db610accb1bc6db8c07ef43f2ea13ee47fd5ec34dfd57a6a0b40115047c3925475c1f3234691a4db5037773d33ebf6006ed
7
- data.tar.gz: ad2091ccff93424d1616bcca3eff51f9af9592245d1846b95a4d91509247072685bc777b7c54687dd35854d50e1b3ccaf6fca4e06f45a9445f389246244b17ab
6
+ metadata.gz: 87cdde62744d43c2567d6b55a59ccddcd0a412925c6d396294396839d12036dab633d3a3fd09d3284952bd5a351d4467c27d0426e5cf088b92b83963860f284c
7
+ data.tar.gz: 19ac4b45404cd36e02d1f1aac7cf8a9598a862ae272e32553258a377dbf2af8aa28ffc7047aa6e817898ab32fbb8922180b4b48bd5009d4243365fbb30187354
data/README.md CHANGED
@@ -36,3 +36,22 @@ class User < ApplicationRecord
36
36
  has_many :creator_accounts, -> (user_id) { where(creator_id: user_id) }, dependent: :destroy
37
37
  end
38
38
  ```
39
+
40
+ # Customization
41
+ ```
42
+ class User
43
+ # This is a way you can do more advanced filtering on association dependencies, and not just wipe everything
44
+ def self.delete_all_and_assocs_without_instantiation_builder models_and_ids_list = {}, errors = [], options = {}
45
+ original_account_ids = models_and_ids_list['Account']
46
+ models_and_ids_list, errors = super(models_and_ids_list, errors, options)
47
+ # we've ignored the deletion command that may or may not have been created by your assoc definition
48
+ models_and_ids_list['Account'] = original_account_ids
49
+
50
+ query = self.where({}).includes(:accounts).joins(:accounts).where(accounts: {marketable: true})
51
+ account_ids = query.collect{|pro| pro.accounts.map(&:id) }.flatten
52
+ models_and_ids_list, errors = Account.unscoped.where(id: account_ids).delete_all_and_assocs_without_instantiation_builder(models_and_ids_list, errors, options)
53
+
54
+ return models_and_ids_list, errors
55
+ end
56
+ end
57
+ ```
@@ -37,7 +37,7 @@ module RailsDeleteAllAndAssocsWithoutInstantiation
37
37
 
38
38
  # prevent infinite recursion here.
39
39
  ids = ids - models_and_ids_list[self.name]
40
- if ids.none?
40
+ if ids.none? || ids.nil?
41
41
  return models_and_ids_list, errors
42
42
  end
43
43
 
@@ -160,22 +160,31 @@ module RailsDeleteAllAndAssocsWithoutInstantiation
160
160
 
161
161
  ActiveRecord::Base.transaction do
162
162
  built_deletions.keys.reverse.each do |class_name|
163
+ ids = "N/A"
163
164
  begin
164
165
  ids = built_deletions[class_name]
165
- next if ids.none?
166
+ next if ids.none? || ids.nil?
166
167
  klass = class_name.constantize
167
168
  klass.unscoped.where(id: ids).delete_all
168
169
  rescue Exception => e
170
+ puts "DEL ATTEMPT 1: #{class_name}.unscoped.where(id: #{ids}).delete_all" if options[:verbose]
169
171
  retry_due_to_errors << class_name
170
172
  end
171
173
  end
172
174
 
175
+ if options[:verbose]
176
+ puts "RETRY DUE TO ERRORS:"
177
+ puts retry_due_to_errors.inspect
178
+ end
179
+
173
180
  # ActiveRecord::InvalidForeignKey can cause issues in ordering.
174
- retry_due_to_errors.reverse.each do |class_name|
181
+ retry_due_to_errors.each do |class_name|
182
+ ids = "N/A"
175
183
  begin
176
184
  ids = built_deletions[class_name]
177
- next if ids.none?
185
+ next if ids.none? || ids.nil?
178
186
  klass = class_name.constantize
187
+ puts "DEL ATTEMPT 2: #{class_name}.unscoped.where(id: #{ids}).delete_all" if options[:verbose]
179
188
  klass.unscoped.where(id: ids).delete_all
180
189
  # if Rails.env.test? || Rails.env.development?
181
190
  # if count = klass.unscoped.where(id: ids).count > 0
@@ -185,20 +194,27 @@ module RailsDeleteAllAndAssocsWithoutInstantiation
185
194
  rescue Exception => e
186
195
  # ActiveRecord::Base.transaction obscures the actual errors
187
196
  # - need to run again outside of block to get actual error
188
- retry_to_capture_errors << [klass, ids]
197
+ retry_to_capture_errors << [class_name, ids]
189
198
  raise ActiveRecord::Rollback
190
199
  end
191
200
  end
192
201
  end
193
202
 
194
- retry_to_capture_errors.each do |klass, ids|
203
+ retry_to_capture_errors.each do |class_name, ids|
204
+ if ids == "N/A"
205
+ errors << ["Internal Server Error, could not locate IDs", class_name]
206
+ next
207
+ end
195
208
  begin
209
+ klass = class_name.constantize
210
+ puts "DEL ATTEMPT 3: #{class_name}.unscoped.where(id: #{ids}).delete_all" if options[:verbose]
196
211
  klass.unscoped.where(id: ids).delete_all
197
212
  # should never get past this line, we're expecting an error!
198
213
  # - if we do, maybe was an intermittent issue. Call ourselves recursively to try again.
199
- return current_query.delete_all_and_assocs_without_instantiation(options)
214
+ # return current_query.delete_all_and_assocs_without_instantiation(options)
215
+ errors << ["#{class_name} - expected error, ran into 2 errors on previous deletion attempts", ids]
200
216
  rescue Exception => e
201
- errors << e.class.to_s + "; " + e.message
217
+ errors << [e.class.to_s + "; " + e.message, e.backtrace]
202
218
  end
203
219
 
204
220
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{rails_delete_all_and_assocs_without_instantiation}
5
- s.version = "0.0.3"
5
+ s.version = "0.0.4"
6
6
  s.date = %q{2023-03-02}
7
7
  s.authors = ["benjamin.dana.software.dev@gmail.com"]
8
8
  s.summary = %q{Non-instantiated way of deleting records with dependencies quickly without instantiation.}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_delete_all_and_assocs_without_instantiation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjamin.dana.software.dev@gmail.com