ActiveRecord 的数据库验证
DatabaseValidations helps you to keep the database consistency with better performance. Right now, it supports only ActiveRecord.
The more you use the gem, the more performance increase you have. Try it now!
Add this line to your application's Gemfile:
gem 'database_validations'And then execute:
bundleOr install it yourself as:
gem install database_validationsHave a look at example application for details.
Imagine, you have User model defines as
class User false
user.errors.messages
# => {:company=>["must exist"]}ActiveRecord's belongs_to has optional: false by default. Unfortunately, this
approach does not ensure existence of the related object. For example, we can skip
validations or remove the related object after we save the object. After that, our
database becomes inconsistent because we assume the object has his relation but it
does not.
db_belongs_to solves the problem using foreign key constraints in the database
also providing backward compatibility with nice validations errors.
Advantages:
ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK'] = 'true' if you want to
skip it in some cases. (For example, when you run migrations.) Note: we skip it for the abstract classes.Disadvantages:
| Option name | PostgreSQL | MySQL |
|---|---|---|
| class_name | + | + |
| foreign_key | + | + |
| foreign_type | - | - |
| primary_key | + | + |
| dependent | + | + |
| counter_cache | + | + |
| polymorphic | - | - |
| validate | + | + |
| autosave | + | + |
| touch | + | + |
| inverse_of | + | + |
| optional | - | - |
| required | - | - |
| default | + | + |
Supported databases are PostgreSQL, MySQL and SQLite.
class User false
dupe.errors.messages
# => {:email=>["has already been taken"]}
User.create!(email: '[email protected]')
# => ActiveRecord::RecordInvalid Validation failed: email has already been takenComplete case_sensitive replacement example (for PostgreSQL only):
validates :slug, uniqueness: { case_sensitive: false, scope: :field }Should be replaced by:
validates :slug, db_uniqueness: {index_name: :unique_index, case_sensitive: false, scope: :field}Keep in mind: because valid? method uses default validator you should:
scope optioncase_sensitive optionwhere optionUnfortunately, ActiveRecord's validates_uniqueness_of approach does not ensure
uniqueness. For example, we can skip validations or create two records in parallel
queries. After that, our database becomes inconsistent because we assume some uniqueness
over the table but it has duplicates.
validates_db_uniqueness_of solves the problem using unique index constraints
in the database also providing backward compatibility with nice validations errors.
ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK'] = 'true'
if you want to skip it in some cases. (For example, when you run migrations.) Note: we skip it for the abstract classes.| Option name | PostgreSQL | MySQL | SQLite |
|---|---|---|---|
| mode | + | + | + |
| scope | + | + | + |
| message | + | + | + |
| if | + | + | + |
| unless | + | + | + |
| index_name | + | + | - |
| where | + | - | - |
| case_sensitive | + | - | - |
| allow_nil | - | - | - |
| allow_blank | - | - | - |
The validation has an option :rescue with two values:
:default (default option) that follows default ActiveRecord behavior. It respects validate: false option for save/save! (for example, this is being used for nested associations):always that catches database constraint errors and turns them to ActiveRecord validations filling .errors properly.You may want to use rescue: :always in case you save nested associations with accepts_nested_attributes_for helper and you want the validation to happen automatically when a user
provides duplicated data in the same request.
There are 3 mode options:
:optimized - the default one. In this mode it turns DB constraint exceptions into proper validation messages.:enhanced - a combination of the standard uniqueness validation and the db uniqueness validation. Runs a query first but also rescues from exception. The preferable mode for user-facing validations.:standard - in this mode works pretty much the same way as validates_uniqueness_of (except the index existence check).Add require database_validations/rspec/matchers' to your spec file.
Example:
class User = 1.72, add the plugin to your `.rubocop.yml` file:
```yaml
plugins:
- database_validations:
require_path: rubocop-database_validationsFor older versions of RuboCop, use require instead:
require:
- database_validations/rubocop/copsThe easiest way to get started is with Docker. A docker-compose.yaml is
included that runs PostgreSQL and MySQL with preconfigured test databases:
docker compose up -dThen create the test databases and run the specs:
rake db:all:create
rake specTo drop the test databases:
rake db:all:dropTo check the conformance with the style guides, run:
rubocopTo run benchmarks, run:
ruby -I lib benchmarks/composed_benchmarks.rbTo 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.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the DatabaseValidations project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
暂无开放 Issues,或尚未同步最近议题。