bulk_dependency_eraser 1.3.3 → 1.4.1
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: 5258d3b15269fb6bd51a5f89f949983a3cf48862cc83f26a8f189bbc11e2ae84
|
4
|
+
data.tar.gz: dbab67950500c1037266232592915a3c27f0b54102619092be465ded4a53024d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 351634d20e814264885ddd4746e2fbaa7a8d27c4ab78fda5d3690e21697de51d1fd51c75d2b749e6f9a4727f79e6fa43d571617364df1e44870a55aeb0e88166
|
7
|
+
data.tar.gz: e8e477e1d5139bd4ac38cb3d4e4371b06688b5dc5e000d8a6a6077cc300ec998916cdd754c00d87ea1efd0b0a179479c60491450467cfc8eefbb6b0bda391690
|
@@ -1,6 +1,22 @@
|
|
1
1
|
module BulkDependencyEraser
|
2
2
|
class Base
|
3
|
-
|
3
|
+
# Default Custom Scope for all classes, no effect.
|
4
|
+
DEFAULT_SCOPE_WRAPPER = ->(query) { nil }
|
5
|
+
# Default Custom Scope for mapped-by-name classes, no effect.
|
6
|
+
DEFAULT_KLASS_MAPPED_SCOPE_WRAPPER = ->(query) { query }
|
7
|
+
|
8
|
+
DEFAULT_OPTS = {
|
9
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
10
|
+
# - not indexed by klass name. Proc would handle the logic for that.
|
11
|
+
# - 3rd, and lowest, priority of scopes
|
12
|
+
# - accepts rails query as parameter
|
13
|
+
# - return nil if no applicable scope.
|
14
|
+
proc_scopes: self::DEFAULT_SCOPE_WRAPPER,
|
15
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
16
|
+
# - 2nd highest priority of scopes
|
17
|
+
proc_scopes_per_class_name: {},
|
18
|
+
}.freeze
|
19
|
+
|
4
20
|
|
5
21
|
# Default Database wrapper, no effect.
|
6
22
|
DEFAULT_DB_WRAPPER = ->(block) { block.call }
|
@@ -21,6 +37,21 @@ module BulkDependencyEraser
|
|
21
37
|
|
22
38
|
protected
|
23
39
|
|
40
|
+
def custom_scope_for_klass_name(klass)
|
41
|
+
if opts_c.proc_scopes_per_class_name.key?(klass.name)
|
42
|
+
opts_c.proc_scopes_per_class_name[klass.name]
|
43
|
+
else
|
44
|
+
# See if non-class-mapped proc returns a value
|
45
|
+
non_class_name_mapped_query = opts_c.proc_scopes.call(klass.where({})) # convert klass to query
|
46
|
+
if !non_class_name_mapped_query.nil?
|
47
|
+
return opts_c.proc_scopes
|
48
|
+
else
|
49
|
+
# No custom wrapper, return non-effect default
|
50
|
+
return self.class::DEFAULT_KLASS_MAPPED_SCOPE_WRAPPER
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
24
55
|
# Create options container
|
25
56
|
def options_container
|
26
57
|
Struct.new(
|
@@ -20,6 +20,18 @@ 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
|
+
# - not indexed by klass name. Proc would handle the logic for that.
|
25
|
+
# - 3rd, and lowest, priority of scopes
|
26
|
+
# - accepts rails query as parameter
|
27
|
+
# - return nil if no applicable scope.
|
28
|
+
proc_scopes: self::DEFAULT_SCOPE_WRAPPER,
|
29
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
30
|
+
# - 2nd highest priority of scopes
|
31
|
+
proc_scopes_per_class_name: {},
|
32
|
+
# Applied to reading queries
|
33
|
+
# - 1st priority of scopes
|
34
|
+
reading_proc_scopes_per_class_name: {},
|
23
35
|
}.freeze
|
24
36
|
|
25
37
|
DEFAULT_DB_WRAPPER = ->(block) do
|
@@ -151,10 +163,20 @@ module BulkDependencyEraser
|
|
151
163
|
attr_reader :table_names_to_parsed_klass_names
|
152
164
|
attr_reader :ignore_table_name_and_dependencies, :ignore_klass_name_and_dependencies
|
153
165
|
|
166
|
+
def custom_scope_for_klass_name(klass)
|
167
|
+
if opts_c.reading_proc_scopes_per_class_name.key?(klass.name)
|
168
|
+
opts_c.reading_proc_scopes_per_class_name[klass.name]
|
169
|
+
else
|
170
|
+
super(klass)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
154
174
|
def pluck_from_query query, column = :id
|
155
175
|
# ordering shouldn't matter in these queries, and would slow it down
|
156
176
|
# - we're ignoring default_scope ordering, but assoc-defined ordering would still take effect
|
157
177
|
query = query.reorder('')
|
178
|
+
query = custom_scope_for_klass_name(query.klass).call(query)
|
179
|
+
|
158
180
|
query_ids = []
|
159
181
|
read_from_db do
|
160
182
|
# If the query has a limit, then we don't want to clobber with batching.
|
@@ -617,6 +639,8 @@ module BulkDependencyEraser
|
|
617
639
|
return
|
618
640
|
end
|
619
641
|
|
642
|
+
query = custom_scope_for_klass_name(query.klass).call(query)
|
643
|
+
|
620
644
|
foreign_ids_by_type = read_from_db do
|
621
645
|
if batching_disabled? || !query.where({}).limit_value.nil?
|
622
646
|
# query without batching
|
@@ -12,6 +12,18 @@ 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
|
+
# - not indexed by klass name. Proc would handle the logic for that.
|
17
|
+
# - 3rd, and lowest, priority of scopes
|
18
|
+
# - accepts rails query as parameter
|
19
|
+
# - return nil if no applicable scope.
|
20
|
+
proc_scopes: self::DEFAULT_SCOPE_WRAPPER,
|
21
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
22
|
+
# - 2nd highest priority of scopes
|
23
|
+
proc_scopes_per_class_name: {},
|
24
|
+
# Applied to deletion queries
|
25
|
+
# - 1st priority of scopes
|
26
|
+
deletion_proc_scopes_per_class_name: {},
|
15
27
|
}.freeze
|
16
28
|
|
17
29
|
DEFAULT_DB_WRAPPER = ->(block) do
|
@@ -65,6 +77,14 @@ module BulkDependencyEraser
|
|
65
77
|
|
66
78
|
attr_reader :class_names_and_ids
|
67
79
|
|
80
|
+
def custom_scope_for_klass_name(klass)
|
81
|
+
if opts_c.deletion_proc_scopes_per_class_name.key?(klass.name)
|
82
|
+
opts_c.deletion_proc_scopes_per_class_name[klass.name]
|
83
|
+
else
|
84
|
+
super(klass)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
68
88
|
def batch_size
|
69
89
|
opts_c.delete_batch_size || opts_c.batch_size
|
70
90
|
end
|
@@ -75,16 +95,19 @@ module BulkDependencyEraser
|
|
75
95
|
|
76
96
|
def delete_by_klass_and_ids klass, ids
|
77
97
|
puts "Deleting #{klass.name}'s IDs: #{ids}" if opts_c.verbose
|
98
|
+
query = klass.unscoped
|
99
|
+
query = custom_scope_for_klass_name(klass).call(query)
|
100
|
+
|
78
101
|
if batching_disabled?
|
79
102
|
puts "Deleting without batching" if opts_c.verbose
|
80
103
|
delete_in_db do
|
81
|
-
|
104
|
+
query.where(id: ids).delete_all
|
82
105
|
end
|
83
106
|
else
|
84
107
|
puts "Deleting with batching" if opts_c.verbose
|
85
108
|
ids.each_slice(batch_size) do |ids_subset|
|
86
109
|
delete_in_db do
|
87
|
-
|
110
|
+
query.where(id: ids_subset).delete_all
|
88
111
|
end
|
89
112
|
end
|
90
113
|
end
|
@@ -14,6 +14,18 @@ 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
|
+
# - not indexed by klass name. Proc would handle the logic for that.
|
19
|
+
# - 3rd, and lowest, priority of scopes
|
20
|
+
# - accepts rails query as parameter
|
21
|
+
# - return nil if no applicable scope.
|
22
|
+
proc_scopes: self::DEFAULT_SCOPE_WRAPPER,
|
23
|
+
# Applied to all queries. Useful for taking advantage of specific indexes
|
24
|
+
# - 2nd highest priority of scopes
|
25
|
+
proc_scopes_per_class_name: {},
|
26
|
+
# Applied to nullification queries
|
27
|
+
# - 1st priority of scopes
|
28
|
+
nullification_proc_scopes_per_class_name: {},
|
17
29
|
}.freeze
|
18
30
|
|
19
31
|
DEFAULT_DB_WRAPPER = ->(block) do
|
@@ -123,6 +135,14 @@ module BulkDependencyEraser
|
|
123
135
|
|
124
136
|
attr_reader :class_names_columns_and_ids
|
125
137
|
|
138
|
+
def custom_scope_for_klass_name(klass)
|
139
|
+
if opts_c.nullification_proc_scopes_per_class_name.key?(klass.name)
|
140
|
+
opts_c.nullification_proc_scopes_per_class_name[klass.name]
|
141
|
+
else
|
142
|
+
super(klass)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
126
146
|
def batch_size
|
127
147
|
opts_c.nullify_batch_size || opts_c.batch_size
|
128
148
|
end
|
@@ -132,8 +152,10 @@ module BulkDependencyEraser
|
|
132
152
|
end
|
133
153
|
|
134
154
|
def nullify_by_klass_column_and_ids klass, columns, ids
|
135
|
-
|
155
|
+
query = klass.unscoped
|
156
|
+
query = custom_scope_for_klass_name(klass).call(query)
|
136
157
|
|
158
|
+
nullify_columns = {}
|
137
159
|
# supporting nullification of groups of columns simultaneously
|
138
160
|
if columns.is_a?(Array)
|
139
161
|
columns.each do |column|
|
@@ -145,12 +167,12 @@ module BulkDependencyEraser
|
|
145
167
|
|
146
168
|
if batching_disabled?
|
147
169
|
nullify_in_db do
|
148
|
-
|
170
|
+
query.where(id: ids).update_all(nullify_columns)
|
149
171
|
end
|
150
172
|
else
|
151
173
|
ids.each_slice(batch_size) do |ids_subset|
|
152
174
|
nullify_in_db do
|
153
|
-
|
175
|
+
query.where(id: ids_subset).update_all(nullify_columns)
|
154
176
|
end
|
155
177
|
end
|
156
178
|
end
|