bulk_dependency_eraser 1.3.3 → 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: bb8d638145b6ff810e2cb3deb79c11a134b9df5e6abdc25755a166d92158c583
4
- data.tar.gz: 0f7525a93ddb69db0b0fdb1243973c2a1db1db1d33051fa3fe49e1aaefdb538d
3
+ metadata.gz: 9c8e832cdb51105145cc3a1ba8487d32dab71b32d5a9f25808c84026d3f82b25
4
+ data.tar.gz: 556c96e6eb0a2b98aca3afbaf03ce8aad41012b793a7a45bccd4f2f459e1aaa5
5
5
  SHA512:
6
- metadata.gz: 8841e5385fdd92b2d1c14315d849df28a4d8af00a7d8f151a4e04767fd27a9b1cd1653d269912a23385520cb7eb18f8e4b483ab74c7f9a0a5249048fbb81f20b
7
- data.tar.gz: c0701a14385970abc0ac5ec0025215a11c5818d391d2bcbef89c36e47c8f7a7919a6dbded77521efdb60108bdb88266ef21268e743265399d58c9a7c4044f648
6
+ metadata.gz: 8b23c1429318d5c56850d7fa615956f083929751b58a72a2e584d2470feff430b4c374691cd2e436beb5890619c7d1c533d64a1fa9cd1b6aa3df7776f6c73387
7
+ data.tar.gz: 06e3bebaf98dd80af62ca714a3c8edd92548873ac84b88569d344cad64709444464197c1e2ec7febc7c74a5c981e234677b5accfb29e4936ca1a1ec06acb44d1
@@ -1,6 +1,12 @@
1
1
  module BulkDependencyEraser
2
2
  class Base
3
- DEFAULT_OPTS = {}.freeze
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.
@@ -617,6 +631,8 @@ module BulkDependencyEraser
617
631
  return
618
632
  end
619
633
 
634
+ query = custom_scope_for_klass_name(query.klass.name).call(query)
635
+
620
636
  foreign_ids_by_type = read_from_db do
621
637
  if batching_disabled? || !query.where({}).limit_value.nil?
622
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
- klass.unscoped.where(id: ids).delete_all
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
- klass.unscoped.where(id: ids_subset).delete_all
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
- nullify_columns = {}
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
- klass.unscoped.where(id: ids).update_all(nullify_columns)
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
- klass.unscoped.where(id: ids_subset).update_all(nullify_columns)
167
+ query.where(id: ids_subset).update_all(nullify_columns)
154
168
  end
155
169
  end
156
170
  end
@@ -1,3 +1,3 @@
1
1
  module BulkDependencyEraser
2
- VERSION = "1.3.3".freeze
2
+ VERSION = "1.4.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulk_dependency_eraser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjamin.dana.software.dev@gmail.com