savepoint_name should return nil for non-savepoint transactions

Also add test to assets the savepoint name
上级 97bb76dc
......@@ -69,6 +69,10 @@ def initialize(connection)
def state
@state
end
def savepoint_name
nil
end
end
class TransactionState
......@@ -233,7 +237,8 @@ def initialize(connection, parent, options = {})
super
@savepoint_name = "active_record_#{number}"
# Savepoint name only counts the Savepoint transactions, so we need to subtract 1
@savepoint_name = "active_record_#{number - 1}"
connection.create_savepoint(@savepoint_name)
end
......
......@@ -358,9 +358,7 @@ def case_insensitive_comparison(table, attribute, column, value)
end
def current_savepoint_name
if current_transaction.is_a? SavepointTransaction
current_transaction.savepoint_name
end
current_transaction.savepoint_name
end
# Check the connection back in to the connection pool
......
......@@ -424,6 +424,26 @@ def test_releasing_named_savepoints
end
end
def test_savepoints_name
Topic.transaction do
assert_nil Topic.connection.current_savepoint_name
assert_nil Topic.connection.current_transaction.savepoint_name
Topic.transaction(requires_new: true) do
assert_equal "active_record_1", Topic.connection.current_savepoint_name
assert_equal "active_record_1", Topic.connection.current_transaction.savepoint_name
Topic.transaction(requires_new: true) do
assert_equal "active_record_2", Topic.connection.current_savepoint_name
assert_equal "active_record_2", Topic.connection.current_transaction.savepoint_name
end
assert_equal "active_record_1", Topic.connection.current_savepoint_name
assert_equal "active_record_1", Topic.connection.current_transaction.savepoint_name
end
end
end
def test_rollback_when_commit_raises
Topic.connection.expects(:begin_db_transaction)
Topic.connection.expects(:commit_db_transaction).raises('OH NOES')
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册