Module: DataMapper::Migrations::DataObjectsAdapter

### Home Classes Methods

### In Files

- lib/dm-migrations/adapters/dm-do-adapter.rb

### Namespace

- MODULE DataMapper::Migrations::DataObjectsAdapter::ClassMethods

### Methods

- #create_model_storage
- #destroy_model_storage
- #field_exists?
- #storage_exists?
- #upgrade_model_storage

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- DataMapper
- DataMapper::Adapters
- DataMapper::Migration
- DataMapper::MigrationRunner
- DataMapper::Migrations
- DataMapper::Migrations::DataObjectsAdapter
- DataMapper::Migrations::DataObjectsAdapter::ClassMethods
- DataMapper::Migrations::DuplicateMigration
- DataMapper::Migrations::Model
- DataMapper::Migrations::MysqlAdapter
- DataMapper::Migrations::MysqlAdapter::ClassMethods
- DataMapper::Migrations::OracleAdapter
- DataMapper::Migrations::OracleAdapter::ClassMethods
- DataMapper::Migrations::PostgresAdapter
- DataMapper::Migrations::PostgresAdapter::ClassMethods
- DataMapper::Migrations::Repository
- DataMapper::Migrations::SingletonMethods
- DataMapper::Migrations::SqliteAdapter
- DataMapper::Migrations::SqliteAdapter::ClassMethods
- DataMapper::Migrations::SqlserverAdapter
- DataMapper::Migrations::SqlserverAdapter::ClassMethods
- DataMapper::Migrations::YamlAdapter
- SQL
- SQL::Column
- SQL::Mysql
- SQL::Mysql::Column
- SQL::Mysql::Table
- SQL::Postgres
- SQL::Postgres::Column
- SQL::Postgres::Table
- SQL::Sqlite
- SQL::Sqlite::Column
- SQL::Sqlite::Table
- SQL::Table
- SQL::TableCreator
- SQL::TableCreator::Column
- SQL::TableCreator::SqlExpr
- SQL::TableModifier
- Spec
- Spec::Example
- Spec::Example::MigrationExampleGroup
- Spec::Matchers
- Spec::Matchers::Migration
- Spec::Matchers::Migration::HaveColumnMatcher
- Spec::Matchers::Migration::HaveTableMatcher
- Spec::Matchers::Migration::NullableColumnMatcher
- Spec::Matchers::Migration::PrimaryKeyMatcher
- Object

No matching classes.

# 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 86
    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 108
    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

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 40
    def field_exists?(storage_name, column_name)
      statement = DataMapper::Ext::String.compress_lines(          SELECT COUNT(*)          FROM "information_schema"."columns"          WHERE "table_schema" = ?          AND "table_name" = ?          AND "column_name" = ?)

      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 17
    def storage_exists?(storage_name)
      statement = DataMapper::Ext::String.compress_lines(          SELECT COUNT(*)          FROM "information_schema"."tables"          WHERE "table_type" = 'BASE TABLE'          AND "table_schema" = ?          AND "table_name" = ?)

      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 53
    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

          # For simple :index => true columns, add an appropriate index.
          # Upgrading doesn't know how to deal with complex indexes yet.
          if property.options[:index] === true
            statement = create_index_statement(model, property.name, [property.field])
            command   = connection.create_command(statement)
            command.execute_non_query
          end

          property
        end.compact
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.