Class: PgHaMigrations::Index

Inherits:
Relation
  • Object
show all
Defined in:
lib/pg_ha_migrations/relation.rb

Constant Summary collapse

MAX_NAME_SIZE =

bytes

63

Instance Attribute Summary collapse

Attributes inherited from Relation

#mode, #name, #schema

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Relation

#==, #conflicts_with?, connection, #eql?, #fully_qualified_name, #hash, #present?

Constructor Details

#initialize(name, table) ⇒ Index

Returns a new instance of Index.



188
189
190
191
192
193
194
# File 'lib/pg_ha_migrations/relation.rb', line 188

def initialize(name, table)
  super(name, table.schema)

  self.table = table

  connection.send(:validate_index_length!, table.name, name)
end

Instance Attribute Details

#tableObject

Returns the value of attribute table.



186
187
188
# File 'lib/pg_ha_migrations/relation.rb', line 186

def table
  @table
end

Class Method Details

.from_table_and_columns(table, columns) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/pg_ha_migrations/relation.rb', line 169

def self.from_table_and_columns(table, columns)
  name = connection.index_name(table.name, columns)

  # modified from https://github.com/rails/rails/pull/47753
  if name.bytesize > MAX_NAME_SIZE
    hashed_identifier = "_#{OpenSSL::Digest::SHA256.hexdigest(name).first(10)}"
    description = name.sub("index_#{table.name}_on", "idx_on")

    short_limit = MAX_NAME_SIZE - hashed_identifier.bytesize
    short_description = description.mb_chars.limit(short_limit).to_s

    name = "#{short_description}#{hashed_identifier}"
  end

  new(name, table)
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


196
197
198
199
200
201
202
203
204
205
# File 'lib/pg_ha_migrations/relation.rb', line 196

def valid?
  !!connection.select_value(<<~SQL)
    SELECT pg_index.indisvalid
    FROM pg_index, pg_class, pg_namespace
    WHERE pg_class.oid = pg_index.indexrelid
      AND pg_class.relnamespace = pg_namespace.oid
      AND pg_namespace.nspname = #{connection.quote(schema)}
      AND pg_class.relname = #{connection.quote(name)}
  SQL
end