1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Allow to specify a type for foreign key column in migrations

[Andrey Novikov & Łukasz Sarnacki]
This commit is contained in:
Andrey Novikov 2014-07-19 23:38:38 +04:00
parent bb3b2d0b09
commit 6d327dbc0c
6 changed files with 46 additions and 3 deletions

View file

@ -1,3 +1,14 @@
* Allow to specify a type for created foreign key column in `references` and
`add_reference` in migrations.
Example:
change_table :vehicle do |t|
t.references :station, type: :uuid
end
*Andrey Novikov & Łukasz Sarnacki*
* `create_join_table` removes a common prefix when generating the join table.
This matches the existing behavior of HABTM associations.

View file

@ -280,12 +280,19 @@ module ActiveRecord
column(:updated_at, :datetime, options)
end
# Adds an appropriately-named _id column as <tt>:integer</tt> (or whatever <tt>:type</tt> option specifies),
# plus a corresponding _type column if the <tt>:polymorphic</tt> option is supplied. If <tt>:polymorphic</tt>
# is a hash of options, these will be used when creating the <tt>_type</tt> column. The <tt>:index</tt> option
# will also create an index, similar to calling <tt>add_index</tt>.
#
# references :tagger, polymorphic: true, index: true, type: :uuid
def references(*args)
options = args.extract_options!
polymorphic = options.delete(:polymorphic)
index_options = options.delete(:index)
type = options.delete(:type) || :integer
args.each do |col|
column("#{col}_id", :integer, options)
column("#{col}_id", type, options)
column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
index(polymorphic ? %w(id type).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
end

View file

@ -619,7 +619,8 @@ module ActiveRecord
def add_reference(table_name, ref_name, options = {})
polymorphic = options.delete(:polymorphic)
index_options = options.delete(:index)
add_column(table_name, "#{ref_name}_id", :integer, options)
type = options.delete(:type) || :integer
add_column(table_name, "#{ref_name}_id", type, options)
add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
end

View file

@ -233,7 +233,7 @@ class PostgresqlUUIDTestInverseOf < ActiveRecord::TestCase
t.string 'title'
end
connection.create_table('pg_uuid_comments', id: :uuid) do |t|
t.uuid :uuid_post_id, default: 'uuid_generate_v4()'
t.references :uuid_post, type: :uuid
t.string 'content'
end
end

View file

@ -72,6 +72,20 @@ module ActiveRecord
end
end
def test_references_column_type_with_polymorphic_and_type
with_change_table do |t|
@connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true, type: :string]
t.references :taggable, polymorphic: true, type: :string
end
end
def test_remove_references_column_type_with_polymorphic_and_type
with_change_table do |t|
@connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true, type: :string]
t.remove_references :taggable, polymorphic: true, type: :string
end
end
def test_timestamps_creates_updated_at_and_created_at
with_change_table do |t|
@connection.expect :add_timestamps, nil, [:delete_me]

View file

@ -55,6 +55,16 @@ module ActiveRecord
assert index_exists?(table_name, :tag_id, name: 'index_taggings_on_tag_id')
end
def test_creates_reference_id_with_specified_type
add_reference table_name, :user, type: :string
assert column_exists?(table_name, :user_id, :string)
end
def test_does_not_create_reference_id_with_specified_type
add_reference table_name, :user
assert_not column_exists?(table_name, :user_id, :string)
end
def test_deletes_reference_id_column
remove_reference table_name, :supplier
assert_not column_exists?(table_name, :supplier_id, :integer)