From 8f037a5ab929f929fe0897edc9ecadf901c32e56 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Mon, 25 Sep 2017 08:27:41 +0900 Subject: Extract `integer_like_primary_key_type` to ease to handle it for adapters --- .../connection_adapters/abstract/schema_definitions.rb | 7 +++++++ .../connection_adapters/mysql/schema_definitions.rb | 10 +++++----- .../connection_adapters/postgresql/schema_definitions.rb | 9 +++------ .../connection_adapters/sqlite3/schema_definitions.rb | 9 +++------ 4 files changed, 18 insertions(+), 17 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 1041db0b8f..be2f625d74 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -396,6 +396,9 @@ module ActiveRecord alias :belongs_to :references def new_column_definition(name, type, **options) # :nodoc: + if integer_like_primary_key?(type, options) + type = integer_like_primary_key_type(type, options) + end type = aliased_types(type.to_s, type) options[:primary_key] ||= type == :primary_key options[:null] = false if options[:primary_key] @@ -414,6 +417,10 @@ module ActiveRecord def integer_like_primary_key?(type, options) options[:primary_key] && [:integer, :bigint].include?(type) && !options.key?(:default) end + + def integer_like_primary_key_type(type, options) + type + end end class AlterTable # :nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb index eff96af87a..da25e4863c 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb @@ -57,17 +57,12 @@ 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] when :primary_key type = :integer options[:limit] ||= 8 - options[:auto_increment] = true options[:primary_key] = true when /\Aunsigned_(?.+)\z/ type = $~[:type].to_sym @@ -81,6 +76,11 @@ module ActiveRecord def aliased_types(name, fallback) fallback end + + def integer_like_primary_key_type(type, options) + options[:auto_increment] = true + type + end end class Table < ActiveRecord::ConnectionAdapters::Table diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb index cb13f9fec1..75622eb304 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb @@ -179,17 +179,14 @@ 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 + private + def integer_like_primary_key_type(type, options) + if type == :bigint || options[:limit] == 8 :bigserial else :serial end end - - super - end end class Table < ActiveRecord::ConnectionAdapters::Table diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb index 2010de1ce2..c9855019c1 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb @@ -9,13 +9,10 @@ module ActiveRecord end alias :belongs_to :references - def new_column_definition(name, type, **options) # :nodoc: - if integer_like_primary_key?(type, options) - type = :primary_key + private + def integer_like_primary_key_type(type, options) + :primary_key end - - super - end end end end -- cgit v1.2.3