提交 6fa6c739 编写于 作者: R Ryuta Kamizono

Correctly dump `serial` and `bigserial`

上级 5366cc6b
* Correctly dump `serial` and `bigserial`.
*Ryuta Kamizono*
* Fix default `format` value in `ActiveRecord::Tasks::DatabaseTasks#schema_file`.
*James Cox*
......
......@@ -24,7 +24,7 @@ def column_spec_for_primary_key(column)
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 @@ def migration_keys
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 @@ def initialize(name, default, sql_type_metadata = nil, null = true, default_func
end
def has_default?
!default.nil?
!default.nil? || default_function
end
def bigint?
......
......@@ -6,7 +6,9 @@ class PostgreSQLColumn < Column #:nodoc:
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 @@ def column_spec_for_primary_key(column)
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 @@ def migration_keys
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?
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册