Ecto.ParameterizedType for persisting a Tempo.Interval.t/0
as a PostgreSQL tstzrange value.
Usage
schema "meetings" do
field :window, Tempo.Ecto.Interval
endand in the migration:
add :window, :tstzrange(or use Tempo.SQL.Migration.add_interval/2).
Options
:resolution— on load, truncate both endpoints to the given component and drop all sub-components. One of:year,:month,:day,:hour,:minute, or:second. Defaults to:second(no truncation). See the storage contract guide for the semantics and caveats.field :reporting_period, Tempo.Ecto.Interval, resolution: :year
Storage contract
This type refuses to store values it cannot round-trip
semantically. The dump callback returns :error for:
Intervals with a recurrence count (
recurrence != 1) or arepeat_rule. Materialise into aTempo.IntervalSet.t/0viaTempo.to_interval/1and store the set asTempo.Ecto.IntervalSetinstead.Tempo endpoints with a
:qualification(:uncertain,:approximate), a non-Gregorian calendar, or multi-valued token slots (day_of_week: [1, 3, 5],day: 1..15).Intervals that are unbounded on both sides. Partial open ends (
from: :undefinedorto: :undefined) are stored as(,b]/[a,)ranges in Postgres.
Bracket normalisation on load
PostgreSQL does not canonicalise tstzrange values on
output — a column populated by another writer might hold
[a, b], (a, b), or (a, b]. The loader normalises any
non-half-open range to Tempo's [first, last) convention
by shifting the offending endpoint one second (Tempo is
second-resolution so this is exact).
Summary
Functions
Builds the parameterized-type tuple for use outside a schema.
Functions
Builds the parameterized-type tuple for use outside a schema.
Inside a schema, field :window, Tempo.Ecto.Interval, resolution: :day
does this for you. Reach for this when calling Ecto.Type.cast/2
and friends directly.
Arguments
optionsis a keyword list of options.
Options
:resolutiontruncates both endpoints on load to the given component. One of:year,:month,:day,:hour,:minute, or:second. Defaults to:second.
Returns
- A
Ecto.ParameterizedType.opts/0tuple.
Examples
iex> Tempo.Ecto.Interval.cast_type()
{:parameterized, {Tempo.Ecto.Interval, %{resolution: :second}}}
iex> Tempo.Ecto.Interval.cast_type(resolution: :year)
{:parameterized, {Tempo.Ecto.Interval, %{resolution: :year}}}