Module: DynamicMigrations::Postgres::Server::Database::Schema::Table::Indexes

Included in:
DynamicMigrations::Postgres::Server::Database::Schema::Table
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb

Overview

This module has all the tables methods for working with indexes

Defined Under Namespace

Classes: IndexAlreadyExistsError, IndexDoesNotExistError

Instance Method Summary collapse

Instance Method Details

#add_index(name, column_names, include_column_names: [], **index_options) ⇒ Object

adds a new index to this table, and returns it include_column_names in broken out from index_options, because it is converted from an array of column names into an array of columns, and then recombined with the other options which are sent to the index initialize method



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb', line 44

def add_index name, column_names, include_column_names: [], **index_options
  if has_index? name
    raise(IndexAlreadyExistsError, "index #{name} already exists")
  end
  columns = column_names.map { |column_name| column column_name }
  include_columns = include_column_names.map { |column_name| column column_name }
  included_target = self
  if included_target.is_a? Table
    new_index = @indexes[name] = Index.new source, included_target, columns, name, include_columns: include_columns, **index_options
  else
    raise ModuleIncludedIntoUnexpectedTargetError, included_target
  end
  # sort the hash so that the indexes are in alphabetical order by name
  sorted_indexes = {}
  @indexes.keys.sort.each do |name|
    sorted_indexes[name] = @indexes[name]
  end
  @indexes = sorted_indexes
  # return the new index
  new_index
end

#has_index?(name) ⇒ Boolean

returns true if this table has a index with the provided name, otherwise false

Returns:

  • (Boolean)

Raises:



26
27
28
29
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb', line 26

def has_index? name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @indexes.key? name
end

#index(name) ⇒ Object

returns the index object for the provided index name, and raises an error if the index does not exist



19
20
21
22
23
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb', line 19

def index name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  raise IndexDoesNotExistError unless has_index? name
  @indexes[name]
end

#indexesObject

returns an array of this tables indexes



32
33
34
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb', line 32

def indexes
  @indexes.values
end

#indexes_hashObject



36
37
38
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/indexes.rb', line 36

def indexes_hash
  @indexes
end