Class: MotionRecord::Schema::IndexDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/motion_record/schema/index_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(table_name, columns, options = {}) ⇒ IndexDefinition

Initialize the index definition

table_name - name of the table columns - either the String name of the column to index or an Array

of column names

options - optional Hash of options for the index

:unique - set to true to create a unique index
:name - provide a String to override the default index name


13
14
15
16
17
18
19
# File 'lib/motion_record/schema/index_definition.rb', line 13

def initialize(table_name, columns, options={})
  @table_name = table_name
  @columns = columns.is_a?(Array) ? columns : [columns]

  @name    = options[:name] || build_name_from_columns
  @unique  = !!options[:unique]
end

Instance Method Details

#executeObject

Add the index to the database



22
23
24
25
26
# File 'lib/motion_record/schema/index_definition.rb', line 22

def execute
  index_statement = "CREATE#{' UNIQUE' if @unique} INDEX #{@name} ON #{@table_name} (#{@columns.join ", "})"

  MotionRecord::Base.connection.execute index_statement
end