提交 12e9a75f 编写于 作者: K Konstantin Shabanov

Add uuid type support to PostgreSQL adapter

上级 122f6de2
......@@ -223,6 +223,7 @@ def self.registered_type?(name)
alias_type 'bit', 'text'
alias_type 'varbit', 'text'
alias_type 'macaddr', 'text'
alias_type 'uuid', 'text'
# FIXME: I don't think this is correct. We should probably be returning a parsed date,
# but the tests pass with a string returned.
......
......@@ -89,7 +89,6 @@ def string_to_cidr(string)
else
string
end
end
def cidr_to_string(object)
......@@ -256,7 +255,7 @@ def simplified_type(field_type)
:integer
# UUID type
when 'uuid'
:string
:uuid
# Small and big integer types
when /^(?:small|big)int$/
:integer
......@@ -319,6 +318,10 @@ def cidr(name, options = {})
def macaddr(name, options = {})
column(name, 'macaddr', options)
end
def uuid(name, options = {})
column(name, 'uuid', options)
end
end
ADAPTER_NAME = 'PostgreSQL'
......@@ -341,7 +344,8 @@ def macaddr(name, options = {})
:hstore => { :name => "hstore" },
:inet => { :name => "inet" },
:cidr => { :name => "cidr" },
:macaddr => { :name => "macaddr" }
:macaddr => { :name => "macaddr" },
:uuid => { :name => "uuid" }
}
# Returns 'PostgreSQL' as adapter name for identification purposes.
......
......@@ -27,6 +27,9 @@ class PostgresqlOid < ActiveRecord::Base
class PostgresqlTimestampWithZone < ActiveRecord::Base
end
class PostgresqlUUID < ActiveRecord::Base
end
class PostgresqlDataTypeTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
......@@ -61,6 +64,9 @@ def setup
@first_oid = PostgresqlOid.find(1)
@connection.execute("INSERT INTO postgresql_timestamp_with_zones (time) VALUES ('2010-01-01 10:00:00-1')")
@connection.execute("INSERT INTO postgresql_uuids (guid, compact_guid) VALUES('d96c3da0-96c1-012f-1316-64ce8f32c6d8', 'f06c715096c1012f131764ce8f32c6d8')")
@first_uuid = PostgresqlUUID.find(1)
end
def test_data_type_of_array_types
......@@ -100,6 +106,10 @@ def test_data_type_of_oid_types
assert_equal :integer, @first_oid.column_for_attribute(:obj_id).type
end
def test_data_type_of_uuid_types
assert_equal :uuid, @first_uuid.column_for_attribute(:guid).type
end
def test_array_values
assert_equal '{35000,21000,18000,17000}', @first_array.commission_by_quarter
assert_equal '{foo,bar,baz}', @first_array.nicknames
......@@ -143,6 +153,11 @@ def test_network_address_values_ipaddr
assert_equal '01:23:45:67:89:0a', @first_network_address.mac_address
end
def test_uuid_values
assert_equal 'd96c3da0-96c1-012f-1316-64ce8f32c6d8', @first_uuid.guid
assert_equal 'f06c7150-96c1-012f-1317-64ce8f32c6d8', @first_uuid.compact_guid
end
def test_bit_string_values
assert_equal '00010101', @first_bit_string.bit_string
assert_equal '00010101', @first_bit_string.bit_string_varying
......
......@@ -136,12 +136,6 @@ def test_smallint_column_should_map_to_integer
smallint_column = PostgreSQLColumn.new('number', nil, oid, "smallint")
assert_equal :integer, smallint_column.type
end
def test_uuid_column_should_map_to_string
oid = PostgreSQLAdapter::OID::Identity.new
uuid_column = PostgreSQLColumn.new('unique_id', nil, oid, "uuid")
assert_equal :string, uuid_column.type
end
end
end
end
......
......@@ -257,6 +257,13 @@ def test_schema_dump_includes_macaddr_shorthand_definition
end
end
def test_schema_dump_includes_uuid_shorthand_definition
output = standard_dump
if %r{create_table "poistgresql_uuids"} =~ output
assert_match %r{t.uuid "guid"}, output
end
end
def test_schema_dump_includes_hstores_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_hstores"} =~ output
......
ActiveRecord::Schema.define do
%w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings
%w(postgresql_tsvectors postgresql_hstores postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings postgresql_uuids
postgresql_oids postgresql_xml_data_type defaults geometrics postgresql_timestamp_with_zones postgresql_partitioned_table postgresql_partitioned_table_parent).each do |table_name|
execute "DROP TABLE IF EXISTS #{quote_table_name table_name}"
end
......@@ -58,6 +58,14 @@
);
_SQL
execute <<_SQL
CREATE TABLE postgresql_uuids (
id SERIAL PRIMARY KEY,
guid uuid,
compact_guid uuid
);
_SQL
execute <<_SQL
CREATE TABLE postgresql_tsvectors (
id SERIAL PRIMARY KEY,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册