Class: BazaMigrations::Commands::AddIndex

Inherits:
Base
  • Object
show all
Defined in:
lib/baza_migrations/commands/add_index.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, columns, args) ⇒ AddIndex

Returns a new instance of AddIndex.



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

def initialize(table_name, columns, args)
  @table_name = table_name
  @columns = columns
  @args = args
end

Instance Method Details

#changed_rollback_sqlObject



25
26
27
# File 'lib/baza_migrations/commands/add_index.rb', line 25

def changed_rollback_sql
  ["DROP INDEX `#{@db.escape_column(index_name)}`"]
end

#sqlObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/baza_migrations/commands/add_index.rb', line 8

def sql
  sql = "CREATE"
  sql << " UNIQUE" if @args[:unique]
  sql << " INDEX `#{@db.escape_column(index_name)}` ON `#{@db.escape_table(@table_name)}` ("

  first = true
  columns_as_array.each do |column|
    sql << ", " unless first
    first = false if first
    sql << "`#{column}`"
  end

  sql << ")"

  [sql]
end