Browse files

fk: infere column name from table names.

This allows to create and remove foreign keys without specifying a column.
  • Loading branch information...
1 parent 6073d7c commit d074b821489b6d58101d1474dd514990f4bdf0fa @senny senny committed Jun 10, 2014
View
18 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -647,10 +647,11 @@ def foreign_keys(table_name)
end
def add_foreign_key(from_table, to_table, options = {})
+ options[:column] ||= foreign_key_column_for(to_table)
primary_key = options.fetch(:primary_key, "id")
options = {
- column: options.fetch(:column),
+ column: options[:column],
primary_key: primary_key,
name: foreign_key_name(from_table, options),
dependent: options.fetch(:dependent, nil)
@@ -661,17 +662,26 @@ def add_foreign_key(from_table, to_table, options = {})
execute schema_creation.accept at
end
- def remove_foreign_key(from_table, options = {})
+ def remove_foreign_key(from_table, options_or_to_table = {})
+ if options_or_to_table.is_a?(Hash)
+ options = options_or_to_table
+ else
+ options = { column: foreign_key_column_for(options_or_to_table) }
+ end
+
at = create_alter_table from_table
at.drop_foreign_key foreign_key_name(from_table, options)
execute schema_creation.accept at
end
+ def foreign_key_column_for(table_name) # :nodoc:
+ "#{table_name.to_s.singularize}_id"
+ end
+
def foreign_key_name(table_name, options) # :nodoc:
options.fetch(:name) do
- column_name = options.fetch(:column)
- "#{table_name}_#{column_name}_fk"
+ "#{table_name}_#{options.fetch(:column)}_fk"
end
end
View
4 activerecord/lib/active_record/migration/command_recorder.rb
@@ -170,13 +170,15 @@ def invert_change_column_null(args)
end
def invert_add_foreign_key(args)
- from_table, _to_table, add_options = *args
+ from_table, to_table, add_options = *args
add_options ||= {}
if add_options[:name]
options = {name: add_options[:name]}
elsif add_options[:column]
options = {column: add_options[:column]}
+ else
+ options = to_table
end
[:remove_foreign_key, [from_table, options]]
View
5 activerecord/test/cases/migration/command_recorder_test.rb
@@ -271,6 +271,11 @@ def test_invert_disable_extension
assert_equal [:enable_extension, ['uuid-ossp'], nil], enable
end
+ def test_invert_add_foreign_key
+ enable = @recorder.inverse_of :add_foreign_key, [:dogs, :people]
+ assert_equal [:remove_foreign_key, [:dogs, :people]], enable
+ end
+
def test_invert_add_foreign_key_with_column
enable = @recorder.inverse_of :add_foreign_key, [:dogs, :people, column: "owner_id"]
assert_equal [:remove_foreign_key, [:dogs, column: "owner_id"]], enable
View
26 activerecord/test/cases/migration/foreign_key_test.rb
@@ -46,7 +46,21 @@ def test_foreign_keys
assert_equal "fk_name", fk.name
end
- def test_add_foreign_key
+ def test_add_foreign_key_inferes_column
+ @connection.add_foreign_key :astronauts, :rockets
+
+ foreign_keys = @connection.foreign_keys("astronauts")
+ assert_equal 1, foreign_keys.size
+
+ fk = foreign_keys.first
+ assert_equal "astronauts", fk.from_table
+ assert_equal "rockets", fk.to_table
+ assert_equal "rocket_id", fk.column
+ assert_equal "id", fk.primary_key
+ assert_equal "astronauts_rocket_id_fk", fk.name
+ end
+
+ def test_add_foreign_key_with_column
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"
foreign_keys = @connection.foreign_keys("astronauts")
@@ -112,7 +126,15 @@ def test_add_dependent_nullify_foreign_key
assert_equal :nullify, fk.dependent
end
- def test_remove_foreign_key
+ def test_remove_foreign_key_inferes_column
+ @connection.add_foreign_key :astronauts, :rockets
+
+ assert_equal 1, @connection.foreign_keys("astronauts").size
+ @connection.remove_foreign_key :astronauts, :rockets
+ assert_equal [], @connection.foreign_keys("astronauts")
+ end
+
+ def test_remove_foreign_key_by_column
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"
assert_equal 1, @connection.foreign_keys("astronauts").size

0 comments on commit d074b82

Please sign in to comment.