Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
c550e719
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,发现更多精彩内容 >>
提交
c550e719
编写于
8月 06, 2019
作者:
J
John Hawthorn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #36870 from jhawthorn/schema_sha
Sync test DB from schema using its SHA1
上级
26ab0ad3
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
25 addition
and
13 deletion
+25
-13
activerecord/lib/active_record/migration.rb
activerecord/lib/active_record/migration.rb
+9
-5
activerecord/lib/active_record/tasks/database_tasks.rb
activerecord/lib/active_record/tasks/database_tasks.rb
+15
-0
railties/test/application/test_test.rb
railties/test/application/test_test.rb
+1
-8
未找到文件。
activerecord/lib/active_record/migration.rb
浏览文件 @
c550e719
...
...
@@ -588,18 +588,22 @@ def check_pending!(connection = Base.connection)
end
def
load_schema_if_pending!
if
Base
.
connection
.
migration_context
.
needs_migration?
||
!
Base
.
connection
.
migration_context
.
any_migrations?
current_config
=
Base
.
connection_config
all_configs
=
ActiveRecord
::
Base
.
configurations
.
configs_for
(
env_name:
Rails
.
env
)
unless
all_configs
.
all?
{
|
config
|
Tasks
::
DatabaseTasks
.
schema_up_to_date?
(
config
.
config
)
}
# Roundtrip to Rake to allow plugins to hook into database initialization.
root
=
defined?
(
ENGINE_ROOT
)
?
ENGINE_ROOT
:
Rails
.
root
FileUtils
.
cd
(
root
)
do
current_config
=
Base
.
connection_config
Base
.
clear_all_connections!
system
(
"bin/rails db:test:prepare"
)
# Establish a new connection, the old database may be gone (db:test:prepare uses purge)
Base
.
establish_connection
(
current_config
)
end
check_pending!
end
# Establish a new connection, the old database may be gone (db:test:prepare uses purge)
Base
.
establish_connection
(
current_config
)
check_pending!
end
def
maintain_test_schema!
#:nodoc:
...
...
activerecord/lib/active_record/tasks/database_tasks.rb
浏览文件 @
c550e719
...
...
@@ -333,10 +333,21 @@ def load_schema(configuration, format = ActiveRecord::Base.schema_format, file =
end
ActiveRecord
::
InternalMetadata
.
create_table
ActiveRecord
::
InternalMetadata
[
:environment
]
=
environment
ActiveRecord
::
InternalMetadata
[
:schema_sha1
]
=
schema_sha1
(
file
)
ensure
Migration
.
verbose
=
verbose_was
end
def
schema_up_to_date?
(
configuration
,
format
=
ActiveRecord
::
Base
.
schema_format
,
file
=
nil
,
environment
=
env
,
spec_name
=
"primary"
)
file
||=
dump_filename
(
spec_name
,
format
)
return
true
unless
File
.
exist?
(
file
)
ActiveRecord
::
Base
.
establish_connection
(
configuration
)
return
false
unless
ActiveRecord
::
InternalMetadata
.
table_exists?
ActiveRecord
::
InternalMetadata
[
:schema_sha1
]
==
schema_sha1
(
file
)
end
def
dump_schema
(
configuration
,
format
=
ActiveRecord
::
Base
.
schema_format
,
spec_name
=
"primary"
)
# :nodoc:
require
"active_record/schema_dumper"
filename
=
dump_filename
(
spec_name
,
format
)
...
...
@@ -468,6 +479,10 @@ def each_local_configuration
def
local_database?
(
configuration
)
configuration
[
"host"
].
blank?
||
LOCAL_HOSTS
.
include?
(
configuration
[
"host"
])
end
def
schema_sha1
(
file
)
Digest
::
SHA1
.
hexdigest
(
File
.
read
(
file
))
end
end
end
end
railties/test/application/test_test.rb
浏览文件 @
c550e719
...
...
@@ -232,10 +232,7 @@ class UserTest < ActiveSupport::TestCase
assert_successful_test_run
(
"models/user_test.rb"
)
end
# TODO: would be nice if we could detect the schema change automatically.
# For now, the user has to synchronize the schema manually.
# This test case serves as a reminder for this use case.
test
"manually synchronize test schema after rollback"
do
test
"automatically synchronizes test schema after rollback"
do
output
=
rails
(
"generate"
,
"model"
,
"user"
,
"name:string"
)
version
=
output
.
match
(
/(\d+)_create_users\.rb/
)[
1
]
...
...
@@ -268,10 +265,6 @@ class UserTest < ActiveSupport::TestCase
end
RUBY
assert_successful_test_run
"models/user_test.rb"
rails
"db:test:prepare"
assert_unsuccessful_run
"models/user_test.rb"
,
<<-
ASSERTION
Expected: ["id", "name"]
Actual: ["id", "name", "age"]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录