This gem can validate database against Active Record validation rules. This is useful since error may occur when manipulating such data.
How this can happen?
- when database is modified without through Active Record api
- when migrations modify database
How this gem can prevent such unexpected error?
Here is how this gem works:
- This gem search for all models (all final descendants of
ApplicationRecord, which are not abstract).
- For each model, each record in database is fetch
- Active Record validations are played on these records
- All violations are logged
Here is the recommended way to use it:
- restore production database in a staging/preprod/non-production environment
- validate the database
- add missing migrations in case of violations
- repeat validation and fix until there is no more violation
- then, you are ready to deploy your application in production
This gem fetch all record in database, and, for each record run the Active Record validation. So, because of performances reason, this may be only acceptable for tiny databases (thousand of records).
Add this line to your application"s Gemfile:
And then execute:
$ bundle install
Or install it yourself as:
$ gem install model_validator
Rake task usage
rails db:validate to validate database of current environment. This is a rake task (see
Rails -T db:validate)
You can skip some model with
MODEL_VALIDATOR_SKIPPED_MODELS env var (ex:
Validation engine can also be used anywhere in your code:
require "model_validator" ModelValidator.validate_all
This will skip
Model2 for validation:
ModelValidator.validate_all skipped_models: [Model1,Model2]
After checking out the repo, run
make install to install dependencies.
make test to run the tests (test coverage is available in
make lint to run linters (rubocop).
To install this gem onto your local machine, run
How to release new version?
Make sure your are in
main branch. Then, run:
Example for building a new minor release:
Why not contributing to existing gem?
Many already existing gems may do the same, but none are satisfying:
- validb: depends on sidekiq
- validates_blacklist: requires to add configuration in each model 😨
- valid_items: not compliant with rails 6 + cannot find sources to contribute
- schema-validations: I didn't understand what it really does 🤪
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.
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the ModelValidator project"s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.