提交 202d6578 编写于 作者: R Ryuta Kamizono

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.
上级 dfe6939e
......@@ -410,6 +410,10 @@ def create_column_definition(name, type, options)
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:
......
......@@ -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 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
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]
......
......@@ -44,15 +44,8 @@ module ColumnMethods
# 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 @@ def xml(*args, **options)
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
......
......@@ -3,27 +3,19 @@
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))
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
def new_column_definition(name, type, **options) # :nodoc:
if integer_like_primary_key?(type, options)
type = :primary_key
end
super
end
end
end
end
......
......@@ -39,10 +39,6 @@ def indexes(table_name, name = nil)
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
......
......@@ -93,13 +93,7 @@ class << t
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册