Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First!

  • Loading branch information...
commit ae6982185a015a10ea3ae3337aee61332bd30bfd 0 parents
Matthew Higgins authored
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
65 README
@@ -0,0 +1,65 @@
+Foreigner
+=========
+
+Rails migrations do not have methods to add foreign keys. Foreigner introduces a few
+methods to your migrations for adding and removing foreign key constraints.
+
+Since each adapter implements the API, migrations using Foreigner will continue to
+work on databases that do not support foreign keys, such as sqlite.
+
+
+API
+===
+
+An adapter implementing the Foreigner API must implement two methods.
+(Options are documented in connection_adapters/abstract/schema_definitions.rb):
+
+ add_foreign_key(from_table, to_table, options)
+ remove_foreign_key(from_table, options)
+
+Foreigner also provides extra behavior to 'change_table'. This simply leverages
+the implementation of 'add_foreign_key' and 'remove_foreign_key':
+ ...
+ change_table [table_name] do |t|
+ t.foreign_key [to_table], options
+ t.remove_foreign_key [to_table], options
+ end
+ ...
+
+
+Example
+=======
+
+The most common use of foreign keys is to reference a table that a model belongs to.
+For example, given the following model:
+
+ class Comment < ActiveRecord::Base
+ belongs_to :post
+ end
+
+ class Post < ActiveRecord::Base
+ has_many :comments, :dependent => :delete_all
+ end
+
+You should add a foreign key in your migration:
+
+ add_foreign_key(:comments, :posts)
+ # => "ALTER TABLE `comments` ADD CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)"
+
+The :dependent option can be moved from the has_many definition to the foreign key:
+
+ add_foreign_key(:comments, :posts, :dependent => :delete)
+
+If the column is named article_id instead of post_id, use the :column option:
+
+ add_foreign_key(:comments, :posts, :column => 'article_id')
+
+Foreign keys can also be generated using 'change_table':
+
+ change_table :comments do |t|
+ t.foreign_key :posts, :dependent => :delete, :name => 'comments_to_posts'
+ end
+
+Foreigner should work for those using singular table names. See the rdocs for more details.
+
+Copyright (c) 2009 [Matthew Higgins], released under the MIT license
23 Rakefile
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the foreigner plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the foreigner plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Foreigner'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1  init.rb
@@ -0,0 +1 @@
+require 'foreigner'
1  install.rb
@@ -0,0 +1 @@
+# Install hook code here
50 lib/connection_adapters/abstract/schema_definitions.rb
@@ -0,0 +1,50 @@
+module Foreigner
+ module AdapterMethods
+ def supports_foreign_keys?
+ false
+ end
+
+ # Adds a new foreign key to the +from_table+, referencing the primary key of +to_table+
+ #
+ # The foreign key will be named after the from and to tables unless you pass
+ # <tt>:name</tt> as an option.
+ #
+ # ===== Examples
+ # ====== Creating a foreign key
+ # add_foreign_key(:comments, :posts)
+ # generates
+ # ALTER TABLE `comments` ADD CONSTRAINT
+ # `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`)
+ #
+ # ====== Removing a foreign key
+ # remove_foreign_key(:comments, :posts)
+ # generates
+ # ALTER TABLE `comments` DROP FOREIGN KEY `comments_post_id_fk`
+ #
+ #
+ # === Supported options
+ # [:column]
+ # Specify the column name on the from_table that references the to_table. By default this is guessed
+ # to be the singular name of the to_table with "_id" suffixed. So a to_table of :posts will use "post_id"
+ # as the default <tt>:column</tt>.
+ # [:name]
+ # Specify the name of the foreign key constraint. This defaults to use the from table and column.
+ # [:dependent]
+ # If set to <tt>:delete</tt>, the associated records in from_table are deleted when records in to_table table are deleted.
+ # If set to <tt>:nullify</tt>, the from_table column is set to +NULL+.
+ def add_foreign_key(from_table, to_table, options = {})
+ end
+
+ # Remove the given foreign key from the table.
+ #
+ # ===== Examples
+ # ====== Remove the suppliers_company_id_fk in the suppliers table.
+ # remove_foreign_key :suppliers, :companies
+ # ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
+ # remove_foreign_key :accounts, :column => :branch_id
+ # ====== Remove the foreign key named party_foreign_key in the accounts table.
+ # remove_foreign_key :accounts, :name => :party_foreign_key
+ def remove_foreign_key(from_table, options)
+ end
+ end
+end
30 lib/connection_adapters/abstract/schema_statements.rb
@@ -0,0 +1,30 @@
+module Foreigner
+ module TableMethods
+ # Adds a new foreign key to the table. +column_name+ can be a single Symbol, or
+ # an Array of Symbols. See SchemaStatements#add_foreign_key
+ #
+ # ===== Examples
+ # ====== Creating a simple foreign key
+ # t.foreign_key(:people)
+ # ====== Defining the column
+ # t.foreign_key(:people, :column => :sender_id)
+ # ====== Creating a named foreign key
+ # t.foreign_key(:people, :column => :sender_id, :name => 'sender_foreign_key')
+ def foreign_key(table, options = {})
+ @base.add_foreign_key(@table_name, table, options)
+ end
+
+ # Remove the given foreign key from the table.
+ #
+ # ===== Examples
+ # ====== Remove the suppliers_company_id_fk in the suppliers table.
+ # t.remove_foreign_key :companies
+ # ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
+ # remove_foreign_key :column => :branch_id
+ # ====== Remove the foreign key named party_foreign_key in the accounts table.
+ # remove_index :name => :party_foreign_key
+ def remove_foreign_key(options = {})
+ @base.remove_foreign_key(@table_name, options)
+ end
+ end
+end
46 lib/connection_adapters/mysql_adapter.rb
@@ -0,0 +1,46 @@
+module Foreigner
+ module MysqlAdapter
+ def supports_foreign_keys?
+ true
+ end
+
+ def add_foreign_key(from_table, to_table, options = {})
+ column = options[:column] || "#{to_table.to_s.singularize}_id"
+ dependency = dependency_sql(options[:dependent])
+
+ execute %{
+ ALTER TABLE #{from_table}
+ ADD CONSTRAINT #{foreign_key_name(from_table, column, options)}
+ FOREIGN KEY (#{column}) REFERENCES #{to_table}(id)
+ #{dependency}
+ }
+ 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 #{table} DROP FOREIGN KEY #{foreign_key_name}"
+ end
+
+ private
+ def foreign_key_name(table, column, options = {})
+ if options[:name]
+ options[:name]
+ else
+ "#{table}_#{column}_fk"
+ end
+ end
+
+ def dependency_sql(dependency)
+ case dependency
+ when :nullify then "ON DELETE SET NULL"
+ when :delete then "ON DELETE CASCADE"
+ else ""
+ end
+ end
+ end
+end
21 lib/foreigner.rb
@@ -0,0 +1,21 @@
+require 'connection_adapters/abstract/schema_definitions'
+require 'connection_adapters/abstract/schema_statements'
+require 'connection_adapters/mysql_adapter'
+
+module ActiveRecord
+ module ConnectionAdapters
+ AbstractAdapter.class_eval do
+ include Foreigner::AdapterMethods
+ end
+
+ MysqlAdapter.class_eval do
+ include Foreigner::MysqlAdapter
+ end
+
+ class TableDefinition
+ Table.class_eval do
+ include Foreigner::TableMethods
+ end
+ end
+ end
+end
4 tasks/foreigner_tasks.rake
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :foreigner do
+# # Task goes here
+# end
19 test/foreigner_test.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class ForeignerTest < ActiveRecord::TestCase
+ def setup
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
+ alias_method :execute_without_stub, :execute
+ def execute(sql, name = nil) return sql end
+ end
+ end
+
+ def teardown
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
+ remove_method :execute
+ alias_method :execute, :execute_without_stub
+ end
+ end
+
+
+end
7 test/test_helper.rb
@@ -0,0 +1,7 @@
+require 'test/unit'
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
+require 'active_record'
+require 'active_record/test_case'
+require File.dirname(__FILE__) + '/../lib/foreigner'
1  uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.