Skip to content

Commit

Permalink
Merge pull request #86 from dskim/active_record_transaction
Browse files Browse the repository at this point in the history
Don't clean it if nothing can be cleaned, fixes AR transaction bug.
  • Loading branch information
bmabey committed Dec 16, 2011
2 parents 7d4b21a + f921a26 commit d9259c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/database_cleaner/active_record/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def start


def clean
return unless connection_klass.connection.open_transactions > 0

connection_klass.connection.rollback_db_transaction

if connection_klass.connection.respond_to?(:decrement_open_transactions)
Expand Down
12 changes: 12 additions & 0 deletions spec/database_cleaner/active_record/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,32 @@ module ActiveRecord

describe "#clean" do
it "should start a transaction" do
connection.should_receive(:open_transactions).and_return(1)

connection.stub!(:decrement_open_transactions)

connection.should_receive(:rollback_db_transaction)
Transaction.new.clean
end

it "should decrement open transactions if possible" do
connection.should_receive(:open_transactions).and_return(1)

connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
connection.stub!(:rollback_db_transaction)

connection.should_receive(:decrement_open_transactions)
Transaction.new.clean
end

it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
connection.should_receive(:open_transactions).and_return(0)

Transaction.new.clean
end

it "should decrement connection via ActiveRecord::Base if connection won't" do
connection.should_receive(:open_transactions).and_return(1)
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
connection.stub!(:rollback_db_transaction)

Expand Down

0 comments on commit d9259c3

Please sign in to comment.