aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/transactions_test.rb
diff options
context:
space:
mode:
authorBrian Durand <brian@embellishedvisions.com>2009-06-02 14:42:22 -0500
committerJeremy Kemper <jeremy@bitsweat.net>2010-04-29 12:24:15 -0700
commitda840d13da865331297d5287391231b1ed39721b (patch)
tree70ffe972358503c16fadd04a1dda336abba9c216 /activerecord/test/cases/transactions_test.rb
parent20f0e9f3203560f34f451a37130caf237bb94867 (diff)
downloadrails-da840d13da865331297d5287391231b1ed39721b.tar.gz
rails-da840d13da865331297d5287391231b1ed39721b.tar.bz2
rails-da840d13da865331297d5287391231b1ed39721b.zip
Add after_commit and after_rollback callbacks to ActiveRecord that are called after transactions either commit or rollback on all records saved or destroyed in the transaction.
[#2991 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/test/cases/transactions_test.rb')
-rw-r--r--activerecord/test/cases/transactions_test.rb61
1 files changed, 47 insertions, 14 deletions
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index c550030329..958a4e4f94 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -262,22 +262,22 @@ class TransactionTest < ActiveRecord::TestCase
assert !@first.reload.approved?
assert !@second.reload.approved?
end if Topic.connection.supports_savepoints?
-
+
def test_many_savepoints
Topic.transaction do
@first.content = "One"
@first.save!
-
+
begin
Topic.transaction :requires_new => true do
@first.content = "Two"
@first.save!
-
+
begin
Topic.transaction :requires_new => true do
@first.content = "Three"
@first.save!
-
+
begin
Topic.transaction :requires_new => true do
@first.content = "Four"
@@ -286,22 +286,22 @@ class TransactionTest < ActiveRecord::TestCase
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
@@ -319,7 +319,34 @@ class TransactionTest < ActiveRecord::TestCase
end
end
end
-
+
+ def test_restore_active_record_state_for_all_records_in_a_transaction
+ topic_1 = Topic.new(:title => 'test_1')
+ topic_2 = Topic.new(:title => 'test_2')
+ Topic.transaction do
+ assert topic_1.save
+ assert topic_2.save
+ @first.save
+ @second.destroy
+ assert_equal false, topic_1.new_record?
+ assert_not_nil topic_1.id
+ assert_equal false, topic_2.new_record?
+ assert_not_nil topic_2.id
+ assert_equal false, @first.new_record?
+ assert_not_nil @first.id
+ assert_equal true, @second.destroyed?
+ raise ActiveRecord::Rollback
+ end
+
+ assert_equal true, topic_1.new_record?
+ assert_nil topic_1.id
+ assert_equal true, topic_2.new_record?
+ assert_nil topic_2.id
+ assert_equal false, @first.new_record?
+ assert_not_nil @first.id
+ assert_equal false, @second.destroyed?
+ end
+
if current_adapter?(:PostgreSQLAdapter) && defined?(PGconn::PQTRANS_IDLE)
def test_outside_transaction_works
assert Topic.connection.outside_transaction?
@@ -328,7 +355,7 @@ class TransactionTest < ActiveRecord::TestCase
Topic.connection.rollback_db_transaction
assert Topic.connection.outside_transaction?
end
-
+
def test_rollback_wont_be_executed_if_no_transaction_active
assert_raise RuntimeError do
Topic.transaction do
@@ -338,7 +365,7 @@ class TransactionTest < ActiveRecord::TestCase
end
end
end
-
+
def test_open_transactions_count_is_reset_to_zero_if_no_transaction_active
Topic.transaction do
Topic.transaction do
@@ -358,12 +385,12 @@ class TransactionTest < ActiveRecord::TestCase
#
# We go back to the connection for the column queries because
# Topic.columns is cached and won't report changes to the DB
-
+
assert_nothing_raised do
Topic.reset_column_information
Topic.connection.add_column('topics', 'stuff', :string)
assert Topic.column_names.include?('stuff')
-
+
Topic.reset_column_information
Topic.connection.remove_column('topics', 'stuff')
assert !Topic.column_names.include?('stuff')
@@ -382,6 +409,12 @@ class TransactionTest < ActiveRecord::TestCase
end
private
+ def define_callback_method(callback_method)
+ define_method(callback_method) do
+ self.history << [callback_method, :method]
+ end
+ end
+
def add_exception_raising_after_save_callback_to_topic
Topic.class_eval <<-eoruby, __FILE__, __LINE__ + 1
remove_method(:after_save_for_transaction)
@@ -440,7 +473,7 @@ class TransactionsWithTransactionalFixturesTest < ActiveRecord::TestCase
def test_automatic_savepoint_in_outer_transaction
@first = Topic.find(1)
-
+
begin
Topic.transaction do
@first.approved = true