mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Correctly dump serial
and bigserial
This commit is contained in:
parent
5366cc6b55
commit
6fa6c739d1
6 changed files with 93 additions and 4 deletions
|
@ -1,3 +1,7 @@
|
|||
* Correctly dump `serial` and `bigserial`.
|
||||
|
||||
*Ryuta Kamizono*
|
||||
|
||||
* Fix default `format` value in `ActiveRecord::Tasks::DatabaseTasks#schema_file`.
|
||||
|
||||
*James Cox*
|
||||
|
|
|
@ -24,7 +24,7 @@ module ActiveRecord
|
|||
def prepare_column_options(column)
|
||||
spec = {}
|
||||
spec[:name] = column.name.inspect
|
||||
spec[:type] = column.type.to_s
|
||||
spec[:type] = schema_type(column)
|
||||
spec[:null] = 'false' unless column.null
|
||||
|
||||
limit = column.limit || native_database_types[column.type][:limit]
|
||||
|
@ -45,6 +45,10 @@ module ActiveRecord
|
|||
|
||||
private
|
||||
|
||||
def schema_type(column)
|
||||
column.type.to_s
|
||||
end
|
||||
|
||||
def schema_default(column)
|
||||
type = lookup_cast_type_from_column(column)
|
||||
default = type.deserialize(column.default)
|
||||
|
|
|
@ -31,7 +31,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def has_default?
|
||||
!default.nil?
|
||||
!default.nil? || default_function
|
||||
end
|
||||
|
||||
def bigint?
|
||||
|
|
|
@ -6,7 +6,9 @@ module ActiveRecord
|
|||
alias :array? :array
|
||||
|
||||
def serial?
|
||||
default_function && default_function =~ /\Anextval\(.*\)\z/
|
||||
return unless default_function
|
||||
|
||||
%r{\Anextval\('(?<table_name>.+)_#{name}_seq'::regclass\)\z} === default_function
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -145,7 +145,6 @@ module ActiveRecord
|
|||
def prepare_column_options(column) # :nodoc:
|
||||
spec = super
|
||||
spec[:array] = 'true' if column.array?
|
||||
spec[:default] = "\"#{column.default_function}\"" if column.default_function
|
||||
spec
|
||||
end
|
||||
|
||||
|
@ -154,6 +153,26 @@ module ActiveRecord
|
|||
super + [:array]
|
||||
end
|
||||
|
||||
def schema_type(column)
|
||||
return super unless column.serial?
|
||||
|
||||
if column.bigint?
|
||||
'bigserial'
|
||||
else
|
||||
'serial'
|
||||
end
|
||||
end
|
||||
private :schema_type
|
||||
|
||||
def schema_default(column)
|
||||
if column.default_function
|
||||
column.default_function.inspect unless column.serial?
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
private :schema_default
|
||||
|
||||
# Returns +true+, since this connection adapter supports prepared statement
|
||||
# caching.
|
||||
def supports_statement_cache?
|
||||
|
|
60
activerecord/test/cases/adapters/postgresql/serial_test.rb
Normal file
60
activerecord/test/cases/adapters/postgresql/serial_test.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
require "cases/helper"
|
||||
require 'support/schema_dumping_helper'
|
||||
|
||||
class PostgresqlSerialTest < ActiveRecord::TestCase
|
||||
include SchemaDumpingHelper
|
||||
|
||||
class PostgresqlSerial < ActiveRecord::Base; end
|
||||
|
||||
setup do
|
||||
@connection = ActiveRecord::Base.connection
|
||||
@connection.create_table "postgresql_serials", force: true do |t|
|
||||
t.serial :seq
|
||||
end
|
||||
end
|
||||
|
||||
teardown do
|
||||
@connection.drop_table "postgresql_serials", if_exists: true
|
||||
end
|
||||
|
||||
def test_serial_column
|
||||
column = PostgresqlSerial.columns_hash["seq"]
|
||||
assert_equal :integer, column.type
|
||||
assert_equal "integer", column.sql_type
|
||||
assert column.serial?
|
||||
end
|
||||
|
||||
def test_schema_dump_with_shorthand
|
||||
output = dump_table_schema "postgresql_serials"
|
||||
assert_match %r{t\.serial\s+"seq"}, output
|
||||
end
|
||||
end
|
||||
|
||||
class PostgresqlBigSerialTest < ActiveRecord::TestCase
|
||||
include SchemaDumpingHelper
|
||||
|
||||
class PostgresqlBigSerial < ActiveRecord::Base; end
|
||||
|
||||
setup do
|
||||
@connection = ActiveRecord::Base.connection
|
||||
@connection.create_table "postgresql_big_serials", force: true do |t|
|
||||
t.bigserial :seq
|
||||
end
|
||||
end
|
||||
|
||||
teardown do
|
||||
@connection.drop_table "postgresql_big_serials", if_exists: true
|
||||
end
|
||||
|
||||
def test_bigserial_column
|
||||
column = PostgresqlBigSerial.columns_hash["seq"]
|
||||
assert_equal :integer, column.type
|
||||
assert_equal "bigint", column.sql_type
|
||||
assert column.serial?
|
||||
end
|
||||
|
||||
def test_schema_dump_with_shorthand
|
||||
output = dump_table_schema "postgresql_big_serials"
|
||||
assert_match %r{t\.bigserial\s+"seq"}, output
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue