diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-06-11 11:35:26 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-06-26 22:03:49 +0200 |
commit | 0938d5305758dfcbec4e813c777bb627e82a5906 (patch) | |
tree | 7739228701950754fb612f9f2d648d0bf1f07d51 /activerecord | |
parent | 9ae1a2c69f51a9065090a9c505f4d22ffbb84094 (diff) | |
download | rails-0938d5305758dfcbec4e813c777bb627e82a5906.tar.gz rails-0938d5305758dfcbec4e813c777bb627e82a5906.tar.bz2 rails-0938d5305758dfcbec4e813c777bb627e82a5906.zip |
fk: dump foreign keys at the bottom to make sure tables exist.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/schema_dumper.rb | 15 | ||||
-rw-r--r-- | activerecord/test/cases/schema_dumper_test.rb | 7 |
2 files changed, 16 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 9b46f7751b..84ce725409 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -91,7 +91,8 @@ HEADER end def tables(stream) - @connection.tables.sort.each do |tbl| + sorted_tables = @connection.tables.sort + sorted_tables.each do |tbl| next if ['schema_migrations', ignore_tables].flatten.any? do |ignored| case ignored when String; remove_prefix_and_suffix(tbl) == ignored @@ -102,6 +103,13 @@ HEADER end table(tbl, stream) end + + # dump foreign keys at the end to make sure all dependent tables exist. + if @connection.supports_foreign_keys? + sorted_tables.each do |tbl| + foreign_keys(tbl, stream) + end + end end def table(table, stream) @@ -171,8 +179,6 @@ HEADER indexes(table, tbl) - foreign_keys(table, tbl) - tbl.rewind stream.print tbl.read rescue => e @@ -215,8 +221,6 @@ HEADER 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 = [ @@ -233,7 +237,6 @@ HEADER end stream.puts add_foreign_key_statements.sort.join("\n") - stream.puts end end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index b1e7420c66..4e71d04bc0 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -372,6 +372,13 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_match %r{create_table "subscribers", id: false}, output end + if ActiveRecord::Base.connection.supports_foreign_keys? + def test_foreign_keys_are_dumped_at_the_bottom_to_circumvent_dependency_issues + output = standard_dump + assert_match(/^\s+add_foreign_key "fk_test_has_fk"[^\n]+\n\s+add_foreign_key "lessons_students"/, output) + end + end + class CreateDogMigration < ActiveRecord::Migration def up create_table("dog_owners") do |t| |