Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
4fc30744
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 搜索 >>
提交
4fc30744
编写于
1月 14, 2016
作者:
S
schneems
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Revert "Revert "Set environment even when no migration runs""
This reverts commit
11e85b91
.
上级
13fc4950
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
58 addition
and
19 deletion
+58
-19
activerecord/lib/active_record/migration.rb
activerecord/lib/active_record/migration.rb
+30
-18
activerecord/test/cases/migration_test.rb
activerecord/test/cases/migration_test.rb
+28
-1
未找到文件。
activerecord/lib/active_record/migration.rb
浏览文件 @
4fc30744
...
...
@@ -1170,45 +1170,58 @@ def load_migrated
private
# Used for running a specific migration.
def
run_without_lock
migration
=
migrations
.
detect
{
|
m
|
m
.
version
==
@target_version
}
raise
UnknownMigrationVersionError
.
new
(
@target_version
)
if
migration
.
nil?
unless
(
up?
&&
migrated
.
include?
(
migration
.
version
.
to_i
))
||
(
down?
&&
!
migrated
.
include?
(
migration
.
version
.
to_i
))
begin
execute_migration_in_transaction
(
migration
,
@direction
)
rescue
=>
e
canceled_msg
=
use_transaction?
(
migration
)
?
", this migration was canceled"
:
""
raise
StandardError
,
"An error has occurred
#{
canceled_msg
}
:
\n\n
#{
e
}
"
,
e
.
backtrace
end
end
execute_migration_in_transaction
(
migration
,
@direction
)
record_environment
end
# Used for running multiple migrations up to or down to a certain value.
def
migrate_without_lock
if
!
target
&&
@target_version
&&
@target_version
>
0
if
invalid_target?
raise
UnknownMigrationVersionError
.
new
(
@target_version
)
end
runnable
.
each
do
|
migration
|
Base
.
logger
.
info
"Migrating to
#{
migration
.
name
}
(
#{
migration
.
version
}
)"
if
Base
.
logger
begin
execute_migration_in_transaction
(
migration
,
@direction
)
rescue
=>
e
canceled_msg
=
use_transaction?
(
migration
)
?
"this and "
:
""
raise
StandardError
,
"An error has occurred,
#{
canceled_msg
}
all later migrations canceled:
\n\n
#{
e
}
"
,
e
.
backtrace
end
execute_migration_in_transaction
(
migration
,
@direction
)
end
record_environment
end
# Stores the current environment in the database.
def
record_environment
return
if
down?
ActiveRecord
::
InternalMetadata
[
:environment
]
=
ActiveRecord
::
Migrator
.
current_environment
end
def
ran?
(
migration
)
migrated
.
include?
(
migration
.
version
.
to_i
)
end
# Return true if a valid version is not provided.
def
invalid_target?
!
target
&&
@target_version
&&
@target_version
>
0
end
def
execute_migration_in_transaction
(
migration
,
direction
)
return
if
down?
&&
!
migrated
.
include?
(
migration
.
version
.
to_i
)
return
if
up?
&&
migrated
.
include?
(
migration
.
version
.
to_i
)
Base
.
logger
.
info
"Migrating to
#{
migration
.
name
}
(
#{
migration
.
version
}
)"
if
Base
.
logger
ddl_transaction
(
migration
)
do
migration
.
migrate
(
direction
)
record_version_state_after_migrating
(
migration
.
version
)
end
rescue
=>
e
msg
=
"An error has occurred, "
msg
<<
"this and "
if
use_transaction?
(
migration
)
msg
<<
"all later migrations canceled:
\n\n
#{
e
}
"
raise
StandardError
,
msg
,
e
.
backtrace
end
def
target
...
...
@@ -1238,7 +1251,6 @@ def record_version_state_after_migrating(version)
else
migrated
<<
version
ActiveRecord
::
SchemaMigration
.
create!
(
version:
version
.
to_s
)
ActiveRecord
::
InternalMetadata
[
:environment
]
=
ActiveRecord
::
Migrator
.
current_environment
end
end
...
...
activerecord/test/cases/migration_test.rb
浏览文件 @
4fc30744
...
...
@@ -301,7 +301,7 @@ def migrate(x)
e
=
assert_raise
(
StandardError
)
{
migrator
.
run
}
assert_equal
"An error has occurred, this
migration wa
s canceled:
\n\n
Something broke"
,
e
.
message
assert_equal
"An error has occurred, this
and all later migration
s canceled:
\n\n
Something broke"
,
e
.
message
assert_no_column
Person
,
:last_name
,
"On error, the Migrator should revert schema changes but it did not."
...
...
@@ -398,6 +398,33 @@ def test_internal_metadata_stores_environment
ENV
[
"RACK_ENV"
]
=
original_rack_env
end
def
test_migration_sets_internal_metadata_even_when_fully_migrated
current_env
=
ActiveRecord
::
ConnectionHandling
::
DEFAULT_ENV
.
call
migrations_path
=
MIGRATIONS_ROOT
+
"/valid"
old_path
=
ActiveRecord
::
Migrator
.
migrations_paths
ActiveRecord
::
Migrator
.
migrations_paths
=
migrations_path
ActiveRecord
::
Migrator
.
up
(
migrations_path
)
assert_equal
current_env
,
ActiveRecord
::
InternalMetadata
[
:environment
]
original_rails_env
=
ENV
[
"RAILS_ENV"
]
original_rack_env
=
ENV
[
"RACK_ENV"
]
ENV
[
"RAILS_ENV"
]
=
ENV
[
"RACK_ENV"
]
=
"foofoo"
new_env
=
ActiveRecord
::
ConnectionHandling
::
DEFAULT_ENV
.
call
refute_equal
current_env
,
new_env
sleep
1
# mysql by default does not store fractional seconds in the database
ActiveRecord
::
Migrator
.
up
(
migrations_path
)
assert_equal
new_env
,
ActiveRecord
::
InternalMetadata
[
:environment
]
ensure
ActiveRecord
::
Migrator
.
migrations_paths
=
old_path
ENV
[
"RAILS_ENV"
]
=
original_rails_env
ENV
[
"RACK_ENV"
]
=
original_rack_env
end
def
test_proper_table_name_on_migration
reminder_class
=
new_isolated_reminder_class
migration
=
ActiveRecord
::
Migration
.
new
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录