aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorYuki Masutomi <tommy@palette.cloud>2017-09-19 18:51:30 +0900
committerYuki Masutomi <tommy@palette.cloud>2017-09-20 10:27:25 +0900
commit0c8bed9f848446a6876c27781a38badb2c916be4 (patch)
treec892db0b2bc13651faeafcc7baae43d724ec0957 /activerecord
parentee033fd11308553ba118fb3974b99848c463156e (diff)
downloadrails-0c8bed9f848446a6876c27781a38badb2c916be4.tar.gz
rails-0c8bed9f848446a6876c27781a38badb2c916be4.tar.bz2
rails-0c8bed9f848446a6876c27781a38badb2c916be4.zip
make create_join_table compatible.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/migration/compatibility.rb15
-rw-r--r--activerecord/test/cases/migration/compatibility_test.rb30
2 files changed, 45 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/migration/compatibility.rb b/activerecord/lib/active_record/migration/compatibility.rb
index 784292f3f9..87c1c58aff 100644
--- a/activerecord/lib/active_record/migration/compatibility.rb
+++ b/activerecord/lib/active_record/migration/compatibility.rb
@@ -71,6 +71,21 @@ module ActiveRecord
end
end
+ def create_join_table(table_1, table_2, column_options: {}, **options)
+ column_options.reverse_merge!(type: :integer)
+
+ if block_given?
+ super(table_1, table_2, column_options: column_options, **options) do |t|
+ class << t
+ prepend TableDefinition
+ end
+ yield t
+ end
+ else
+ super
+ end
+ end
+
def add_reference(table_name, ref_name, **options)
super(table_name, ref_name, type: :integer, **options)
end
diff --git a/activerecord/test/cases/migration/compatibility_test.rb b/activerecord/test/cases/migration/compatibility_test.rb
index cb3b02c02a..e1311c0f21 100644
--- a/activerecord/test/cases/migration/compatibility_test.rb
+++ b/activerecord/test/cases/migration/compatibility_test.rb
@@ -199,6 +199,36 @@ class LegacyPrimaryKeyTest < ActiveRecord::TestCase
assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema
end
+ def test_legacy_join_table_foreign_keys_should_be_integer
+ @migration = Class.new(ActiveRecord::Migration[5.0]) {
+ def change
+ create_join_table :apples, :bananas do |t|
+ end
+ end
+ }.new
+
+ @migration.migrate(:up)
+
+ schema = dump_table_schema "apples_bananas"
+ assert_match %r{integer "apple_id", null: false}, schema
+ assert_match %r{integer "banana_id", null: false}, schema
+ end
+
+ def test_legacy_join_table_column_options_should_be_overwritten
+ @migration = Class.new(ActiveRecord::Migration[5.0]) {
+ def change
+ create_join_table :apples, :bananas, column_options: { type: :bigint } do |t|
+ end
+ end
+ }.new
+
+ @migration.migrate(:up)
+
+ schema = dump_table_schema "apples_bananas"
+ assert_match %r{bigint "apple_id", null: false}, schema
+ assert_match %r{bigint "banana_id", null: false}, schema
+ end
+
if current_adapter?(:Mysql2Adapter)
def test_legacy_bigint_primary_key_should_be_auto_incremented
@migration = Class.new(ActiveRecord::Migration[5.0]) {