bulk_dependency_eraser 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/bulk_dependency_eraser/builder.rb +34 -18
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cafe34b586a54a169d2d728ba0a39d9403c90796db9f40c9050620eaa4ef156
|
4
|
+
data.tar.gz: a1a7001243a75db7740e2fbcbdfbf43dad5253f83de8c757bb7b2d0ab2f57763
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fc46dd24138b1be6c2ff436e9afdaa4f4f877c37fe23bec00ffec8ce142ffda71c6cccefd7a5d8354986757f9df02f0c64918419d270471eea866226cce87a6
|
7
|
+
data.tar.gz: ecfa3aca0fdae9d2de444f8fb8427758420c8ffa7ae8370daff95b5ef975db83ec9fc573e91d83e93f84acfd11e117171a1a276b45841954526cba6c481d2b21
|
@@ -12,6 +12,7 @@ module BulkDependencyEraser
|
|
12
12
|
ignore_tables: [],
|
13
13
|
# Won't parse any table in this list
|
14
14
|
ignore_tables_and_dependencies: [],
|
15
|
+
ignore_klass_names_and_dependencies: [],
|
15
16
|
}.freeze
|
16
17
|
|
17
18
|
DEPENDENCY_NULLIFY = %i[
|
@@ -52,10 +53,12 @@ module BulkDependencyEraser
|
|
52
53
|
@ignore_table_deletion_list = {}
|
53
54
|
@ignore_table_nullification_list = {}
|
54
55
|
|
56
|
+
@table_names_to_parsed_klass_names = {}
|
57
|
+
|
55
58
|
super(opts:)
|
56
59
|
|
57
|
-
|
58
|
-
@
|
60
|
+
@ignore_table_name_and_dependencies = opts_c.ignore_tables_and_dependencies.collect { |table_name| table_name }
|
61
|
+
@ignore_klass_name_and_dependencies = opts_c.ignore_klass_names_and_dependencies.collect { |klass_name| klass_name }
|
59
62
|
end
|
60
63
|
|
61
64
|
def execute
|
@@ -63,10 +66,12 @@ module BulkDependencyEraser
|
|
63
66
|
build_result = build
|
64
67
|
|
65
68
|
# move any klass names if told to ignore them into their respective new lists
|
69
|
+
# - prior approach was to use table_name.classify, but we can't trust that approach.
|
66
70
|
opts_c.ignore_tables.each do |table_name|
|
67
|
-
|
68
|
-
|
69
|
-
|
71
|
+
table_names_to_parsed_klass_names.dig(table_name)&.each do |klass_name|
|
72
|
+
ignore_table_deletion_list[klass_name] = deletion_list.delete(klass_name) if deletion_list.key?(klass_name)
|
73
|
+
ignore_table_nullification_list[klass_name] = nullification_list.delete(klass_name) if nullification_list.key?(klass_name)
|
74
|
+
end
|
70
75
|
end
|
71
76
|
|
72
77
|
return build_result
|
@@ -108,15 +113,24 @@ module BulkDependencyEraser
|
|
108
113
|
if query.is_a?(ActiveRecord::Relation)
|
109
114
|
klass = query.klass
|
110
115
|
klass_name = query.klass.name
|
111
|
-
table_klass_name = query.klass.table_name.classify
|
112
116
|
else
|
113
117
|
# current_query is a normal rails class
|
114
118
|
klass = query
|
115
119
|
klass_name = query.name
|
116
|
-
table_klass_name = query.table_name.classify
|
117
120
|
end
|
118
121
|
|
119
|
-
|
122
|
+
table_names_to_parsed_klass_names[klass.table_name] ||= []
|
123
|
+
# Need to populate this list here, so we can have access to it later for the :ignore_tables option
|
124
|
+
unless table_names_to_parsed_klass_names[klass.table_name].include?(klass_name)
|
125
|
+
table_names_to_parsed_klass_names[klass.table_name] << klass_name
|
126
|
+
end
|
127
|
+
|
128
|
+
if ignore_table_name_and_dependencies.include?(klass.table_name)
|
129
|
+
# Not parsing, table and dependencies ignorable
|
130
|
+
return
|
131
|
+
end
|
132
|
+
|
133
|
+
if ignore_klass_name_and_dependencies.include?(klass_name)
|
120
134
|
# Not parsing, table and dependencies ignorable
|
121
135
|
return
|
122
136
|
end
|
@@ -141,25 +155,25 @@ module BulkDependencyEraser
|
|
141
155
|
query.pluck(:id)
|
142
156
|
end
|
143
157
|
|
144
|
-
deletion_list[
|
158
|
+
deletion_list[klass_name] ||= []
|
145
159
|
|
146
160
|
# prevent infinite recursion here.
|
147
161
|
# - Remove any IDs that have been processed before
|
148
|
-
query_ids = query_ids - deletion_list[
|
162
|
+
query_ids = query_ids - deletion_list[klass_name]
|
149
163
|
# If ids are nil, let's find that error
|
150
164
|
if query_ids.none? #|| query_ids.nil?
|
151
165
|
# quick cleanup, if turns out was an empty class
|
152
|
-
deletion_list.delete(
|
166
|
+
deletion_list.delete(klass_name) if deletion_list[klass_name].none?
|
153
167
|
return
|
154
168
|
end
|
155
169
|
|
156
170
|
# Use-case: We have more IDs to process
|
157
171
|
# - can now safely add to the list, since we've prevented infinite recursion
|
158
|
-
deletion_list[
|
172
|
+
deletion_list[klass_name] += query_ids
|
159
173
|
|
160
174
|
# Hard to test if not sorted
|
161
175
|
# - if we had more advanced rspec matches, we could do away with this.
|
162
|
-
deletion_list[
|
176
|
+
deletion_list[klass_name].sort! if Rails.env.test?
|
163
177
|
|
164
178
|
# ignore associations that aren't a dependent destroyable type
|
165
179
|
destroy_associations = query.reflect_on_all_associations.select do |reflection|
|
@@ -209,7 +223,7 @@ module BulkDependencyEraser
|
|
209
223
|
reflection = parent_class.reflect_on_association(association_name)
|
210
224
|
reflection_type = reflection.class.name
|
211
225
|
assoc_klass = reflection.klass
|
212
|
-
|
226
|
+
assoc_klass_name = assoc_klass.name
|
213
227
|
|
214
228
|
assoc_query = assoc_klass.unscoped
|
215
229
|
|
@@ -288,10 +302,10 @@ module BulkDependencyEraser
|
|
288
302
|
# No assoc_ids, no need to add it to the nullification list
|
289
303
|
return if assoc_ids.none?
|
290
304
|
|
291
|
-
nullification_list[
|
292
|
-
nullification_list[
|
293
|
-
nullification_list[
|
294
|
-
nullification_list[
|
305
|
+
nullification_list[assoc_klass_name] ||= {}
|
306
|
+
nullification_list[assoc_klass_name][specified_foreign_key] ||= []
|
307
|
+
nullification_list[assoc_klass_name][specified_foreign_key] += assoc_ids
|
308
|
+
nullification_list[assoc_klass_name][specified_foreign_key].uniq!
|
295
309
|
else
|
296
310
|
raise "invalid parsing type: #{type}"
|
297
311
|
end
|
@@ -300,6 +314,8 @@ module BulkDependencyEraser
|
|
300
314
|
protected
|
301
315
|
|
302
316
|
attr_reader :ignore_klass_and_dependencies
|
317
|
+
attr_reader :table_names_to_parsed_klass_names
|
318
|
+
attr_reader :ignore_table_name_and_dependencies, :ignore_klass_name_and_dependencies
|
303
319
|
|
304
320
|
# A dependent assoc may be through another association. Follow the throughs to find the correct assoc to destroy.
|
305
321
|
def find_root_association_from_through_assocs klass, association_name
|