mirror of
https://github.com/DatabaseCleaner/database_cleaner
synced 2023-03-27 23:22:03 -04:00
Merge pull request #189 from dchelimsky/fix-188
Use begin_transaction if connection responds to it (AR 4)
This commit is contained in:
commit
0f6af3efc8
2 changed files with 56 additions and 46 deletions
|
@ -14,14 +14,22 @@ module DatabaseCleaner::ActiveRecord
|
||||||
connection_class.__send__(:increment_open_transactions)
|
connection_class.__send__(:increment_open_transactions)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
if connection_class.connection.respond_to?(:begin_transaction)
|
||||||
|
connection_class.connection.begin_transaction
|
||||||
|
else
|
||||||
connection_class.connection.begin_db_transaction
|
connection_class.connection.begin_db_transaction
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def clean
|
def clean
|
||||||
return unless connection_class.connection.open_transactions > 0
|
return unless connection_class.connection.open_transactions > 0
|
||||||
|
|
||||||
|
if connection_class.connection.respond_to?(:rollback_transaction)
|
||||||
|
connection_class.connection.rollback_transaction
|
||||||
|
else
|
||||||
connection_class.connection.rollback_db_transaction
|
connection_class.connection.rollback_db_transaction
|
||||||
|
end
|
||||||
|
|
||||||
# The below is for handling after_commit hooks.. see https://github.com/bmabey/database_cleaner/issues/99
|
# The below is for handling after_commit hooks.. see https://github.com/bmabey/database_cleaner/issues/99
|
||||||
if connection_class.connection.respond_to?(:rollback_transaction_records)
|
if connection_class.connection.respond_to?(:rollback_transaction_records)
|
||||||
|
|
|
@ -8,40 +8,45 @@ module DatabaseCleaner
|
||||||
describe Transaction do
|
describe Transaction do
|
||||||
let (:connection) { mock("connection") }
|
let (:connection) { mock("connection") }
|
||||||
before(:each) do
|
before(:each) do
|
||||||
::ActiveRecord::Base.stub!(:connection).and_return(connection)
|
::ActiveRecord::Base.stub(:connection).and_return(connection)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#start" do
|
describe "#start" do
|
||||||
it "should increment open transactions if possible" do
|
[:begin_transaction, :begin_db_transaction].each do |begin_transaction_method|
|
||||||
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(true)
|
context "using #{begin_transaction_method}" do
|
||||||
connection.stub!(:begin_db_transaction)
|
before do
|
||||||
|
connection.stub(begin_transaction_method)
|
||||||
|
connection.stub(:respond_to?).with(:begin_transaction).and_return(:begin_transaction == begin_transaction_method)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should increment open transactions if possible" do
|
||||||
|
connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
|
||||||
connection.should_receive(:increment_open_transactions)
|
connection.should_receive(:increment_open_transactions)
|
||||||
Transaction.new.start
|
Transaction.new.start
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
|
it "should tell ActiveRecord to increment connection if its not possible to increment current connection" do
|
||||||
connection.stub!(:respond_to?).with(:increment_open_transactions).and_return(false)
|
connection.stub(:respond_to?).with(:increment_open_transactions).and_return(false)
|
||||||
connection.stub!(:begin_db_transaction)
|
|
||||||
|
|
||||||
::ActiveRecord::Base.should_receive(:increment_open_transactions)
|
::ActiveRecord::Base.should_receive(:increment_open_transactions)
|
||||||
Transaction.new.start
|
Transaction.new.start
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should start a transaction" do
|
it "should start a transaction" do
|
||||||
connection.stub!(:increment_open_transactions)
|
connection.stub(:respond_to?).with(:increment_open_transactions).and_return(true)
|
||||||
|
connection.stub(:increment_open_transactions)
|
||||||
connection.should_receive(:begin_db_transaction)
|
connection.should_receive(begin_transaction_method)
|
||||||
Transaction.new.start
|
Transaction.new.start
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "#clean" do
|
describe "#clean" do
|
||||||
context "manual accounting of transaction count" do
|
context "manual accounting of transaction count" do
|
||||||
it "should start a transaction" do
|
it "should start a transaction" do
|
||||||
connection.should_receive(:open_transactions).and_return(1)
|
connection.should_receive(:open_transactions).and_return(1)
|
||||||
|
|
||||||
connection.stub!(:decrement_open_transactions)
|
connection.stub(:decrement_open_transactions)
|
||||||
|
|
||||||
connection.should_receive(:rollback_db_transaction)
|
connection.should_receive(:rollback_db_transaction)
|
||||||
Transaction.new.clean
|
Transaction.new.clean
|
||||||
|
@ -50,9 +55,10 @@ module DatabaseCleaner
|
||||||
it "should decrement open transactions if possible" do
|
it "should decrement open transactions if possible" do
|
||||||
connection.should_receive(:open_transactions).and_return(1)
|
connection.should_receive(:open_transactions).and_return(1)
|
||||||
|
|
||||||
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(true)
|
connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(true)
|
||||||
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
|
connection.stub(:respond_to?).with(:rollback_transaction_records).and_return(false)
|
||||||
connection.stub!(:rollback_db_transaction)
|
connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
|
||||||
|
connection.stub(:rollback_db_transaction)
|
||||||
|
|
||||||
connection.should_receive(:decrement_open_transactions)
|
connection.should_receive(:decrement_open_transactions)
|
||||||
Transaction.new.clean
|
Transaction.new.clean
|
||||||
|
@ -66,30 +72,30 @@ module DatabaseCleaner
|
||||||
|
|
||||||
it "should decrement connection via ActiveRecord::Base if connection won't" do
|
it "should decrement connection via ActiveRecord::Base if connection won't" do
|
||||||
connection.should_receive(:open_transactions).and_return(1)
|
connection.should_receive(:open_transactions).and_return(1)
|
||||||
connection.stub!(:respond_to?).with(:decrement_open_transactions).and_return(false)
|
connection.stub(:respond_to?).with(:decrement_open_transactions).and_return(false)
|
||||||
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
|
connection.stub(:respond_to?).with(:rollback_transaction_records).and_return(false)
|
||||||
connection.stub!(:rollback_db_transaction)
|
connection.stub(:respond_to?).with(:rollback_transaction).and_return(false)
|
||||||
|
connection.stub(:rollback_db_transaction)
|
||||||
|
|
||||||
::ActiveRecord::Base.should_receive(:decrement_open_transactions)
|
::ActiveRecord::Base.should_receive(:decrement_open_transactions)
|
||||||
Transaction.new.clean
|
Transaction.new.clean
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "automatic accounting of transaction count" do
|
context "automatic accounting of transaction count (AR 4)" do
|
||||||
|
before {stub_const("ActiveRecord::VERSION::MAJOR", 4) }
|
||||||
|
|
||||||
it "should start a transaction" do
|
it "should start a transaction" do
|
||||||
stub_const("ActiveRecord::VERSION::MAJOR", 4)
|
connection.stub(:rollback_db_transaction)
|
||||||
connection.stub!(:rollback_db_transaction)
|
|
||||||
connection.should_receive(:open_transactions).and_return(1)
|
connection.should_receive(:open_transactions).and_return(1)
|
||||||
|
|
||||||
connection.should_not_receive(:decrement_open_transactions)
|
connection.should_not_receive(:decrement_open_transactions)
|
||||||
connection.should_receive(:rollback_db_transaction)
|
connection.should_receive(:rollback_transaction)
|
||||||
Transaction.new.clean
|
Transaction.new.clean
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should decrement open transactions if possible" do
|
it "should decrement open transactions if possible" do
|
||||||
stub_const("ActiveRecord::VERSION::MAJOR", 4)
|
connection.stub(:rollback_transaction)
|
||||||
connection.stub!(:rollback_db_transaction)
|
|
||||||
connection.should_receive(:open_transactions).and_return(1)
|
connection.should_receive(:open_transactions).and_return(1)
|
||||||
|
|
||||||
connection.should_not_receive(:decrement_open_transactions)
|
connection.should_not_receive(:decrement_open_transactions)
|
||||||
|
@ -97,17 +103,16 @@ module DatabaseCleaner
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
|
it "should not try to decrement or rollback if open_transactions is 0 for whatever reason" do
|
||||||
stub_const("ActiveRecord::VERSION::MAJOR", 4)
|
|
||||||
connection.should_receive(:open_transactions).and_return(0)
|
connection.should_receive(:open_transactions).and_return(0)
|
||||||
|
|
||||||
Transaction.new.clean
|
Transaction.new.clean
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should decrement connection via ActiveRecord::Base if connection won't" do
|
it "should decrement connection via ActiveRecord::Base if connection won't" do
|
||||||
stub_const("ActiveRecord::VERSION::MAJOR", 4)
|
|
||||||
connection.should_receive(:open_transactions).and_return(1)
|
connection.should_receive(:open_transactions).and_return(1)
|
||||||
connection.stub!(:respond_to?).with(:rollback_transaction_records).and_return(false)
|
connection.stub(:respond_to?).with(:rollback_transaction_records).and_return(false)
|
||||||
connection.stub!(:rollback_db_transaction)
|
connection.stub(:respond_to?).with(:rollback_transaction).and_return(true)
|
||||||
|
connection.stub(:rollback_transaction)
|
||||||
|
|
||||||
::ActiveRecord::Base.should_not_receive(:decrement_open_transactions)
|
::ActiveRecord::Base.should_not_receive(:decrement_open_transactions)
|
||||||
Transaction.new.clean
|
Transaction.new.clean
|
||||||
|
@ -127,8 +132,5 @@ module DatabaseCleaner
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue