Browse files

Add regression test for foreign key schema dump caching

If you had a foreign key set and then decided to add `on_delete:
:cascade` later in another migration that migration would run but
wouldn't refresh the schema dump.

The reason for this was because `create_table_info` caches the statement
and sets it to be the same as the original declaration for the foreign
key (without the `on_delete: :cascade`.

PR #25307 ended up fixing this bug because it removes the check for
`create_table_info` and relies on reading from `information_schema`. The
fix however was intended to patch another bug. The reason this fixes the
issue is we're no longer parsing the regex from the cached

This regression test is to ensure that the issue does not return if we
for some reason go back to using `create_table_info` to set the foreign
  • Loading branch information...
eileencodes authored and rafaelfranca committed Jul 1, 2016
1 parent 72d18e5 commit 7a9e88eee629ef6bf326897e3a510c39d3a5c617
Showing with 13 additions and 0 deletions.
  1. +13 −0 activerecord/test/cases/migration/foreign_key_test.rb
@@ -232,6 +232,10 @@ def change
t.column :city_id, :integer
add_foreign_key :houses, :cities, column: "city_id"
+ # remove and re-add to test that schema is updated and not accidently cached
+ remove_foreign_key :houses, :cities
+ add_foreign_key :houses, :cities, column: "city_id", on_delete: :cascade
@@ -243,6 +247,15 @@ def test_add_foreign_key_is_reversible
silence_stream($stdout) { migration.migrate(:down) }
+ def test_foreign_key_constraint_is_not_cached_incorrectly
+ migration =
+ silence_stream($stdout) { migration.migrate(:up) }
+ output = dump_table_schema "houses"
+ assert_match %r{\s+add_foreign_key "houses",.+on_delete: :cascade$}, output
+ ensure
+ silence_stream($stdout) { migration.migrate(:down) }
+ end
class CreateSchoolsAndClassesMigration < ActiveRecord::Migration::Current
def change

0 comments on commit 7a9e88e

Please sign in to comment.