Model or dataset
djezzzl/database_consistency avatar
djezzzl/database_consistency

database_consistency: A Ruby Gem That Audits Rails Schema and Model Drift

The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models.

1,193 stars66 forksRubyMIT

At a glance

What is it?
database_consistency is a Ruby gem that compares an ActiveRecord schema against its models, flagging missing constraints, indexes, and validations. It supports SQLite3, PostgreSQL, and MySQL, and offers auto-correction and TODO files for gradual adoption.
Who is it for?
Adopt database_consistency if you maintain a Rails app with ActiveRecord and want to catch schema and model drift before it becomes a runtime bug. It is especially useful for teams that need a safe, incremental rollout via TODO files and auto-correction.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 22 days ago.
What is it written in?
Mainly Ruby, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on October 4, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • djezzzl/database_consistency1,193 ★ · +2
Star history
Repository2026-08-242026-10-04
djezzzl/database_consistency1.2k1.2k

2 Stars in the last 41 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The Problem: Schema and Model Drift in Rails

Rails applications often accumulate inconsistencies between the database schema and the ActiveRecord models that interact with it. A column may be NOT NULL in the database but have no presence validator in the model, allowing nil values to slip through and cause runtime errors. Alternatively, a uniqueness validation may exist without a corresponding unique index, leading to race conditions. These mismatches are easy to miss during code review and can surface only in production. database_consistency targets this exact gap. It is a Ruby gem that inspects the schema and the models, then reports where they disagree. The intended audience is Rails developers who want a static analysis pass before deploying, particularly those managing large schemas where manual checks are impractical.

What It Checks: From Null Constraints to UUID Ordering

The README lists over 20 distinct checkers. The most common are missing null constraints, missing presence validations, missing uniqueness validations, and missing foreign keys for belongs_to associations. Others are more niche: it finds primary keys with integer/serial types, mismatched primary key and foreign key types, redundant non-unique indexes, and inconsistent enum values between the database and ActiveRecord enums. It also checks for three-state booleans (missing null constraints on boolean fields), broken associations that point to non-existent models, and models that lack a corresponding table. A notable checker is for UUID primary keys without a specified ordering column, which can cause unpredictable query ordering. Each checker has a dedicated wiki page, but the README does not detail the exact algorithm for each. The breadth suggests that the tool is designed to be a comprehensive safety net rather than a single-purpose linter.

How It Works: Static Analysis Without Running the App

The tool operates as a command-line executable, run via bundle exec database_consistency. It does not require the application to be running. Instead, it loads the Rails environment to access the model definitions and the database schema, then compares them. The output is a list of failures, each formatted as CheckerName fail Model column message. For example, the README shows NullConstraintChecker fail User code column is NOT NULL but does not have a validator disallowing nil values. This implies that the tool reads the schema for null constraints and the model for validators, then flags mismatches. The mechanism is straightforward: it relies on ActiveRecord's introspection capabilities. Because it only supports ActiveRecord, it can assume a certain structure in models, such as validations and associations, which makes the analysis deterministic. The tool does not execute queries against the database; it reads the schema definition, likely from the ActiveRecord connection.

Getting Started: Gemfile, Bundle, and First Run

Installation is a two-step process. Add gem 'database_consistency', group: :development, require: false to your Gemfile, then run bundle install. The require: false is important because the gem should not load in production. After that, run bundle exec database_consistency from the project root. The README provides a sample output showing multiple failures across different checkers. There is no mention of a configuration file in the basic usage, but the README references a flexible configuration page in the wiki, which likely allows disabling or tweaking individual checkers. The tool also supports auto-correction, but the README does not show the command for it; it only links to a wiki page. For a first run, expect a list of failures that may be overwhelming if the schema is already inconsistent. The tool offers a slow start with TODO files, which allows you to defer existing issues while catching new ones. That feature is likely the key to adopting it on a legacy codebase.

Limitations: ActiveRecord Only and No Real-Time Checks

