Ecto migration helpers for the Tempo PostgreSQL range types.
These helpers wrap the underlying Postgres types — tstzrange
and tstzmultirange — under Tempo-flavoured names, so a
migration reads in the same vocabulary as the schema:
import Tempo.SQL.Migration
create table(:meetings) do
add_interval :window
add_interval_set :busy, null: false
end
create_interval_index :meetings, :windowAll helpers are thin delegates to Ecto.Migration.add/3 and
Ecto.Migration.index/3 — callers can also write the raw
add :window, :tstzrange form if they prefer.
Summary
Functions
Add a tstzrange column for a Tempo.Ecto.Interval field.
Add a tstzmultirange column for a Tempo.Ecto.IntervalSet
field.
Adds a tempo_multirange column for a Tempo.Ecto.TempoMultirange
field.
Adds a tempo_range column for a Tempo.Ecto.TempoRange field.
Create a GiST index on a tstzrange or tstzmultirange column.
Create the composite types used by Tempo.Ecto.TempoRange and
Tempo.Ecto.TempoMultirange.
Drops the composite types created by create_tempo_types/0.
Functions
Add a tstzrange column for a Tempo.Ecto.Interval field.
Arguments
fieldis the column name as an atom.optionsare passed through toEcto.Migration.add/3— for examplenull: false,default: ....
Returns
- The column declaration, as
Ecto.Migration.add/3returns it. Call it inside acreate tableblock.
Examples
add_interval :window
add_interval :window, null: false
Add a tstzmultirange column for a Tempo.Ecto.IntervalSet
field.
Requires PostgreSQL 14 or later.
Arguments
fieldis the column name as an atom.optionsare passed through toEcto.Migration.add/3.
Returns
- The column declaration, as
Ecto.Migration.add/3returns it. Call it inside acreate tableblock.
Examples
add_interval_set :busy_times
add_interval_set :free_slots, null: false
Adds a tempo_multirange column for a Tempo.Ecto.TempoMultirange
field.
Requires create_tempo_types/0 to have been run in an earlier
migration, or earlier in this one.
Arguments
fieldis the column name as an atom.optionsare passed through toEcto.Migration.add/3.
Returns
- The column declaration, as
Ecto.Migration.add/3returns it. Call it inside acreate tableblock.
Examples
add_tempo_multirange :busy_times
add_tempo_multirange :busy_times, null: false
Adds a tempo_range column for a Tempo.Ecto.TempoRange field.
Requires create_tempo_types/0 to have been run in an earlier
migration, or earlier in this one.
Arguments
fieldis the column name as an atom.optionsare passed through toEcto.Migration.add/3.
Returns
- The column declaration, as
Ecto.Migration.add/3returns it. Call it inside acreate tableblock.
Examples
add_tempo_range :reporting_period
add_tempo_range :reporting_period, null: false
Create a GiST index on a tstzrange or tstzmultirange column.
Range-operator queries (@>, &&, -|-) are only fast under a
GiST index — this helper wraps create index(..., using: :gist).
Arguments
tableis the table name.columnis the range column to index.optionsare passed through toEcto.Migration.index/3. The:usingoption defaults to:gist.
Returns
- The index creation, as
Ecto.Migration.create/1returns it.
Examples
create_interval_index :meetings, :window
create_interval_index :calendars, :busy_times, name: "calendars_busy_gist"
Create the composite types used by Tempo.Ecto.TempoRange and
Tempo.Ecto.TempoMultirange.
This should be run once, in an early migration, before any
schema declares a tempo_range or tempo_multirange column:
defmodule MyApp.Repo.Migrations.CreateTempoTypes do
use Ecto.Migration
import Tempo.SQL.Migration
def up, do: create_tempo_types()
def down, do: drop_tempo_types()
endCreates two PostgreSQL composite types:
CREATE TYPE tempo_range AS (
range tstzrange,
resolution text,
meta jsonb
);
CREATE TYPE tempo_multirange AS (
ranges tstzmultirange,
resolution text,
meta jsonb
);These are the round-trip-preserving counterparts to the plain
tstzrange / tstzmultirange columns used by
Tempo.Ecto.Interval and Tempo.Ecto.IntervalSet.
Returns
- The two
CREATE TYPEstatements, asEcto.Migration.execute/2returns them. Both are reversible, so achange/0that calls this rolls back cleanly.
Examples
def change do
create_tempo_types()
create table(:meetings) do
add_tempo_range :window
end
end
Drops the composite types created by create_tempo_types/0.
Intended for use in down/0 callbacks. Fails if any column still
uses either type — drop those columns first.
Returns
- The two
DROP TYPEstatements, asEcto.Migration.execute/2returns them.
Examples
def up, do: create_tempo_types()
def down, do: drop_tempo_types()