Module: SchemaFerry::Converter::IdentifierShortener

Extended by:
Warnings
Defined in:
lib/schema_ferry/converter/identifier_shortener.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

Class Method Summary collapse

Class Method Details

.shorten(name, kind:, table:) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/schema_ferry/converter/identifier_shortener.rb', line 18

def shorten(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

.warn_long_table_name(name) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/schema_ferry/converter/identifier_shortener.rb', line 28

def warn_long_table_name(name)
  return if name.bytesize <= MAX_BYTES

  emit_warning "table name #{name.inspect} exceeds PostgreSQL's #{MAX_BYTES}-byte " \
               "identifier limit and will be truncated by PostgreSQL. Rename the " \
               "table before applying to avoid ridgepole re-creating it on every run."
end