Skip to content

Commit

Permalink
Basic support for native UUID type in postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Sadauskas committed Sep 25, 2008
1 parent 4857fdc commit 8f12b04
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions dm-types/lib/dm-types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
require dir / 'ip_address'
require dir / "json"""
require dir / 'uri'
require dir / 'uuid'
require dir / 'yaml'
require dir / 'serial'
require dir / 'regexp'
Expand Down
41 changes: 41 additions & 0 deletions dm-types/lib/dm-types/uuid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rubygems'
require 'uuidtools'

module DataMapper
module Types
# UUID Type
# First run at this, because I need it. A few caveats:
# * Only works on postgres, using the built-in native uuid type.
# To make it work in mysql, you'll have to add a typemap entry to
# the mysql_adapter. I think. I don't have mysql handy, so I'm
# not going to try. For SQLite, this will have to inherit from the
# String primitive
# * Won't accept a random default, because of the namespace clash
# between this and the UUIDtools gem. Also can't set the default
# type to UUID() (postgres-contrib's native generator) and
# automigrate, because auto_migrate! tries to make it a string "UUID()"
# Feel free to enchance this, and delete these caveats when they're fixed.
#
# -- Rando Sept 25, 08
#
class UUID < DataMapper::Type
def self.load(value, property)
return nil if value.nil?
::UUID.parse(value)
end

def self.dump(value, property)
return nil if value.nil?
value.to_s
end

def self.typecast(value, property)
value.kind_of?(::UUID) ? value : load(value, property)
end

::DataMapper::Property::TYPES << self
end
end
end


37 changes: 37 additions & 0 deletions dm-types/spec/integration/uuid_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'pathname'
require 'uuidtools'
require Pathname(__FILE__).dirname.parent.expand_path + 'spec_helper'

describe DataMapper::Types::UUID do

before(:all) do
class UUIDTest
include DataMapper::Resource

property :id, Serial
property :uuid, DataMapper::Types::UUID
end
UUIDTest.auto_migrate!
end

it "should be settable as a uuid" do
u = UUIDTest.create(:uuid => UUID.parse('b0fc632e-d744-4821-afe3-4ea0701859ee'))

u.uuid.should be_kind_of(UUID)
u.uuid.to_s.should == 'b0fc632e-d744-4821-afe3-4ea0701859ee'
end

it "should be settable as a string" do
u = UUIDTest.create(:uuid => 'b0fc632e-d744-4821-afe3-4ea0701859ee')

u.uuid.should be_kind_of(UUID)
u.uuid.to_s.should == 'b0fc632e-d744-4821-afe3-4ea0701859ee'
end

it "should be allowed to be null" do
u = UUIDTest.create()

u.uuid.should be_nil
end

end
2 changes: 2 additions & 0 deletions dm-types/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
ENV["MYSQL_SPEC_URI"] ||= 'mysql://localhost/dm_core_test'
ENV["POSTGRES_SPEC_URI"] ||= 'postgres://postgres@localhost/dm_more_test'

# DataMapper::Logger.new(STDOUT, :debug)

def setup_adapter(name, default_uri = nil)
begin
DataMapper.setup(name, ENV["#{ENV['ADAPTER'].to_s.upcase}_SPEC_URI"] || default_uri)
Expand Down

0 comments on commit 8f12b04

Please sign in to comment.