Electric.Replication.Eval (electric v1.4.13)

View Source

Utilities for evaluating and converting replication-related types.

Summary

Functions

Converts a type specification to a PostgreSQL cast string.

Functions

type_to_pg_cast(type, is_in_array? \\ false)

Converts a type specification to a PostgreSQL cast string.

Parameters

  • type - A type specification, which can be:
    • An atom representing a basic PostgreSQL type
    • A tuple {:array, type} for array types
    • A tuple {:enum, name} for enum types

Returns

A string representation of the PostgreSQL cast type.

Examples

Basic types:

iex> Electric.Replication.Eval.type_to_pg_cast(:int4)
"int4"

iex> Electric.Replication.Eval.type_to_pg_cast(:text)
"text"

iex> Electric.Replication.Eval.type_to_pg_cast(:bool)
"bool"

Array types:

iex> Electric.Replication.Eval.type_to_pg_cast({:array, :int4})
"int4[]"

iex> Electric.Replication.Eval.type_to_pg_cast({:array, :text})
"text[]"

Nested array types:

iex> Electric.Replication.Eval.type_to_pg_cast({:array, {:array, :int4}})
"int4[]"

Enum types:

iex> Electric.Replication.Eval.type_to_pg_cast({:enum, :my_enum})
"my_enum"

iex> Electric.Replication.Eval.type_to_pg_cast({:enum, "custom_enum"})
"custom_enum"

Unsupported types raise errors:

iex> Electric.Replication.Eval.type_to_pg_cast({:row, []})
** (RuntimeError) Unsupported type: row

iex> Electric.Replication.Eval.type_to_pg_cast({:internal, :something})
** (RuntimeError) Unsupported type: internal