Skip to content

Commit

Permalink
Patch mega-fail into 2.3 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
trptcolin committed Jun 17, 2011
1 parent 6cd9a81 commit 29237e0
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
11 changes: 11 additions & 0 deletions bin/consistency_fail
@@ -1,5 +1,16 @@
#!/usr/bin/env ruby

begin
require File.join(Dir.pwd, "config", "boot")
rescue LoadError => e
puts "\nUh-oh! You must be in the root directory of a Rails project.\n"
raise
end

require 'active_record'
require File.join(Dir.pwd, "config", "environment")

$:<< File.join(File.dirname(__FILE__), "..", "lib")
require "consistency_fail"

def problems(models, introspector)
Expand Down
11 changes: 0 additions & 11 deletions lib/consistency_fail.rb
@@ -1,14 +1,3 @@
begin
require File.join(Dir.pwd, "config", "boot")
rescue LoadError => e
puts "\nUh-oh! You must be in the root directory of a Rails project.\n"
raise
end

require 'active_record'
require 'validation_reflection'
require File.join(Dir.pwd, "config", "environment")

require 'consistency_fail/models'
require 'consistency_fail/introspectors/table_data'
require 'consistency_fail/introspectors/validates_uniqueness_of'
Expand Down
56 changes: 56 additions & 0 deletions lib/consistency_fail/enforcer.rb
@@ -0,0 +1,56 @@
require 'consistency_fail'

module ConsistencyFail
class Enforcer
def problems(models, introspector)
problems = models.map do |m|
[m, introspector.missing_indexes(m)]
end.reject do |m, indexes|
indexes.empty?
end
end

def self.enforce!
models = ConsistencyFail::Models.new($LOAD_PATH)
models.preload_all

introspectors = [ConsistencyFail::Introspectors::ValidatesUniquenessOf.new,
ConsistencyFail::Introspectors::HasOne.new]

problem_models_exist = models.all.detect do |model|
introspectors.any? {|i| !i.missing_indexes(model).empty?}
end

if problem_models_exist
mega_fail!
end
end

def self.mega_fail!
ActiveRecord::Base.class_eval do
class << self
def panic
raise "You've got missing indexes! Run `consistency_fail` to find and fix them."
end

def find(*arguments)
panic
end

alias :first :find
alias :last :find
alias :count :find
end

def save
self.class.panic
end

def save!
self.class.panic
end
end
end

end
end
@@ -1,3 +1,4 @@
require 'validation_reflection'
require 'consistency_fail/index'

module ConsistencyFail
Expand Down

0 comments on commit 29237e0

Please sign in to comment.