Skip to content

Loading…

Add support for foreign_key during create_table #97

Merged
merged 7 commits into from

1 participant

@matthuhiggins

You can now do:

create_table :comments do |t|
  t.string :text
  t.integer :post_id
  t.foreign_key :posts
end
@matthuhiggins matthuhiggins merged commit 7934126 into master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 3, 2012
Commits on Jan 14, 2013
  1. test file

    committed
  2. Rails 4.0 supports adding indexes during create_table. We shall do th…

    committed
    …e same for foreign_keys.
    
    spread out some code. Make table_definitions test work
    
    test file
    
    fix up create_table override
  3. Merge branch 'master' into create_table_definition

    committed
    Conflicts:
    	lib/foreigner/connection_adapters/abstract/schema_definitions.rb
View
2 lib/foreigner.rb
@@ -13,6 +13,8 @@ module ConnectionAdapters
autoload :ForeignKeyDefinition
autoload :SchemaDefinitions
autoload :SchemaStatements
+ autoload :Table
+ autoload :TableDefinition
end
end
View
56 lib/foreigner/connection_adapters/abstract/schema_definitions.rb
@@ -5,62 +5,10 @@ def self.included(base)
base::Table.class_eval do
include Foreigner::ConnectionAdapters::Table
end
- end
- end
-
- module Table
- extend ActiveSupport::Concern
-
- included do
- alias_method_chain :references, :foreign_keys
- end
- # Adds a new foreign key to the table. +to_table+ 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')
- # ====== Defining the column of the +to_table+.
- # t.foreign_key(:people, column: :sender_id, primary_key: :person_id)
- def foreign_key(to_table, options = {})
- @base.add_foreign_key(@table_name, to_table, options)
- end
-
- # Remove the given foreign key from the table.
- #
- # ===== Examples
- # ====== Remove the suppliers_company_id_fk in the suppliers table.
- # change_table :suppliers do |t|
- # t.remove_foreign_key :companies
- # end
- # ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
- # change_table :accounts do |t|
- # t.remove_foreign_key column: :branch_id
- # end
- # ====== Remove the foreign key named party_foreign_key in the accounts table.
- # change_table :accounts do |t|
- # t.remove_foreign_key name: :party_foreign_key
- # end
- def remove_foreign_key(options)
- @base.remove_foreign_key(@table_name, options)
- end
-
- # Deprecated
- def references_with_foreign_keys(*args)
- options = args.extract_options!
-
- if fk_options = options.delete(:foreign_key)
- p ActiveSupport::Deprecation.send(:deprecation_message, caller,
- ":foreign_key in t.references is deprecated. " \
- "Use t.foreign_key instead")
+ base::TableDefinition.class_eval do
+ include Foreigner::ConnectionAdapters::TableDefinition
end
-
- references_without_foreign_keys(*(args.dup << options))
end
end
end
View
9 lib/foreigner/connection_adapters/abstract/schema_statements.rb
@@ -9,6 +9,15 @@ def self.included(base)
end
module AbstractAdapter
+ def create_table(table_name, *args, &block)
+ definition = nil
+ super do |td|
+ definition = td # This is my trick to get the definition
+ block.call(td)
+ end
+ definition.foreign_keys.each { |c,o| add_foreign_key table_name, c, o }
+ end
+
def supports_foreign_keys?
false
end
View
59 lib/foreigner/connection_adapters/abstract/table.rb
@@ -0,0 +1,59 @@
+module Foreigner
+ module ConnectionAdapters
+ module Table
+ extend ActiveSupport::Concern
+
+ included do
+ alias_method_chain :references, :foreign_keys
+ end
+
+ # Adds a new foreign key to the table. +to_table+ 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')
+ # ====== Defining the column of the +to_table+.
+ # t.foreign_key(:people, column: :sender_id, primary_key: :person_id)
+ def foreign_key(to_table, options = {})
+ @base.add_foreign_key(@table_name, to_table, options)
+ end
+
+ # Remove the given foreign key from the table.
+ #
+ # ===== Examples
+ # ====== Remove the suppliers_company_id_fk in the suppliers table.
+ # change_table :suppliers do |t|
+ # t.remove_foreign_key :companies
+ # end
+ # ====== Remove the foreign key named accounts_branch_id_fk in the accounts table.
+ # change_table :accounts do |t|
+ # t.remove_foreign_key column: :branch_id
+ # end
+ # ====== Remove the foreign key named party_foreign_key in the accounts table.
+ # change_table :accounts do |t|
+ # t.remove_foreign_key name: :party_foreign_key
+ # end
+ def remove_foreign_key(options)
+ @base.remove_foreign_key(@table_name, options)
+ end
+
+ # Deprecated
+ def references_with_foreign_keys(*args)
+ options = args.extract_options!
+
+ if fk_options = options.delete(:foreign_key)
+ p ActiveSupport::Deprecation.send(:deprecation_message, caller,
+ ":foreign_key in t.references is deprecated. " \
+ "Use t.foreign_key instead")
+ end
+
+ references_without_foreign_keys(*(args.dup << options))
+ end
+ end
+ end
+end
View
13 lib/foreigner/connection_adapters/abstract/table_definition.rb
@@ -0,0 +1,13 @@
+module Foreigner
+ module ConnectionAdapters
+ module TableDefinition
+ def foreign_key(to_table, options = {})
+ foreign_keys[to_table] = options
+ end
+
+ def foreign_keys
+ @foreign_keys ||= {}
+ end
+ end
+ end
+end
View
5 test/foreigner/connection_adapters/abstract/schema_statements_test.rb
@@ -0,0 +1,5 @@
+require 'helper'
+
+class Foreigner::ConnectionAdapters::SchemaStatementsTest < ActiveSupport::TestCase
+
+end
View
13 test/foreigner/connection_adapters/abstract/table_definition_test.rb
@@ -0,0 +1,13 @@
+require 'helper'
+
+class Foreigner::ConnectionAdapters::TableDefinitionsTest < ActiveSupport::TestCase
+ class TestDefinition
+ include Foreigner::ConnectionAdapters::TableDefinition
+ end
+
+ test "foreign_key" do
+ definition = TestDefinition.new
+ definition.foreign_key :poops, and: :one;
+ assert_equal definition.foreign_keys[:poops], and: :one
+ end
+end
Something went wrong with that request. Please try again.