Permalink
Browse files

fk: support dependent option (:delete, :nullify and :restrict).

  • Loading branch information...
1 parent 69c711f commit 402f303f1d938cf2c7781d7734c4ff8e6b874f35 @senny senny committed Jun 10, 2014
@@ -19,11 +19,13 @@ def visit_AddColumn(o)
end
def visit_AddForeignKey(o)
- <<-SQL
+ sql = <<-SQL
ADD CONSTRAINT #{quote_column_name(o.name)}
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
end
def visit_DropForeignKey(name)
@@ -98,6 +100,15 @@ def quote_value(value, column)
def options_include_default?(options)
options.include?(:default) && !(options[:null] == false && options[:default].nil?)
end
+
+ def dependency_sql(dependency)
+ case dependency
+ when :nullify then "ON DELETE SET NULL"
+ when :delete then "ON DELETE CASCADE"
+ when :restrict then "ON DELETE RESTRICT"
+ else ""
+ end
+ end
end
end
end
@@ -37,6 +37,10 @@ def column
def primary_key
options[:primary_key]
end
+
+ def dependent
+ options[:dependent]
+ end
end
# Represents the schema of an SQL table in an abstract way. This class
@@ -652,7 +652,8 @@ def add_foreign_key(from_table, to_table, options = {})
options = {
column: options.fetch(:column),
primary_key: primary_key,
- name: foreign_key_name(from_table, options)
+ name: foreign_key_name(from_table, options),
+ dependent: options.fetch(:dependent, nil)
}
at = create_alter_table from_table
at.add_foreign_key to_table, options
@@ -524,7 +524,19 @@ def foreign_keys(table_name)
create_table_info = select_one("SHOW CREATE TABLE #{quote_table_name(table_name)}")["Create Table"]
fk_info.map do |row|
- options = {column: row['column'], name: row['name'], primary_key: row['primary_key']}
+ options = {
+ column: row['column'],
+ name: row['name'],
+ primary_key: row['primary_key']
+ }
+
+ 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
+ when 'SET NULL' then :nullify
+ end
+ end
+
ForeignKeyDefinition.new(table_name, row['to_table'], options)
end
end
@@ -467,7 +467,14 @@ def foreign_keys(table_name)
options = {
column: row['column'],
name: row['name'],
- primary_key: row['primary_key'] }
+ primary_key: row['primary_key']
+ }
+
+ options[:dependent] = case row['dependency']
+ when 'c'; :delete
+ when 'n'; :nullify
+ when 'r'; :restrict
+ end
ForeignKeyDefinition.new(table_name, row["to_table"], options)
end
end
@@ -226,6 +226,8 @@ 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.join(', ')
end
@@ -77,6 +77,41 @@ 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
+
+ foreign_keys = @connection.foreign_keys("astronauts")
+ assert_equal 1, foreign_keys.size
+
+ fk = foreign_keys.first
+ if current_adapter?(:MysqlAdapter, :Mysql2Adapter)
+ # ON DELETE RESTRICT is the default on MySQL
+ assert_equal nil, fk.dependent
+ else
+ assert_equal :restrict, fk.dependent
+ end
+ end
+
+ def test_add_dependent_delete_foreign_key
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", dependent: :delete
+
+ foreign_keys = @connection.foreign_keys("astronauts")
+ assert_equal 1, foreign_keys.size
+
+ fk = foreign_keys.first
+ assert_equal :delete, fk.dependent
+ end
+
+ def test_add_dependent_nullify_foreign_key
+ @connection.add_foreign_key :astronauts, :rockets, column: "rocket_id", dependent: :nullify
+
+ foreign_keys = @connection.foreign_keys("astronauts")
+ assert_equal 1, foreign_keys.size
+
+ fk = foreign_keys.first
+ assert_equal :nullify, fk.dependent
+ end
+
def test_remove_foreign_key
@connection.add_foreign_key :astronauts, :rockets, column: "rocket_id"
@@ -97,6 +132,13 @@ def test_schema_dumping
output = dump_table_schema "fk_test_has_fk"
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
+
+ output = dump_table_schema "astronauts"
+ assert_match %r{\s+add_foreign_key "astronauts",.+dependent: :nullify$}, output
+ end
end
end
end

0 comments on commit 402f303

Please sign in to comment.