Correctly handle limit on int4 and int8 types in PG

PG doesn't register it's types using the `int(4)` format that others do.
As such, if we alias `int8` to the other integer types, the range
information is lost. This is fixed by simply registering it separately.

The other option (which I specifically chose to avoid) is to pass the
information of the original type that was being aliased as an argument.
I'd rather avoid that, since an alias should truly be treated the same.
If we need different behavior for a different type, we should explicitly
register it with that, and not have a conditional based on aliasing.

Fixes #18144

[Sean Griffin & ysbaddaden]
This commit is contained in:
Sean Griffin 2014-12-22 09:52:02 -07:00
parent 0369808917
commit b0f2b94dd3
2 changed files with 27 additions and 2 deletions

@ -445,8 +445,8 @@ def get_oid_type(oid, fmod, column_name, sql_type = '') # :nodoc:
def initialize_type_map(m) # :nodoc:
register_class_with_limit m, 'int2', OID::Integer
m.alias_type 'int4', 'int2'
m.alias_type 'int8', 'int2'
register_class_with_limit m, 'int4', OID::Integer
register_class_with_limit m, 'int8', OID::Integer
m.alias_type 'oid', 'int2'
m.register_type 'float4', OID::Float.new
m.alias_type 'float8', 'float4'

@ -0,0 +1,25 @@
require "cases/helper"
require "active_support/core_ext/numeric/bytes"
class PostgresqlIntegerTest < ActiveRecord::TestCase
class PgInteger < ActiveRecord::Base
end
def setup
@connection = ActiveRecord::Base.connection
@connection.transaction do
@connection.create_table "pg_integers", force: true do |t|
t.integer :quota, limit: 8, default: 2.gigabytes
end
end
end
teardown do
@connection.execute "drop table if exists pg_integers"
end
test "schema properly respects bigint ranges" do
assert_equal 2.gigabytes, PgInteger.new.quota
end
end