Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3802de4a
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,发现更多精彩内容 >>
提交
3802de4a
编写于
11月 25, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'derekprior-dp-add-foreign-keys-with-generators'
Closes #17759
上级
ac0432cf
323334a7
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
60 addition
and
1 deletion
+60
-1
activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
...tive_record/migration/templates/create_table_migration.rb
+3
-0
activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
...generators/active_record/migration/templates/migration.rb
+6
-0
railties/CHANGELOG.md
railties/CHANGELOG.md
+5
-0
railties/test/generators/migration_generator_test.rb
railties/test/generators/migration_generator_test.rb
+25
-1
railties/test/generators/model_generator_test.rb
railties/test/generators/model_generator_test.rb
+21
-0
未找到文件。
activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb
浏览文件 @
3802de4a
...
...
@@ -14,6 +14,9 @@ def change
end
<% attributes_with_index.each do |attribute| -%>
add_index
:<
%=
table_name
%>, :<%= attribute.index_name %>
<
%= attribute.inject_index_options %>
<% end -%>
<% attributes.select(&:reference?).reject(&:polymorphic?).each do |attribute| -%>
add_foreign_key :<%=
table_name
%>, :<%= attribute.name.pluralize %>
<
% end
-
%>
end
end
activerecord/lib/rails/generators/active_record/migration/templates/migration.rb
浏览文件 @
3802de4a
...
...
@@ -4,6 +4,9 @@ def change
<% attributes.each do |attribute| -%>
<
%- if attribute.reference? -%>
add_reference :<%= table_name %>
,
:<
%=
attribute
.
name
%><%= attribute.inject_options %>
<
%- unless attribute.polymorphic? -%>
add_foreign_key :<%= table_name %>
,
:<
%=
attribute
.
name
.
pluralize
%>
<%- end -%>
<
%- else -%>
add_column :<%= table_name %>
,
:<
%=
attribute
.
name
%>, :<%= attribute.type %>
<
%= attribute.inject_options %>
<%- if attribute.has_index? -%>
...
...
@@ -26,6 +29,9 @@ def change
<%- if migration_action -%>
<
%- if attribute.reference? -%>
remove_reference :<%= table_name %>
,
:<
%=
attribute
.
name
%><%= attribute.inject_options %>
<
%- unless attribute.polymorphic? -%>
remove_foreign_key :<%= table_name %>
,
:<
%=
attribute
.
name
.
pluralize
%>
<%- end -%>
<
%- else -%>
<%- if attribute.has_index? -%>
remove_index
:<
%=
table_name
%>, :<%= attribute.index_name %>
<
%= attribute.inject_index_options %>
...
...
railties/CHANGELOG.md
浏览文件 @
3802de4a
*
Generated migrations add the appropriate foreign key constraints to
references.
*Derek Prior*
*
Deprecate different default for
`log_level`
in production.
*Godfrey Chan*, *Matthew Draper*
...
...
railties/test/generators/migration_generator_test.rb
浏览文件 @
3802de4a
...
...
@@ -85,6 +85,18 @@ def test_remove_migration_with_references_options
end
end
def
test_remove_migration_with_references_removes_foreign_keys
migration
=
"remove_references_from_books"
run_generator
[
migration
,
"author:belongs_to"
,
"distributor:references{polymorphic}"
]
assert_migration
"db/migrate/
#{
migration
}
.rb"
do
|
content
|
assert_method
:change
,
content
do
|
change
|
assert_match
(
/remove_foreign_key :books, :authors/
,
change
)
assert_no_match
(
/remove_foreign_key :books, :distributors/
,
change
)
end
end
end
def
test_add_migration_with_attributes_and_indices
migration
=
"add_title_with_index_and_body_to_posts"
run_generator
[
migration
,
"title:string:index"
,
"body:text"
,
"user_id:integer:uniq"
]
...
...
@@ -171,6 +183,18 @@ def test_add_migration_with_required_references
end
end
def
test_add_migration_with_references_adds_foreign_keys
migration
=
"add_references_to_books"
run_generator
[
migration
,
"author:belongs_to"
,
"distributor:references{polymorphic}"
]
assert_migration
"db/migrate/
#{
migration
}
.rb"
do
|
content
|
assert_method
:change
,
content
do
|
change
|
assert_match
(
/add_foreign_key :books, :authors/
,
change
)
assert_no_match
(
/add_foreign_key :books, :distributors/
,
change
)
end
end
end
def
test_create_join_table_migration
migration
=
"add_media_join_table"
run_generator
[
migration
,
"artist_id"
,
"musics:uniq"
]
...
...
@@ -205,7 +229,7 @@ def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_
end
end
end
def
test_properly_identifies_usage_file
assert
generator_class
.
send
(
:usage_path
)
end
...
...
railties/test/generators/model_generator_test.rb
浏览文件 @
3802de4a
...
...
@@ -407,6 +407,27 @@ def test_required_adds_null_false_to_column
end
end
def
test_foreign_key_is_added_for_references
run_generator
[
"account"
,
"supplier:belongs_to"
,
"user:references"
]
assert_migration
"db/migrate/create_accounts.rb"
do
|
m
|
assert_method
:change
,
m
do
|
up
|
assert_match
(
/add_foreign_key :accounts, :suppliers/
,
up
)
assert_match
(
/add_foreign_key :accounts, :users/
,
up
)
end
end
end
def
test_foreign_key_is_skipped_for_polymorphic_references
run_generator
[
"account"
,
"supplier:belongs_to{polymorphic}"
]
assert_migration
"db/migrate/create_accounts.rb"
do
|
m
|
assert_method
:change
,
m
do
|
up
|
assert_no_match
(
/add_foreign_key :accounts, :suppliers/
,
up
)
end
end
end
private
def
assert_generated_fixture
(
path
,
parsed_contents
)
fixture_file
=
File
.
new
File
.
expand_path
(
path
,
destination_root
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录