bulk_dependency_eraser 1.5.0 → 2.1.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: 3466516854a6d613dae24320c55a3a5de1d921b53f8e9de06f08cde2226ff87d
4
- data.tar.gz: 34c1025b73e2410eb3dca55ff7197903a1da148ed1bad55833f8495d70900265
3
+ metadata.gz: e4bda32570c3319238a5ea03f6a71cbae1e20015caf5fe7bdba78cab3c7883a7
4
+ data.tar.gz: 9e371750cd1a05b54bfe724873daaf2c7e9aa66942f168d6417505a1427d46c1
5
5
  SHA512:
6
- metadata.gz: 67caff3857272988508f54f08aad5b8eddefb4fbd545a652e1d1cb060690b49c01270758237f6a37be4cac52a7c2f8ca43d3d543ab25d5287e0868cca9819443
7
- data.tar.gz: 366cc6941a419a0a5437422f7c22f64c712a1cf4a222638de58c22b672f5d60086aadffda673f7edc60565e34eda7bf613345d4d87593097ed24a0aef60aa592
6
+ metadata.gz: 33130d09c651a9757af826cbbd5e1363a8f136a3d6e3bf1e2a128dad776185b544fd75a493013cd28d279690094e90cc3dc0f3b23597704df9842eb80e215808
7
+ data.tar.gz: 27f4e6014155961a1286f2d1f490a7ca2709fd2b9594a7c8ab3fb3164c26bbdf99e8609c5494e15e8595b0db9eee94d485a20a772f5a37d58554b497eaee5154
@@ -94,27 +94,6 @@ module BulkDependencyEraser
94
94
  end
95
95
  end
96
96
 
97
- # Reverse order IDs
98
- # - if a class self-referenced itself the children will be deleted/nullified before parent
99
- deletion_list.keys.each do |class_name|
100
- deletion_list[class_name] = deletion_list.delete(class_name).reverse
101
-
102
- # TODO: Hard to test if not sorted
103
- deletion_list[class_name] = deletion_list[class_name].sort if Rails.env.test?
104
- end
105
-
106
- nullification_list.keys.each do |class_name|
107
- columns_and_ids = nullification_list.delete(class_name)
108
- # we don't need to reverse the column keys
109
- columns_and_ids.keys.each do |column_name|
110
- columns_and_ids[column_name] = columns_and_ids.delete(column_name).reverse
111
-
112
- # TODO: Hard to test if not sorted
113
- columns_and_ids[column_name] = columns_and_ids.delete(column_name).sort if Rails.env.test?
114
- end
115
- nullification_list[class_name] = columns_and_ids
116
- end
117
-
118
97
  return build_result
119
98
  end
120
99
 
@@ -1,7 +1,29 @@
1
1
  module BulkDependencyEraser
2
2
  class Deleter < Base
