bulk_dependency_eraser 1.4.1 → 1.4.3
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: a505cd8d1072193bb45a5896b9ec1882793c030552ff4a992f06f48b446deca9
|
4
|
+
data.tar.gz: 0061d59c8ccd8abe86eedf0bd78e7fb57f6ed2b14e03fd0ad2c247c136c1ffef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6363cea019a2c86478e3ba5c79c6348db9868c64c40a02a326849827e404bae1542bb3c9686f1ad258717a432c52706b3ce2cbde58e820e520604743c7e7b12
|
7
|
+
data.tar.gz: 1af86850bdacf28110bfa80236ee188c8a5995e92214ee6e4e099696870385f3d62c504031501039747ac4c03cc43b95af7cb24aaae22825c6630e7740c143e4
|
@@ -5,6 +5,28 @@ module BulkDependencyEraser
|
|
5
5
|
# Default Custom Scope for mapped-by-name classes, no effect.
|
6
6
|
DEFAULT_KLASS_MAPPED_SCOPE_WRAPPER = ->(query) { query }
|
7
7
|
|
8
|
+
DEFAULT_DB_READ_WRAPPER = ->(block) {
|
9
|
+
begin
|
10
|
+
ActiveRecord::Base.connected_to(role: :reading) do
|
11
|
+
block.call
|
12
|
+
end
|
13
|
+
rescue ActiveRecord::ConnectionNotEstablished
|
14
|
+
# No role: :reading setup, use regular connection
|
15
|
+
block.call
|
16
|
+
end
|
17
|
+
}
|
18
|
+
DEFAULT_DB_WRITE_WRAPPER = ->(block) {
|
19
|
+
begin
|
20
|
+
ActiveRecord::Base.connected_to(role: :writing) do
|
21
|
+
block.call
|
22
|
+
end
|
23
|
+
rescue ActiveRecord::ConnectionNotEstablished
|
24
|
+
# No role: :writing setup, use regular connection
|
25
|
+
block.call
|
26
|
+
end
|
27
|
+
}
|
28
|
+
DEFAULT_DB_BLANK_WRAPPER = ->(block) { block.call }
|
29
|
+
|
8
30
|
DEFAULT_OPTS = {
|
9
31
|
# Applied to all queries. Useful for taking advantage of specific indexes
|
10
32
|
# - not indexed by klass name. Proc would handle the logic for that.
|
@@ -17,10 +39,6 @@ module BulkDependencyEraser
|
|
17
39
|
proc_scopes_per_class_name: {},
|
18
40
|
}.freeze
|
19
41
|
|
20
|
-
|
21
|
-
# Default Database wrapper, no effect.
|
22
|
-
DEFAULT_DB_WRAPPER = ->(block) { block.call }
|
23
|
-
|
24
42
|
attr_reader :errors
|
25
43
|
|
26
44
|
def initialize opts: {}
|
@@ -37,17 +55,18 @@ module BulkDependencyEraser
|
|
37
55
|
|
38
56
|
protected
|
39
57
|
|
40
|
-
def
|
58
|
+
def custom_scope_for_query(query)
|
59
|
+
klass = query.klass
|
41
60
|
if opts_c.proc_scopes_per_class_name.key?(klass.name)
|
42
|
-
opts_c.proc_scopes_per_class_name[klass.name]
|
61
|
+
opts_c.proc_scopes_per_class_name[klass.name].call(query)
|
43
62
|
else
|
44
63
|
# See if non-class-mapped proc returns a value
|
45
|
-
non_class_name_mapped_query = opts_c.proc_scopes.call(
|
64
|
+
non_class_name_mapped_query = opts_c.proc_scopes.call(query)
|
46
65
|
if !non_class_name_mapped_query.nil?
|
47
|
-
return
|
66
|
+
return non_class_name_mapped_query
|
48
67
|
else
|
49
68
|
# No custom wrapper, return non-effect default
|
50
|
-
return self.class::DEFAULT_KLASS_MAPPED_SCOPE_WRAPPER
|
69
|
+
return self.class::DEFAULT_KLASS_MAPPED_SCOPE_WRAPPER.call(query)
|
51
70
|
end
|
52
71
|
end
|
53
72
|
end
|
@@ -7,7 +7,7 @@ module BulkDependencyEraser
|
|
7
7
|
# - We would have to instantiate if we wanted to apply that scope filter.
|
8
8
|
instantiate_if_assoc_scope_with_arity: false,
|
9
9
|
# wraps around the DB reading
|
10
|
-
db_read_wrapper: self::
|
10
|
+
db_read_wrapper: self::DEFAULT_DB_READ_WRAPPER,
|
11
11
|
# Will parse these tables and their dependencies, but will remove the tables from the lists after parsing.
|
12
12
|
ignore_tables: [],
|
13
13
|
# Won't parse any table in this list
|
@@ -34,12 +34,6 @@ module BulkDependencyEraser
|
|
34
34
|
reading_proc_scopes_per_class_name: {},
|
35
35
|
}.freeze
|
36
36
|
|
37
|
-
DEFAULT_DB_WRAPPER = ->(block) do
|
38
|
-
ActiveRecord::Base.connected_to(role: :reading) do
|
39
|
-
block.call
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
37
|
DEPENDENCY_NULLIFY = %i[
|
44
38
|
nullify
|
45
39
|
].freeze
|
@@ -163,11 +157,12 @@ module BulkDependencyEraser
|
|
163
157
|
attr_reader :table_names_to_parsed_klass_names
|
164
158
|
attr_reader :ignore_table_name_and_dependencies, :ignore_klass_name_and_dependencies
|
165
159
|
|
166
|
-
def
|
160
|
+
def custom_scope_for_query(query)
|
161
|
+
klass = query.klass
|
167
162
|
if opts_c.reading_proc_scopes_per_class_name.key?(klass.name)
|
168
|
-
opts_c.reading_proc_scopes_per_class_name[klass.name]
|
163
|
+
opts_c.reading_proc_scopes_per_class_name[klass.name].call(query)
|
169
164
|
else
|
170
|
-
super(
|
165
|
+
super(query)
|
171
166
|
end
|
172
167
|
end
|
173
168
|
|
@@ -175,7 +170,7 @@ module BulkDependencyEraser
|
|
175
170
|
# ordering shouldn't matter in these queries, and would slow it down
|
176
171
|
# - we're ignoring default_scope ordering, but assoc-defined ordering would still take effect
|
177
172
|
query = query.reorder('')
|
178
|
-
query =
|
173
|
+
query = custom_scope_for_query(query)
|
179
174
|
|
180
175
|
query_ids = []
|
181
176
|
read_from_db do
|
@@ -639,7 +634,7 @@ module BulkDependencyEraser
|
|
639
634
|
return
|
640
635
|
end
|
641
636
|
|
642
|
-
query =
|
637
|
+
query = custom_scope_for_query(query)
|
643
638
|
|
644
639
|
foreign_ids_by_type = read_from_db do
|
645
640
|
if batching_disabled? || !query.where({}).limit_value.nil?
|
@@ -2,7 +2,7 @@ module BulkDependencyEraser
|
|
2
2
|
class Deleter < Base
|
3
3
|
DEFAULT_OPTS = {
|
4
4
|
verbose: false,
|
5
|
-
db_delete_wrapper: self::
|
5
|
+
db_delete_wrapper: self::DEFAULT_DB_WRITE_WRAPPER,
|
6
6
|
# Set to true if you want 'ActiveRecord::InvalidForeignKey' errors raised during deletions
|
7
7
|
enable_invalid_foreign_key_detection: false,
|
8
8
|
disable_batching: false,
|
@@ -26,12 +26,6 @@ module BulkDependencyEraser
|
|
26
26
|
deletion_proc_scopes_per_class_name: {},
|
27
27
|
}.freeze
|
28
28
|
|
29
|
-
DEFAULT_DB_WRAPPER = ->(block) do
|
30
|
-
ActiveRecord::Base.connected_to(role: :writing) do
|
31
|
-
block.call
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
29
|
def initialize class_names_and_ids: {}, opts: {}
|
36
30
|
@class_names_and_ids = class_names_and_ids
|
37
31
|
super(opts:)
|
@@ -77,11 +71,12 @@ module BulkDependencyEraser
|
|
77
71
|
|
78
72
|
attr_reader :class_names_and_ids
|
79
73
|
|
80
|
-
def
|
74
|
+
def custom_scope_for_query(query)
|
75
|
+
klass = query.klass
|
81
76
|
if opts_c.deletion_proc_scopes_per_class_name.key?(klass.name)
|
82
|
-
opts_c.deletion_proc_scopes_per_class_name[klass.name]
|
77
|
+
opts_c.deletion_proc_scopes_per_class_name[klass.name].call(query)
|
83
78
|
else
|
84
|
-
super(
|
79
|
+
super(query)
|
85
80
|
end
|
86
81
|
end
|
87
82
|
|
@@ -96,7 +91,7 @@ module BulkDependencyEraser
|
|
96
91
|
def delete_by_klass_and_ids klass, ids
|
97
92
|
puts "Deleting #{klass.name}'s IDs: #{ids}" if opts_c.verbose
|
98
93
|
query = klass.unscoped
|
99
|
-
query =
|
94
|
+
query = custom_scope_for_query(query)
|
100
95
|
|
101
96
|
if batching_disabled?
|
102
97
|
puts "Deleting without batching" if opts_c.verbose
|
@@ -2,7 +2,7 @@ module BulkDependencyEraser
|
|
2
2
|
class Nullifier < Base
|
3
3
|
DEFAULT_OPTS = {
|
4
4
|
verbose: false,
|
5
|
-
db_nullify_wrapper: self::
|
5
|
+
db_nullify_wrapper: self::DEFAULT_DB_WRITE_WRAPPER,
|
6
6
|
# Set to true if you want 'ActiveRecord::InvalidForeignKey' errors raised during nullifications
|
7
7
|
# - I can't think of a use-case where a nullification would generate an invalid key error
|
8
8
|
# - Not hurting anything to leave it in, but might remove it in the future.
|
@@ -28,12 +28,6 @@ module BulkDependencyEraser
|
|
28
28
|
nullification_proc_scopes_per_class_name: {},
|
29
29
|
}.freeze
|
30
30
|
|
31
|
-
DEFAULT_DB_WRAPPER = ->(block) do
|
32
|
-
ActiveRecord::Base.connected_to(role: :writing) do
|
33
|
-
block.call
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
31
|
# @param class_names_columns_and_ids [Hash] - model names with columns to nullify pointing towards the record IDs that require the nullification.
|
38
32
|
# - structure:
|
39
33
|
# {
|
@@ -135,11 +129,12 @@ module BulkDependencyEraser
|
|
135
129
|
|
136
130
|
attr_reader :class_names_columns_and_ids
|
137
131
|
|
138
|
-
def
|
132
|
+
def custom_scope_for_query(query)
|
133
|
+
klass = query.klass
|
139
134
|
if opts_c.nullification_proc_scopes_per_class_name.key?(klass.name)
|
140
|
-
opts_c.nullification_proc_scopes_per_class_name[klass.name]
|
135
|
+
opts_c.nullification_proc_scopes_per_class_name[klass.name].call(query)
|
141
136
|
else
|
142
|
-
super(
|
137
|
+
super(query)
|
143
138
|
end
|
144
139
|
end
|
145
140
|
|
@@ -153,7 +148,7 @@ module BulkDependencyEraser
|
|
153
148
|
|
154
149
|
def nullify_by_klass_column_and_ids klass, columns, ids
|
155
150
|
query = klass.unscoped
|
156
|
-
query =
|
151
|
+
query = custom_scope_for_query(query)
|
157
152
|
|
158
153
|
nullify_columns = {}
|
159
154
|
# supporting nullification of groups of columns simultaneously
|