aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2014-06-20 08:56:30 +0200
committerYves Senn <yves.senn@gmail.com>2014-06-26 22:03:49 +0200
commit8550ba307d712ebede0d0695b5172bb3e9af16c9 (patch)
treeaa760c961323c5943401b3d4f1f1fbfa736ab812 /activerecord
parent31e4c19331c9262574d354250675bba7dcf9dba2 (diff)
downloadrails-8550ba307d712ebede0d0695b5172bb3e9af16c9.tar.gz
rails-8550ba307d712ebede0d0695b5172bb3e9af16c9.tar.bz2
rails-8550ba307d712ebede0d0695b5172bb3e9af16c9.zip
fk: raise for invalid :on_update / :on_delete values
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb11
-rw-r--r--activerecord/test/cases/migration/foreign_key_test.rb10
2 files changed, 18 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
index b896bd25e4..e495304376 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
@@ -104,9 +104,14 @@ FOREIGN KEY (#{quote_column_name(o.column)})
def action_sql(action, dependency)
case dependency
- when :nullify then "ON #{action} SET NULL"
- when :cascade then "ON #{action} CASCADE"
- when :restrict then "ON #{action} RESTRICT"
+ when :nullify then "ON #{action} SET NULL"
+ when :cascade then "ON #{action} CASCADE"
+ when :restrict then "ON #{action} RESTRICT"
+ else
+ raise ArgumentError, <<-MSG
+'#{dependency}' is not supported for :on_update or :on_delete.
+Supported values are: :nullify, :cascade, :restrict
+ MSG
end
end
end
diff --git a/activerecord/test/cases/migration/foreign_key_test.rb b/activerecord/test/cases/migration/foreign_key_test.rb
index 7dad67ef88..6a24df076d 100644
--- a/activerecord/test/cases/migration/foreign_key_test.rb
+++ b/activerecord/test/cases/migration/foreign_key_test.rb
@@ -126,6 +126,16 @@ module ActiveRecord
assert_equal :nullify, fk.on_delete
end
+ def test_on_update_and_on_delete_raises_with_invalid_values
+ assert_raises ArgumentError do
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :invalid
+ end
+
+ assert_raises ArgumentError do
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_update: :invalid
+ end
+ end
+
def test_add_foreign_key_with_on_update
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_update: :nullify