3
+ DEFAULT_DB_DELETE_ALL_WRAPPER = ->(block) do
4
+ begin
5
+ block.call
6
+ rescue StandardError => e
7
+ report_error("Issue attempting to delete '#{current_class_name}': #{e.class.name} - #{e.message}")
8
+ end
9
+ end
10
+
3
11
  DEFAULT_OPTS = {
4
12
  verbose: false,
13
+ # Runs once, all deletions occur within it
14
+ # - useful if you wanted to implement a rollback:
15
+ # - i.e:
16
+ # db_delete_all_wrapper: lambda do |block|
17
+ # ActiveRecord::Base.transaction do
18
+ # begin
19
+ # block.call
20
+ # rescue StandardError => e
21
+ # report_error("Issue attempting to delete '#{current_class_name}': #{e.class.name} - #{e.message}")
22
+ # raise ActiveRecord::Rollback
23
+ # end
24
+ # end
25
+ # end
26
+ db_delete_all_wrapper: self::DEFAULT_DB_DELETE_ALL_WRAPPER,
5
27
  db_delete_wrapper: self::DEFAULT_DB_WRITE_WRAPPER,
6
28
  # Set to true if you want 'ActiveRecord::InvalidForeignKey' errors raised during deletions
7
29
  enable_invalid_foreign_key_detection: false,
@@ -36,31 +58,25 @@ module BulkDependencyEraser
36
58
  puts "ActiveRecord::Base.connection.disable_referential_integrity - disabled!"
37
59
  end
38
60
 
39
- ActiveRecord::Base.transaction do
40
- current_class_name = 'N/A'
41
- begin
42
- # IDs should have already been reversed in builder
43
- class_names_and_ids.keys.reverse.each do |class_name|
44
- current_class_name = class_name
45
- ids = class_names_and_ids[class_name]
46
- klass = class_name.constantize
61
+ current_class_name = 'N/A'
62
+ delete_all_in_db do
63
+ class_names_and_ids.keys.reverse.each do |class_name|
64
+ current_class_name = class_name
65
+ ids = class_names_and_ids[class_name].reverse
66
+ klass = class_name.constantize
47
67
 
48
- if opts_c.enable_invalid_foreign_key_detection
49
- # delete with referential integrity
68
+ if opts_c.enable_invalid_foreign_key_detection
69
+ # delete with referential integrity
70
+ delete_by_klass_and_ids(klass, ids)
71
+ else
72
+ # delete without referential integrity
73
+ # Disable any ActiveRecord::InvalidForeignKey raised errors.
74
+ # - src: https://stackoverflow.com/questions/41005849/rails-migrations-temporarily-ignore-foreign-key-constraint
75
+ # https://apidock.com/rails/ActiveRecord/ConnectionAdapters/AbstractAdapter/disable_referential_integrity
76
+ ActiveRecord::Base.connection.disable_referential_integrity do
50
77
  delete_by_klass_and_ids(klass, ids)
51
- else
52
- # delete without referential integrity
53
- # Disable any ActiveRecord::InvalidForeignKey raised errors.
54
- # - src: https://stackoverflow.com/questions/41005849/rails-migrations-temporarily-ignore-foreign-key-constraint
55
- # https://apidock.com/rails/ActiveRecord/ConnectionAdapters/AbstractAdapter/disable_referential_integrity
56
- ActiveRecord::Base.connection.disable_referential_integrity do
57
- delete_by_klass_and_ids(klass, ids)
58
- end
59
78
  end
60
79
  end
61
- rescue StandardError => e
62
- report_error("Issue attempting to delete '#{current_class_name}': #{e.class.name} - #{e.message}")
63
- raise ActiveRecord::Rollback
64
80
  end
65
81
  end
66
82
 
@@ -119,6 +135,13 @@ module BulkDependencyEraser
119
135
  def delete_in_db(&block)
120
136
  puts "Deleting from DB..." if opts_c.verbose
121
137
  opts_c.db_delete_wrapper.call(block)
138
+ puts "Deleting from DB complete." if opts_c.verbose
139
+ end
140
+
141
+ def delete_all_in_db(&block)
142
+ puts "Deleting all from DB..." if opts_c.verbose
143
+ opts_c.db_delete_all_wrapper.call(block)
144
+ puts "Deleting all from DB complete." if opts_c.verbose
122
145
  end
123
146
  end
124
147
  end
@@ -1,7 +1,29 @@
1
1
  module BulkDependencyEraser
2
2
  class Nullifier < Base
3
+ DEFAULT_DB_NULLIFY_ALL_WRAPPER = lambda do |block|
4
+ begin
5
+ block.call
6
+ rescue StandardError => e
7
+ report_error("Issue attempting to nullify '#{current_class_name}' column '#{current_column}': #{e.class.name} - #{e.message}")
8
+ end
9
+ end
10
+
3
11
  DEFAULT_OPTS = {
4
12
  verbose: false,
13
+ # Runs once, all deletions occur within it
14
+ # - useful if you wanted to implement a rollback:
15
+ # - i.e:
16
+ # db_nullify_all_wrapper: lambda do |block|
17
+ # ActiveRecord::Base.transaction do
18
+ # begin
19
+ # block.call
20
+ # rescue StandardError => e
21
+ # report_error("Issue attempting to nullify '#{current_class_name}': #{e.class.name} - #{e.message}")
22
+ # raise ActiveRecord::Rollback
23
+ # end
24
+ # end
25
+ # end
26
+ db_nullify_all_wrapper: self::DEFAULT_DB_NULLIFY_ALL_WRAPPER,
5
27
  db_nullify_wrapper: self::DEFAULT_DB_WRITE_WRAPPER,
6
28
  # Set to true if you want 'ActiveRecord::InvalidForeignKey' errors raised during nullifications
7
29
  # - I can't think of a use-case where a nullification would generate an invalid key error
@@ -89,36 +111,33 @@ module BulkDependencyEraser
89
111
  end
90
112
 
91
113
  def execute
92
- ActiveRecord::Base.transaction do
93
- current_class_name = 'N/A'
94
- current_column = 'N/A'
95
- begin
96
- # column_and_ids should have already been reversed in builder
97
- class_names_columns_and_ids.keys.reverse.each do |class_name|
98
- current_class_name = class_name
99
- klass = class_name.constantize
100
- columns_and_ids = class_names_columns_and_ids[class_name]
101
-
102
- columns_and_ids.each do |column, ids|
103
- current_column = column
104
-
105
- if opts_c.enable_invalid_foreign_key_detection
106
- # nullify with referential integrity
114
+ current_class_name = 'N/A'
115
+ current_column = 'N/A'
116
+ nullify_all_in_db do
117
+ # column_and_ids should have already been reversed in builder
118
+ class_names_columns_and_ids.keys.reverse.each do |class_name|
119
+ current_class_name = class_name
120
+ klass = class_name.constantize
121
+ columns_and_ids = class_names_columns_and_ids[class_name]
122
+
123
+ columns_and_ids.each do |column, ids|
124
+ current_column = column
125
+ # Reversing IDs. Last ones in are more likely to be dependencies, and should be deleted first.
126
+ ids = ids.reverse
127
+
128
+ if opts_c.enable_invalid_foreign_key_detection
129
+ # nullify with referential integrity
130
+ nullify_by_klass_column_and_ids(klass, column, ids)
131
+ else
132
+ # nullify without referential integrity
133
+ # Disable any ActiveRecord::InvalidForeignKey raised errors.
134
+ # - src: https://stackoverflow.com/questions/41005849/rails-migrations-temporarily-ignore-foreign-key-constraint
135
+ # https://apidock.com/rails/ActiveRecord/ConnectionAdapters/AbstractAdapter/disable_referential_integrity
136
+ ActiveRecord::Base.connection.disable_referential_integrity do
107
137
  nullify_by_klass_column_and_ids(klass, column, ids)
108
- else
109
- # nullify without referential integrity
110
- # Disable any ActiveRecord::InvalidForeignKey raised errors.
111
- # - src: https://stackoverflow.com/questions/41005849/rails-migrations-temporarily-ignore-foreign-key-constraint
112
- # https://apidock.com/rails/ActiveRecord/ConnectionAdapters/AbstractAdapter/disable_referential_integrity
113
- ActiveRecord::Base.connection.disable_referential_integrity do
114
- nullify_by_klass_column_and_ids(klass, column, ids)
115
- end
116
138
  end
117
139
  end
118
140
  end
119
- rescue StandardError => e
120
- report_error("Issue attempting to nullify '#{current_class_name}' column '#{current_column}': #{e.class.name} - #{e.message}")
121
- raise ActiveRecord::Rollback
122
141
  end
123
142
  end
124
143
 
@@ -146,6 +165,9 @@ module BulkDependencyEraser
146
165
  opts_c.disable_nullify_batching.nil? ? opts_c.disable_batching : opts_c.disable_nullify_batching
147
166
  end
148
167
 
168
+ # @param klass [ActiveRecord::Base]
169
+ # @param columns [Symbol | String | Array<String | Symbol>]
170
+ # @param ids [Array[String | Integer]]
149
171
  def nullify_by_klass_column_and_ids klass, columns, ids
150
172
  query = klass.unscoped
151
173
  query = custom_scope_for_query(query)
@@ -184,6 +206,13 @@ module BulkDependencyEraser
184
206
  def nullify_in_db(&block)
185
207
  puts "Nullifying from DB..." if opts_c.verbose
186
208
  opts_c.db_nullify_wrapper.call(block)
209
+ puts "Nullifying from DB complete." if opts_c.verbose
210
+ end
211
+
212
+ def nullify_all_in_db(&block)
213
+ puts "Nullifying all from DB..." if opts_c.verbose
214
+ opts_c.db_nullify_all_wrapper.call(block)
215
+ puts "Nullifying all from DB complete." if opts_c.verbose
187
216
  end
188
217
 
189
218
  end
@@ -1,3 +1,3 @@
1
1
  module BulkDependencyEraser
2
- VERSION = "1.5.0".freeze
2
+ VERSION = "2.1.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.5.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjamin.dana.software.dev@gmail.com