invalid_record_finder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5ef7639bb7c4e24b75291c9ddaf3561283b190c490379a51e8a3e35d1f927a74
4
+ data.tar.gz: e1a2c6a9b544fb3de431f5b9e7f003a416f466f21e17a3a80bff225aefa40177
5
+ SHA512:
6
+ metadata.gz: d6faecd6d67a3cd6a11cb6a008621857ecda7eaf76aa881180c294be6ee578e621e19114a79def11052214e4d4360d14a33a1530177648228d339ab6c2844df4
7
+ data.tar.gz: 326e337b4b2ce97d3534a3c7e0071a97f17cd2cd474d32d8ad5f8a6f3f3e4852bb99793fc1632613f0c07993fba6618a1aaac3ce8ec7fa463166c556389936b0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2021-11-24
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in invalid_record_finder.gemspec
4
+ gemspec
5
+
6
+ gem 'rails', '>= 5.0'
7
+ gem 'rake', '~> 13.0'
8
+ gem 'rspec', '~> 3.10'
9
+ gem 'sqlite3'
data/Gemfile.lock ADDED
@@ -0,0 +1,174 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ invalid_record_finder (1.0.0)
5
+ activerecord (>= 5.0)
6
+ fortschritt (~> 0.3)
7
+ tabulo (~> 2.7)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (6.1.4.1)
13
+ actionpack (= 6.1.4.1)
14
+ activesupport (= 6.1.4.1)
15
+ nio4r (~> 2.0)
16
+ websocket-driver (>= 0.6.1)
17
+ actionmailbox (6.1.4.1)
18
+ actionpack (= 6.1.4.1)
19
+ activejob (= 6.1.4.1)
20
+ activerecord (= 6.1.4.1)
21
+ activestorage (= 6.1.4.1)
22
+ activesupport (= 6.1.4.1)
23
+ mail (>= 2.7.1)
24
+ actionmailer (6.1.4.1)
25
+ actionpack (= 6.1.4.1)
26
+ actionview (= 6.1.4.1)
27
+ activejob (= 6.1.4.1)
28
+ activesupport (= 6.1.4.1)
29
+ mail (~> 2.5, >= 2.5.4)
30
+ rails-dom-testing (~> 2.0)
31
+ actionpack (6.1.4.1)
32
+ actionview (= 6.1.4.1)
33
+ activesupport (= 6.1.4.1)
34
+ rack (~> 2.0, >= 2.0.9)
35
+ rack-test (>= 0.6.3)
36
+ rails-dom-testing (~> 2.0)
37
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
38
+ actiontext (6.1.4.1)
39
+ actionpack (= 6.1.4.1)
40
+ activerecord (= 6.1.4.1)
41
+ activestorage (= 6.1.4.1)
42
+ activesupport (= 6.1.4.1)
43
+ nokogiri (>= 1.8.5)
44
+ actionview (6.1.4.1)
45
+ activesupport (= 6.1.4.1)
46
+ builder (~> 3.1)
47
+ erubi (~> 1.4)
48
+ rails-dom-testing (~> 2.0)
49
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
50
+ activejob (6.1.4.1)
51
+ activesupport (= 6.1.4.1)
52
+ globalid (>= 0.3.6)
53
+ activemodel (6.1.4.1)
54
+ activesupport (= 6.1.4.1)
55
+ activerecord (6.1.4.1)
56
+ activemodel (= 6.1.4.1)
57
+ activesupport (= 6.1.4.1)
58
+ activestorage (6.1.4.1)
59
+ actionpack (= 6.1.4.1)
60
+ activejob (= 6.1.4.1)
61
+ activerecord (= 6.1.4.1)
62
+ activesupport (= 6.1.4.1)
63
+ marcel (~> 1.0.0)
64
+ mini_mime (>= 1.1.0)
65
+ activesupport (6.1.4.1)
66
+ concurrent-ruby (~> 1.0, >= 1.0.2)
67
+ i18n (>= 1.6, < 2)
68
+ minitest (>= 5.1)
69
+ tzinfo (~> 2.0)
70
+ zeitwerk (~> 2.3)
71
+ builder (3.2.4)
72
+ concurrent-ruby (1.1.9)
73
+ crass (1.0.6)
74
+ diff-lcs (1.4.4)
75
+ erubi (1.10.0)
76
+ fortschritt (0.3.6)
77
+ globalid (1.0.0)
78
+ activesupport (>= 5.0)
79
+ i18n (1.8.11)
80
+ concurrent-ruby (~> 1.0)
81
+ loofah (2.12.0)
82
+ crass (~> 1.0.2)
83
+ nokogiri (>= 1.5.9)
84
+ mail (2.7.1)
85
+ mini_mime (>= 0.1.1)
86
+ marcel (1.0.2)
87
+ method_source (1.0.0)
88
+ mini_mime (1.1.2)
89
+ mini_portile2 (2.6.1)
90
+ minitest (5.14.4)
91
+ nio4r (2.5.8)
92
+ nokogiri (1.12.5)
93
+ mini_portile2 (~> 2.6.1)
94
+ racc (~> 1.4)
95
+ nokogiri (1.12.5-x86_64-darwin)
96
+ racc (~> 1.4)
97
+ racc (1.6.0)
98
+ rack (2.2.3)
99
+ rack-test (1.1.0)
100
+ rack (>= 1.0, < 3)
101
+ rails (6.1.4.1)
102
+ actioncable (= 6.1.4.1)
103
+ actionmailbox (= 6.1.4.1)
104
+ actionmailer (= 6.1.4.1)
105
+ actionpack (= 6.1.4.1)
106
+ actiontext (= 6.1.4.1)
107
+ actionview (= 6.1.4.1)
108
+ activejob (= 6.1.4.1)
109
+ activemodel (= 6.1.4.1)
110
+ activerecord (= 6.1.4.1)
111
+ activestorage (= 6.1.4.1)
112
+ activesupport (= 6.1.4.1)
113
+ bundler (>= 1.15.0)
114
+ railties (= 6.1.4.1)
115
+ sprockets-rails (>= 2.0.0)
116
+ rails-dom-testing (2.0.3)
117
+ activesupport (>= 4.2.0)
118
+ nokogiri (>= 1.6)
119
+ rails-html-sanitizer (1.4.2)
120
+ loofah (~> 2.3)
121
+ railties (6.1.4.1)
122
+ actionpack (= 6.1.4.1)
123
+ activesupport (= 6.1.4.1)
124
+ method_source
125
+ rake (>= 0.13)
126
+ thor (~> 1.0)
127
+ rake (13.0.6)
128
+ rspec (3.10.0)
129
+ rspec-core (~> 3.10.0)
130
+ rspec-expectations (~> 3.10.0)
131
+ rspec-mocks (~> 3.10.0)
132
+ rspec-core (3.10.1)
133
+ rspec-support (~> 3.10.0)
134
+ rspec-expectations (3.10.1)
135
+ diff-lcs (>= 1.2.0, < 2.0)
136
+ rspec-support (~> 3.10.0)
137
+ rspec-mocks (3.10.2)
138
+ diff-lcs (>= 1.2.0, < 2.0)
139
+ rspec-support (~> 3.10.0)
140
+ rspec-support (3.10.3)
141
+ sprockets (4.0.2)
142
+ concurrent-ruby (~> 1.0)
143
+ rack (> 1, < 3)
144
+ sprockets-rails (3.4.1)
145
+ actionpack (>= 5.2)
146
+ activesupport (>= 5.2)
147
+ sprockets (>= 3.0.0)
148
+ sqlite3 (1.4.2)
149
+ tabulo (2.7.1)
150
+ tty-screen (= 0.8.1)
151
+ unicode-display_width (= 2.1.0)
152
+ thor (1.1.0)
153
+ tty-screen (0.8.1)
154
+ tzinfo (2.0.4)
155
+ concurrent-ruby (~> 1.0)
156
+ unicode-display_width (2.1.0)
157
+ websocket-driver (0.7.5)
158
+ websocket-extensions (>= 0.1.0)
159
+ websocket-extensions (0.1.5)
160
+ zeitwerk (2.5.1)
161
+
162
+ PLATFORMS
163
+ ruby
164
+ x86_64-darwin-19
165
+
166
+ DEPENDENCIES
167
+ invalid_record_finder!
168
+ rails (>= 5.0)
169
+ rake (~> 13.0)
170
+ rspec (~> 3.10)
171
+ sqlite3
172
+
173
+ BUNDLED WITH
174
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,190 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ Copyright 2021 gut.org gAG
179
+
180
+ Licensed under the Apache License, Version 2.0 (the "License");
181
+ you may not use this file except in compliance with the License.
182
+ You may obtain a copy of the License at
183
+
184
+ http://www.apache.org/licenses/LICENSE-2.0
185
+
186
+ Unless required by applicable law or agreed to in writing, software
187
+ distributed under the License is distributed on an "AS IS" BASIS,
188
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
189
+ See the License for the specific language governing permissions and
190
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # InvalidRecordFinder
2
+
3
+ Find entries in your database that no longer pass your validations.
4
+
5
+ These poor little records need your help because they can't be updated anymore 😢
6
+
7
+ ## Installation
8
+
9
+ Bundle or gem install `invalid_record_finder`.
10
+
11
+ ## Usage
12
+
13
+ ### Basic example
14
+
15
+ A common use case is to periodically check all records and send mails if invalid records are found.
16
+
17
+ Models must inherit from `ApplicationRecord` and `ActionMailer` must be configured for the mail sending to work.
18
+
19
+ ```ruby
20
+ # my_regular_task.rake
21
+
22
+ result = InvalidRecordFinder.call
23
+ result.mail(from: 'noreply@myapp.com', to: 'devs@myapp.com')
24
+ ```
25
+
26
+ ### Targetting specific models
27
+
28
+ To check only specific models or scopes:
29
+
30
+ ```ruby
31
+ result = InvalidRecordFinder.call(models: [ImportantModel])
32
+ ```
33
+
34
+ To check all models except some:
35
+
36
+ ```ruby
37
+ result = InvalidRecordFinder.call(
38
+ ignored_models: [HugeTableThing, IrrelevantThing],
39
+ ignored_namespaces: 'Legacy::',
40
+ )
41
+ ```
42
+
43
+ ### Other options
44
+
45
+ Findings can also be put into CSV files.
46
+
47
+ The files will be in `Rails.root.join('tmp', 'invalid_records')` by default.
48
+
49
+ ```ruby
50
+ InvalidRecordFinder.call.save_csvs
51
+ ```
52
+
53
+ Per default, there is one mail or CSV for each model with invalid records.
54
+
55
+ To send just a single mail or write a single CSV:
56
+
57
+ ```ruby
58
+ result = InvalidRecordFinder.call.flatten
59
+ result.mail(from: 'noreply@myapp.com', to: 'devs@myapp.com')
60
+ result.save_csvs(to: my_custom_dir)
61
+ ```
62
+
63
+ ## Development
64
+
65
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+
69
+ ## Contributing
70
+
71
+ Bug reports and pull requests are welcome on GitHub at https://github.com/betterplace/invalid_record_finder.
72
+
73
+ ## License
74
+
75
+ `InvalidRecordFinder` is released under the [Apache License 2.0](LICENSE.txt) and Copyright 2021 [gut.org gAG](https://gut.org).
76
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: %i[spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'invalid_record_finder'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,32 @@
1
+
2
+ require_relative 'lib/invalid_record_finder/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'invalid_record_finder'
6
+ spec.version = InvalidRecordFinder::VERSION
7
+ spec.authors = ['betterplace development team']
8
+ spec.email = ['developers@betterplace.org']
9
+
10
+ spec.summary = 'Finds invalid ActiveRecord entries and reports them'
11
+ spec.homepage = 'https://github.com/betterplace/invalid_record_finder'
12
+ spec.license = 'Apache-2.0'
13
+
14
+ spec.required_ruby_version = '>= 2.6.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+ spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
19
+
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_dependency 'activerecord', '>= 5.0'
30
+ spec.add_dependency 'fortschritt', '~> 0.3'
31
+ spec.add_dependency 'tabulo', '~> 2.7'
32
+ end
@@ -0,0 +1,26 @@
1
+ # lightweight representation of a single validation error on one record
2
+ module InvalidRecordFinder
3
+ class Finding
4
+ attr_reader :model, :field, :field_value, :message, :id
5
+
6
+ def initialize(record, field, message)
7
+ @model = record.class.name
8
+ @field = field
9
+ @field_value = record[field]
10
+ @message = message
11
+ @id = record.id
12
+ end
13
+
14
+ def to_a
15
+ [model, field, field_value, message, id]
16
+ end
17
+
18
+ def to_h
19
+ { model: model, field: field, field_value: field_value, message: message, id: id }
20
+ end
21
+
22
+ def ==(other)
23
+ other.is_a?(self.class) && other.to_h == to_h
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,53 @@
1
+ # Collects Findings
2
+ module InvalidRecordFinder
3
+ class FindingsList
4
+ require 'forwardable'
5
+ extend Forwardable
6
+
7
+ def_delegators :@findings, :[], :empty?, :size, :to_a
8
+
9
+ def initialize(data = [])
10
+ @findings = data
11
+ end
12
+
13
+ def add_if_invalid(record)
14
+ return if record.valid? # Just make sure that the validation has run
15
+
16
+ record.errors.each do |err|
17
+ findings << InvalidRecordFinder::Finding.new(
18
+ record, err.attribute.to_s, err.message
19
+ )
20
+ end
21
+ rescue => e
22
+ # handle and report exceptions that occur during validation
23
+ findings << InvalidRecordFinder::Finding.new(record, 'EXCEPTION', e.message)
24
+ end
25
+
26
+ require 'tabulo'
27
+
28
+ def to_table
29
+ Tabulo::Table.new(findings.first(100), *headers, align_header: :left).pack
30
+ end
31
+
32
+ require 'csv'
33
+
34
+ def to_csv
35
+ CSV.generate do |csv|
36
+ csv << headers
37
+ findings.map(&:to_a).each { |line| csv << line }
38
+ end
39
+ end
40
+
41
+ def ==(other)
42
+ other.is_a?(self.class) && other.to_a == to_a
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :findings
48
+
49
+ def headers
50
+ %i[model field field_value message id]
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,40 @@
1
+ begin
2
+ require 'action_mailer'
3
+ rescue LoadError
4
+ # leave this class undefined if ActionMailer is not present
5
+ return
6
+ end
7
+
8
+ module InvalidRecordFinder
9
+
10
+ # sends mails
11
+ class Mailer < ActionMailer::Base
12
+ def mail(model_name:, findings_list:, from:, to:, subject_prefix: nil)
13
+ attachments["#{model_name}.csv"] = findings_list.to_csv
14
+ super(
15
+ from: from,
16
+ to: to,
17
+ body: build_body(model_name, findings_list),
18
+ subject: build_subject(model_name, findings_list, subject_prefix),
19
+ )
20
+ end
21
+
22
+ private
23
+
24
+ def build_body(model_name, findings_list)
25
+ <<~END
26
+ # Errors on #{model_name}
27
+ ```
28
+ #{findings_list.to_table}
29
+ ```
30
+ END
31
+ end
32
+
33
+ def build_subject(model_name, findings_list, prefix)
34
+ [
35
+ prefix,
36
+ "InvalidRecordFinder found #{findings_list.size} invalid #{model_name} records",
37
+ ].compact.join(' ')
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ module InvalidRecordFinder
2
+ module ModelPicker
3
+ def self.call(given_models: [], ignored_models: [], ignored_namespaces: [])
4
+ given_models = parse_given_models(given_models)
5
+ return given_models if given_models.any?
6
+
7
+ ignored_models = to_string_array(ignored_models)
8
+ ignored_namespaces = to_string_array(ignored_namespaces)
9
+
10
+ Rails.configuration.cache_classes || Rails.application.eager_load!
11
+
12
+ models = defined?(ApplicationRecord) && ApplicationRecord
13
+ .descendants
14
+ .reject(&:abstract_class?)
15
+ .map(&:name)
16
+ models.blank? && raise(Error, 'No models inherit from ApplicationRecord.')
17
+
18
+ if ignored_namespaces.any?
19
+ models = models.grep_v(/\A(?:#{Regexp.union(ignored_namespaces)})/)
20
+ end
21
+ models.empty? && raise(Error, 'All models are ignored through namespaces.')
22
+
23
+ if ignored_models.any?
24
+ models -= ignored_models.map(&:to_s)
25
+ end
26
+ models.empty? && raise(Error, 'All models are ignored.')
27
+
28
+ models.map(&:constantize)
29
+ end
30
+
31
+ def self.parse_given_models(arg)
32
+ Array(arg).map { |el| el.respond_to?(:constantize) ? el.constantize : el }.compact
33
+ end
34
+
35
+ def self.to_string_array(arg)
36
+ Array(arg).map { |obj| obj.respond_to?(:name) ? obj.name : obj&.to_s }.compact
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,59 @@
1
+ # A map of model names to FindingsLists.
2
+ module InvalidRecordFinder
3
+ class Result
4
+ require 'forwardable'
5
+ extend Forwardable
6
+
7
+ def_delegators :@hash, :[], :[]=, :each, :empty?, :to_h
8
+
9
+ def initialize(data = {})
10
+ @hash = data
11
+ end
12
+
13
+ # Flattens all the FindingsLists (for multiple models) into one,
14
+ # e.g. to send as a single mail or write into a single CSV.
15
+ #
16
+ # @return [InvalidRecordFinder::Result]
17
+ def flatten
18
+ flat_list = FindingsList.new(@hash.values.flat_map(&:to_a))
19
+ self.class.new('ApplicationRecord' => flat_list)
20
+ end
21
+
22
+ def mail(from:, to:, subject_prefix: nil, delivery_method: :deliver_now)
23
+ each do |model_name, findings_list|
24
+ findings_list.empty? || Mailer.mail(
25
+ model_name: model_name,
26
+ findings_list: findings_list,
27
+ from: from,
28
+ to: to,
29
+ subject_prefix: subject_prefix,
30
+ ).send(delivery_method)
31
+ end
32
+ end
33
+
34
+ require 'fileutils'
35
+
36
+ def save_csvs(to: default_csv_dir)
37
+ FileUtils.mkdir_p(to)
38
+
39
+ each.with_object([]) do |(model_name, findings_list), acc|
40
+ next if findings_list.empty?
41
+
42
+ acc << (path = File.join(to, "#{model_name}.csv"))
43
+ File.write(path, findings_list.to_csv)
44
+ end
45
+ end
46
+
47
+ def default_csv_dir
48
+ Rails.root.join('tmp', 'invalid_records')
49
+ end
50
+
51
+ def ==(other)
52
+ other.is_a?(self.class) && other.to_h == to_h
53
+ end
54
+
55
+ def inspect
56
+ "#<#{self.class}:0x%x>" % (object_id << 1)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ # Goes through all records of the given models, collects all invalid ones.
2
+ module InvalidRecordFinder
3
+ module Scanner
4
+ class << self
5
+ # @return [InvalidRecordFinder::Result]
6
+ def call(models:, verbose: false)
7
+ models.each_with_object(Result.new) do |model, acc|
8
+ puts "Checking #{model}..." if verbose
9
+ list = FindingsList.new
10
+ each_record(model, verbose: verbose) { |r| list.add_if_invalid(r) }
11
+ puts "Found #{list.size} invalid #{model}\n\n" if verbose
12
+ acc[model.name] = list
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ require 'fortschritt'
19
+
20
+ def each_record(model, verbose: false)
21
+ scope = model.all
22
+ if verbose
23
+ scope = scope.with_fortschritt
24
+ end
25
+ scope.find_each { |record| yield(record.fortschritt) }
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module InvalidRecordFinder
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'invalid_record_finder/finding'
2
+ require_relative 'invalid_record_finder/findings_list'
3
+ require_relative 'invalid_record_finder/mailer'
4
+ require_relative 'invalid_record_finder/model_picker'
5
+ require_relative 'invalid_record_finder/result'
6
+ require_relative 'invalid_record_finder/scanner'
7
+ require_relative 'invalid_record_finder/version'
8
+
9
+ module InvalidRecordFinder
10
+ class Error < StandardError; end
11
+
12
+ # @return [InvalidRecordFinder::Result]
13
+ def self.call(
14
+ models: [],
15
+ ignored_namespaces: [],
16
+ ignored_models: [],
17
+ verbose: false
18
+ )
19
+ models = ModelPicker.call(
20
+ given_models: models,
21
+ ignored_models: ignored_models,
22
+ ignored_namespaces: ignored_namespaces,
23
+ )
24
+ Scanner.call(models: models, verbose: verbose)
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: invalid_record_finder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - betterplace development team
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fortschritt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tabulo
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ description:
56
+ email:
57
+ - developers@betterplace.org
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - CHANGELOG.md
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - invalid_record_finder.gemspec
72
+ - lib/invalid_record_finder.rb
73
+ - lib/invalid_record_finder/finding.rb
74
+ - lib/invalid_record_finder/findings_list.rb
75
+ - lib/invalid_record_finder/mailer.rb
76
+ - lib/invalid_record_finder/model_picker.rb
77
+ - lib/invalid_record_finder/result.rb
78
+ - lib/invalid_record_finder/scanner.rb
79
+ - lib/invalid_record_finder/version.rb
80
+ homepage: https://github.com/betterplace/invalid_record_finder
81
+ licenses:
82
+ - Apache-2.0
83
+ metadata:
84
+ homepage_uri: https://github.com/betterplace/invalid_record_finder
85
+ source_code_uri: https://github.com/betterplace/invalid_record_finder
86
+ changelog_uri: https://github.com/betterplace/invalid_record_finder/blob/master/CHANGELOG.md
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.6.0
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.2.3
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Finds invalid ActiveRecord entries and reports them
106
+ test_files: []