Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generators add foreign keys on references #17759

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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? -%>
Expand All @@ -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 %>
Expand Down
5 changes: 5 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Generated migrations add the appropriate foreign key constraints to
references.

*Derek Prior*

* Deprecate different default for `log_level` in production.

*Godfrey Chan*, *Matthew Draper*
Expand Down
26 changes: 25 additions & 1 deletion railties/test/generators/migration_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions railties/test/generators/model_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down