mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Implement savepoints.
This commit is contained in:
parent
18bf7b421d
commit
b3420f5a2e
7 changed files with 185 additions and 14 deletions
|
@ -55,12 +55,14 @@ module ActiveRecord
|
|||
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
|
||||
|
@ -68,21 +70,23 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
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 @@ module ActiveRecord
|
|||
# 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 @@ module ActiveRecord
|
|||
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 @@ module Test #:nodoc:
|
|||
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 @@ module Test #:nodoc:
|
|||
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 @@ module ActiveRecord
|
|||
# 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 @@ class TransactionTest < ActiveRecord::TestCase
|
|||
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 @@ class TransactionTest < ActiveRecord::TestCase
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue