Class: BazaMigrations::Commands::AddColumn

Inherits:
Base
  • Object
show all
Defined in:
lib/baza_migrations/commands/add_column.rb

Instance Attribute Summary

Attributes inherited from Base

#db, #table

Instance Method Summary collapse

Methods inherited from Base

#default_args

Constructor Details

#initialize(table_name, column_name, type) ⇒ AddColumn

Returns a new instance of AddColumn.



2
3
4
5
6
# File 'lib/baza_migrations/commands/add_column.rb', line 2

def initialize(table_name, column_name, type)
  @table_name = table_name
  @column_name = column_name
  @type = type
end

Instance Method Details

#changed_rollback_sqlObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/baza_migrations/commands/add_column.rb', line 12

def changed_rollback_sql
  sqls = []
  db_type = db.opts.fetch(:type)

  if db_type.to_s.include?("sqlite3")
    sqls << proc do
      table = db.tables[@table_name]

      column = table.column(@column_name)
      column.drop
    end
  else
    sqls << "ALTER TABLE `#{@table_name}` DROP COLUMN `#{@column_name}`"
  end

  sqls
end

#sqlObject



8
9
10
# File 'lib/baza_migrations/commands/add_column.rb', line 8

def sql
  ["ALTER TABLE `#{@table_name}` ADD COLUMN `#{@column_name}` #{@type};"]
end