• Stars
    star
    339
  • Rank 124,632 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 13 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A tool for detecting missing unique indexes in Rails projects

Consistency Fail

Description

consistency_fail is a tool to detect missing unique indexes in Rails projects.

With more than one application server, validates_uniqueness_of becomes a lie. Two app servers -> two requests -> two near-simultaneous uniqueness checks -> two processes that commit to the database independently, violating this faux constraint. You'll need a database-level constraint for cases like these.

consistency_fail will find your missing unique indexes, so you can add them and stop ignoring the C in ACID.

Similar problems arise with has_one, so consistency_fail finds places where database-level enforcement is lacking there as well.

For more detail, see my blog post on the subject.

Installation

You can install the gem directly:

gem install consistency_fail

Or if you're using Bundler (which you probably are), add it to your Gemfile.

gem 'consistency_fail'

Limitations

The master branch should work for the following ActiveRecord versions:

  • 5.x
  • 4.x (has a known issue around views)
  • 3.x
  • 2.3 (on the rails-2.3 branch)

The known issue with views in ActiveRecord 4.x is that in this version, the connection adapter's tables method includes both tables and views. This means that without additional monkeypatches to the various connection adapters, we cannot reliably detect whether a given model is backed by a table or a view. I wouldn't mind monkeypatching a bounded set of adapters, but I don't want to be on the hook for arbitrary connection adapters that may require licenses to test (e.g. SQL Server, Oracle).

consistency_fail depends on being able to find all your ActiveRecord::Base subclasses with some $LOAD_PATH trickery. If any models are in a path either not on your project's load path or in a path that doesn't include the word "models", consistency_fail won't be able to find or analyze them. I'm open to making the text "models" configurable if people want that. Please open an issue or pull request if so!

Usage

The normal run mode is to generate a report of the problematic spots in your application. From your Rails project directory, run:

consistency_fail

from your terminal / shell. This will spit a report to standard output, which you can view directly, redirect to a file as evidence to embarrass a teammate, or simply beam in happiness at your application's perfect record for validates_uniqueness_of and has_one usage.

The somewhat more sinister and awesome run mode is to include an initializer that does this:

require 'consistency_fail/enforcer'
ConsistencyFail::Enforcer.enforce!

This will make it so that you can't save or load any ActiveRecord models until you go back and add your unique indexes. Of course, you'll need to make it so Rails can find consistency_fail/enforcer by having consistency_fail in your Gemfile, or by some other mechanism.

This mega-fail mode is nice to have if you have a large team and want to ensure that new models or validations/associations follow the rules.

If you're using the Enforcer, depending on your project, you may need to delay the initializer until later, so that model files can be loaded only once gem dependencies have been satisfied. One possible way is to move the code above to the end of environment.rb or to the more specific config/environment/* files.

Using with Guard

There is a guard integration plugin available. See guard-consistency_fail.

License

Released under the MIT License. See the LICENSE file for further details.

More Repositories

1

reply

REPL-y: A fitter, happier, more productive REPL for Clojure.
Clojure
338
star
2

macro-workshop

A Clojure macros workshop
Clojure
39
star
3

versioneer

Provides version introspection for Leiningen-generated projects
Clojure
39
star
4

sicp-study

Working through exercises in SICP (Abelson/Sussman _The Structure and Interpretation of Computer Programs_)
Scheme
9
star
5

tictactoe-scala

The classic Tic-Tac-Toe game in Scala
Scala
7
star
6

toga

A Clojure wrapper for Cassandra
Clojure
7
star
7

clojureslim

The Slim (Simple List Invocation Method) protocol for Fitnesse, implented for Clojure
Clojure
7
star
8

c10k

Learning about C and networking via the C10K problem: http://www.kegel.com/c10k.html
C
7
star
9

tictactoe-clojure

The classic Tic-Tac-Toe game in Clojure
Clojure
7
star
10

vimfiles

my Vim setup
Vim Script
6
star
11

tictactoe-java

My first apprenticeship project at 8th Light
Java
6
star
12

lein-plugin

A Leiningen plugin to manage Leiningen plugins
Clojure
5
star
13

algorithms-clojure

8th Light University homework
Clojure
5
star
14

taking-command-of-the-command-line-workshop

A hands-on workshop focused on the Unix command line!
Ruby
4
star
15

node-mini-twitter

A toy Node.js app for the Twitter API
JavaScript
3
star
16

keypressr

Merb web app to improve programmers' typing skills
Ruby
3
star
17

minisculus

An Eden Development programming challenge
Clojure
3
star
18

object-vector-is-not-a-value

Scala
2
star
19

slowdb

A non-blockchain slow database
C
2
star
20

speclj-template

A leiningen template for speclj
Clojure
2
star
21

presentations

2
star
22

story-of-a-girl

JavaScript
2
star
23

speling_expirt

for Ruby Sparring 2009: Hangman
Ruby
2
star
24

trptcolin.github.com

an online resume. ish.
2
star
25

algorithms-ruby

8th Light University homework
Ruby
2
star
26

test-driven-carousel

Toy project from Test-Driven React by Trevor Burnham (PragProg)
JavaScript
1
star
27

clojure-refactoring-exercises

Clojure
1
star
28

spec-race-repro

CLJ-2026 repro case
Clojure
1
star
29

try_git

1
star
30

centiservices

HTML
1
star