Class: DynamicMigrations::Postgres::Server::Database::Schema::Table::Index

Inherits:
DynamicMigrations::Postgres::Server::Database::Source show all
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table/index.rb

Overview

This class represents a postgres table index

Defined Under Namespace

Classes: DuplicateColumnError, ExpectedArrayOfColumnsError, ExpectedTableError, UnexpectedIndexTypeError, UnexpectedNullsPositionError, UnexpectedOrderError

Constant Summary collapse

INDEX_TYPES =
[:btree, :hash, :gist, :gin, :bring, :spgist]
ORDERS =
[:asc, :desc]
NULL_POSITIONS =
[:first, :last]

Instance Attribute Summary collapse

Attributes inherited from DynamicMigrations::Postgres::Server::Database::Source

#source

Instance Method Summary collapse

Methods inherited from DynamicMigrations::Postgres::Server::Database::Source

#assert_is_a_symbol!, #from_configuration?, #from_database?

Constructor Details

#initialize(source, table, columns, name, description: nil, unique: false, where: nil, type: :btree, include_columns: [], order: :asc, nulls_position: :last) ⇒ Index

initialize a new object to represent a index in a postgres table

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 43

def initialize source, table, columns, name, description: nil, unique: false, where: nil, type: :btree, include_columns: [], order: :asc, nulls_position: :last
  super source
  raise ExpectedTableError, table unless table.is_a? Table
  @table = table
  @columns = {}
  @include_columns = {}

  # assert that the provided columns is an array
  unless columns.is_a?(Array) && columns.count > 0
    raise ExpectedArrayOfColumnsError
  end

  columns.each do |column|
    add_column column
  end

  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @name = name

  unless description.nil?
    raise ExpectedStringError, description unless description.is_a? String
    @description = description.strip
    @description = nil if description == ""
  end

  raise ExpectedBooleanError, unique unless [true, false].include?(unique)
  @unique = unique

  unless where.nil?
    raise ExpectedStringError, where unless where.is_a? String
    @where = where
  end

  raise UnexpectedIndexTypeError, type unless INDEX_TYPES.include?(type)
  @type = type

  # assert that the include_columns is an array (it's optional, so can be an empty array)
  unless include_columns.is_a?(Array)
    raise ExpectedArrayOfColumnsError
  end

  include_columns.each do |include_column|
    add_column include_column, is_include_column: true
  end

  raise UnexpectedOrderError, order unless ORDERS.include?(order)
  @order = order

  raise UnexpectedNullsPositionError, nulls_position unless NULL_POSITIONS.include?(nulls_position)
  @nulls_position = nulls_position
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



40
41
42
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 40

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 34

def name
  @name
end

#nulls_positionObject (readonly)

Returns the value of attribute nulls_position.



39
40
41
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 39

def nulls_position
  @nulls_position
end

#orderObject (readonly)

Returns the value of attribute order.



38
39
40
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 38

def order
  @order
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#uniqueObject (readonly)

Returns the value of attribute unique.



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

def unique
  @unique
end

#whereObject (readonly)

Returns the value of attribute where.



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

def where
  @where
end

Instance Method Details

#column_namesObject



105
106
107
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 105

def column_names
  @columns.keys
end

#columnsObject

return an array of this indexes columns



101
102
103
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 101

def columns
  @columns.values
end

#differences_descriptions(other_index) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 118

def differences_descriptions other_index
  method_differences_descriptions other_index, [
    :column_names,
    :unique,
    :where,
    :type,
    :order,
    :nulls_position
  ]
end

#has_description?Boolean

return true if this has a description, otherwise false

Returns:

  • (Boolean)


96
97
98
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 96

def has_description?
  !@description.nil?
end

#include_column_namesObject



114
115
116
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 114

def include_column_names
  @include_columns.keys
end

#include_columnsObject

return an array of this indexes include_columns



110
111
112
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/index.rb', line 110

def include_columns
  @include_columns.values
end