提交 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) ...@@ -410,6 +410,10 @@ def create_column_definition(name, type, options)
def aliased_types(name, fallback) def aliased_types(name, fallback)
"timestamp" == name ? :datetime : fallback "timestamp" == name ? :datetime : fallback
end end
def integer_like_primary_key?(type, options)
options[:primary_key] && [:integer, :bigint].include?(type) && !options.key?(:default)
end
end end
class AlterTable # :nodoc: class AlterTable # :nodoc:
......
...@@ -4,11 +4,6 @@ module ActiveRecord ...@@ -4,11 +4,6 @@ module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
module MySQL module MySQL
module ColumnMethods 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) def blob(*args, **options)
args.each { |name| column(name, :blob, options) } args.each { |name| column(name, :blob, options) }
end end
...@@ -62,6 +57,10 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition ...@@ -62,6 +57,10 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
include ColumnMethods include ColumnMethods
def new_column_definition(name, type, **options) # :nodoc: def new_column_definition(name, type, **options) # :nodoc:
if integer_like_primary_key?(type, options)
options[:auto_increment] = true
end
case type case type
when :virtual when :virtual
type = options[:type] type = options[:type]
......
...@@ -44,15 +44,8 @@ module ColumnMethods ...@@ -44,15 +44,8 @@ module ColumnMethods
# a record (as primary keys cannot be +nil+). This might be done via the # a record (as primary keys cannot be +nil+). This might be done via the
# +SecureRandom.uuid+ method and a +before_save+ callback, for instance. # +SecureRandom.uuid+ method and a +before_save+ callback, for instance.
def primary_key(name, type = :primary_key, **options) def primary_key(name, type = :primary_key, **options)
options[:auto_increment] = true if [:integer, :bigint].include?(type) && !options.key?(:default)
if type == :uuid if type == :uuid
options[:default] = options.fetch(:default, "gen_random_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 end
super super
...@@ -185,6 +178,18 @@ def xml(*args, **options) ...@@ -185,6 +178,18 @@ def xml(*args, **options)
class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
include ColumnMethods 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 end
class Table < ActiveRecord::ConnectionAdapters::Table class Table < ActiveRecord::ConnectionAdapters::Table
......
...@@ -3,27 +3,19 @@ ...@@ -3,27 +3,19 @@
module ActiveRecord module ActiveRecord
module ConnectionAdapters module ConnectionAdapters
module SQLite3 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 class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
include ColumnMethods
def references(*args, **options) def references(*args, **options)
super(*args, type: :integer, **options) super(*args, type: :integer, **options)
end end
alias :belongs_to :references alias :belongs_to :references
end
class Table < ActiveRecord::ConnectionAdapters::Table def new_column_definition(name, type, **options) # :nodoc:
include ColumnMethods if integer_like_primary_key?(type, options)
type = :primary_key
end
super
end
end end
end end
end end
......
...@@ -39,10 +39,6 @@ def indexes(table_name, name = nil) ...@@ -39,10 +39,6 @@ def indexes(table_name, name = nil)
end end
end end
def update_table_definition(table_name, base)
SQLite3::Table.new(table_name, base)
end
def create_schema_dumper(options) def create_schema_dumper(options)
SQLite3::SchemaDumper.create(self, options) SQLite3::SchemaDumper.create(self, options)
end end
......
...@@ -93,13 +93,7 @@ class << t ...@@ -93,13 +93,7 @@ class << t
def add_column(table_name, column_name, type, options = {}) def add_column(table_name, column_name, type, options = {})
if type == :primary_key if type == :primary_key
case adapter_name type = :integer
when "PostgreSQL"
type = :serial
when "Mysql2"
type = :integer
options[:auto_increment] = true
end
options[:primary_key] = true options[:primary_key] = true
end end
super super
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册