Sha256: 997ecf6dea3f8d2566fa2e00fbeccc8be9c7d26f2924b51cd5d536a3782fa278

Contents?: true

Size: 1.67 KB

Versions: 12

Compression:

Stored size: 1.67 KB

Contents

module SQL
  class TableModifier
    attr_accessor :table_name, :opts, :statements, :adapter

    def initialize(adapter, table_name, opts = {}, &block)
      @adapter = adapter
      @table_name = table_name.to_s
      @opts = (opts)

      @statements = []

      self.instance_eval &block
    end

    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

    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
    alias drop_columns drop_column

    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

    def change_column(name, type, opts = {})
      # raise NotImplemented for SQLite3
      @statements << "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(name)} TYPE #{type}"
    end

    def quote_column_name(name)
      @adapter.send(:quote_column_name, name.to_s)
    end

    def quoted_table_name
      @adapter.send(:quote_table_name, table_name)
    end

  end

end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
dm-migrations-0.9.10 lib/sql/table_modifier.rb
dm-migrations-0.9.11 lib/sql/table_modifier.rb
dm-migrations-0.9.3 lib/sql/table_modifier.rb
dm-migrations-0.9.4 lib/sql/table_modifier.rb
dm-migrations-0.9.5 lib/sql/table_modifier.rb
dm-migrations-0.9.6 lib/sql/table_modifier.rb
dm-migrations-0.9.7 lib/sql/table_modifier.rb
dm-migrations-0.9.8 lib/sql/table_modifier.rb
dm-migrations-0.9.9 lib/sql/table_modifier.rb
mack-data_mapper-0.8.2 lib/gems/dm-migrations-0.9.7/lib/sql/table_modifier.rb
mack-data_mapper-0.8.3.1 lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb
mack-data_mapper-0.8.3 lib/gems/dm-migrations-0.9.9/lib/sql/table_modifier.rb