From 974e860172b5e0f05a649211c11c328bc9e94a0c Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 6 Dec 2016 05:38:14 +0900 Subject: [PATCH] Make `:auto_increment` option works on `:bigint` Follow up to #27272. --- .../postgresql/schema_definitions.rb | 8 ++++-- .../sqlite3/schema_definitions.rb | 2 +- activerecord/test/cases/primary_keys_test.rb | 28 +++++++++++++------ 3 files changed, 26 insertions(+), 12 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 617b5c35bd..4afb4733eb 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_definitions.rb @@ -44,8 +44,12 @@ module ActiveRecord def primary_key(name, type = :primary_key, **options) if type == :uuid options[:default] = options.fetch(:default, "gen_random_uuid()") - elsif options.delete(:auto_increment) == true && type == :integer - type = :serial + elsif options.delete(:auto_increment) == true && %i(integer bigint).include?(type) + type = if type == :bigint || options[:limit] == 8 + :bigserial + else + :serial + end end super 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 5d1e8811f8..d0b38dff4c 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb @@ -3,7 +3,7 @@ module ActiveRecord module SQLite3 module ColumnMethods def primary_key(name, type = :primary_key, **options) - if options.delete(:auto_increment) == true && type == :integer + if options.delete(:auto_increment) == true && %i(integer bigint).include?(type) type = :primary_key end diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 7599434f4f..d2207e2489 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -224,7 +224,6 @@ class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase def setup @connection = ActiveRecord::Base.connection - @connection.create_table(:auto_increments, id: :integer, auto_increment: true, force: true) end def teardown @@ -232,15 +231,26 @@ class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase end def test_primary_key_with_auto_increment - record1 = AutoIncrement.create! - assert_not_nil record1.id - - record1.destroy - - record2 = AutoIncrement.create! - assert_not_nil record2.id - assert_operator record2.id, :>, record1.id + @connection.create_table(:auto_increments, id: :integer, auto_increment: true, force: true) + assert_auto_incremented end + + def test_primary_key_with_auto_increment_and_bigint + @connection.create_table(:auto_increments, id: :bigint, auto_increment: true, force: true) + assert_auto_incremented + end + + private + def assert_auto_incremented + record1 = AutoIncrement.create! + assert_not_nil record1.id + + record1.destroy + + record2 = AutoIncrement.create! + assert_not_nil record2.id + assert_operator record2.id, :>, record1.id + end end class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase