class MotionRecord::Schema::IndexDefinition

Public Class Methods

new(table_name, columns, options={}) click to toggle source

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
# 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

Public Instance Methods

execute() click to toggle source

Add the index to the database

# 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

Protected Instance Methods

build_name_from_columns() click to toggle source
# File lib/motion_record/schema/index_definition.rb, line 30
def build_name_from_columns
  "index_#{@table_name}_on_#{@columns.join "_and_"}"
end