Class: Sequel::Schema::DbIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/schema/db_index.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, columns, unique = false) ⇒ DbIndex

Creates a new DbIndex definition.

columns may be a single column name as a symbol, or an array of column symbol names.

Indexes are not unique by default.



19
20
21
22
23
# File 'lib/sequel/schema/db_index.rb', line 19

def initialize(name, columns, unique=false)
  @name = name.to_sym
  @columns = columns.kind_of?(Array) ? columns.clone : [columns]
  @unique = !! unique
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'lib/sequel/schema/db_index.rb', line 5

def columns
  @columns
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/sequel/schema/db_index.rb', line 4

def name
  @name
end

Class Method Details

.build_from_hash(definitions) ⇒ Object

Builds an Array of DbIndexes from the index hash returned by Sequel::Database#indexes



9
10
11
# File 'lib/sequel/schema/db_index.rb', line 9

def self.build_from_hash(definitions)
  definitions.map {|name,attrs| new(name, attrs[:columns], attrs[:unique]) }
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Indexes are equal if all their attributes are equal.



54
55
56
57
# File 'lib/sequel/schema/db_index.rb', line 54

def ==(other)
  other.kind_of?(self.class) &&
    @name == other.name && @columns == other.columns && @unique == other.unique?
end

#add_statementObject

Returns the sequel migration statement to add an index in an alter_table block.



43
44
45
# File 'lib/sequel/schema/db_index.rb', line 43

def add_statement
  base_add_statement('add_index')
end

#define_statementObject

Returns the sequel migration statement to define an index in a create_table block.



37
38
39
# File 'lib/sequel/schema/db_index.rb', line 37

def define_statement
  base_add_statement('index')
end

#drop_statementObject

Returns the sequel migration statement to remove an index in an alter_table block.



49
50
51
# File 'lib/sequel/schema/db_index.rb', line 49

def drop_statement
  "drop_index #{columns_for_statement.inspect}, :name => #{name.inspect}"
end

#hashObject

:nodoc:



60
61
62
# File 'lib/sequel/schema/db_index.rb', line 60

def hash # :nodoc:
  @name.hash
end

#multi_column?Boolean

Returns true if this index has more than one column.

Returns:

  • (Boolean)


31
32
33
# File 'lib/sequel/schema/db_index.rb', line 31

def multi_column?
  @columns.size > 1
end

#unique?Boolean

Returns true if this index is unique

Returns:

  • (Boolean)


26
27
28
# File 'lib/sequel/schema/db_index.rb', line 26

def unique?
  @unique
end