consistency_fail 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/consistency_fail CHANGED
@@ -1,5 +1,16 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ begin
4
+ require File.join(Dir.pwd, "config", "boot")
5
+ rescue LoadError => e
6
+ puts "\nUh-oh! You must be in the root directory of a Rails project.\n"
7
+ raise
8
+ end
9
+
10
+ require 'active_record'
11
+ require File.join(Dir.pwd, "config", "environment")
12
+
13
+ $:<< File.join(File.dirname(__FILE__), "..", "lib")
3
14
  require "consistency_fail"
4
15
 
5
16
  def problems(models, introspector)
@@ -1,14 +1,3 @@
1
- begin
2
- require File.join(Dir.pwd, "config", "boot")
3
- rescue LoadError => e
4
- puts "\nUh-oh! You must be in the root directory of a Rails project.\n"
5
- raise
6
- end
7
-
8
- require 'active_record'
9
- require 'validation_reflection'
10
- require File.join(Dir.pwd, "config", "environment")
11
-
12
1
  require 'consistency_fail/models'
13
2
  require 'consistency_fail/introspectors/table_data'
14
3
  require 'consistency_fail/introspectors/validates_uniqueness_of'
@@ -0,0 +1,56 @@
1
+ require 'consistency_fail'
2
+
3
+ module ConsistencyFail
4
+ class Enforcer
5
+ def problems(models, introspector)
6
+ problems = models.map do |m|
7
+ [m, introspector.missing_indexes(m)]
8
+ end.reject do |m, indexes|
9
+ indexes.empty?
10
+ end
11
+ end
12
+
13
+ def self.enforce!
14
+ models = ConsistencyFail::Models.new($LOAD_PATH)
15
+ models.preload_all
16
+
17
+ introspectors = [ConsistencyFail::Introspectors::ValidatesUniquenessOf.new,
18
+ ConsistencyFail::Introspectors::HasOne.new]
19
+
20
+ problem_models_exist = models.all.detect do |model|
21
+ introspectors.any? {|i| !i.missing_indexes(model).empty?}
22
+ end
23
+
24
+ if problem_models_exist
25
+ mega_fail!
26
+ end
27
+ end
28
+
29
+ def self.mega_fail!
30
+ ActiveRecord::Base.class_eval do
31
+ class << self
32
+ def panic
33
+ raise "You've got missing indexes! Run `consistency_fail` to find and fix them."
34
+ end
35
+
36
+ def find(*arguments)
37
+ panic
38
+ end
39
+
40
+ alias :first :find
41
+ alias :last :find
42
+ alias :count :find
43
+ end
44
+
45
+ def save
46
+ self.class.panic
47
+ end
48
+
49
+ def save!
50
+ self.class.panic
51
+ end
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -1,3 +1,4 @@
1
+ require 'validation_reflection'
1
2
  require 'consistency_fail/index'
2
3
 
3
4
  module ConsistencyFail
@@ -1,3 +1,3 @@
1
1
  module ConsistencyFail
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consistency_fail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Colin Jones
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-10 00:00:00 -05:00
18
+ date: 2011-06-17 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -89,6 +89,7 @@ files:
89
89
  - bin/consistency_fail
90
90
  - consistency_fail.gemspec
91
91
  - lib/consistency_fail.rb
92
+ - lib/consistency_fail/enforcer.rb
92
93
  - lib/consistency_fail/index.rb
93
94
  - lib/consistency_fail/introspectors/has_one.rb
94
95
  - lib/consistency_fail/introspectors/table_data.rb