consistency_fail 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/consistency_fail CHANGED
@@ -14,7 +14,7 @@ $:<< File.join(File.dirname(__FILE__), "..", "lib")
14
14
  require "consistency_fail"
15
15
 
16
16
  def problems(models, introspector)
17
- problems = models.map do |m|
17
+ models.map do |m|
18
18
  [m, introspector.missing_indexes(m)]
19
19
  end.reject do |m, indexes|
20
20
  indexes.empty?
@@ -38,6 +38,11 @@ problems = problems(models.all, introspector)
38
38
  reporter.report_has_one_problems(problems)
39
39
  success &&= problems.empty?
40
40
 
41
+ introspector = ConsistencyFail::Introspectors::Polymorphic.new
42
+ problems = problems(models.all, introspector)
43
+ reporter.report_polymorphic_problems(problems)
44
+ success &&= problems.empty?
45
+
41
46
  if success
42
47
  exit 0
43
48
  else
@@ -2,4 +2,5 @@ require 'consistency_fail/models'
2
2
  require 'consistency_fail/introspectors/table_data'
3
3
  require 'consistency_fail/introspectors/validates_uniqueness_of'
4
4
  require 'consistency_fail/introspectors/has_one'
5
+ require 'consistency_fail/introspectors/polymorphic'
5
6
  require 'consistency_fail/reporter'
@@ -5,7 +5,7 @@ module ConsistencyFail
5
5
  class HasOne
6
6
  def instances(model)
7
7
  model.reflect_on_all_associations.select do |a|
8
- a.macro == :has_one
8
+ a.macro == :has_one && a.options[:as].to_s.length == 0
9
9
  end
10
10
  end
11
11
 
@@ -0,0 +1,43 @@
1
+ require 'consistency_fail/index'
2
+
3
+ module ConsistencyFail
4
+ module Introspectors
5
+ class Polymorphic
6
+ def instances(model)
7
+ model.reflect_on_all_associations.select do |a|
8
+ a.macro == :has_one && a.options[:as].to_s.length > 0
9
+ end
10
+ end
11
+
12
+ def desired_indexes(model)
13
+ instances(model).map do |a|
14
+ as = a.options[:as]
15
+ as_type = "#{as}_type"
16
+ as_id = "#{as}_id"
17
+
18
+ ConsistencyFail::Index.new(
19
+ a.class_name.constantize,
20
+ a.table_name.to_s,
21
+ [as_type, as_id]
22
+ )
23
+ end.compact
24
+ end
25
+ private :desired_indexes
26
+
27
+ def missing_indexes(model)
28
+ desired = desired_indexes(model)
29
+
30
+ existing_indexes = desired.inject([]) do |acc, d|
31
+ acc += TableData.new.unique_indexes_by_table(d.model,
32
+ d.model.connection,
33
+ d.table_name)
34
+ end
35
+
36
+ desired.reject do |index|
37
+ existing_indexes.include?(index)
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -1,5 +1,6 @@
1
1
  require 'consistency_fail/reporters/validates_uniqueness_of'
2
2
  require 'consistency_fail/reporters/has_one'
3
+ require 'consistency_fail/reporters/polymorphic'
3
4
 
4
5
  module ConsistencyFail
5
6
  class Reporter
@@ -10,5 +11,9 @@ module ConsistencyFail
10
11
  def report_has_one_problems(indexes_by_model)
11
12
  ConsistencyFail::Reporters::HasOne.new.report(indexes_by_model)
12
13
  end
14
+
15
+ def report_polymorphic_problems(indexes_by_model)
16
+ ConsistencyFail::Reporters::Polymorphic.new.report(indexes_by_model)
17
+ end
13
18
  end
14
19
  end
@@ -0,0 +1,13 @@
1
+ require 'consistency_fail/reporters/base'
2
+
3
+ module ConsistencyFail
4
+ module Reporters
5
+ class Polymorphic < Base
6
+ attr_reader :macro
7
+
8
+ def initialize
9
+ @macro = :has_one_with_polymorphic
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module ConsistencyFail
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -17,7 +17,7 @@ describe ConsistencyFail::Introspectors::HasOne do
17
17
 
18
18
  it "finds one" do
19
19
  model = fake_ar_model("User")
20
- association = double("association", :macro => :has_one)
20
+ association = double("association", :macro => :has_one, :options => {})
21
21
  model.stub!(:reflect_on_all_associations).and_return([association])
22
22
 
23
23
  subject.instances(model).should == [association]
@@ -30,11 +30,19 @@ describe ConsistencyFail::Introspectors::HasOne do
30
30
 
31
31
  subject.instances(model).should == []
32
32
  end
33
+
34
+ it "finds one, but it's a polymorphic association" do
35
+ model = fake_ar_model("User")
36
+ association = double("association", :macro => :has_one, :options => {:as => "addressable"})
37
+ model.stub!(:reflect_on_all_associations).and_return([association])
38
+
39
+ subject.instances(model).should == []
40
+ end
33
41
  end
34
42
 
35
43
  describe "finding missing indexes" do
36
44
  before do
