bulk_dependency_eraser 1.3.2 → 1.4.0
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c8e832cdb51105145cc3a1ba8487d32dab71b32d5a9f25808c84026d3f82b25
|
4
|
+
data.tar.gz: 556c96e6eb0a2b98aca3afbaf03ce8aad41012b793a7a45bccd4f2f459e1aaa5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b23c1429318d5c56850d7fa615956f083929751b58a72a2e584d2470feff430b4c374691cd2e436beb5890619c7d1c533d64a1fa9cd1b6aa3df7776f6c73387
|
7
|
+
data.tar.gz: 06e3bebaf98dd80af62ca714a3c8edd92548873ac84b88569d344cad64709444464197c1e2ec7febc7c74a5c981e234677b5accfb29e4936ca1a1ec06acb44d1
|
@@ -1,6 +1,12 @@
|
|
1
1
|
module BulkDependencyEraser
|
2
2
|
class Base
|
3
|
-
DEFAULT_OPTS = {
|
3
|
+
DEFAULT_OPTS = {
|
4
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
5
|
+
proc_scopes_per_class_name: {},
|
6
|
+
}.freeze
|
7
|
+
|
8
|
+
# Default Custom Scope for all classes, no effect.
|
9
|
+
DEFAULT_SCOPE_WRAPPER = ->(query) { query }
|
4
10
|
|
5
11
|
# Default Database wrapper, no effect.
|
6
12
|
DEFAULT_DB_WRAPPER = ->(block) { block.call }
|
@@ -21,6 +27,15 @@ module BulkDependencyEraser
|
|
21
27
|
|
22
28
|
protected
|
23
29
|
|
30
|
+
def custom_scope_for_klass_name(klass_name)
|
31
|
+
if opts_c.proc_scopes_per_class_name.key?(klass_name)
|
32
|
+
opts_c.proc_scopes_per_class_name[klass_name]
|
33
|
+
else
|
34
|
+
# No custom wrapper, return non-effect default
|
35
|
+
DEFAULT_SCOPE_WRAPPER
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
24
39
|
# Create options container
|
25
40
|
def options_container
|
26
41
|
Struct.new(
|
@@ -20,6 +20,10 @@ module BulkDependencyEraser
|
|
20
20
|
read_batch_size: nil,
|
21
21
|
# A specific read batching disable option
|
22
22
|
disable_read_batching: nil,
|
23
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
24
|
+
proc_scopes_per_class_name: {},
|
25
|
+
# Applied to deletion queries
|
26
|
+
reading_proc_scopes_per_class_name: {},
|
23
27
|
}.freeze
|
24
28
|
|
25
29
|
DEFAULT_DB_WRAPPER = ->(block) do
|
@@ -151,10 +155,20 @@ module BulkDependencyEraser
|
|
151
155
|
attr_reader :table_names_to_parsed_klass_names
|
152
156
|
attr_reader :ignore_table_name_and_dependencies, :ignore_klass_name_and_dependencies
|
153
157
|
|
158
|
+
def custom_scope_for_klass_name(klass_name)
|
159
|
+
if opts_c.reading_proc_scopes_per_class_name.key?(klass_name)
|
160
|
+
opts_c.reading_proc_scopes_per_class_name[klass_name]
|
161
|
+
else
|
162
|
+
super(klass_name)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
154
166
|
def pluck_from_query query, column = :id
|
155
167
|
# ordering shouldn't matter in these queries, and would slow it down
|
156
168
|
# - we're ignoring default_scope ordering, but assoc-defined ordering would still take effect
|
157
169
|
query = query.reorder('')
|
170
|
+
query = custom_scope_for_klass_name(query.klass.name).call(query)
|
171
|
+
|
158
172
|
query_ids = []
|
159
173
|
read_from_db do
|
160
174
|
# If the query has a limit, then we don't want to clobber with batching.
|
@@ -427,11 +441,8 @@ module BulkDependencyEraser
|
|
427
441
|
assoc_query = assoc_query.where(specified_foreign_key.to_sym => query_ids)
|
428
442
|
end
|
429
443
|
|
430
|
-
#
|
431
|
-
|
432
|
-
# if opts[:limit]
|
433
|
-
# assoc_query = assoc_query.limit(opts[:limit])
|
434
|
-
# end
|
444
|
+
# remove any ordering or limits imposed on the association queries from the association definitions
|
445
|
+
assoc_query = assoc_query.reorder('').unscope(:limit)
|
435
446
|
|
436
447
|
if type == :delete
|
437
448
|
# Recursively call 'deletion_query_parser' on association query, to delete any if the assoc's dependencies
|
@@ -554,6 +565,9 @@ module BulkDependencyEraser
|
|
554
565
|
specified_primary_key.to_sym => foreign_keys
|
555
566
|
)
|
556
567
|
|
568
|
+
# remove any ordering or limits imposed on the association queries from the association definitions
|
569
|
+
assoc_query = assoc_query.reorder('').unscope(:limit)
|
570
|
+
|
557
571
|
if type == :delete
|
558
572
|
# Recursively call 'deletion_query_parser' on association query, to delete any if the assoc's dependencies
|
559
573
|
deletion_query_parser(assoc_query, parent_class)
|
@@ -572,6 +586,7 @@ module BulkDependencyEraser
|
|
572
586
|
# This method will replicate association_parser, but instantiate and iterate in batches
|
573
587
|
def association_parser_belongs_to_instantiation(parent_class, query, query_ids, association_name, type)
|
574
588
|
# pending
|
589
|
+
raise "Invalid State! Not ready to instantiate!"
|
575
590
|
end
|
576
591
|
|
577
592
|
# In this case, it's like a `belongs_to :polymorphicable, polymorphic: true, dependent: :destroy` use-case
|
@@ -616,6 +631,8 @@ module BulkDependencyEraser
|
|
616
631
|
return
|
617
632
|
end
|
618
633
|
|
634
|
+
query = custom_scope_for_klass_name(query.klass.name).call(query)
|
635
|
+
|
619
636
|
foreign_ids_by_type = read_from_db do
|
620
637
|
if batching_disabled? || !query.where({}).limit_value.nil?
|
621
638
|
# query without batching
|
@@ -12,6 +12,10 @@ module BulkDependencyEraser
|
|
12
12
|
delete_batch_size: nil,
|
13
13
|
# A specific batching size for this class, overrides the batch_size
|
14
14
|
disable_delete_batching: nil,
|
15
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
16
|
+
proc_scopes_per_class_name: {},
|
17
|
+
# Applied to deletion queries
|
18
|
+
deletion_proc_scopes_per_class_name: {},
|
15
19
|
}.freeze
|
16
20
|
|
17
21
|
DEFAULT_DB_WRAPPER = ->(block) do
|
@@ -65,6 +69,14 @@ module BulkDependencyEraser
|
|
65
69
|
|
66
70
|
attr_reader :class_names_and_ids
|
67
71
|
|
72
|
+
def custom_scope_for_klass_name(klass_name)
|
73
|
+
if opts_c.deletion_proc_scopes_per_class_name.key?(klass_name)
|
74
|
+
opts_c.deletion_proc_scopes_per_class_name[klass_name]
|
75
|
+
else
|
76
|
+
super(klass_name)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
68
80
|
def batch_size
|
69
81
|
opts_c.delete_batch_size || opts_c.batch_size
|
70
82
|
end
|
@@ -75,16 +87,19 @@ module BulkDependencyEraser
|
|
75
87
|
|
76
88
|
def delete_by_klass_and_ids klass, ids
|
77
89
|
puts "Deleting #{klass.name}'s IDs: #{ids}" if opts_c.verbose
|
90
|
+
query = klass.unscoped
|
91
|
+
query = custom_scope_for_klass_name(klass.name).call(query)
|
92
|
+
|
78
93
|
if batching_disabled?
|
79
94
|
puts "Deleting without batching" if opts_c.verbose
|
80
95
|
delete_in_db do
|
81
|
-
|
96
|
+
query.where(id: ids).delete_all
|
82
97
|
end
|
83
98
|
else
|
84
99
|
puts "Deleting with batching" if opts_c.verbose
|
85
100
|
ids.each_slice(batch_size) do |ids_subset|
|
86
101
|
delete_in_db do
|
87
|
-
|
102
|
+
query.where(id: ids_subset).delete_all
|
88
103
|
end
|
89
104
|
end
|
90
105
|
end
|
@@ -14,6 +14,10 @@ module BulkDependencyEraser
|
|
14
14
|
nullify_batch_size: nil,
|
15
15
|
# A specific batching size for this class, overrides the batch_size
|
16
16
|
disable_nullify_batching: nil,
|
17
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
18
|
+
proc_scopes_per_class_name: {},
|
19
|
+
# Applied to deletion queries
|
20
|
+
nullification_proc_scopes_per_class_name: {},
|
17
21
|
}.freeze
|
18
22
|
|
19
23
|
DEFAULT_DB_WRAPPER = ->(block) do
|
@@ -123,6 +127,14 @@ module BulkDependencyEraser
|
|
123
127
|
|
124
128
|
attr_reader :class_names_columns_and_ids
|
125
129
|
|
130
|
+
def custom_scope_for_klass_name(klass_name)
|
131
|
+
if opts_c.nullification_proc_scopes_per_class_name.key?(klass_name)
|
132
|
+
opts_c.nullification_proc_scopes_per_class_name[klass_name]
|
133
|
+
else
|
134
|
+
super(klass_name)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
126
138
|
def batch_size
|
127
139
|
opts_c.nullify_batch_size || opts_c.batch_size
|
128
140
|
end
|
@@ -132,8 +144,10 @@ module BulkDependencyEraser
|
|
132
144
|
end
|
133
145
|
|
134
146
|
def nullify_by_klass_column_and_ids klass, columns, ids
|
135
|
-
|
147
|
+
query = klass.unscoped
|
148
|
+
query = custom_scope_for_klass_name(klass.name).call(query)
|
136
149
|
|
150
|
+
nullify_columns = {}
|
137
151
|
# supporting nullification of groups of columns simultaneously
|
138
152
|
if columns.is_a?(Array)
|
139
153
|
columns.each do |column|
|
@@ -145,12 +159,12 @@ module BulkDependencyEraser
|
|
145
159
|
|
146
160
|
if batching_disabled?
|
147
161
|
nullify_in_db do
|
148
|
-
|
162
|
+
query.where(id: ids).update_all(nullify_columns)
|
149
163
|
end
|
150
164
|
else
|
151
165
|
ids.each_slice(batch_size) do |ids_subset|
|
152
166
|
nullify_in_db do
|
153
|
-
|
167
|
+
query.where(id: ids_subset).update_all(nullify_columns)
|
154
168
|
end
|
155
169
|
end
|
156
170
|
end
|