Module: DataMapper::Migrations::DataObjectsAdapter::SQL

Included in:
DataMapper::Migrations::DataObjectsAdapter
Defined in:
lib/dm-core/migrations.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#alter_table_add_column_statement(connection, table_name, schema_hash) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



191
192
193
# File 'lib/dm-core/migrations.rb', line 191

def alter_table_add_column_statement(connection, table_name, schema_hash)
  "ALTER TABLE #{quote_name(table_name)} ADD COLUMN #{property_schema_statement(connection, schema_hash)}"
end

#create_index_statements(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



219
220
221
222
223
224
225
226
227
# File 'lib/dm-core/migrations.rb', line 219

def create_index_statements(model)
  table_name = model.storage_name(name)
  model.properties(name).indexes.map do |index_name, fields|
    <<-SQL.compress_lines
      CREATE INDEX #{quote_name("index_#{table_name}_#{index_name}")} ON
      #{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
    SQL
  end
end

#create_table_statement(connection, model, properties) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



197
198
199
200
201
202
203
204
205
# File 'lib/dm-core/migrations.rb', line 197

def create_table_statement(connection, model, properties)
  statement = <<-SQL.compress_lines
    CREATE TABLE #{quote_name(model.storage_name(name))}
    (#{properties.map { |property| property_schema_statement(connection, property_schema_hash(property)) }.join(', ')},
    PRIMARY KEY(#{ properties.key.map { |property| quote_name(property.field) }.join(', ')}))
  SQL

  statement
end

#create_unique_index_statements(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



231
232
233
234
235
236
237
238
239
# File 'lib/dm-core/migrations.rb', line 231

def create_unique_index_statements(model)
  table_name = model.storage_name(name)
  model.properties(name).unique_indexes.map do |index_name, fields|
    <<-SQL.compress_lines
      CREATE UNIQUE INDEX #{quote_name("unique_#{table_name}_#{index_name}")} ON
      #{quote_name(table_name)} (#{fields.map { |field| quote_name(field) }.join(', ')})
    SQL
  end
end

#drop_table_statement(model) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



209
210
211
212
213
214
215
# File 'lib/dm-core/migrations.rb', line 209

def drop_table_statement(model)
  if supports_drop_table_if_exists?
    "DROP TABLE IF EXISTS #{quote_name(model.storage_name(name))}"
  else
    "DROP TABLE #{quote_name(model.storage_name(name))}"
  end
end

#property_schema_hash(property) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/dm-core/migrations.rb', line 243

def property_schema_hash(property)
  schema = (self.class.type_map[property.type] || self.class.type_map[property.primitive]).merge(:name => property.field)

  if property.primitive == String && schema[:primitive] != 'TEXT' && schema[:primitive] != 'CLOB'
    schema[:length] = property.length
  elsif property.primitive == BigDecimal || property.primitive == Float
    schema[:precision] = property.precision
    schema[:scale]     = property.scale
  end

  schema[:nullable] = property.nullable?
  schema[:serial]   = property.serial?

  if property.default.nil? || property.default.respond_to?(:call)
    # remove the default if the property is not nullable
    schema.delete(:default) unless property.nullable?
  else
    if property.type.respond_to?(:dump)
      schema[:default] = property.type.dump(property.default, property)
    else
      schema[:default] = property.default
    end
  end

  schema
end

#property_schema_statement(connection, schema) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document



272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/dm-core/migrations.rb', line 272

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

  if schema[:precision] && schema[:scale]
    statement << "(#{[ :precision, :scale ].map { |key| connection.quote_value(schema[key]) }.join(', ')})"
  elsif schema[:length]
    statement << "(#{connection.quote_value(schema[:length])})"
  end

  statement << " DEFAULT #{connection.quote_value(schema[:default])}" if schema.key?(:default)
  statement << ' NOT NULL' unless schema[:nullable]
  statement
end

#schema_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document

Raises:

  • (NotImplementedError)


185
186
187
# File 'lib/dm-core/migrations.rb', line 185

def schema_name
  raise NotImplementedError, "#{self.class}#schema_name not implemented"
end

#supports_drop_table_if_exists?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: document

Returns:

  • (Boolean)


179
180
181
# File 'lib/dm-core/migrations.rb', line 179

def supports_drop_table_if_exists?
  false
end

#supports_serial?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adapters that support AUTO INCREMENT fields for CREATE TABLE statements should overwrite this to return true

Returns:

  • (Boolean)


173
174
175
# File 'lib/dm-core/migrations.rb', line 173

def supports_serial?
  false
end