Database Consistency Gem

The database consistency gem was new to me but it is fast becoming a must-install tool

I love gems that make me look like a better developer for very little work. The database consistency gem is a great example of this!

Just like it’s title suggests, it attempts to make your database consistent with what you have said it should do.

To quote the project’s README

The main goal of the project is to help you avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.

database_consistency does several things, but what I’ve found it most useful for is;

  • Checking for three-state booleans. In other words, allowing null to be set when really it should be true or false
  • Missing indexes on columns that should use them. If your model uses “has_one” but the field it reference isn’t indexed, then lookups could be slower than you’d like
  • Checking for not null requirements. If a column in your database is told it can’t be null, but your model doesn’t enforce it at the Rails level.

Installing database_consistency

Add the following to your Gemfile;

gem 'database_consistency',
    group: :development,
    require: false

Note that it should be in the development group, because we only want to run this locally, and require: false is set because we don’t want Rails to automatically load in anything to do with this gem.

Running database_consistency

If you want a report on everything it thinks you should fix, you can run;

bundle exec database_consistency

Be warned, on older projects with lots of tables, you will almost certainly have lots of output.

If you are overwhelmed, you can create an ignore file, which acts like a todo list. To create one you can call;

bundle exec database_consistency -g

Which creates a file called .database_consistency.todo.yml, now when calling the gem you can pass in this file and old issues will be ignored;

 bundle exec database_consistency -c .database_consistency.todo.yml

As you address the issues, you can remove them from the yml file.

Automatically fix issues

If you are happy with the suggestions, and don’t want to manually create each migration or code change, you can run a command to generate everything for you. (New to Rails and not 100% what a migration is? We have an article explaining the basics of Rails migrations)

Remember: always check the results of the change and be sure you understand them and are happy with them.

bundle exec database_consistency -f

I’m very happy with this gem

We’ve used this on a couple of projects already and have been very happy with the suggestions and improvements it has made. It is over four years old at this point and I only wish I’d heard about it sooner!


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.