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

Move integer-like primary key normalization to new_column_definition

Currently the normalization only exists in `primary_key` shorthand. It
should be moved to `new_column_definition` to also affect to
`add_column` with primary key.
This commit is contained in:
Ryuta Kamizono 2017-09-23 15:34:56 +09:00
parent dfe6939e16
commit 202d6578f4
6 changed files with 29 additions and 39 deletions

View file

@ -410,6 +410,10 @@ module ActiveRecord
def aliased_types(name, fallback)
"timestamp" == name ? :datetime : fallback
end
def integer_like_primary_key?(type, options)
options[:primary_key] && [:integer, :bigint].include?(type) && !options.key?(:default)
end
end
class AlterTable # :nodoc:

View file

@ -4,11 +4,6 @@ module ActiveRecord
module ConnectionAdapters
module MySQL
module ColumnMethods
def primary_key(name, type = :primary_key, **options)
options[:auto_increment] = true if [:integer, :bigint].include?(type) && !options.key?(:default)
super
end
def blob(*args, **options)
args.each { |name| column(name, :blob, options) }
end
@ -62,6 +57,10 @@ module ActiveRecord
include ColumnMethods
def new_column_definition(name, type, **options) # :nodoc:
if integer_like_primary_key?(type, options)
options[:auto_increment] = true
end
case type
when :virtual
type = options[:type]

View file

@ -44,15 +44,8 @@ module ActiveRecord
# a record (as primary keys cannot be +nil+). This might be done via the
# +SecureRandom.uuid+ method and a +before_save+ callback, for instance.
def primary_key(name, type = :primary_key, **options)
options[:auto_increment] = true if [:integer, :bigint].include?(type) && !options.key?(:default)
if type == :uuid
options[:default] = options.fetch(:default, "gen_random_uuid()")
elsif options.delete(:auto_increment) == true && %i(integer bigint).include?(type)
type = if type == :bigint || options[:limit] == 8
:bigserial
else
:serial
end
end
super
@ -185,6 +178,18 @@ module ActiveRecord
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
include ColumnMethods
def new_column_definition(name, type, **options) # :nodoc:
if integer_like_primary_key?(type, options)
type = if type == :bigint || options[:limit] == 8
:bigserial
else
:serial
end
end
super
end
end
class Table < ActiveRecord::ConnectionAdapters::Table

View file

@ -3,28 +3,20 @@
module ActiveRecord
module ConnectionAdapters
module SQLite3
module ColumnMethods
def primary_key(name, type = :primary_key, **options)
if %i(integer bigint).include?(type) && (options.delete(:auto_increment) == true || !options.key?(:default))
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
def references(*args, **options)
super(*args, type: :integer, **options)
end
alias :belongs_to :references
def new_column_definition(name, type, **options) # :nodoc:
if integer_like_primary_key?(type, options)
type = :primary_key
end
super
end
end
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
include ColumnMethods
def references(*args, **options)
super(*args, type: :integer, **options)
end
alias :belongs_to :references
end
class Table < ActiveRecord::ConnectionAdapters::Table
include ColumnMethods
end
end
end
end

View file

@ -39,10 +39,6 @@ module ActiveRecord
end
end
def update_table_definition(table_name, base)
SQLite3::Table.new(table_name, base)
end
def create_schema_dumper(options)
SQLite3::SchemaDumper.create(self, options)
end

View file

@ -93,13 +93,7 @@ module ActiveRecord
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
type = :integer
options[:primary_key] = true
end
super