The most obvious limitation is that the gem supports only ActiveRecord. If you use Sequel or another ORM, it is useless. Even within Rails, it only supports SQLite3, PostgreSQL, and MySQL, which covers most deployments but not every database. The tool is a static analyzer, so it cannot catch issues that arise from dynamic model changes or runtime schema modifications. It also depends on the accuracy of ActiveRecord's schema introspection; if a custom type or a non-standard column is used, the checker may produce false positives or miss issues. The README does not mention performance for large schemas, but running it on every CI build could add time. A more subtle limitation is that the checks are based on the presence of validations, not their correctness. For example, a length validator with a wrong limit would not be flagged if a limit exists. The tool checks for the existence of a validation, not its exact value, unless a specific checker is designed for that.

Alternatives: Strong Migrations and Linter-Style Tools

A common alternative is the strong_migrations gem, which focuses on safe database migrations rather than schema and model consistency. Strong Migrations catches dangerous operations like adding a column with a default value or changing a column type, but it does not compare models to the schema. database_consistency fills a different niche: it is a linter for the entire schema-model relationship. Another alternative is to write custom SQL queries or use database tools to inspect constraints, but that requires manual effort and does not integrate with Rails models. For teams already using RuboCop, a custom cop could be written to check some of these patterns, but that would be a significant investment. The key difference is that database_consistency is purpose-built for this task, offering a comprehensive set of checkers out of the box, while alternatives either focus on migrations or require custom code.

Maintenance and License: MIT with Active Development

The gem is licensed under MIT, which means you can use, modify, and distribute it freely, even in commercial projects. The repository is not archived, and the README mentions a test workflow and a RuboCop workflow, indicating active maintenance. However, the last push date is unknown, and no recent releases were retrieved, so it is unclear how frequently updates are published. The project is supported by Pennylane, a company that likely uses it internally, which is a positive sign for ongoing maintenance. The README also invites contributions and donations, suggesting a small maintainer team. For upgrade cost, the tool is a development dependency, so upgrading it is as simple as bumping the version in the Gemfile. The main cost is re-running the checks and reviewing any new checkers that might be added. Since it is a static analyzer, there is no runtime overhead. The auto-correction feature can modify your schema or model files, so you must review those changes carefully, especially in a team environment.

Editorial conclusion

Adopt database_consistency if you maintain a Rails app with ActiveRecord and want to catch schema and model drift before it becomes a runtime bug. It is especially useful for teams that need a safe, incremental rollout via TODO files and auto-correction. Do not use it if you are on a non-Rails ORM, or if your database is not SQLite3, PostgreSQL, or MySQL. Before adopting, verify that your existing schema and models are clean enough to avoid a flood of failures, and confirm that the gem's checks align with your validation style, especially around presence and length validators. The gem is MIT licensed, so commercial use is unrestricted, but you must review each auto-correction because it changes schema or model files.

Frequently asked questions

What does the database_consistency gem check?

It compares ActiveRecord models against the live schema and reports twenty-four kinds of drift, including missing null constraints, missing check constraints behind numericality and length validations, missing foreign keys for belongs_to associations, mismatching primary key and foreign key types, redundant indexes, enum type and value mismatches, and columns used in find_by calls with no index behind them.

How do I install and run database_consistency?

Add gem 'database_consistency', group: :development, require: false to the Gemfile, run bundle install, and then run bundle exec database_consistency. Output is one line per finding, giving the checker name, the word fail, the model and field, and a sentence describing the mismatch.

Which databases and ORMs does database_consistency support?

SQLite3, PostgreSQL and MySQL are supported, and ActiveRecord is the only framework supported at the moment. The repository's own compose file starts a single untagged postgres service for its tests, so only the PostgreSQL path is exercised there.

What does database_consistency do about redundant indexes?

Three of its twenty-four checkers look for things to remove: redundant non-unique indexes, redundant uniqueness constraints, and a redundant case_sensitive: false option on a unique validation against a type that is already case-insensitive. Checks of that kind are the part of the list a migration-heavy codebase is most likely to act on.

What else does database_consistency offer besides checking?

Four documented extras: auto-correction, flexible configuration, a slow start that writes findings to TODO files instead of fixing them at once, and support for a custom checker. Each is documented in the wiki rather than in the README, which carries only the twenty-four checker names.

Official sources

  1. Official README
  2. Project repository
  3. README
  4. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/djezzzl-database-consistency.svg)](https://hysenlabs.com/projects/djezzzl-database-consistency)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/djezzzl-database-consistency.svg)](https://hysenlabs.com/projects/djezzzl-database-consistency)