Browse files

Fix major bug in has_one introspector which reversed table names. Issue

  • Loading branch information...
1 parent e028bb3 commit f84c14cac3df10e7ba4d38c41e42f2bc23ac8929 @trptcolin committed Jan 4, 2012
View
9 lib/consistency_fail/introspectors/has_one.rb
@@ -18,9 +18,14 @@ def desired_indexes(model)
private :desired_indexes
def missing_indexes(model)
- existing_indexes = TableData.new.unique_indexes(model)
+ desired = desired_indexes(model)
- desired_indexes(model).reject do |index|
+ existing_indexes = desired.inject([]) do |acc, d|
+ # TODO: This assumes the models share a database. Need to make that configurable somehow.
+ acc += TableData.new.unique_indexes_by_table(model.connection, d.table_name)
+ end
+
+ desired.reject do |index|
existing_indexes.include?(index)
end
end
View
8 lib/consistency_fail/introspectors/table_data.rb
@@ -6,9 +6,13 @@ class TableData
def unique_indexes(model)
return [] if !model.table_exists?
- ar_indexes = model.connection.indexes(model.table_name).select(&:unique)
+ unique_indexes_by_table(model.connection, model.table_name)
+ end
+
+ def unique_indexes_by_table(connection, table_name)
+ ar_indexes = connection.indexes(table_name).select(&:unique)
ar_indexes.map do |index|
- ConsistencyFail::Index.new(model.table_name, index.columns)
+ ConsistencyFail::Index.new(table_name, index.columns)
end
end
end
View
9 spec/introspectors/has_one_spec.rb
@@ -42,7 +42,7 @@ def introspector(model)
it "finds one" do
@association.stub!(:table_name => :addresses, :primary_key_name => "user_id")
- @model.stub_chain(:connection, :indexes).with("users").and_return([])
+ @model.stub_chain(:connection, :indexes).with("addresses").and_return([])
indexes = subject.missing_indexes(@model)
indexes.should == [ConsistencyFail::Index.new("addresses", ["user_id"])]
@@ -51,7 +51,12 @@ def introspector(model)
it "finds none when they're already in place" do
@association.stub!(:table_name => :addresses, :primary_key_name => "user_id")
index = ConsistencyFail::Index.new("addresses", ["user_id"])
- ConsistencyFail::Introspectors::TableData.stub_chain(:new, :unique_indexes).
+
+ fake_connection = double("connection")
+ @model.stub_chain(:connection).and_return(fake_connection)
+
+ ConsistencyFail::Introspectors::TableData.stub_chain(:new, :unique_indexes_by_table).
+ with(fake_connection, "addresses").
and_return([index])
subject.missing_indexes(@model).should == []

0 comments on commit f84c14c

Please sign in to comment.