Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
208908f0
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,发现更多精彩内容 >>
提交
208908f0
编写于
11月 25, 2014
作者:
Y
Yves Senn
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #17739 from rails/bring_back_db_test_prepare
bring back `db:test:prepare`.
上级
dd986814
5c449553
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
120 addition
and
17 deletion
+120
-17
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+15
-0
activerecord/lib/active_record/migration.rb
activerecord/lib/active_record/migration.rb
+8
-1
activerecord/lib/active_record/railtie.rb
activerecord/lib/active_record/railtie.rb
+0
-2
activerecord/lib/active_record/railties/databases.rake
activerecord/lib/active_record/railties/databases.rake
+5
-5
railties/test/application/rake/dbs_test.rb
railties/test/application/rake/dbs_test.rb
+0
-9
railties/test/application/test_test.rb
railties/test/application/test_test.rb
+92
-0
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
208908f0
*
Bring back
`db:test:prepare`
to synchronize the test database schema.
Manual synchronization using `bin/rake db:test:prepare` is required
when a migration is rolled-back, edited and reapplied.
`ActiveRecord::Base.maintain_test_schema` now uses `db:test:prepare`
to synchronize the schema. Plugins can use this task as a hook to
provide custom behavior after the schema has been loaded.
NOTE: `test:prepare` runs before the schema was synchronized.
Fixes #17171, #15787.
*Yves Senn*
*
Change
`reflections`
public api to return the keys as String objects.
Fixes #16928.
...
...
activerecord/lib/active_record/migration.rb
浏览文件 @
208908f0
...
...
@@ -395,7 +395,14 @@ def check_pending!(connection = Base.connection)
def
load_schema_if_pending!
if
ActiveRecord
::
Migrator
.
needs_migration?
||
!
ActiveRecord
::
Migrator
.
any_migrations?
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema_current_if_exists
# Roundrip to Rake to allow plugins to hook into database initialization.
FileUtils
.
cd
Rails
.
root
do
current_config
=
Base
.
connection_config
Base
.
clear_all_connections!
system
(
"bin/rake 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
end
...
...
activerecord/lib/active_record/railtie.rb
浏览文件 @
208908f0
...
...
@@ -36,8 +36,6 @@ class Railtie < Rails::Railtie # :nodoc:
config
.
eager_load_namespaces
<<
ActiveRecord
rake_tasks
do
require
"active_record/base"
namespace
:db
do
task
:load_config
do
ActiveRecord
::
Tasks
::
DatabaseTasks
.
database_configuration
=
Rails
.
application
.
config
.
database_configuration
...
...
activerecord/lib/active_record/railties/databases.rake
浏览文件 @
208908f0
...
...
@@ -305,7 +305,7 @@ db_namespace = namespace :db do
end
# desc "Recreate the test database from the current schema"
task
:load
=>
%w(db:test:
deprecated db:test:
purge)
do
task
:load
=>
%w(db:test:purge)
do
case
ActiveRecord
::
Base
.
schema_format
when
:ruby
db_namespace
[
"test:load_schema"
].
invoke
...
...
@@ -315,7 +315,7 @@ db_namespace = namespace :db do
end
# desc "Recreate the test database from an existent schema.rb file"
task
:load_schema
=>
%w(db:test:
deprecated db:test:
purge)
do
task
:load_schema
=>
%w(db:test:purge)
do
begin
should_reconnect
=
ActiveRecord
::
Base
.
connection_pool
.
active_connection?
ActiveRecord
::
Schema
.
verbose
=
false
...
...
@@ -328,7 +328,7 @@ db_namespace = namespace :db do
end
# desc "Recreate the test database from an existent structure.sql file"
task
:load_structure
=>
%w(db:test:
deprecated db:test:
purge)
do
task
:load_structure
=>
%w(db:test:purge)
do
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema_for
ActiveRecord
::
Base
.
configurations
[
'test'
],
:sql
,
ENV
[
'SCHEMA'
]
end
...
...
@@ -349,12 +349,12 @@ db_namespace = namespace :db do
task
:clone_structure
=>
%w(db:test:deprecated db:structure:dump db:test:load_structure)
# desc "Empty the test database"
task
:purge
=>
%w(
db:test:deprecated
environment load_config)
do
task
:purge
=>
%w(environment load_config)
do
ActiveRecord
::
Tasks
::
DatabaseTasks
.
purge
ActiveRecord
::
Base
.
configurations
[
'test'
]
end
# desc 'Check for pending migrations and load the test schema'
task
:prepare
=>
%w(
db:test:deprecated
environment load_config)
do
task
:prepare
=>
%w(environment load_config)
do
unless
ActiveRecord
::
Base
.
configurations
.
blank?
db_namespace
[
'test:load'
].
invoke
end
...
...
railties/test/application/rake/dbs_test.rb
浏览文件 @
208908f0
...
...
@@ -175,15 +175,6 @@ def db_test_load_structure
db_test_load_structure
end
test
'db:test deprecation'
do
require
"
#{
app_path
}
/config/environment"
Dir
.
chdir
(
app_path
)
do
output
=
`bundle exec rake db:migrate db:test:prepare 2>&1`
assert_equal
"WARNING: db:test:prepare is deprecated. The Rails test helper now maintains "
\
"your test schema automatically, see the release notes for details.
\n
"
,
output
end
end
test
'db:setup loads schema and seeds database'
do
begin
@old_rails_env
=
ENV
[
"RAILS_ENV"
]
...
...
railties/test/application/test_test.rb
浏览文件 @
208908f0
...
...
@@ -193,6 +193,98 @@ 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
output
=
script
(
'generate model user name:string'
)
version
=
output
.
match
(
/(\d+)_create_users\.rb/
)[
1
]
app_file
'test/models/user_test.rb'
,
<<-
RUBY
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user" do
assert_equal ["id", "name"], User.columns_hash.keys
end
end
RUBY
app_file
'db/schema.rb'
,
<<-
RUBY
ActiveRecord::Schema.define(version:
#{
version
}
) do
create_table :users do |t|
t.string :name
end
end
RUBY
assert_successful_test_run
"models/user_test.rb"
# Simulate `db:rollback` + edit of the migration file + `db:migrate`
app_file
'db/schema.rb'
,
<<-
RUBY
ActiveRecord::Schema.define(version:
#{
version
}
) do
create_table :users do |t|
t.string :name
t.integer :age
end
end
RUBY
assert_successful_test_run
"models/user_test.rb"
Dir
.
chdir
(
app_path
)
{
`bin/rake db:test:prepare`
}
assert_unsuccessful_run
"models/user_test.rb"
,
<<-
ASSERTION
Expected: ["id", "name"]
Actual: ["id", "name", "age"]
ASSERTION
end
test
"hooks for plugins"
do
output
=
script
(
'generate model user name:string'
)
version
=
output
.
match
(
/(\d+)_create_users\.rb/
)[
1
]
app_file
'lib/tasks/hooks.rake'
,
<<-
RUBY
task :before_hook do
has_user_table = ActiveRecord::Base.connection.table_exists?('users')
puts "before: " + has_user_table.to_s
end
task :after_hook do
has_user_table = ActiveRecord::Base.connection.table_exists?('users')
puts "after: " + has_user_table.to_s
end
Rake::Task["db:test:prepare"].enhance [:before_hook] do
Rake::Task[:after_hook].invoke
end
RUBY
app_file
'test/models/user_test.rb'
,
<<-
RUBY
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user" do
User.create! name: "Jon"
end
end
RUBY
# Simulate `db:migrate`
app_file
'db/schema.rb'
,
<<-
RUBY
ActiveRecord::Schema.define(version:
#{
version
}
) do
create_table :users do |t|
t.string :name
end
end
RUBY
output
=
assert_successful_test_run
"models/user_test.rb"
assert_includes
output
,
"before: false
\n
after: true"
# running tests again won't trigger a schema update
output
=
assert_successful_test_run
"models/user_test.rb"
assert_not_includes
output
,
"before:"
assert_not_includes
output
,
"after:"
end
private
def
assert_unsuccessful_run
(
name
,
message
)
result
=
run_test_file
(
name
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录