aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Viney <jonathan@jonathan-desktop.(none)>2008-08-31 21:09:16 +1200
committerHongli Lai (Phusion) <hongli@phusion.nl>2008-11-03 20:55:26 +0100
commitb3420f5a2e3c38e5efc2b3d995354c39af09569e (patch)
tree6d24a360aa1407d2653ea178e0e191f82a444254
parent18bf7b421d55c60029289edef1df409a58d021e4 (diff)
downloadrails-b3420f5a2e3c38e5efc2b3d995354c39af09569e.tar.gz
rails-b3420f5a2e3c38e5efc2b3d995354c39af09569e.tar.bz2
rails-b3420f5a2e3c38e5efc2b3d995354c39af09569e.zip
Implement savepoints.
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb16
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb15
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb11
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb11
-rw-r--r--activerecord/lib/active_record/fixtures.rb2
-rw-r--r--activerecord/lib/active_record/transactions.rb12
-rw-r--r--activerecord/test/cases/transactions_test.rb132
7 files changed, 185 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
index 97c6cd4331..f23fe5a90c 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -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
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index c5183357a1..cb5c5740c3 100755
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -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?
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 1e452ae88a..721b365bc7 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -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]
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 60ec01b95e..f34093f0ff 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -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 ========================================
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 114141a646..3d8b0e40a9 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -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
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 27b5aca18f..56b62474d9 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -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
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index b12ec36455..27cc841dd3 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -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