Class: SQL::TableModifier

### Home Classes Methods

### In Files

- lib/dm-migrations/sql/table_modifier.rb

### Parent

Object

### Methods

- ::new
- #add_column
- #change_column
- #drop_column
- #drop_columns
- #quote_column_name
- #quoted_table_name
- #rename_column
- #to_sql

### 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.

# SQL::TableModifier

### Attributes

adapter[RW]
opts[RW]
statements[RW]
table_name[RW]

### Public Class Methods

new(adapter, table_name, opts = {}, &block) click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 7
    def initialize(adapter, table_name, opts = {}, &block)
      @adapter = adapter
      @table_name = table_name.to_s
      @opts = (opts)

      @statements = []

      self.instance_eval &block
    end

### Public Instance Methods

add_column(name, type, opts = {}) click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 17
    def add_column(name, type, opts = {})
      column = SQL::TableCreator::Column.new(@adapter, name, type, opts)
      @statements << "ALTER TABLE #{quoted_table_name} ADD COLUMN #{column.to_sql}"
    end

change_column(name, type, opts = {}) click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 40
    def change_column(name, type, opts = {})
      column = SQL::TableCreator::Column.new(@adapter, name, type, opts)
      @statements << @adapter.change_column_type_statement(table_name, column)
    end

drop_column(name) click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 22
    def drop_column(name)
      # raise NotImplemented for SQLite3. Can't ALTER TABLE, need to copy table.
      # We'd have to inspect it, and we can't, since we aren't executing any queries yet.
      # TODO instead of building the SQL queries when executing the block, create AddColumn,
      # AlterColumn and DropColumn objects that get #to_sql'd
      if name.is_a?(Array)
        name.each{ |n| drop_column(n) }
      else
        @statements << "ALTER TABLE #{quoted_table_name} DROP COLUMN #{quote_column_name(name)}"
      end
    end

Also aliased as: drop_columns
drop_columns(name) click to toggle source
Alias for: drop_column
quote_column_name(name) click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 45
    def quote_column_name(name)
      @adapter.send(:quote_name, name.to_s)
    end

quoted_table_name() click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 49
    def quoted_table_name
      @adapter.send(:quote_name, table_name)
    end

rename_column(name, new_name, opts = {}) click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 35
    def rename_column(name, new_name, opts = {})
      # raise NotImplemented for SQLite3
      @statements << "ALTER TABLE #{quoted_table_name} RENAME COLUMN #{quote_column_name(name)} TO #{quote_column_name(new_name)}"
    end

to_sql() click to toggle source

    # File lib/dm-migrations/sql/table_modifier.rb, line 53
    def to_sql
      @statements.join(';')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.