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

Adding legacy primary key should be compatible

Currently implicit legacy primary key is compatible, but adding explicit
legacy primary key is not compatible. It should also be fixed.

Fixes #30664.
This commit is contained in:
Ryuta Kamizono 2017-09-23 13:18:11 +09:00
parent c371eeffb3
commit dfe6939e16
2 changed files with 70 additions and 0 deletions

View file

@ -20,6 +20,11 @@ module ActiveRecord
class V5_0 < V5_1 class V5_0 < V5_1
module TableDefinition module TableDefinition
def primary_key(name, type = :primary_key, **options)
type = :integer if type == :primary_key
super
end
def references(*args, **options) def references(*args, **options)
super(*args, type: :integer, **options) super(*args, type: :integer, **options)
end end
@ -86,6 +91,20 @@ module ActiveRecord
end end
end end
def add_column(table_name, column_name, type, options = {})
if type == :primary_key
case adapter_name
when "PostgreSQL"
type = :serial
when "Mysql2"
type = :integer
options[:auto_increment] = true
end
options[:primary_key] = true
end
super
end
def add_reference(table_name, ref_name, **options) def add_reference(table_name, ref_name, **options)
super(table_name, ref_name, type: :integer, **options) super(table_name, ref_name, type: :integer, **options)
end end

View file

@ -199,6 +199,57 @@ class LegacyPrimaryKeyTest < ActiveRecord::TestCase
assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema assert_match %r{create_table "legacy_primary_keys", id: :integer, default: nil}, schema
end end
if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
def test_legacy_primary_key_in_create_table_should_be_integer
@migration = Class.new(ActiveRecord::Migration[5.0]) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.primary_key :id
end
end
}.new
@migration.migrate(:up)
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
end
def test_legacy_primary_key_in_change_table_should_be_integer
@migration = Class.new(ActiveRecord::Migration[5.0]) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.integer :dummy
end
change_table :legacy_primary_keys do |t|
t.primary_key :id
end
end
}.new
@migration.migrate(:up)
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
end
def test_add_column_with_legacy_primary_key_should_be_integer
@migration = Class.new(ActiveRecord::Migration[5.0]) {
def change
create_table :legacy_primary_keys, id: false do |t|
t.integer :dummy
end
add_column :legacy_primary_keys, :id, :primary_key
end
}.new
@migration.migrate(:up)
schema = dump_table_schema "legacy_primary_keys"
assert_match %r{create_table "legacy_primary_keys", id: :(?:integer|serial), (?!default: nil)}, schema
end
end
def test_legacy_join_table_foreign_keys_should_be_integer def test_legacy_join_table_foreign_keys_should_be_integer
@migration = Class.new(ActiveRecord::Migration[5.0]) { @migration = Class.new(ActiveRecord::Migration[5.0]) {
def change def change