Tempo.Ecto.Interval (Tempo SQL v0.1.0)

View Source

Ecto.ParameterizedType for persisting a Tempo.Interval.t/0 as a PostgreSQL tstzrange value.

Usage

schema "meetings" do
  field :window, Tempo.Ecto.Interval
end

and 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 a repeat_rule. Materialise into a Tempo.IntervalSet.t/0 via Tempo.to_interval/1 and store the set as Tempo.Ecto.IntervalSet instead.

  • 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: :undefined or to: :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

Helper for use outside a schema — returns a Ecto.ParameterizedType.opts/0 tuple that can be passed to Ecto.Type.cast/2 etc.

Functions

cast_type(options \\ [])

Helper for use outside a schema — returns a Ecto.ParameterizedType.opts/0 tuple that can be passed to Ecto.Type.cast/2 etc.

Examples

params = Tempo.Ecto.Interval.cast_type(resolution: :year)
Ecto.Type.cast(Tempo.Ecto.Interval, range, params)