model_validator
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
Limitations
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).
Installation
Add this line to your application"s Gemfile:
gem "model_validator"
And then execute:
$ bundle install
Or install it yourself as:
$ gem install model_validator
Rake task usage
Run 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: DB_VALIDATE_SKIP=Model1,Model2
).
Ruby usage
Validation engine can also be used anywhere in your code:
require "model_validator"
ModelValidator.validate_all
This will skip ModelA
and Model2
for validation:
ModelValidator.validate_all skipped_models: [Model1,Model2]
Development
After checking out the repo, run make install
to install dependencies.
Then, run make test
to run the tests (test coverage is available in coverage/index.html
).
Then, run make lint
to run linters (rubocop).
To install this gem onto your local machine, run make install-locally
.
How to release new version?
Make sure your are in main
branch. Then, run:
rake release:make[major|minor|patch|x.y.z]
Example for building a new minor release: rake release:make[minor]
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 🤪
Contributing
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.
License
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.