View Source Vtc.Ecto.Postgres.PgRational.Migrations (vtc v0.10.4)

Migrations for adding rational types, functions and constraints to a Postgres database.

Link to this section Summary

Full

Adds raw SQL queries to a migration for creating the database types, associated functions, casts, operators, and operator families.

PgConstraints

Creates basic constraints for a PgRational database field.

PgTypes

Adds rational composite type.

Link to this section Full

@spec create_all() :: :ok

Adds raw SQL queries to a migration for creating the database types, associated functions, casts, operators, and operator families.

Safe to run multiple times when new functionality is added in updates to this library. Existing values will be skipped.

types-created

Types Created

Calling this macro creates the following type definitions:

CREATE TYPE public.rational AS (
  numerator bigint,
  denominator bigint
);

## Examples

defmodule MyMigration do use Ecto.Migration

alias Vtc.Ecto.Postgres.PgRational require PgRational.Migrations

def change do

PgRational.Migrations.create_all()

end end

Link to this section PgConstraints

Link to this function

create_field_constraints(table, field_name)

View Source
@spec create_field_constraints(atom(), atom()) :: :ok

Creates basic constraints for a PgRational database field.

constraints-created

Constraints created:

  • {field_name}_denominator_positive: Checks that the denominator of the field is positive.

examples

Examples

create table("rationals", primary_key: false) do
  add(:id, :uuid, primary_key: true, null: false)
  add(:a, PgRational.type())
  add(:b, PgRational.type())
end

PgRational.migration_add_field_constraints(:rationals, :a)
PgRational.migration_add_field_constraints(:rationals, :b)

Link to this section PgTypes

@spec create_type() :: :ok

Adds rational composite type.