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

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

Creates schemas to act as namespaces for rational functions

PgFunctions

Adds rational_private.greatest_common_denominator(a, b) function that finds the greatest common denominator between two bigint values.

Adds rational_private.simplify(rat) function that simplifies a rational. Used at the end of every rational operation to avoid overflows.

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.

This migration included all migraitons under the PgTypes and PgFunctions headings.

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
);

schemas-created

Schemas Created

Up to two schemas are created as detailed by the Configuring Database Objects section below.

configuring-database-objects

Configuring Database Objects

To change where supporting functions are created, add the following to your Repo confiugration:

config :vtc, Vtc.Test.Support.Repo,
  adapter: Ecto.Adapters.Postgres,
  ...
  vtc: [
    pg_rational: [
      functions_schema: :rational,
      functions_private_schema: :rational_private,
      functions_prefix: "rational"
    ]
  ]

Option definitions are as follows:

  • functions_schema: The schema for "public" functions that will have backwards compatibility guarantees and application code support. Default: :public.

  • functions_private_schema: The schema for for developer-only "private" functions that support the functions in the "rational" schema. Will NOT havr backwards compatibility guarantees NOR application code support. Default: :public.

  • functions_prefix: A prefix to add before all functions. Defaults to "rational" for any function created in the "public" schema, and "" otherwise.

functions-created

Functions Created

See PgFunctions section of these docs for details on native database functions created.

examples

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

Link to this function

create_function_schemas()

View Source
@spec create_function_schemas() :: :ok

Creates schemas to act as namespaces for rational functions:

  • rational: for user-facing "public" functions that will have backwards compatibility guarantees and application code support.

  • rational_private: for developer-only "private" functions that support the functions in the "rational" schema. Will NOT havr backwards compatibility guarantees NOR application code support.

@spec create_type() :: :ok

Adds:

  • rational composite type
  • rationals schema
  • rationals_helpers schema

Link to this section PgFunctions

Link to this function

create_greatest_common_denominator()

View Source
@spec create_greatest_common_denominator() :: :ok

Adds rational_private.greatest_common_denominator(a, b) function that finds the greatest common denominator between two bigint values.

@spec create_simplify() :: :ok

Adds rational_private.simplify(rat) function that simplifies a rational. Used at the end of every rational operation to avoid overflows.