The main goal of the project is to provide an easy way to check the consistency of the database constraints with the application validations.
Add this line to your application's Gemfile:
gem 'database_consistency', group: :development, require: false
And then execute:
Or install it yourself as:
gem install database_consistency
In the root directory of your Rails project run
bundle exec database_consistency.
To get a full output run
LOG_LEVEL=DEBUG bundle exec database_consistency.
You can also configure the gem to skip some of its checks using .database_consistency.yml file.
How it works?
- As first step, we iterate over all validators and check their consistency with the database constraints. Right now, we only check consistency for presence validator.
- As second step, we iterate over all column in the database and check if they have proper validations. Right now, we only check if field should have presence validator.
This comparator is used for PresenceValidator.
|at least one provided||required||fail|
|at least one provided||optional||ok|
We fail if the column satisfy conditions:
- column is required in the database
- column is not a primary key (we don't need need presence validators for primary keys)
- model records timestamps and column's name is not
- column is not used for any Presence or Inclusion validators, or BelongsTo reflection
- column has not a default value
$ bundle exec database_consistency fail column phone of table users of model User should be required in the database fail column name of table users of model User is required but possible null value insert fail column code of table users of model User is required but do not have presence validator
See example project for more details.
Configuration example for overcommit gem.
PreCommit: DatabaseConsistency: enabled: true quiet: false command: ['bundle', 'exec', 'database_consistency']
After checking out the repo, run
bundle install to install dependencies. Then, run
bundle exec rspec to run the tests.
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 tags, and push the
.gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Code of Conduct
Everyone interacting in the DatabaseConsistency project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
DatabaseConsistency's changelog is available here.
Copyright (c) Evgeniy Demin. See LICENSE.txt for further details.