Active Record Doctor
Active Record Doctor helps to keep the database in a good shape.
Used 47 times
V
Viktor Schmidt
Usage
Run this command in your Rails app directory in the terminal:
rails app:template LOCATION="https://railsbytes.com/script/zr4sBr"
Template Source
Review the code before running this template on your machine.
def do_bundle
Bundler.with_unbundled_env { run "bundle install" }
end
def print_green(heredoc)
puts set_color heredoc, :green
end
def do_commit
git :init
git add: "."
Bundler.with_unbundled_env { git commit: " -m 'Add Active Record Doctor to identify database issues' " }
end
def puts_usage
say "\nUsage:"
say "`bin/rails active_record_doctor`"
end
say "Applying Active Record Doctor..."
inject_into_file 'Gemfile', after: 'group :development do' do
<<-RUBY
# Active Record Doctor helps to keep the database in a good shape.
gem "active_record_doctor"
RUBY
end
do_bundle
say "\nAdding documentation for developers..."
create_file "docs/performance.md", "# Performance\n" unless File.exist? "docs/performance.md"
append_file "docs/performance.md" do
<<~EOF
## Active Record Doctor
[Active Record Doctor](https://rubygems.org/gems/active_record_doctor) identify database issues before they hit production.
You can run all available detectors via:
`bin/rails active_record_doctor`
You can run a specific detector via:
`bin/rails active_record_doctor:extraneous_indexes`
Currently, it can detect:
- extraneous indexes - `active_record_doctor:extraneous_indexes`
- unindexed deleted_at columns - `active_record_doctor:unindexed_deleted_at`
- missing foreign key constraints - `active_record_doctor:missing_foreign_keys`
- models referencing undefined tables - `active_record_doctor:undefined_table_references`
- uniqueness validations not backed by an unique index - `active_record_doctor:missing_unique_indexes`
- missing non-NULL constraints - `active_record_doctor:missing_non_null_constraint`
- missing presence validations - `active_record_doctor:missing_presence_validation`
- incorrect presence validations on boolean columns - `active_record_doctor:incorrect_boolean_presence_validation`
- mismatches between model length validations and database validation constraints - `active_record_doctor:incorrect_length_validation`
- incorrect values of dependent on associations - `active_record_doctor:incorrect_dependent_option`
- primary keys having short integer types - `active_record_doctor:short_primary_key_type`
- mismatched foreign key types - `active_record_doctor:mismatched_foreign_key_type`
- tables without primary keys - `active_record_doctor:table_without_primary_key`
It can also:
- index unindexed foreign keys - `active_record_doctor:unindexed_foreign_keys`
EOF
end
run "bin/rubocop -AS"
do_commit
print_green "\nAdded Active Record Doctor successfully!"
puts_usage