Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
388c14e5
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,发现更多精彩内容 >>
提交
388c14e5
编写于
6月 05, 2019
作者:
E
Eileen M. Uchitelle
提交者:
eileencodes
6月 05, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Merge pull request #36416 from freeletics/fix-db-prepare
Fixed db:prepare task for multiple databases.
上级
1bbb520f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
99 addition
and
31 deletion
+99
-31
activerecord/lib/active_record/railties/databases.rake
activerecord/lib/active_record/railties/databases.rake
+24
-16
activerecord/lib/active_record/tasks/database_tasks.rb
activerecord/lib/active_record/tasks/database_tasks.rb
+25
-3
railties/test/application/rake/multi_dbs_test.rb
railties/test/application/rake/multi_dbs_test.rb
+20
-0
railties/test/isolation/abstract_unit.rb
railties/test/isolation/abstract_unit.rb
+30
-12
未找到文件。
activerecord/lib/active_record/railties/databases.rake
浏览文件 @
388c14e5
...
...
@@ -266,12 +266,31 @@ db_namespace = namespace :db do
desc
"Runs setup if database does not exist, or runs migrations if it does"
task
prepare: :load_config
do
seed
=
false
ActiveRecord
::
Base
.
configurations
.
configs_for
(
env_name:
ActiveRecord
::
Tasks
::
DatabaseTasks
.
env
).
each
do
|
db_config
|
ActiveRecord
::
Base
.
establish_connection
(
db_config
.
config
)
db_namespace
[
"migrate"
].
invoke
ActiveRecord
::
Tasks
::
DatabaseTasks
.
migrate
# Skipped when no database
ActiveRecord
::
Tasks
::
DatabaseTasks
.
dump_schema
(
db_config
.
config
,
ActiveRecord
::
Base
.
schema_format
,
db_config
.
spec_name
)
rescue
ActiveRecord
::
NoDatabaseError
db_namespace
[
"setup"
].
invoke
ActiveRecord
::
Tasks
::
DatabaseTasks
.
create_current
(
db_config
.
env_name
,
db_config
.
spec_name
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_schema
(
db_config
.
config
,
ActiveRecord
::
Base
.
schema_format
,
nil
,
db_config
.
env_name
,
db_config
.
spec_name
)
seed
=
true
end
ActiveRecord
::
Base
.
establish_connection
ActiveRecord
::
Tasks
::
DatabaseTasks
.
load_seed
if
seed
end
desc
"Loads the seed data from db/seeds.rb"
...
...
@@ -336,13 +355,9 @@ db_namespace = namespace :db do
namespace
:schema
do
desc
"Creates a db/schema.rb file that is portable against any DB supported by Active Record"
task
dump: :load_config
do
require
"active_record/schema_dumper"
ActiveRecord
::
Base
.
configurations
.
configs_for
(
env_name:
ActiveRecord
::
Tasks
::
DatabaseTasks
.
env
).
each
do
|
db_config
|
filename
=
ActiveRecord
::
Tasks
::
DatabaseTasks
.
dump_filename
(
db_config
.
spec_name
,
:ruby
)
File
.
open
(
filename
,
"w:utf-8"
)
do
|
file
|
ActiveRecord
::
Base
.
establish_connection
(
db_config
.
config
)
ActiveRecord
::
SchemaDumper
.
dump
(
ActiveRecord
::
Base
.
connection
,
file
)
end
ActiveRecord
::
Base
.
establish_connection
(
db_config
.
config
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
dump_schema
(
db_config
.
config
,
:ruby
,
db_config
.
spec_name
)
end
db_namespace
[
"schema:dump"
].
reenable
...
...
@@ -385,14 +400,7 @@ db_namespace = namespace :db do
task
dump: :load_config
do
ActiveRecord
::
Base
.
configurations
.
configs_for
(
env_name:
ActiveRecord
::
Tasks
::
DatabaseTasks
.
env
).
each
do
|
db_config
|
ActiveRecord
::
Base
.
establish_connection
(
db_config
.
config
)
filename
=
ActiveRecord
::
Tasks
::
DatabaseTasks
.
dump_filename
(
db_config
.
spec_name
,
:sql
)
ActiveRecord
::
Tasks
::
DatabaseTasks
.
structure_dump
(
db_config
.
config
,
filename
)
if
ActiveRecord
::
SchemaMigration
.
table_exists?
File
.
open
(
filename
,
"a"
)
do
|
f
|
f
.
puts
ActiveRecord
::
Base
.
connection
.
dump_schema_information
f
.
print
"
\n
"
end
end
ActiveRecord
::
Tasks
::
DatabaseTasks
.
dump_schema
(
db_config
.
config
,
:sql
,
db_config
.
spec_name
)
end
db_namespace
[
"structure:dump"
].
reenable
...
...
activerecord/lib/active_record/tasks/database_tasks.rb
浏览文件 @
388c14e5
...
...
@@ -169,8 +169,8 @@ def raise_for_multi_db(environment = env, command:)
end
end
def
create_current
(
environment
=
env
)
each_current_configuration
(
environment
)
{
|
configuration
|
def
create_current
(
environment
=
env
,
spec_name
=
nil
)
each_current_configuration
(
environment
,
spec_name
)
{
|
configuration
|
create
configuration
}
ActiveRecord
::
Base
.
establish_connection
(
environment
.
to_sym
)
...
...
@@ -325,6 +325,26 @@ def load_schema(configuration, format = ActiveRecord::Base.schema_format, file =
Migration
.
verbose
=
verbose_was
end
def
dump_schema
(
configuration
,
format
=
ActiveRecord
::
Base
.
schema_format
,
spec_name
=
"primary"
)
require
"active_record/schema_dumper"
filename
=
dump_filename
(
spec_name
,
format
)
case
format
when
:ruby
File
.
open
(
filename
,
"w:utf-8"
)
do
|
file
|
ActiveRecord
::
SchemaDumper
.
dump
(
ActiveRecord
::
Base
.
connection
,
file
)
end
when
:sql
structure_dump
(
configuration
,
filename
)
if
ActiveRecord
::
SchemaMigration
.
table_exists?
File
.
open
(
filename
,
"a"
)
do
|
f
|
f
.
puts
ActiveRecord
::
Base
.
connection
.
dump_schema_information
f
.
print
"
\n
"
end
end
end
end
def
schema_file
(
format
=
ActiveRecord
::
Base
.
schema_format
)
File
.
join
(
db_dir
,
schema_file_type
(
format
))
end
...
...
@@ -406,12 +426,14 @@ def class_for_adapter(adapter)
task
.
is_a?
(
String
)
?
task
.
constantize
:
task
end
def
each_current_configuration
(
environment
)
def
each_current_configuration
(
environment
,
spec_name
=
nil
)
environments
=
[
environment
]
environments
<<
"test"
if
environment
==
"development"
environments
.
each
do
|
env
|
ActiveRecord
::
Base
.
configurations
.
configs_for
(
env_name:
env
).
each
do
|
db_config
|
next
if
spec_name
&&
spec_name
!=
db_config
.
spec_name
yield
db_config
.
config
,
db_config
.
spec_name
,
env
end
end
...
...
railties/test/application/rake/multi_dbs_test.rb
浏览文件 @
388c14e5
...
...
@@ -303,6 +303,26 @@ class TwoMigration < ActiveRecord::Migration::Current
require
"
#{
app_path
}
/config/environment"
db_prepare
end
test
"db:prepare setups missing database without clearing existing one"
do
require
"
#{
app_path
}
/config/environment"
Dir
.
chdir
(
app_path
)
do
# Bug not visible on SQLite3. Can be simplified when https://github.com/rails/rails/issues/36383 resolved
use_postgresql
(
multi_db:
true
)
generate_models_for_animals
rails
"db:create:animals"
,
"db:migrate:animals"
,
"db:create:primary"
,
"db:migrate:primary"
,
"db:schema:dump"
rails
"db:drop:primary"
Dog
.
create!
output
=
rails
(
"db:prepare"
)
assert_match
(
/Created database/
,
output
)
assert_equal
1
,
Dog
.
count
ensure
Dog
.
connection
.
disconnect!
rails
"db:drop"
rescue
nil
end
end
end
end
end
railties/test/isolation/abstract_unit.rb
浏览文件 @
388c14e5
...
...
@@ -448,18 +448,36 @@ def use_frameworks(arr)
$:
.
reject!
{
|
path
|
path
=~
%r'/(
#{
to_remove
.
join
(
'|'
)
}
)/'
}
end
def
use_postgresql
File
.
open
(
"
#{
app_path
}
/config/database.yml"
,
"w"
)
do
|
f
|
f
.
puts
<<-
YAML
default: &default
adapter: postgresql
pool: 5
database: railties_test
development:
<<: *default
test:
<<: *default
YAML
def
use_postgresql
(
multi_db:
false
)
if
multi_db
File
.
open
(
"
#{
app_path
}
/config/database.yml"
,
"w"
)
do
|
f
|
f
.
puts
<<-
YAML
default: &default
adapter: postgresql
pool: 5
development:
primary:
<<: *default
database: railties_test
animals:
<<: *default
database: railties_animals_test
migrations_paths: db/animals_migrate
YAML
end
else
File
.
open
(
"
#{
app_path
}
/config/database.yml"
,
"w"
)
do
|
f
|
f
.
puts
<<-
YAML
default: &default
adapter: postgresql
pool: 5
database: railties_test
development:
<<: *default
test:
<<: *default
YAML
end
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录