Module: DataMapper::Migrations::PostgresAdapter::SQL

Defined in:
lib/dm-pg-json.rb

Instance Method Summary collapse

Instance Method Details

#property_schema_statement(connection, schema) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dm-pg-json.rb', line 14

def property_schema_statement(connection, schema)
  statement = quote_name(schema[:name])
  statement << " #{schema[:primitive]}"

  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