model_validator 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: 64f760c55b0d9771afce630e56523e689a3bf1ebd65f21acc003009518f1c340
4
+ data.tar.gz: 5854b1987c516583bd2e50a8b97ff576594a6bd9cb3fc1157262ce2995cd4cd4
5
+ SHA512:
6
+ metadata.gz: 6550ec508bfa1a5670f6ad3548e3efddc744dd08df7302d7dbbdbe5afd331e59aa44ca4dbe8681172ed6ed0299a500370d98220153d5a7df63fed0025cc86900
7
+ data.tar.gz: e66fb4032041a9b6323a70afd95939f8083736b089d3a27e2f09843c266e43d1d414085ae05d5919dadeac8daf6e8416d03378a2a534bf8e3e1b729a810f733c
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # macOS files
14
+ .DS_Store
15
+
16
+ # Ignore the default SQLite databases
17
+ /db/*.sqlite3
18
+ /db/*.sqlite3-*
19
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 2.4
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Metrics/BlockLength:
17
+ Exclude:
18
+ - 'spec/**/*_spec.rb'
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.2.7
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jibidus@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake", "~> 13.0"
8
+ gem "rspec", "~> 3.0"
9
+ gem "rubocop", "~> 1.7"
10
+
11
+ gem "sqlite3", "~> 1.4"
12
+
13
+ gem "rubocop-rspec", "~> 2.2"
14
+
15
+ gem "rubocop-rake", "~> 0.5.1"
data/Gemfile.lock ADDED
@@ -0,0 +1,191 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ model_validator (1.0.0)
5
+ rails (~> 6.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.1.0)
11
+ actionpack (= 6.1.0)
12
+ activesupport (= 6.1.0)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (6.1.0)
16
+ actionpack (= 6.1.0)
17
+ activejob (= 6.1.0)
18
+ activerecord (= 6.1.0)
19
+ activestorage (= 6.1.0)
20
+ activesupport (= 6.1.0)
21
+ mail (>= 2.7.1)
22
+ actionmailer (6.1.0)
23
+ actionpack (= 6.1.0)
24
+ actionview (= 6.1.0)
25
+ activejob (= 6.1.0)
26
+ activesupport (= 6.1.0)
27
+ mail (~> 2.5, >= 2.5.4)
28
+ rails-dom-testing (~> 2.0)
29
+ actionpack (6.1.0)
30
+ actionview (= 6.1.0)
31
+ activesupport (= 6.1.0)
32
+ rack (~> 2.0, >= 2.0.9)
33
+ rack-test (>= 0.6.3)
34
+ rails-dom-testing (~> 2.0)
35
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
36
+ actiontext (6.1.0)
37
+ actionpack (= 6.1.0)
38
+ activerecord (= 6.1.0)
39
+ activestorage (= 6.1.0)
40
+ activesupport (= 6.1.0)
41
+ nokogiri (>= 1.8.5)
42
+ actionview (6.1.0)
43
+ activesupport (= 6.1.0)
44
+ builder (~> 3.1)
45
+ erubi (~> 1.4)
46
+ rails-dom-testing (~> 2.0)
47
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
48
+ activejob (6.1.0)
49
+ activesupport (= 6.1.0)
50
+ globalid (>= 0.3.6)
51
+ activemodel (6.1.0)
52
+ activesupport (= 6.1.0)
53
+ activerecord (6.1.0)
54
+ activemodel (= 6.1.0)
55
+ activesupport (= 6.1.0)
56
+ activestorage (6.1.0)
57
+ actionpack (= 6.1.0)
58
+ activejob (= 6.1.0)
59
+ activerecord (= 6.1.0)
60
+ activesupport (= 6.1.0)
61
+ marcel (~> 0.3.1)
62
+ mimemagic (~> 0.3.2)
63
+ activesupport (6.1.0)
64
+ concurrent-ruby (~> 1.0, >= 1.0.2)
65
+ i18n (>= 1.6, < 2)
66
+ minitest (>= 5.1)
67
+ tzinfo (~> 2.0)
68
+ zeitwerk (~> 2.3)
69
+ ast (2.4.2)
70
+ builder (3.2.4)
71
+ concurrent-ruby (1.1.7)
72
+ crass (1.0.6)
73
+ diff-lcs (1.4.4)
74
+ erubi (1.10.0)
75
+ globalid (0.4.2)
76
+ activesupport (>= 4.2.0)
77
+ i18n (1.8.5)
78
+ concurrent-ruby (~> 1.0)
79
+ loofah (2.8.0)
80
+ crass (~> 1.0.2)
81
+ nokogiri (>= 1.5.9)
82
+ mail (2.7.1)
83
+ mini_mime (>= 0.1.1)
84
+ marcel (0.3.3)
85
+ mimemagic (~> 0.3.2)
86
+ method_source (1.0.0)
87
+ mimemagic (0.3.5)
88
+ mini_mime (1.0.2)
89
+ mini_portile2 (2.4.0)
90
+ minitest (5.14.2)
91
+ nio4r (2.5.4)
92
+ nokogiri (1.10.10)
93
+ mini_portile2 (~> 2.4.0)
94
+ parallel (1.20.1)
95
+ parser (3.0.0.0)
96
+ ast (~> 2.4.1)
97
+ rack (2.2.3)
98
+ rack-test (1.1.0)
99
+ rack (>= 1.0, < 3)
100
+ rails (6.1.0)
101
+ actioncable (= 6.1.0)
102
+ actionmailbox (= 6.1.0)
103
+ actionmailer (= 6.1.0)
104
+ actionpack (= 6.1.0)
105
+ actiontext (= 6.1.0)
106
+ actionview (= 6.1.0)
107
+ activejob (= 6.1.0)
108
+ activemodel (= 6.1.0)
109
+ activerecord (= 6.1.0)
110
+ activestorage (= 6.1.0)
111
+ activesupport (= 6.1.0)
112
+ bundler (>= 1.15.0)
113
+ railties (= 6.1.0)
114
+ sprockets-rails (>= 2.0.0)
115
+ rails-dom-testing (2.0.3)
116
+ activesupport (>= 4.2.0)
117
+ nokogiri (>= 1.6)
118
+ rails-html-sanitizer (1.3.0)
119
+ loofah (~> 2.3)
120
+ railties (6.1.0)
121
+ actionpack (= 6.1.0)
122
+ activesupport (= 6.1.0)
123
+ method_source
124
+ rake (>= 0.8.7)
125
+ thor (~> 1.0)
126
+ rainbow (3.0.0)
127
+ rake (13.0.1)
128
+ regexp_parser (2.0.3)
129
+ rexml (3.2.4)
130
+ rspec (3.10.0)
131
+ rspec-core (~> 3.10.0)
132
+ rspec-expectations (~> 3.10.0)
133
+ rspec-mocks (~> 3.10.0)
134
+ rspec-core (3.10.0)
135
+ rspec-support (~> 3.10.0)
136
+ rspec-expectations (3.10.0)
137
+ diff-lcs (>= 1.2.0, < 2.0)
138
+ rspec-support (~> 3.10.0)
139
+ rspec-mocks (3.10.0)
140
+ diff-lcs (>= 1.2.0, < 2.0)
141
+ rspec-support (~> 3.10.0)
142
+ rspec-support (3.10.0)
143
+ rubocop (1.9.1)
144
+ parallel (~> 1.10)
145
+ parser (>= 3.0.0.0)
146
+ rainbow (>= 2.2.2, < 4.0)
147
+ regexp_parser (>= 1.8, < 3.0)
148
+ rexml
149
+ rubocop-ast (>= 1.2.0, < 2.0)
150
+ ruby-progressbar (~> 1.7)
151
+ unicode-display_width (>= 1.4.0, < 3.0)
152
+ rubocop-ast (1.4.1)
153
+ parser (>= 2.7.1.5)
154
+ rubocop-rake (0.5.1)
155
+ rubocop
156
+ rubocop-rspec (2.2.0)
157
+ rubocop (~> 1.0)
158
+ rubocop-ast (>= 1.1.0)
159
+ ruby-progressbar (1.11.0)
160
+ sprockets (4.0.2)
161
+ concurrent-ruby (~> 1.0)
162
+ rack (> 1, < 3)
163
+ sprockets-rails (3.2.2)
164
+ actionpack (>= 4.0)
165
+ activesupport (>= 4.0)
166
+ sprockets (>= 3.0.0)
167
+ sqlite3 (1.4.2)
168
+ thor (1.0.1)
169
+ tzinfo (2.0.3)
170
+ concurrent-ruby (~> 1.0)
171
+ unicode-display_width (2.0.0)
172
+ websocket-driver (0.7.3)
173
+ websocket-extensions (>= 0.1.0)
174
+ websocket-extensions (0.1.5)
175
+ zeitwerk (2.4.2)
176
+
177
+ PLATFORMS
178
+ x86_64-darwin-20
179
+ x86_64-linux
180
+
181
+ DEPENDENCIES
182
+ model_validator!
183
+ rake (~> 13.0)
184
+ rspec (~> 3.0)
185
+ rubocop (~> 1.7)
186
+ rubocop-rake (~> 0.5.1)
187
+ rubocop-rspec (~> 2.2)
188
+ sqlite3 (~> 1.4)
189
+
190
+ BUNDLED WITH
191
+ 2.2.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Jibidus
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Makefile ADDED
@@ -0,0 +1,14 @@
1
+ help:
2
+ @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
3
+
4
+ install: ## Install dependencies
5
+ bundle install
6
+
7
+ test: ## Test application
8
+ bundle exec rspec
9
+
10
+ lint: ## Check file format, smell code, conventions…
11
+ bundle exec rubocop
12
+
13
+ lint-fix: ## Fix violations when possible
14
+ bundle exec rubocop -a
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ [![Build Status](https://www.travis-ci.com/jibidus/model_validation.svg?branch=main)](https://www.travis-ci.com/jibidus/model_validation)
2
+
3
+ # model_validator
4
+
5
+ This gem can validate database against Active Record validation rules.
6
+ This is useful since error may occur when manipulating such data.
7
+
8
+ ## How this can happen?
9
+
10
+ - when database is modified without through Active Record api
11
+ - when migrations modify database
12
+
13
+ ## How this gem can prevent such unexpected error?
14
+
15
+ It is recommended to use this gem during deployment step:
16
+
17
+ - restore production database in a staging/preprod/non-production environment
18
+ - validate the database
19
+ - add missing migrations in case of violations
20
+ - repeat validation and fix until there is no more violation
21
+ - then, you are ready to deploy your application in production
22
+
23
+ ## Limitations
24
+
25
+ This gem fetch **all** record in database, and, for each record run the Active Record validation.
26
+ So, because of performances reason, this is only acceptable for tiny databases (thousand of records).
27
+
28
+ ## Installation
29
+
30
+ Add this line to your application's Gemfile:
31
+
32
+ ```ruby
33
+ gem 'model_validator'
34
+ ```
35
+
36
+ And then execute:
37
+
38
+ ```bash
39
+ $ bundle install
40
+ ```
41
+
42
+ Or install it yourself as:
43
+
44
+ ```bash
45
+ $ gem install model_validator
46
+ ```
47
+
48
+ ## Rake task usage
49
+
50
+ Run `rails db:validate` to validate database of current environment. This is a rake task (see `Rails -T db:validate`)
51
+
52
+ You can skip some model with `MODEL_VALIDATOR_SKIPPED_MODELS` env var (ex: `DB_VALIDATE_SKIP=Model1,Model2`).
53
+
54
+ ## Ruby usage
55
+
56
+ Validation engine can also be used anywhere in your code:
57
+
58
+ ```ruby
59
+ require "model_validator"
60
+ ModelValidator.validate_all
61
+ ```
62
+
63
+ This will skip `ModelA` and `Model2` for validation:
64
+
65
+ ```ruby
66
+ ModelValidator.validate_all skipped_models: [Model1,Model2]
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `make install` to install dependencies.
72
+
73
+ Then, run `make test` to run the tests.
74
+
75
+ Then, run `make lint` to run linters ([rubocop](https://github.com/rubocop-hq/rubocop)).
76
+
77
+ To install this gem onto your local machine, run `bundle exec rake install`.
78
+
79
+ ### How to release new version?
80
+
81
+ - Update the version number in `version.rb`
82
+ - 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).
83
+
84
+ ## Why not contributing to existing gem?
85
+
86
+ Many already existing gems may do the same, but none are satisfying:
87
+
88
+ - [validb](https://github.com/jgeiger/validb): depends on [sidekiq](https://github.com/mperham/sidekiq)
89
+ - [validates_blacklist](https://www.rubydoc.info/gems/validates_blacklist/0.0.1): requires to add configuration in each model 😨
90
+ - [valid_items](https://rubygems.org/gems/valid_items): requires rails eager load 🤔
91
+ - [schema-validations](https://github.com/robworley/schema-validations): I don't understand what it really does 🤪
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jibidus/model_validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/model_validator/blob/master/CODE_OF_CONDUCT.md).
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
100
+
101
+ ## Code of Conduct
102
+
103
+ Everyone interacting in the ModelValidator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/model_validator/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "model_validator"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+
5
+ # Validate models in database according Active Record model validation rules
6
+ module ModelValidator
7
+ require "model_validator/railtie" if defined?(Rails)
8
+
9
+ def self.validate_all(skipped_models: [])
10
+ if skipped_models.empty?
11
+ Rails.logger.info "No model skipped"
12
+ else
13
+ Rails.logger.info "Skipped model(s): #{skipped_models.map(&:to_s).join(", ")}"
14
+ end
15
+ handler = LogHandler.new
16
+ Validator.new(handler, skip_models: skipped_models).run
17
+ handler.violations_count
18
+ end
19
+
20
+ # Validation engine, which fetch, and validate each database records
21
+ class Validator
22
+ def initialize(handler, skip_models: [])
23
+ @handler = handler
24
+ @skip_models = skip_models
25
+ end
26
+
27
+ def models_to_validate
28
+ ActiveRecord::Base.subclasses
29
+ .reject { |type| type.to_s.include? "::" } # subclassed classes are not our own models
30
+ .reject { |type| @skip_models.include? type }
31
+ end
32
+
33
+ def run
34
+ models_to_validate.each do |type|
35
+ @handler.start_model type
36
+ type.unscoped.find_each do |record|
37
+ @handler.on_violation(type, record) unless record.valid?
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # Validation handler, which logs each violation
44
+ class LogHandler
45
+ attr_reader :violations_count
46
+
47
+ def initialize
48
+ @violations_count = 0
49
+ end
50
+
51
+ def start_model(type)
52
+ Rails.logger.info "Checking #{type}..."
53
+ end
54
+
55
+ def on_violation(type, record)
56
+ Rails.logger.error "#<#{type} id: #{record.id}, errors: #{record.errors.full_messages}>" unless record.valid?
57
+ @violations_count += 1
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "model_validator"
4
+
5
+ path = File.expand_path(__dir__)
6
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| import f }
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "model_validator"
4
+ require "rails"
5
+
6
+ module ModelValidator
7
+ # Used to register rake task in Rails application
8
+ class Railtie < Rails::Railtie
9
+ railtie_name :model_validator
10
+
11
+ rake_tasks do
12
+ path = File.expand_path(__dir__)
13
+ Dir.glob("#{path}/tasks/**/*.rake").each { |f| load f }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails"
4
+
5
+ namespace :db do
6
+ desc "Validates database against model validation rule"
7
+ task validate: :environment do
8
+ Rails.logger.info "Validate database (this will take some time)…"
9
+ skipped_models = (ENV["MODEL_VALIDATOR_SKIPPED_MODELS"] || "").split(",")
10
+ count = ModelValidator.validate_all(skipped_models: skipped_models)
11
+ Rails.logger.info "Validation finished with #{count} violation(s)"
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ModelValidator
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/model_validator/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "model_validator"
7
+ spec.version = ModelValidator::VERSION
8
+ spec.authors = ["Jibidus"]
9
+ spec.email = ["jibidus@gmail.com"]
10
+
11
+ spec.summary = "Validate database against Active Record model validations."
12
+ spec.description = <<~FOO
13
+ Many reason may make database violate Active Record validation rules
14
+ (database modification without Active Record, migration…).
15
+ This raises unexpected error when these records are manipulated by rails.
16
+ FOO
17
+ spec.homepage = "https://github.com/jibidus/model_validation"
18
+ spec.license = "MIT"
19
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/jibidus/model_validation"
23
+ spec.metadata["changelog_uri"] = "https://github.com/jibidus/model_validation"
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "rails", "~> 6.1"
35
+
36
+ # For more information and examples about making a new gem, checkout our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jibidus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.1'
27
+ description: |
28
+ Many reason may make database violate Active Record validation rules
29
+ (database modification without Active Record, migration…).
30
+ This raises unexpected error when these records are manipulated by rails.
31
+ email:
32
+ - jibidus@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - ".gitignore"
38
+ - ".rspec"
39
+ - ".rubocop.yml"
40
+ - ".travis.yml"
41
+ - CODE_OF_CONDUCT.md
42
+ - Gemfile
43
+ - Gemfile.lock
44
+ - LICENSE.txt
45
+ - Makefile
46
+ - README.md
47
+ - Rakefile
48
+ - bin/console
49
+ - bin/setup
50
+ - lib/model_validator.rb
51
+ - lib/model_validator/Rakefile
52
+ - lib/model_validator/railtie.rb
53
+ - lib/model_validator/tasks/db/validate.rake
54
+ - lib/model_validator/version.rb
55
+ - model_validator.gemspec
56
+ homepage: https://github.com/jibidus/model_validation
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ homepage_uri: https://github.com/jibidus/model_validation
61
+ source_code_uri: https://github.com/jibidus/model_validation
62
+ changelog_uri: https://github.com/jibidus/model_validation
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: 2.4.0
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubygems_version: 3.1.4
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Validate database against Active Record model validations.
82
+ test_files: []