module DataMapper::Migrations::DataObjectsAdapter

Public Instance Methods

create_model_storage(model) click to toggle source

@api semipublic

# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 82
def create_model_storage(model)
  name       = self.name
  properties = model.properties_with_subclasses(name)

  return false if storage_exists?(model.storage_name(name))
  return false if properties.empty?

  with_connection do |connection|
    statements = [ create_table_statement(connection, model, properties) ]
    statements.concat(create_index_statements(model))
    statements.concat(create_unique_index_statements(model))

    statements.each do |statement|
      command   = connection.create_command(statement)
      command.execute_non_query
    end
  end

  true
end
destroy_model_storage(model) click to toggle source

@api semipublic

# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 104
def destroy_model_storage(model)
  return true unless supports_drop_table_if_exists? || storage_exists?(model.storage_name(name))
  execute(drop_table_statement(model))
  true
end
dialect() click to toggle source
# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 8
def dialect
  self.class.to_s.sub(/.*::/, '').sub(/Adapter$/,'')
end
field_exists?(storage_name, column_name) click to toggle source

Returns whether the field exists.

@param [String] storage_name

a String defining the name of a storage, for example a table name.

@param [String] field

a String defining the name of a field, for example a column name.

@return [Boolean]

true if the field exists.

@api semipublic

# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 44
      def field_exists?(storage_name, column_name)
        statement = <<-SQL.compress_lines
          SELECT COUNT(*)
          FROM "information_schema"."columns"
          WHERE "table_schema" = ?
          AND "table_name" = ?
          AND "column_name" = ?
        SQL

        select(statement, schema_name, storage_name, column_name).first > 0
      end
storage_exists?(storage_name) click to toggle source

Returns whether the storage_name exists.

@param [String] storage_name

a String defining the name of a storage, for example a table name.

@return [Boolean]

true if the storage exists

@api semipublic

# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 21
      def storage_exists?(storage_name)
        statement = <<-SQL.compress_lines
          SELECT COUNT(*)
          FROM "information_schema"."tables"
          WHERE "table_type" = 'BASE TABLE'
          AND "table_schema" = ?
          AND "table_name" = ?
        SQL

        select(statement, schema_name, storage_name).first > 0
      end
upgrade_model_storage(model) click to toggle source

@api semipublic

# File lib/dm-migrations/adapters/dm-do-adapter.rb, line 57
def upgrade_model_storage(model)
  name       = self.name
  properties = model.properties_with_subclasses(name)

  if success = create_model_storage(model)
    return properties
  end

  table_name = model.storage_name(name)

  with_connection do |connection|
    properties.map do |property|
      schema_hash = property_schema_hash(property)
      next if field_exists?(table_name, schema_hash[:name])

      statement = alter_table_add_column_statement(connection, table_name, schema_hash)
      command   = connection.create_command(statement)
      command.execute_non_query

      property
    end.compact
  end
end