Class: MotionRecord::Schema::IndexDefinition
- Inherits:
-
Object
- Object
- MotionRecord::Schema::IndexDefinition
- Defined in:
- lib/motion_record/schema/index_definition.rb
Instance Method Summary collapse
-
#execute ⇒ Object
Add the index to the database.
-
#initialize(table_name, columns, options = {}) ⇒ IndexDefinition
constructor
Initialize the index definition.
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, ={}) @table_name = table_name @columns = columns.is_a?(Array) ? columns : [columns] @name = [:name] || build_name_from_columns @unique = !![:unique] end |
Instance Method Details
#execute ⇒ Object
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 |