Skip to content

Instantly share code, notes, and snippets.

@baroquebobcat
Created September 24, 2009 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baroquebobcat/193069 to your computer and use it in GitHub Desktop.
Save baroquebobcat/193069 to your computer and use it in GitHub Desktop.
#This Ugly hack adds Json as one of the types hardcoded to use TEXT.
#
# The problem this solves is this
#
# class A
# include DataMapper::Resource
# property :id, Serial
# property :stuff, DataMapper::Types::Json
# end
# DataMapper.auto_migrate!
# #=>sql statement:
# CREATE TABLE `as` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `stuff` VARCHAR(65535), PRIMARY KEY(`id`))...
# VARCHAR cannot be that big, so it fails.
#
# It would be better if the mapping was less reliant on class.
# text_column_statement[1] already chooses the primitive type depending
# on the length, so why not use the type's length to determine what
# it is in the db?
#
# The logic for doing this probably would go somewhere like property_schema_hash[2]
#
# [1] http://github.com/datamapper/dm-core/blob/master/lib/dm-core/migrations.rb#L432
# [2] http://github.com/datamapper/dm-core/blob/master/lib/dm-core/migrations.rb#L243
module DataMapper
module Adapters
class DataObjectsAdapter < AbstractAdapter
class <<self
# Default types for all data object based adapters.
#
# @return [Hash] default types for data objects adapters.
#
# @api private
def type_map
length = Property::DEFAULT_LENGTH
precision = Property::DEFAULT_PRECISION
scale = Property::DEFAULT_SCALE_BIGDECIMAL
@type_map ||= {
Integer => { :primitive => 'INTEGER' },
String => { :primitive => 'VARCHAR', :length => length },
Class => { :primitive => 'VARCHAR', :length => length },
BigDecimal => { :primitive => 'DECIMAL', :precision => precision, :scale => scale },
Float => { :primitive => 'FLOAT', :precision => precision },
DateTime => { :primitive => 'TIMESTAMP' },
Date => { :primitive => 'DATE' },
Time => { :primitive => 'TIMESTAMP' },
TrueClass => { :primitive => 'BOOLEAN' },
Types::Object => { :primitive => 'TEXT' },
Types::Text => { :primitive => 'MEDIUMTEXT' },
Types::Json=> { :primitive => 'MEDIUMTEXT' }, #<-- support for types from dm-types
#...
}.freeze
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment