Permalink
Browse files

Merge pull request #15487 from sgrif/sg-bit-limit

Respect limit for PG bit strings
  • Loading branch information...
2 parents aea8cd4 + 3bc4e9e commit 5e15edfd1a197f450a02c314863106b6027dd0d1 @senny senny committed Jun 3, 2014
@@ -4,80 +4,80 @@ module PostgreSQL
module ColumnMethods
def xml(*args)
options = args.extract_options!
- column(args[0], 'xml', options)
+ column(args[0], :xml, options)
end
def tsvector(*args)
options = args.extract_options!
- column(args[0], 'tsvector', options)
+ column(args[0], :tsvector, options)
end
def int4range(name, options = {})
- column(name, 'int4range', options)
+ column(name, :int4range, options)
end
def int8range(name, options = {})
- column(name, 'int8range', options)
+ column(name, :int8range, options)
end
def tsrange(name, options = {})
- column(name, 'tsrange', options)
+ column(name, :tsrange, options)
end
def tstzrange(name, options = {})
- column(name, 'tstzrange', options)
+ column(name, :tstzrange, options)
end
def numrange(name, options = {})
- column(name, 'numrange', options)
+ column(name, :numrange, options)
end
def daterange(name, options = {})
- column(name, 'daterange', options)
+ column(name, :daterange, options)
end
def hstore(name, options = {})
- column(name, 'hstore', options)
+ column(name, :hstore, options)
end
def ltree(name, options = {})
- column(name, 'ltree', options)
+ column(name, :ltree, options)
end
def inet(name, options = {})
- column(name, 'inet', options)
+ column(name, :inet, options)
end
def cidr(name, options = {})
- column(name, 'cidr', options)
+ column(name, :cidr, options)
end
def macaddr(name, options = {})
- column(name, 'macaddr', options)
+ column(name, :macaddr, options)
end
def uuid(name, options = {})
- column(name, 'uuid', options)
+ column(name, :uuid, options)
end
def json(name, options = {})
- column(name, 'json', options)
+ column(name, :json, options)
end
def citext(name, options = {})
- column(name, 'citext', options)
+ column(name, :citext, options)
end
def point(name, options = {})
- column(name, 'point', options)
+ column(name, :point, options)
end
def bit(name, options)
- column(name, 'bit', options)
+ column(name, :bit, options)
end
def bit_varying(name, options)
- column(name, 'bit varying', options)
+ column(name, :bit_varying, options)
end
end
@@ -104,9 +104,9 @@ class PostgreSQLAdapter < AbstractAdapter
json: { name: "json" },
ltree: { name: "ltree" },
citext: { name: "citext" },
- point: { name: "point"},
- bit: { name: "bit"},
- bit_varying: { name: "bit varying" }
+ point: { name: "point" },
+ bit: { name: "bit" },
+ bit_varying: { name: "bit varying" },
}
OID = PostgreSQL::OID #:nodoc:
@@ -435,8 +435,8 @@ def initialize_type_map(m) # :nodoc:
m.alias_type 'name', 'varchar'
m.alias_type 'bpchar', 'varchar'
m.register_type 'bool', Type::Boolean.new
- m.register_type 'bit', OID::Bit.new
- m.register_type 'varbit', OID::BitVarying.new
+ register_class_with_limit m, 'bit', OID::Bit
+ register_class_with_limit m, 'varbit', OID::BitVarying
m.alias_type 'timestamptz', 'timestamp'
m.register_type 'date', OID::Date.new
m.register_type 'time', OID::Time.new
@@ -14,7 +14,7 @@ def setup
@connection.transaction do
@connection.create_table('postgresql_bit_strings') do |t|
t.bit :a_bit, default: "00000011", limit: 8
- t.bit_varying :a_bit_varying, default: "0011"
+ t.bit_varying :a_bit_varying, default: "0011", limit: 4
end
end
end
@@ -36,7 +36,7 @@ def test_bit_string_column
def test_bit_string_varying_column
column = PostgresqlBitString.columns_hash["a_bit_varying"]
assert_equal :bit_varying, column.type
- assert_equal "bit varying", column.sql_type
+ assert_equal "bit varying(4)", column.sql_type
assert_not column.text?
assert_not column.number?
assert_not column.binary?
@@ -53,8 +53,8 @@ def test_default
def test_schema_dumping
output = dump_table_schema("postgresql_bit_strings")
- assert_match %r{t\.bit\s+"a_bit",\s+default: "00000011"$}, output
- assert_match %r{t\.bit_varying\s+"a_bit_varying",\s+default: "0011"$}, output
+ assert_match %r{t\.bit\s+"a_bit",\s+limit: 8,\s+default: "00000011"$}, output
+ assert_match %r{t\.bit_varying\s+"a_bit_varying",\s+limit: 4,\s+default: "0011"$}, output
end
def test_assigning_invalid_hex_string_raises_exception
@@ -63,17 +63,17 @@ def test_assigning_invalid_hex_string_raises_exception
end
def test_roundtrip
- PostgresqlBitString.create! a_bit: "00001010", a_bit_varying: "00010101"
+ PostgresqlBitString.create! a_bit: "00001010", a_bit_varying: "0101"
record = PostgresqlBitString.first
assert_equal "00001010", record.a_bit
- assert_equal "00010101", record.a_bit_varying
+ assert_equal "0101", record.a_bit_varying
record.a_bit = "11111111"
- record.a_bit_varying = "0xFF"
+ record.a_bit_varying = "0xF"
record.save!
assert record.reload
assert_equal "11111111", record.a_bit
- assert_equal "11111111", record.a_bit_varying
+ assert_equal "1111", record.a_bit_varying
end
end

0 comments on commit 5e15edf

Please sign in to comment.