aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2017-09-23 15:34:56 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-09-23 15:48:49 +0900
commit202d6578f44ab03ee89ed57b73a97d513fc5a008 (patch)
tree40b9dbe22068b0d60c157deb98a98c42b3527619 /activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb
parentdfe6939e16bdcf854d32b61933d55fe313ac49fb (diff)
downloadrails-202d6578f44ab03ee89ed57b73a97d513fc5a008.tar.gz
rails-202d6578f44ab03ee89ed57b73a97d513fc5a008.tar.bz2
rails-202d6578f44ab03ee89ed57b73a97d513fc5a008.zip
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.
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb19
1 files changed, 12 insertions, 7 deletions
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 f1489e4d69..cb13f9fec1 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb
@@ -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