Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
ec54acd1
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,发现更多精彩内容 >>
提交
ec54acd1
编写于
8月 06, 2014
作者:
Y
Yves Senn
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16316 from rails/senny/purge_schema_maintenance
purge database before loading schema, take 2
上级
acb371ca
f15cef67
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
133 addition
and
14 deletion
+133
-14
activerecord/CHANGELOG.md
activerecord/CHANGELOG.md
+13
-0
activerecord/lib/active_record/migration.rb
activerecord/lib/active_record/migration.rb
+1
-1
activerecord/lib/active_record/railties/databases.rake
activerecord/lib/active_record/railties/databases.rake
+4
-10
activerecord/lib/active_record/tasks/database_tasks.rb
activerecord/lib/active_record/tasks/database_tasks.rb
+20
-1
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
...verecord/lib/active_record/tasks/sqlite_database_tasks.rb
+5
-1
railties/test/application/test_test.rb
railties/test/application/test_test.rb
+90
-1
未找到文件。
activerecord/CHANGELOG.md
浏览文件 @
ec54acd1
*
Deprecate
`DatabaseTasks.load_schema`
to act on the current connection.
Use
`.load_schema_current`
instead. In the future
`load_schema`
will
require the
`configuration`
to act on as an argument.
*Yves Senn*
*
Fixed automatic maintaining test schema to properly handle sql structure
schema format.
Fixes #15394.
*Wojciech Wnętrzak*
*
Fix type casting to Decimal from Float with large precision.
*Tomohiro Hashidate*
...
...
activerecord/lib/active_record/migration.rb
浏览文件 @
ec54acd1
...
...
@@ -399,7 +399,7 @@ def check_pending!(connection = Base.connection)
def
load_schema_if_pending!
if
ActiveRecord
::
Migrator
.
needs_migration?
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema
_current
check_pending!
end
end
...
...
activerecord/lib/active_record/railties/databases.rake
浏览文件 @
ec54acd1
...
...
@@ -240,7 +240,7 @@ db_namespace = namespace :db do
desc
'Load a schema.rb file into the database'
task
:load
=>
[
:environment
,
:load_config
]
do
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema
(
:ruby
,
ENV
[
'SCHEMA'
])
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema
_current
(
:ruby
,
ENV
[
'SCHEMA'
])
end
task
:load_if_ruby
=>
[
'db:create'
,
:environment
]
do
...
...
@@ -286,7 +286,7 @@ db_namespace = namespace :db do
desc
"Recreate the databases from the structure.sql file"
task
:load
=>
[
:environment
,
:load_config
]
do
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema
(
:sql
,
ENV
[
'DB_STRUCTURE'
])
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema
_current
(
:sql
,
ENV
[
'DB_STRUCTURE'
])
end
task
:load_if_sql
=>
[
'db:create'
,
:environment
]
do
...
...
@@ -317,9 +317,8 @@ db_namespace = namespace :db do
task
:load_schema
=>
%w(db:test:deprecated db:test:purge)
do
begin
should_reconnect
=
ActiveRecord
::
Base
.
connection_pool
.
active_connection?
ActiveRecord
::
Base
.
establish_connection
(
ActiveRecord
::
Base
.
configurations
[
'test'
])
ActiveRecord
::
Schema
.
verbose
=
false
db_namespace
[
"schema:load"
].
invoke
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema_for
ActiveRecord
::
Base
.
configurations
[
'test'
],
:ruby
,
ENV
[
'SCHEMA'
]
ensure
if
should_reconnect
ActiveRecord
::
Base
.
establish_connection
(
ActiveRecord
::
Base
.
configurations
[
ActiveRecord
::
Tasks
::
DatabaseTasks
.
env
])
...
...
@@ -329,12 +328,7 @@ db_namespace = namespace :db do
# desc "Recreate the test database from an existent structure.sql file"
task
:load_structure
=>
%w(db:test:deprecated db:test:purge)
do
begin
ActiveRecord
::
Tasks
::
DatabaseTasks
.
current_config
(
:config
=>
ActiveRecord
::
Base
.
configurations
[
'test'
])
db_namespace
[
"structure:load"
].
invoke
ensure
ActiveRecord
::
Tasks
::
DatabaseTasks
.
current_config
(
:config
=>
nil
)
end
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema_for
ActiveRecord
::
Base
.
configurations
[
'test'
],
:sql
,
ENV
[
'SCHEMA'
]
end
# desc "Recreate the test database from a fresh schema"
...
...
activerecord/lib/active_record/tasks/database_tasks.rb
浏览文件 @
ec54acd1
...
...
@@ -184,20 +184,39 @@ def structure_load(*arguments)
end
def
load_schema
(
format
=
ActiveRecord
::
Base
.
schema_format
,
file
=
nil
)
ActiveSupport
::
Deprecation
.
warn
(
<<-
MESSAGE
.
strip_heredoc
)
This method will act on a specific connection in the future.
To act on the current connection, use `load_schema_current` instead.
MESSAGE
load_schema_current
(
format
,
file
)
end
# This method is the successor of +load_schema+. We should rename it
# after +load_schema+ went through a deprecation cycle. (Rails > 4.2)
def
load_schema_for
(
configuration
,
format
=
ActiveRecord
::
Base
.
schema_format
,
file
=
nil
)
# :nodoc:
case
format
when
:ruby
file
||=
File
.
join
(
db_dir
,
"schema.rb"
)
check_schema_file
(
file
)
purge
(
configuration
)
ActiveRecord
::
Base
.
establish_connection
(
configuration
)
load
(
file
)
when
:sql
file
||=
File
.
join
(
db_dir
,
"structure.sql"
)
check_schema_file
(
file
)
structure_load
(
current_config
,
file
)
purge
(
configuration
)
structure_load
(
configuration
,
file
)
else
raise
ArgumentError
,
"unknown format
#{
format
.
inspect
}
"
end
end
def
load_schema_current
(
format
=
ActiveRecord
::
Base
.
schema_format
,
file
=
nil
,
environment
=
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
load_schema_for
configuration
,
format
,
file
}
end
def
check_schema_file
(
filename
)
unless
File
.
exist?
(
filename
)
message
=
%{#{filename} doesn't exist yet. Run `rake db:migrate` to create it, then try again.}
...
...
activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
浏览文件 @
ec54acd1
...
...
@@ -21,7 +21,11 @@ def drop
FileUtils
.
rm
(
file
)
if
File
.
exist?
(
file
)
end
alias
:purge
:drop
def
purge
drop
create
end
def
charset
connection
.
encoding
...
...
railties/test/application/test_test.rb
浏览文件 @
ec54acd1
...
...
@@ -67,7 +67,7 @@ def test_failure
assert_match
%r{/app/test/unit/failing_test
\.
rb}
,
output
end
test
"migrations"
do
test
"
ruby schema
migrations"
do
output
=
script
(
'generate model user name:string'
)
version
=
output
.
match
(
/(\d+)_create_users\.rb/
)[
1
]
...
...
@@ -104,6 +104,95 @@ class UserTest < ActiveSupport::TestCase
assert
!
result
.
include?
(
"create_table(:users)"
)
end
test
"sql structure migrations"
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
User.create! name: "Jon"
end
end
RUBY
app_file
'db/structure.sql'
,
''
app_file
'config/initializers/enable_sql_schema_format.rb'
,
<<-
RUBY
Rails.application.config.active_record.schema_format = :sql
RUBY
assert_unsuccessful_run
"models/user_test.rb"
,
"Migrations are pending"
app_file
'db/structure.sql'
,
<<-
SQL
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
INSERT INTO schema_migrations (version) VALUES ('
#{
version
}
');
SQL
app_file
'config/initializers/disable_maintain_test_schema.rb'
,
<<-
RUBY
Rails.application.config.active_record.maintain_test_schema = false
RUBY
assert_unsuccessful_run
"models/user_test.rb"
,
"Could not find table 'users'"
File
.
delete
"
#{
app_path
}
/config/initializers/disable_maintain_test_schema.rb"
assert_successful_test_run
(
'models/user_test.rb'
)
end
test
"sql structure migrations when adding column to existing table"
do
output_1
=
script
(
'generate model user name:string'
)
version_1
=
output_1
.
match
(
/(\d+)_create_users\.rb/
)[
1
]
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
app_file
'config/initializers/enable_sql_schema_format.rb'
,
<<-
RUBY
Rails.application.config.active_record.schema_format = :sql
RUBY
app_file
'db/structure.sql'
,
<<-
SQL
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
INSERT INTO schema_migrations (version) VALUES ('
#{
version_1
}
');
SQL
assert_successful_test_run
(
'models/user_test.rb'
)
output_2
=
script
(
'generate migration add_email_to_users'
)
version_2
=
output_2
.
match
(
/(\d+)_add_email_to_users\.rb/
)[
1
]
app_file
'test/models/user_test.rb'
,
<<-
RUBY
require 'test_helper'
class UserTest < ActiveSupport::TestCase
test "user" do
User.create! name: "Jon", email: "jon@doe.com"
end
end
RUBY
app_file
'db/structure.sql'
,
<<-
SQL
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255));
INSERT INTO schema_migrations (version) VALUES ('
#{
version_1
}
');
INSERT INTO schema_migrations (version) VALUES ('
#{
version_2
}
');
SQL
assert_successful_test_run
(
'models/user_test.rb'
)
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录