PsqlTetris.Types (PsqlTetris v0.1.2)

View Source

Classifies Ecto migration column types into PostgreSQL alignment groups.

Strategy

Two layers, tried in order:

  1. Ecto translation (preferred). If Ecto.Adapters.Postgres.Connection is loaded (the usual case when the formatter is running inside a Phoenix/Ecto project), we ask Ecto itself to render the PostgreSQL column type for the given migration type, then map the resulting PG type name to its catalog alignment class. This way we always match whatever Ecto version your project happens to use, with no duplicated mapping for us to keep in sync.

  2. Static fallback. If Ecto is not loaded (running stand-alone, or the project simply doesn't depend on ecto_sql), we fall back to a built-in table that mirrors Ecto's documented mapping. Same logical result for common types: think of it as an intentional safety net for projects we can't introspect.

Alignment classes

  • 1: 8-byte aligned, fixed length (bigint, timestamp[tz], float8, ...)
  • 2: 4-byte aligned, fixed length (integer, date, time, uuid, ...)
  • 3: 2-byte aligned, fixed length (smallint)
  • 4: 1-byte aligned, fixed length (boolean, "char")
  • 5: variable length (text, varchar, numeric, jsonb, bytea, arrays)

Lower rank = larger alignment = emitted first.

Summary

Functions

Human-readable label for a rank, used in diagnostics/comments.

Returns the alignment rank (1..5) for a migration type.

Like rank/1, but also takes the keyword list of options passed to add/3. This lets shapes like add :user_id, references(:users, type: :integer) route through the right alignment class.

Functions

label(int)

@spec label(1..5) :: String.t()

Human-readable label for a rank, used in diagnostics/comments.

rank(type)

@spec rank(term()) :: 1..5

Returns the alignment rank (1..5) for a migration type.

rank(type, opts)

@spec rank(
  term(),
  keyword()
) :: 1..5

Like rank/1, but also takes the keyword list of options passed to add/3. This lets shapes like add :user_id, references(:users, type: :integer) route through the right alignment class.