lib/dm-pg-json.rb in dm-pg-json-0.2.0 vs lib/dm-pg-json.rb in dm-pg-json-0.2.1

- old
+ new

@@ -1,37 +1,57 @@ # encoding: utf-8 require 'dm-core' require 'dm-postgres-adapter' +require 'dm-migrations' +require 'dm-validations' require 'dm-pg-json/property/pgjson' require 'dm-pg-json/version' module DataMapper module Migrations module PostgresAdapter - def property_schema_hash(property) - schema = super - - if property.kind_of?(Property::PgJSON) - schema.delete(:length) - end + module SQL + def property_schema_statement(connection, schema) + statement = quote_name(schema[:name]) + statement << " #{schema[:primitive]}" - schema + length = schema[:length] + + if schema[:precision] && schema[:scale] + statement << "(#{[ :precision, :scale ].map { |key| connection.quote_value(schema[key]) }.join(', ')})" + elsif length == 'max' + statement << '(max)' + elsif length + statement << "(#{connection.quote_value(length)})" + end + + default = schema[:default] + if default + if schema[:primitive] == 'JSON' + stmt = " DEFAULT #{connection.quote_value(default)}::JSON" + else + stmt = " DEFAULT #{connection.quote_value(default)}" + end + statement << stmt + end + + statement << ' NOT NULL' unless schema[:allow_nil] + + statement + end end end end +end +module DataMapper module PostgresJSON def self.included(base) base.extend ClassMethods end module ClassMethods - # Types for PostgreSQL databases. - # - # @return [Hash] types for PostgreSQL databases. - # - # @api private def type_map super.merge(Property::PgJSON => {:primitive => 'JSON'}).freeze end end end