Skip to content
Browse files

Define 'remove_foreign_key' in mysql and postgres adapters, because e…

…ach do it differently. There's some duplication here but it's not terrible.
  • Loading branch information...
1 parent 4b64658 commit 9dcb4ad185d51aa7631d937ab72eace6754a151a @matthuhiggins matthuhiggins committed
View
10 lib/foreigner/connection_adapters/mysql_adapter.rb
@@ -3,6 +3,16 @@ module ConnectionAdapters
module MysqlAdapter
include Foreigner::ConnectionAdapters::Sql2003
+ def remove_foreign_key(table, options)
+ if Hash === options
+ foreign_key_name = foreign_key_name(table, options[:column], options)
+ else
+ foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
+ end
+
+ execute "ALTER TABLE #{quote_table_name(table)} DROP FOREIGN KEY #{quote_column_name(foreign_key_name)}"
+ end
+
def foreign_keys(table_name)
fk_info = select_all %{
SELECT fk.referenced_table_name as 'to_table'
View
10 lib/foreigner/connection_adapters/postgresql_adapter.rb
@@ -2,6 +2,16 @@ module Foreigner
module ConnectionAdapters
module PostgreSQLAdapter
include Foreigner::ConnectionAdapters::Sql2003
+
+ def remove_foreign_key(table, options)
+ if Hash === options
+ foreign_key_name = foreign_key_name(table, options[:column], options)
+ else
+ foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
+ end
+
+ execute "ALTER TABLE #{quote_table_name(table)} DROP CONSTRAINT #{quote_column_name(foreign_key_name)}"
+ end
def foreign_keys(table_name)
fk_info = select_all %{
View
10 lib/foreigner/connection_adapters/sql_2003.rb
@@ -27,16 +27,6 @@ def foreign_key_definition(to_table, options = {})
sql
end
- def remove_foreign_key(table, options)
- if Hash === options
- foreign_key_name = foreign_key_name(table, options[:column], options)
- else
- foreign_key_name = foreign_key_name(table, "#{options.to_s.singularize}_id")
- end
-
- execute "ALTER TABLE #{quote_table_name(table)} DROP FOREIGN KEY #{quote_column_name(foreign_key_name)}"
- end
-
private
def foreign_key_name(table, column, options = {})
if options[:name]

0 comments on commit 9dcb4ad

Please sign in to comment.
Something went wrong with that request. Please try again.