37
- @association = double("association", :macro => :has_one)
45
+ @association = double("association", :macro => :has_one, :options => {})
38
46
  @model = fake_ar_model("User", :table_exists? => true,
39
47
  :table_name => "users",
40
48
  :class_name => "User",
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+ require 'consistency_fail/introspectors/table_data'
3
+ require 'consistency_fail/introspectors/polymorphic'
4
+
5
+ describe ConsistencyFail::Introspectors::Polymorphic do
6
+ def introspector(model)
7
+ ConsistencyFail::Introspectors::Polymorphic.new(model)
8
+ end
9
+
10
+ describe "instances of polymorphic" do
11
+ it "finds none" do
12
+ model = fake_ar_model("User")
13
+ model.stub(:reflect_on_all_associations).and_return([])
14
+
15
+ subject.instances(model).should == []
16
+ end
17
+
18
+ it "finds one" do
19
+ model = fake_ar_model("User")
20
+ association = double("association", :macro => :has_one, :options => {:as => "addressable"})
21
+ model.stub!(:reflect_on_all_associations).and_return([association])
22
+
23
+ subject.instances(model).should == [association]
24
+ end
25
+
26
+ it "finds other has_one associations, but not polymorphic" do
27
+ model = fake_ar_model("User")
28
+ validation = double("association", :macro => :has_one, :options => {})
29
+ model.stub!(:reflect_on_all_associations).and_return([validation])
30
+
31
+ subject.instances(model).should == []
32
+ end
33
+
34
+ it "finds other non has_one associations" do
35
+ model = fake_ar_model("User")
36
+ validation = double("association", :macro => :has_many)
37
+ model.stub!(:reflect_on_all_associations).and_return([validation])
38
+
39
+ subject.instances(model).should == []
40
+ end
41
+ end
42
+
43
+ describe "finding missing indexes" do
44
+ before do
45
+ @association = double("association", :macro => :has_one, :options => {:as => "addressable"})
46
+ @model = fake_ar_model("User", :table_exists? => true,
47
+ :table_name => "users",
48
+ :class_name => "User",
49
+ :reflect_on_all_associations => [@association])
50
+ @address_class = double("Address Class")
51
+ @address_string = "Address"
52
+ @address_string.stub(:constantize).and_return(@address_class)
53
+ end
54
+
55
+ it "finds one" do
56
+ @association.stub!(:table_name => :addresses, :class_name => @address_string)
57
+ @address_class.stub_chain(:connection, :indexes).with("addresses").and_return([])
58
+
59
+ indexes = subject.missing_indexes(@model)
60
+ indexes.should == [ConsistencyFail::Index.new(fake_ar_model("Address"), "addresses", ["addressable_type", "addressable_id"])]
61
+ end
62
+
63
+ it "finds none when they're already in place" do
64
+ @association.stub!(:table_name => :addresses, :class_name => @address_string)
65
+ index = ConsistencyFail::Index.new(double('model'), "addresses", ["addressable_type", "addressable_id"])
66
+
67
+ fake_connection = double("connection")
68
+ @address_class.stub_chain(:connection).and_return(fake_connection)
69
+
70
+ ConsistencyFail::Introspectors::TableData.stub_chain(:new, :unique_indexes_by_table).
71
+ with(@address_class, fake_connection, "addresses").
72
+ and_return([index])
73
+
74
+ subject.missing_indexes(@model).should == []
75
+ end
76
+ end
77
+ end
@@ -71,4 +71,20 @@ describe ConsistencyFail::Reporter do
71
71
  @fake_out.string.should =~ /Friend\s+users\s+\(email\)/m
72
72
  end
73
73
  end
74
+
75
+ context "polymorphic" do
76
+ it "says everything's good" do
77
+ subject.report_polymorphic_problems([])
78
+
79
+ @fake_out.string.should =~ /Hooray!/
80
+ end
81
+
82
+ it "shows a missing compound index on a single model" do
83
+ missing_indexes = [ConsistencyFail::Index.new(double('model'), "addresses", ["addressable_type", "addressable_id"])]
84
+
85
+ subject.report_polymorphic_problems(fake_ar_model("Address", :table_name => "addresses") => missing_indexes)
86
+
87
+ @fake_out.string.should =~ /Address\s+addresses\s+\(addressable_type, addressable_id\)/m
88
+ end
89
+ end
74
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consistency_fail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-18 00:00:00.000000000 Z
12
+ date: 2013-07-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -76,16 +76,19 @@ files:
76
76
  - lib/consistency_fail/enforcer.rb
77
77
  - lib/consistency_fail/index.rb
78
78
  - lib/consistency_fail/introspectors/has_one.rb
79
+ - lib/consistency_fail/introspectors/polymorphic.rb
79
80
  - lib/consistency_fail/introspectors/table_data.rb
80
81
  - lib/consistency_fail/introspectors/validates_uniqueness_of.rb
81
82
  - lib/consistency_fail/models.rb
82
83
  - lib/consistency_fail/reporter.rb
83
84
  - lib/consistency_fail/reporters/base.rb
84
85
  - lib/consistency_fail/reporters/has_one.rb
86
+ - lib/consistency_fail/reporters/polymorphic.rb
85
87
  - lib/consistency_fail/reporters/validates_uniqueness_of.rb
86
88
  - lib/consistency_fail/version.rb
87
89
  - spec/index_spec.rb
88
90
  - spec/introspectors/has_one_spec.rb
91
+ - spec/introspectors/polymorphic_spec.rb
89
92
  - spec/introspectors/table_data_spec.rb
90
93
  - spec/introspectors/validates_uniqueness_of_spec.rb
91
94
  - spec/models_spec.rb
@@ -118,6 +121,7 @@ summary: A tool to detect missing unique indexes
118
121
  test_files:
119
122
  - spec/index_spec.rb
120
123
  - spec/introspectors/has_one_spec.rb
124
+ - spec/introspectors/polymorphic_spec.rb
121
125
  - spec/introspectors/table_data_spec.rb
122
126
  - spec/introspectors/validates_uniqueness_of_spec.rb
123
127
  - spec/models_spec.rb