1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #27274 from kamipo/primary_key_with_auto_increment_and_bigint

Make `:auto_increment` option works on `:bigint`
This commit is contained in:
Matthew Draper 2016-12-06 10:01:43 +10:30 committed by GitHub
commit 9b6f312734
3 changed files with 26 additions and 12 deletions

View file

@ -44,8 +44,12 @@ module ActiveRecord
def primary_key(name, type = :primary_key, **options) def primary_key(name, type = :primary_key, **options)
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 && type == :integer elsif options.delete(:auto_increment) == true && %i(integer bigint).include?(type)
type = :serial type = if type == :bigint || options[:limit] == 8
:bigserial
else
:serial
end
end end
super super

View file

@ -3,7 +3,7 @@ module ActiveRecord
module SQLite3 module SQLite3
module ColumnMethods module ColumnMethods
def primary_key(name, type = :primary_key, **options) 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 type = :primary_key
end end

View file

@ -224,7 +224,6 @@ class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase
def setup def setup
@connection = ActiveRecord::Base.connection @connection = ActiveRecord::Base.connection
@connection.create_table(:auto_increments, id: :integer, auto_increment: true, force: true)
end end
def teardown def teardown
@ -232,15 +231,26 @@ class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase
end end
def test_primary_key_with_auto_increment def test_primary_key_with_auto_increment
record1 = AutoIncrement.create! @connection.create_table(:auto_increments, id: :integer, auto_increment: true, force: true)
assert_not_nil record1.id assert_auto_incremented
record1.destroy
record2 = AutoIncrement.create!
assert_not_nil record2.id
assert_operator record2.id, :>, record1.id
end 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 end
class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase class PrimaryKeyAnyTypeTest < ActiveRecord::TestCase