Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
27ce16aa
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
27ce16aa
编写于
2月 24, 2015
作者:
A
Arthur Nogueira Neves
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #18936 from arthurnn/txn_callbacks
Spike on new transaction callbacks
上级
f4bee7ec
45755135
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
105 addition
and
4 deletion
+105
-4
activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
...active_record/connection_adapters/abstract/transaction.rb
+7
-1
activerecord/lib/active_record/core.rb
activerecord/lib/active_record/core.rb
+1
-1
activerecord/lib/active_record/transactions.rb
activerecord/lib/active_record/transactions.rb
+37
-2
activerecord/test/cases/transaction_callbacks_test.rb
activerecord/test/cases/transaction_callbacks_test.rb
+60
-0
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract/transaction.rb
浏览文件 @
27ce16aa
...
...
@@ -77,6 +77,10 @@ def commit
@state
.
set_state
(
:committed
)
end
def
before_commit_records
records
.
uniq
.
each
(
&
:before_committed!
)
end
def
commit_records
ite
=
records
.
uniq
while
record
=
ite
.
shift
...
...
@@ -159,13 +163,15 @@ def begin_transaction(options = {})
def
commit_transaction
inner_transaction
=
@stack
.
pop
inner_transaction
.
commit
if
current_transaction
.
joinable?
inner_transaction
.
commit
inner_transaction
.
records
.
each
do
|
r
|
r
.
add_to_transaction
end
else
inner_transaction
.
before_commit_records
inner_transaction
.
commit
inner_transaction
.
commit_records
end
end
...
...
activerecord/lib/active_record/core.rb
浏览文件 @
27ce16aa
...
...
@@ -488,7 +488,7 @@ def set_transaction_state(state) # :nodoc:
end
def
has_transactional_callbacks?
# :nodoc:
!
_rollback_callbacks
.
empty?
||
!
_commit_callbacks
.
empty?
!
_rollback_callbacks
.
empty?
||
!
_commit_callbacks
.
empty?
||
!
_before_commit_callbacks
.
empty?
end
# Updates the attributes on this particular ActiveRecord object so that
...
...
activerecord/lib/active_record/transactions.rb
浏览文件 @
27ce16aa
...
...
@@ -7,6 +7,10 @@ module Transactions
included
do
define_callbacks
:commit
,
:rollback
,
:before_commit
,
:before_commit_without_transaction_enrollment
,
:commit_without_transaction_enrollment
,
:rollback_without_transaction_enrollment
,
scope:
[
:kind
,
:name
]
end
...
...
@@ -206,6 +210,11 @@ def transaction(options = {}, &block)
connection
.
transaction
(
options
,
&
block
)
end
def
before_commit
(
*
args
,
&
block
)
# :nodoc:
set_options_for_callbacks!
(
args
)
set_callback
(
:before_commit
,
:before
,
*
args
,
&
block
)
end
# This callback is called after a record has been created, updated, or destroyed.
#
# You can specify that the callback should only be fired by a certain action with
...
...
@@ -233,6 +242,21 @@ def after_rollback(*args, &block)
set_callback
(
:rollback
,
:after
,
*
args
,
&
block
)
end
def
before_commit_without_transaction_enrollment
(
*
args
,
&
block
)
# :nodoc:
set_options_for_callbacks!
(
args
)
set_callback
(
:before_commit_without_transaction_enrollment
,
:before
,
*
args
,
&
block
)
end
def
after_commit_without_transaction_enrollment
(
*
args
,
&
block
)
# :nodoc:
set_options_for_callbacks!
(
args
)
set_callback
(
:commit_without_transaction_enrollment
,
:after
,
*
args
,
&
block
)
end
def
after_rollback_without_transaction_enrollment
(
*
args
,
&
block
)
# :nodoc:
set_options_for_callbacks!
(
args
)
set_callback
(
:rollback_without_transaction_enrollment
,
:after
,
*
args
,
&
block
)
end
def
raise_in_transactional_callbacks
ActiveSupport
::
Deprecation
.
warn
(
'ActiveRecord::Base.raise_in_transactional_callbacks is deprecated and will be removed without replacement.'
)
true
...
...
@@ -296,12 +320,20 @@ def rollback_active_record_state!
clear_transaction_record_state
end
def
before_committed!
# :nodoc:
_run_before_commit_without_transaction_enrollment_callbacks
_run_before_commit_callbacks
end
# Call the +after_commit+ callbacks.
#
# Ensure that it is not called if the object was never persisted (failed create),
# but call it after the commit of a destroyed object.
def
committed!
(
should_run_callbacks:
true
)
#:nodoc:
_run_commit_callbacks
if
should_run_callbacks
&&
destroyed?
||
persisted?
if
should_run_callbacks
&&
destroyed?
||
persisted?
_run_commit_without_transaction_enrollment_callbacks
_run_commit_callbacks
end
ensure
force_clear_transaction_record_state
end
...
...
@@ -309,7 +341,10 @@ def committed!(should_run_callbacks: true) #:nodoc:
# Call the +after_rollback+ callbacks. The +force_restore_state+ argument indicates if the record
# state should be rolled back to the beginning or just to the last savepoint.
def
rolledback!
(
force_restore_state:
false
,
should_run_callbacks:
true
)
#:nodoc:
_run_rollback_callbacks
if
should_run_callbacks
if
should_run_callbacks
_run_rollback_without_transaction_enrollment_callbacks
_run_rollback_callbacks
end
ensure
restore_transaction_record_state
(
force_restore_state
)
clear_transaction_record_state
...
...
activerecord/test/cases/transaction_callbacks_test.rb
浏览文件 @
27ce16aa
...
...
@@ -400,3 +400,63 @@ def test_after_commit_on_multiple_actions
assert_equal
[
:update_and_destroy
,
:create_and_destroy
],
topic
.
history
end
end
class
TransactionEnrollmentCallbacksTest
<
ActiveRecord
::
TestCase
class
TopicWithoutTransactionalEnrollmentCallbacks
<
ActiveRecord
::
Base
self
.
table_name
=
:topics
before_commit_without_transaction_enrollment
{
|
r
|
r
.
history
<<
:before_commit
}
after_commit_without_transaction_enrollment
{
|
r
|
r
.
history
<<
:after_commit
}
after_rollback_without_transaction_enrollment
{
|
r
|
r
.
history
<<
:rollback
}
def
history
@history
||=
[]
end
end
def
setup
@topic
=
TopicWithoutTransactionalEnrollmentCallbacks
.
create!
end
def
test_commit_does_not_run_transactions_callbacks_without_enrollment
@topic
.
transaction
do
@topic
.
content
=
'foo'
@topic
.
save!
end
assert
@topic
.
history
.
empty?
end
def
test_commit_run_transactions_callbacks_with_explicit_enrollment
@topic
.
transaction
do
2
.
times
do
@topic
.
content
=
'foo'
@topic
.
save!
end
@topic
.
class
.
connection
.
add_transaction_record
(
@topic
)
end
assert_equal
[
:before_commit
,
:after_commit
],
@topic
.
history
end
def
test_rollback_does_not_run_transactions_callbacks_without_enrollment
@topic
.
transaction
do
@topic
.
content
=
'foo'
@topic
.
save!
raise
ActiveRecord
::
Rollback
end
assert
@topic
.
history
.
empty?
end
def
test_rollback_run_transactions_callbacks_with_explicit_enrollment
@topic
.
transaction
do
2
.
times
do
@topic
.
content
=
'foo'
@topic
.
save!
end
@topic
.
class
.
connection
.
add_transaction_record
(
@topic
)
raise
ActiveRecord
::
Rollback
end
assert_equal
[
:rollback
],
@topic
.
history
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录