Module: SchemaFerry::Core::IdentifierShortenable

Includes:
Support::Warnings
Included in:
EnumCheckBuilder, TableConverter
Defined in:
lib/schema_ferry/core/identifier_shortenable.rb

Overview

PostgreSQL truncates identifiers to 63 bytes (MySQL allows 64). A silently truncated index name makes ridgepole see a diff on every run, so names that would overflow are shortened deterministically instead.

Constant Summary collapse

MAX_BYTES =
63
HASH_LENGTH =
8

Instance Method Summary collapse

Instance Method Details

#shorten_identifier(name, kind:, table:) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/schema_ferry/core/identifier_shortenable.rb', line 16

def shorten_identifier(name, kind:, table:)
  return name if name.nil? || name.bytesize <= MAX_BYTES

  prefix = name.byteslice(0, MAX_BYTES - HASH_LENGTH - 1)
  short  = "#{prefix}_#{Digest::MD5.hexdigest(name)[0, HASH_LENGTH]}"
  emit_warning "#{table}: #{kind} name #{name.inspect} exceeds PostgreSQL's " \
               "#{MAX_BYTES}-byte identifier limit; renamed to #{short.inspect}."
  short
end