Skip to content
Browse files

fk: dump foreign keys to schema.rb

respect `table_name_prefix` and `table_name_suffix`.
  • Loading branch information...
1 parent a48b675 commit 69c711f38cac85e9c8bdbe286591bf88ef720bfa @senny senny committed
View
4 activerecord/lib/active_record/migration.rb
@@ -645,7 +645,9 @@ def method_missing(method, *arguments, &block)
unless @connection.respond_to? :revert
unless arguments.empty? || [:execute, :enable_extension, :disable_extension].include?(method)
arguments[0] = proper_table_name(arguments.first, table_name_options)
- arguments[1] = proper_table_name(arguments.second, table_name_options) if method == :rename_table
+ if [:rename_table, :add_foreign_key].include?(method)
+ arguments[1] = proper_table_name(arguments.second, table_name_options)
+ end
end
end
return super unless connection.respond_to?(method)
View
22 activerecord/lib/active_record/schema_dumper.rb
@@ -171,6 +171,8 @@ def table(table, stream)
indexes(table, tbl)
+ foreign_keys(table, tbl)
+
tbl.rewind
stream.print tbl.read
rescue => e
@@ -212,6 +214,26 @@ def indexes(table, stream)
end
end
+ def foreign_keys(table, stream)
+ return unless @connection.supports_foreign_keys?
+
+ if (foreign_keys = @connection.foreign_keys(table)).any?
+ add_foreign_key_statements = foreign_keys.map do |foreign_key|
+ parts = [
+ 'add_foreign_key ' + remove_prefix_and_suffix(foreign_key.from_table).inspect,
+ remove_prefix_and_suffix(foreign_key.to_table).inspect,
+ 'column: ' + foreign_key.column.inspect,
+ 'primary_key: ' + foreign_key.primary_key.inspect,
+ 'name: ' + foreign_key.name.inspect
+ ]
+ ' ' + parts.join(', ')
+ end
+
+ stream.puts add_foreign_key_statements.sort.join("\n")
+ stream.puts
+ end
+ end
+
def remove_prefix_and_suffix(table)
table.gsub(/^(#{@options[:table_name_prefix]})(.+)(#{@options[:table_name_suffix]})$/, "\\2")
end
View
7 activerecord/test/cases/migration/foreign_key_test.rb
@@ -1,11 +1,13 @@
require 'cases/helper'
require 'support/ddl_helper'
+require 'support/schema_dumping_helper'
if ActiveRecord::Base.connection.supports_foreign_keys?
module ActiveRecord
class Migration
class ForeignKeyTest < ActiveRecord::TestCase
include DdlHelper
+ include SchemaDumpingHelper
class Rocket < ActiveRecord::Base
end
@@ -90,6 +92,11 @@ def test_remove_foreign_key_by_name
@connection.remove_foreign_key :astronauts, name: "fancy_named_fk"
assert_equal [], @connection.foreign_keys("astronauts")
end
+
+ def test_schema_dumping
+ output = dump_table_schema "fk_test_has_fk"
+ assert_match %r{\s+add_foreign_key "fk_test_has_fk", "fk_test_has_pk", column: "fk_id", primary_key: "id", name: "fk_name"$}, output
+ end
end
end
end
View
12 activerecord/test/cases/schema_dumper_test.rb
@@ -374,13 +374,19 @@ def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_
class CreateDogMigration < ActiveRecord::Migration
def up
+ create_table("dog_owners") do |t|
+ end
+
create_table("dogs") do |t|
t.column :name, :string
+ t.column :owner_id, :integer
end
add_index "dogs", [:name]
+ add_foreign_key :dogs, :dog_owners, column: "owner_id" if supports_foreign_keys?
end
def down
drop_table("dogs")
+ drop_table("dog_owners")
end
end
@@ -396,13 +402,17 @@ def test_schema_dump_with_table_name_prefix_and_suffix
assert_no_match %r{create_table "foo_.+_bar"}, output
assert_no_match %r{add_index "foo_.+_bar"}, output
assert_no_match %r{create_table "schema_migrations"}, output
+
+ if ActiveRecord::Base.connection.supports_foreign_keys?
+ assert_no_match %r{add_foreign_key "foo_.+_bar"}, output
+ assert_no_match %r{add_foreign_key "[^"]+", "foo_.+_bar"}, output
+ end
ensure
migration.migrate(:down)
ActiveRecord::Base.table_name_suffix = ActiveRecord::Base.table_name_prefix = ''
$stdout = original
end
-
end
class SchemaDumperDefaultsTest < ActiveRecord::TestCase

0 comments on commit 69c711f

Please sign in to comment.
Something went wrong with that request. Please try again.