Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fk: rename `dependent` to `on_delete`

  • Loading branch information...
commit 6955d864ceb0ba994ef4fb4c5e866463f247944b 1 parent d074b82
@senny senny authored
View
11 activerecord/lib/active_record/connection_adapters/abstract/schema_creation.rb
@@ -24,7 +24,7 @@ def visit_AddForeignKey(o)
FOREIGN KEY (#{quote_column_name(o.column)})
REFERENCES #{quote_table_name(o.to_table)} (#{quote_column_name(o.primary_key)})
SQL
- sql << " #{dependency_sql(o.dependent)}" if o.dependent
+ sql << " #{action_sql(o.on_delete)}" if o.on_delete
sql
end
@@ -101,12 +101,11 @@ def options_include_default?(options)
options.include?(:default) && !(options[:null] == false && options[:default].nil?)
end
- def dependency_sql(dependency)
+ def action_sql(action = "DELETE", dependency)
case dependency
- when :nullify then "ON DELETE SET NULL"
- when :delete then "ON DELETE CASCADE"
- when :restrict then "ON DELETE RESTRICT"
- else ""
+ when :nullify then "ON #{action} SET NULL"
+ when :cascade then "ON #{action} CASCADE"
+ when :restrict then "ON #{action} RESTRICT"
end
end
end
View
4 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -38,8 +38,8 @@ def primary_key
options[:primary_key]
end
- def dependent
- options[:dependent]
+ def on_delete
+ options[:on_delete]
end
end
View
2  activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -654,7 +654,7 @@ def add_foreign_key(from_table, to_table, options = {})
column: options[:column],
primary_key: primary_key,
name: foreign_key_name(from_table, options),
- dependent: options.fetch(:dependent, nil)
+ on_delete: options.fetch(:on_delete, nil)
}
at = create_alter_table from_table
at.add_foreign_key to_table, options
View
4 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -531,8 +531,8 @@ def foreign_keys(table_name)
}
if create_table_info =~ /CONSTRAINT #{quote_column_name(row['name'])} FOREIGN KEY .* REFERENCES .* ON DELETE (CASCADE|SET NULL|RESTRICT)/
- options[:dependent] = case $1
- when 'CASCADE' then :delete
+ options[:on_delete] = case $1
+ when 'CASCADE' then :cascade
when 'SET NULL' then :nullify
end
end
View
4 activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -470,8 +470,8 @@ def foreign_keys(table_name)
primary_key: row['primary_key']
}
- options[:dependent] = case row['dependency']
- when 'c'; :delete
+ options[:on_delete] = case row['dependency']
+ when 'c'; :cascade
when 'n'; :nullify
when 'r'; :restrict
end
View
2  activerecord/lib/active_record/schema_dumper.rb
@@ -226,7 +226,7 @@ def foreign_keys(table, stream)
'primary_key: ' + foreign_key.primary_key.inspect,
'name: ' + foreign_key.name.inspect
]
- parts << ('dependent: ' + foreign_key.dependent.inspect) if foreign_key.dependent
+ parts << ('on_delete: ' + foreign_key.on_delete.inspect) if foreign_key.on_delete
' ' + parts.join(', ')
end
View
26 activerecord/test/cases/migration/foreign_key_test.rb
@@ -91,8 +91,8 @@ def test_add_foreign_key_with_non_standard_primary_key
end
end
- def test_add_dependent_restrict_foreign_key
- @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", dependent: :restrict
+ def test_add_on_delete_restrict_foreign_key
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :restrict
foreign_keys = @connection.foreign_keys("astronauts")
assert_equal 1, foreign_keys.size
@@ -100,30 +100,30 @@ def test_add_dependent_restrict_foreign_key
fk = foreign_keys.first
if current_adapter?(:MysqlAdapter, :Mysql2Adapter)
# ON DELETE RESTRICT is the default on MySQL
- assert_equal nil, fk.dependent
+ assert_equal nil, fk.on_delete
else
- assert_equal :restrict, fk.dependent
+ assert_equal :restrict, fk.on_delete
end
end
- def test_add_dependent_delete_foreign_key
- @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", dependent: :delete
+ def test_add_on_delete_cascade_foreign_key
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :cascade
foreign_keys = @connection.foreign_keys("astronauts")
assert_equal 1, foreign_keys.size
fk = foreign_keys.first
- assert_equal :delete, fk.dependent
+ assert_equal :cascade, fk.on_delete
end
- def test_add_dependent_nullify_foreign_key
- @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", dependent: :nullify
+ def test_add_on_delete_nullify_foreign_key
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :nullify
foreign_keys = @connection.foreign_keys("astronauts")
assert_equal 1, foreign_keys.size
fk = foreign_keys.first
- assert_equal :nullify, fk.dependent
+ assert_equal :nullify, fk.on_delete
end
def test_remove_foreign_key_inferes_column
@@ -155,11 +155,11 @@ def test_schema_dumping
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
- def test_schema_dumping_dependent_option
- @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", dependent: :nullify
+ def test_schema_dumping_on_delete_option
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", on_delete: :nullify
output = dump_table_schema "astronauts"
- assert_match %r{\s+add_foreign_key "astronauts",.+dependent: :nullify$}, output
+ assert_match %r{\s+add_foreign_key "astronauts",.+on_delete: :nullify$}, output
end
class CreateCitiesAndHousesMigration < ActiveRecord::Migration
Please sign in to comment.
Something went wrong with that request. Please try again.