From 12e9a75f227ea7bcc23e2717e9ff5a72ec64e1f1 Mon Sep 17 00:00:00 2001 From: Konstantin Shabanov Date: Tue, 12 Jun 2012 18:10:08 +0400 Subject: Add uuid type support to PostgreSQL adapter --- .../active_record/connection_adapters/postgresql/oid.rb | 1 + .../connection_adapters/postgresql_adapter.rb | 10 +++++++--- .../test/cases/adapters/postgresql/datatype_test.rb | 15 +++++++++++++++ activerecord/test/cases/column_definition_test.rb | 6 ------ activerecord/test/cases/schema_dumper_test.rb | 7 +++++++ activerecord/test/schema/postgresql_specific_schema.rb | 10 +++++++++- 6 files changed, 39 insertions(+), 10 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb index df3d5e4657..6657491c06 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid.rb @@ -223,6 +223,7 @@ module ActiveRecord 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. diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 03c318f5f7..b08c59d97d 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -89,7 +89,6 @@ module ActiveRecord else string end - end def cidr_to_string(object) @@ -256,7 +255,7 @@ module ActiveRecord :integer # UUID type when 'uuid' - :string + :uuid # Small and big integer types when /^(?:small|big)int$/ :integer @@ -319,6 +318,10 @@ module ActiveRecord 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 @@ module ActiveRecord :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. diff --git a/activerecord/test/cases/adapters/postgresql/datatype_test.rb b/activerecord/test/cases/adapters/postgresql/datatype_test.rb index 34660577da..a4d9286d52 100644 --- a/activerecord/test/cases/adapters/postgresql/datatype_test.rb +++ b/activerecord/test/cases/adapters/postgresql/datatype_test.rb @@ -27,6 +27,9 @@ end class PostgresqlTimestampWithZone < ActiveRecord::Base end +class PostgresqlUUID < ActiveRecord::Base +end + class PostgresqlDataTypeTest < ActiveRecord::TestCase self.use_transactional_fixtures = false @@ -61,6 +64,9 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase @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 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase 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 @@ class PostgresqlDataTypeTest < ActiveRecord::TestCase 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 diff --git a/activerecord/test/cases/column_definition_test.rb b/activerecord/test/cases/column_definition_test.rb index a44b49466f..bd2fbaa7db 100644 --- a/activerecord/test/cases/column_definition_test.rb +++ b/activerecord/test/cases/column_definition_test.rb @@ -136,12 +136,6 @@ module ActiveRecord 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 diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index ab80dd1d6d..ed0b6a86ba 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -257,6 +257,13 @@ class SchemaDumperTest < ActiveRecord::TestCase 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 diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index e51db50ae3..5f01f1fc50 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -1,6 +1,6 @@ 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 ); _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, -- cgit v1.2.3