Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
[dm-constraints] auto_migrate! should work with missing has association.
Browse files Browse the repository at this point in the history
Many-to-one associations should now set a NO ACTION constraint in the
database when there is no matching one-to-many association; previously,
this case would cause an exception to be raised during auto-migrate.

Fixes Lighthouse ticket 781.
  • Loading branch information
gcampbell authored and Dan Kubb committed Jan 28, 2009
1 parent 8fb4fdb commit 7315e2f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dm-constraints/lib/dm-constraints/data_objects_adapter.rb
Expand Up @@ -14,7 +14,7 @@ def create_constraints_statements(repository_name, model)
foreign_keys = parent.key.map { |key| property_to_column_name(parent.repository(repository_name), key, false) }

one_to_many_relationship = parent.relationships.values.select { |rel| rel.child_model == model }.first
delete_constraint_type = case one_to_many_relationship.delete_constraint
delete_constraint_type = case one_to_many_relationship.nil? ? :protect : one_to_many_relationship.delete_constraint
when :protect, nil
"NO ACTION"
when :destroy, :destroy!
Expand Down
52 changes: 51 additions & 1 deletion dm-constraints/spec/integration/constraints_spec.rb
Expand Up @@ -43,10 +43,22 @@ class ::Cow
belongs_to :farmer
end

#Used to test a belongs_to association with no has() association
#on the other end
class ::Pig
include DataMapper::Resource
include DataMapper::Constraints

property :id, Serial
property :name, String

belongs_to :farmer
end

DataMapper.auto_migrate!
end

it "is included when DataMapper::Searchable is loaded" do
it "is included when DataMapper::Constraints is loaded" do
Cow.new.should be_kind_of(DataMapper::Constraints)
end

Expand Down Expand Up @@ -217,6 +229,9 @@ class ::Cow
before do
end

class ::Farmer
end

it "should raise an error" do
lambda do
class ::Farmer
Expand All @@ -229,5 +244,40 @@ class ::Farmer

end # describe 'constraint options'

describe "belongs_to without matching has association" do
it "should destroy the parent if there are no children in the association" do
@f1 = Farmer.create(:first_name => "John", :last_name => "Doe")
@f2 = Farmer.create(:first_name => "Some", :last_name => "Body")
@p1 = Pig.create(:name => "Bea", :farmer => @f2)
@f1.destroy.should == true
end
case adapter
when :mysql
it "should raise a MysqlError when destroying the parent if there are children in the association" do
@f = Farmer.create(:first_name => "John", :last_name => "Doe")
@p1 = Pig.create(:name => "Bea", :farmer => @f)
lambda {@f.destroy}.should raise_error(MysqlError)
end
when :postgres
it "should raise a PostgresError when destroying the parent if there are children in the association" do
@f = Farmer.create(:first_name => "John", :last_name => "Doe")
@p1 = Pig.create(:name => "Bea", :farmer => @f)
lambda {@f.destroy}.should raise_error(PostgresError)
end
else
it "should destroy the parent if there are children in the association and the adapter does not support constraints" do
@f = Farmer.create(:first_name => "John", :last_name => "Doe")
@p1 = Pig.create(:name => "Bea", :farmer => @f)
@f.destroy.should == true
end
end

it "the child should be destroyable" do
@f = Farmer.create(:first_name => "John", :last_name => "Doe")
@p = Pig.create(:name => "Bea", :farmer => @f)
@p.destroy.should == true
end

end # describe 'belongs_to without matching has association'
end # DataMapper::Constraints
end # ADAPTERS.each

0 comments on commit 7315e2f

Please sign in to comment.