aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-06-10 15:00:59 +0200
committerYves Senn <yves.senn@gmail.com>2014-06-26 22:03:48 +0200
commit6073d7c683b19fc7394baa9a93bc44e71e071129 (patch)
treea53d01cc85457f47308aa0eb3a843206604d2a7d /activerecord/test/cases
parent402f303f1d938cf2c7781d7734c4ff8e6b874f35 (diff)
downloadrails-6073d7c683b19fc7394baa9a93bc44e71e071129.tar.gz
rails-6073d7c683b19fc7394baa9a93bc44e71e071129.tar.bz2
rails-6073d7c683b19fc7394baa9a93bc44e71e071129.zip
fk: make `add_foreign_key` reversible.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/migration/command_recorder_test.rb20
-rw-r--r--activerecord/test/cases/migration/foreign_key_test.rb19
2 files changed, 39 insertions, 0 deletions
diff --git a/activerecord/test/cases/migration/command_recorder_test.rb b/activerecord/test/cases/migration/command_recorder_test.rb
index 1c0134843b..64a1b9a84e 100644
--- a/activerecord/test/cases/migration/command_recorder_test.rb
+++ b/activerecord/test/cases/migration/command_recorder_test.rb
@@ -270,6 +270,26 @@ module ActiveRecord
enable = @recorder.inverse_of :disable_extension, ['uuid-ossp']
assert_equal [:enable_extension, ['uuid-ossp'], nil], 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
+ end
+
+ def test_invert_add_foreign_key_with_column_and_name
+ enable = @recorder.inverse_of :add_foreign_key, [:dogs, :people, column: "owner_id", name: "fk"]
+ assert_equal [:remove_foreign_key, [:dogs, name: "fk"]], enable
+ end
+
+ def test_remove_foreign_key_is_irreversible
+ assert_raises ActiveRecord::IrreversibleMigration do
+ @recorder.inverse_of :remove_foreign_key, [:dogs, column: "owner_id"]
+ end
+
+ assert_raises ActiveRecord::IrreversibleMigration do
+ @recorder.inverse_of :remove_foreign_key, [:dogs, name: "fk"]
+ end
+ end
end
end
end
diff --git a/activerecord/test/cases/migration/foreign_key_test.rb b/activerecord/test/cases/migration/foreign_key_test.rb
index fbdb921334..5935062efb 100644
--- a/activerecord/test/cases/migration/foreign_key_test.rb
+++ b/activerecord/test/cases/migration/foreign_key_test.rb
@@ -139,6 +139,25 @@ module ActiveRecord
output = dump_table_schema "astronauts"
assert_match %r{\s+add_foreign_key "astronauts",.+dependent: :nullify$}, output
end
+
+ class CreateCitiesAndHousesMigration < ActiveRecord::Migration
+ def change
+ create_table("cities") { |t| }
+
+ create_table("houses") do |t|
+ t.column :city_id, :integer
+ end
+ add_foreign_key :houses, :cities, column: "city_id"
+ end
+ end
+
+ def test_add_foreign_key_is_reversible
+ migration = CreateCitiesAndHousesMigration.new
+ silence_stream($stdout) { migration.migrate(:up) }
+ assert_equal ["houses_city_id_fk"], @connection.foreign_keys("houses").map(&:name)
+ ensure
+ silence_stream($stdout) { migration.migrate(:down) }
+ end
end
end
end