Permalink
Browse files

Implement savepoints.

  • Loading branch information...
1 parent 18bf7b4 commit b3420f5a2e3c38e5efc2b3d995354c39af09569e Jonathan Viney committed with FooBarWidget Aug 31, 2008
@@ -55,34 +55,38 @@ def delete(sql, name = nil)
end
# Wrap a block in a transaction. Returns result of block.
- def transaction(start_db_transaction = true)
+ def transaction(start_db_transaction = false)
+ start_db_transaction ||= open_transactions.zero? || (open_transactions == 1 && transactional_fixtures)
transaction_open = false
begin
if block_given?
if start_db_transaction
- begin_db_transaction
+ open_transactions.zero? ? begin_db_transaction : create_savepoint
+ increment_open_transactions
transaction_open = true
end
yield
end
rescue Exception => database_transaction_rollback
if transaction_open
transaction_open = false
- rollback_db_transaction
+ decrement_open_transactions
+ open_transactions.zero? ? rollback_db_transaction : rollback_to_savepoint
end
raise unless database_transaction_rollback.is_a? ActiveRecord::Rollback
end
ensure
if transaction_open
+ decrement_open_transactions
begin
- commit_db_transaction
+ open_transactions.zero? ? commit_db_transaction : release_savepoint
rescue Exception => database_transaction_rollback
- rollback_db_transaction
+ open_transactions.zero? ? rollback_db_transaction : rollback_to_savepoint
raise
end
end
end
-
+
# Begins the transaction (and turns off auto-committing).
def begin_db_transaction() end
@@ -159,6 +159,21 @@ def increment_open_transactions
def decrement_open_transactions
@open_transactions -= 1
end
+
+ def create_savepoint
+ end
+
+ def rollback_to_savepoint
+ end
+
+ def release_savepoint
+ end
+
+ def current_savepoint_name
+ "rails_savepoint_#{open_transactions}"
+ end
+
+ attr_accessor :transactional_fixtures
def log_info(sql, name, seconds)
if @logger && @logger.debug?
@@ -343,6 +343,17 @@ def rollback_db_transaction #:nodoc:
# Transactions aren't supported
end
+ def create_savepoint
+ execute("SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def rollback_to_savepoint
+ execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def release_savepoint
+ execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ end
def add_limit_offset!(sql, options) #:nodoc:
if limit = options[:limit]
@@ -567,6 +567,17 @@ def transaction(start_db_transaction = true)
end
end
+ def create_savepoint
+ execute("SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def rollback_to_savepoint
+ execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def release_savepoint(savepoint_number)
+ execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ end
# SCHEMA STATEMENTS ========================================
@@ -932,6 +932,7 @@ def setup_fixtures
end
ActiveRecord::Base.connection.increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
+ ActiveRecord::Base.connection.transactional_fixtures = true
# Load fixtures for every test.
else
Fixtures.reset_cache
@@ -954,6 +955,7 @@ def teardown_fixtures
if use_transactional_fixtures? && ActiveRecord::Base.connection.open_transactions != 0
ActiveRecord::Base.connection.rollback_db_transaction
ActiveRecord::Base.connection.decrement_open_transactions
+ ActiveRecord::Base.connection.transactional_fixtures = false
end
ActiveRecord::Base.clear_active_connections!
end
@@ -122,14 +122,10 @@ def self.included(base)
# One should restart the entire transaction if a StatementError occurred.
module ClassMethods
# See ActiveRecord::Transactions::ClassMethods for detailed documentation.
- def transaction(&block)
- connection.increment_open_transactions
-
- begin
- connection.transaction(connection.open_transactions == 1, &block)
- ensure
- connection.decrement_open_transactions
- end
+ def transaction(options = {}, &block)
+ options.assert_valid_keys :force
+
+ connection.transaction(options[:force], &block)
end
end
@@ -213,6 +213,99 @@ def test_manually_rolling_back_a_transaction
assert Topic.find(2).approved?, "Second should still be approved"
end
+ def test_invalid_keys_for_transaction
+ assert_raises ArgumentError do
+ Topic.transaction :forced => true do
+ end
+ end
+ end
+
+ def test_force_savepoint_in_nested_transaction
+ Topic.transaction do
+ @first.approved = true
+ @second.approved = false
+ @first.save!
+ @second.save!
+
+ begin
+ Topic.transaction :force => true do
+ @first.happy = false
+ @first.save!
+ raise
+ end
+ rescue
+ end
+ end
+
+ assert @first.reload.approved?
+ assert !@second.reload.approved?
+ end
+
+ def test_no_savepoint_in_nested_transaction_without_force
+ Topic.transaction do
+ @first.approved = true
+ @second.approved = false
+ @first.save!
+ @second.save!
+
+ begin
+ Topic.transaction do
+ @first.approved = false
+ @first.save!
+ raise
+ end
+ rescue
+ end
+ end
+
+ assert !@first.reload.approved?
+ assert !@second.reload.approved?
+ end
+
+ def test_many_savepoints
+ Topic.transaction do
+ @first.content = "One"
+ @first.save!
+
+ begin
+ Topic.transaction :force => true do
+ @first.content = "Two"
+ @first.save!
+
+ begin
+ Topic.transaction :force => true do
+ @first.content = "Three"
+ @first.save!
+
+ begin
+ Topic.transaction :force => true do
+ @first.content = "Four"
+ @first.save!
+ raise
+ end
+ rescue
+ end
+
+ @three = @first.reload.content
+ raise
+ end
+ rescue
+ end
+
+ @two = @first.reload.content
+ raise
+ end
+ rescue
+ end
+
+ @one = @first.reload.content
+ end
+
+ assert_equal "One", @one
+ assert_equal "Two", @two
+ assert_equal "Three", @three
+ end
+
uses_mocha 'mocking connection.commit_db_transaction' do
def test_rollback_when_commit_raises
Topic.connection.expects(:begin_db_transaction)
@@ -282,6 +375,45 @@ def remove_exception_raising_after_create_callback_to_topic
end
end
+class TransactionsWithTransactionalFixturesTest < ActiveRecord::TestCase
+ self.use_transactional_fixtures = true
+ fixtures :topics
+
+ def test_automatic_savepoint_in_outer_transaction
+ @first = Topic.find(1)
+
+ begin
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+ raise
+ end
+ rescue
+ assert !@first.reload.approved?
+ end
+ end
+
+ def test_no_automatic_savepoint_for_inner_transaction
+ @first = Topic.find(1)
+
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+
+ begin
+ Topic.transaction do
+ @first.approved = false
+ @first.save!
+ raise
+ end
+ rescue
+ end
+ end
+
+ assert !@first.reload.approved?
+ end
+end
+
if current_adapter?(:PostgreSQLAdapter)
class ConcurrentTransactionTest < TransactionTest
use_concurrent_connections

0 comments on commit b3420f5

Please sign in to comment.