From 2c3ca4c4e6d62eb00d46bcf767d04917428f27a7 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 9 Oct 2006 07:48:27 +0000 Subject: Don't rollback in teardown unless a transaction was started. Don't start a transaction in create_fixtures if a transaction is started. Closes #6282. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5270 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/fixtures.rb | 8 ++++---- activerecord/test/fixtures_test.rb | 28 +++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index a88b7523a5..4a3be8bee4 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Don't rollback in teardown unless a transaction was started. Don't start a transaction in create_fixtures if a transaction is started. #6282 [lukfugl, Jeremy Kemper] + * Add #delete support to has_many :through associations. Closes #6049 [Martin Landers] * Reverted old select_limited_ids_list postgresql fix that caused issues in mysql. Closes #5851 [Rick] diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index e5d258288d..6882a44b71 100755 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -252,7 +252,7 @@ class Fixtures < YAML::Omap end all_loaded_fixtures.merge! fixtures_map - connection.transaction do + connection.transaction(Thread.current['open_transactions'] == 0) do fixtures.reverse.each { |fixture| fixture.delete_existing_fixtures } fixtures.each { |fixture| fixture.insert_fixtures } @@ -542,10 +542,10 @@ module Test #:nodoc: def teardown_with_fixtures return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? - # Rollback changes. - if use_transactional_fixtures? + # Rollback changes if a transaction is active. + if use_transactional_fixtures? && !Thread.current['open_transactions'].zero? ActiveRecord::Base.connection.rollback_db_transaction - ActiveRecord::Base.send :decrement_open_transactions + Thread.current['open_transactions'] = 0 end ActiveRecord::Base.verify_active_connections! end diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb index 2f88feda3f..96788a09bd 100755 --- a/activerecord/test/fixtures_test.rb +++ b/activerecord/test/fixtures_test.rb @@ -361,4 +361,30 @@ class ManyToManyFixturesWithClassDefined < Test::Unit::TestCase def test_this_should_run_cleanly assert true end -end \ No newline at end of file +end + + +class FixturesBrokenRollbackTest < Test::Unit::TestCase + def blank_setup; end + alias_method :ar_setup_with_fixtures, :setup_with_fixtures + alias_method :setup_with_fixtures, :blank_setup + alias_method :setup, :blank_setup + + def blank_teardown; end + alias_method :ar_teardown_with_fixtures, :teardown_with_fixtures + alias_method :teardown_with_fixtures, :blank_teardown + alias_method :teardown, :blank_teardown + + def test_no_rollback_in_teardown_unless_transaction_active + assert_equal 0, Thread.current['open_transactions'] + assert_raise(RuntimeError) { ar_setup_with_fixtures } + assert_equal 0, Thread.current['open_transactions'] + assert_nothing_raised { ar_teardown_with_fixtures } + assert_equal 0, Thread.current['open_transactions'] + end + + private + def load_fixtures + raise 'argh' + end +end -- cgit v1.2.3