Class: DeclareSchema::SchemaChange::IndexAdd

Inherits:
Base
  • Object
show all
Defined in:
lib/declare_schema/schema_change/index_add.rb

Direct Known Subclasses

IndexRemove

Instance Method Summary collapse

Methods inherited from Base

#down, format_options, #up

Constructor Details

#initialize(table_name, column_names, name:, unique:, where: nil, length: nil) ⇒ IndexAdd

Returns a new instance of IndexAdd.



8
9
10
11
12
13
14
15
# File 'lib/declare_schema/schema_change/index_add.rb', line 8

def initialize(table_name, column_names, name:, unique:, where: nil, length: nil)
  @table_name = table_name
  @column_names = column_names
  @name = name
  @unique = unique
  @where = where.presence
  @length = length
end

Instance Method Details

#down_commandObject



30
31
32
# File 'lib/declare_schema/schema_change/index_add.rb', line 30

def down_command
  "remove_index #{@table_name.to_sym.inspect}, name: #{@name.to_sym.inspect}"
end

#up_commandObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/declare_schema/schema_change/index_add.rb', line 17

def up_command
  options = {
    name: @name.to_sym,
  }
  options[:unique] = true if @unique
  options[:where] = @where if @where
  options[:length] = @length if @length

  "add_index #{[@table_name.to_sym.inspect,
                @column_names.map(&:to_sym).inspect,
                *self.class.format_options(options)].join(', ')}"
end