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

Migrations for adding rational types, casts, 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.

Pg Constraints

Creates basic constraints for a PgRational database field.

Pg Types

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

Pg Casts

Creates a native cast for

Pg Operators

Creates a custom :rational, :rational + operator.

Creates a custom :rational, :rational / operator.

Creates a custom :rational, :rational = operator.

Creates a custom :rational, :rational < operator.

Creates a custom :rational, :rational < operator.

Creates a custom :rational, :rational < operator.

Creates a custom :rational, :rational < operator.

Creates a custom :rational, :rational % operator.

Creates a custom :rational, :rational * operator.

Creates a custom :rational, :rational != operator.

Creates a custom :rational, :rational <> operator.

Creates a custom :rational, :rational - operator.

Pg Operator Classes

Creates a B-tree operator class to support indexing on comparison operations.

Pg Functions

Creates rational.abs(rat) function that returns the absolute value of the rational value.

Creates rational.minus(rat) function that flips the sign of the input value -- makes a positive value negative and a negative value positive.

Creates rational.round(rat) function that returns the rational input, rounded to the nearest :bigint

Pg Private Functions

Creates rational_private.add(a, b) backing function for the + operator between two rationals.

Creates a native CAST from rational to double precision.

Creates Creates rational_private.cmp(a, b) that returns

Creates Creates rational_private.div(a, b) backing function for the / operator between two rationals.

Creates Creates rational_private.eq(a, b) that backs the = operator.

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

Creates Creates rational_private.eq(a, b) that backs the = operator.

Creates Creates rational_private.eq(a, b) that backs the = operator.

Creates Creates rational_private.eq(a, b) that backs the = operator.

Creates Creates rational_private.eq(a, b) that backs the = operator.

Creates Creates rational_private.modulo(a, b) backing function for the % operator between two rationals.

Creates Creates rational_private.mult(a, b) backing function for the * operator between two rationals.

Creates Creates rational_private.eq(a, b) that backs the = operator.

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

Creates Creates rational_private.sub(a, b) backing function for the - operator between two rationals.

Functions

Returns the config-qualified name of the function for this 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.

This migration included all migraitons under the Pg Types, Pg Operators, Pg Operator Classes, Pg Functions, and Pg Private Functions, 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.

operators-created

Operators Created

See PgOperators section of these docs for details on native database operators created.

casts-created

Casts Created

See PgCasts section of these docs for details on native database operators 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 Pg Constraints

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 Pg Types

Link to this function

create_function_schemas()

View Source
@spec create_function_schemas() :: :ok

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

@spec create_type() :: :ok

Adds:

  • rational composite type
  • rationals schema
  • rationals_helpers schema

Link to this section Pg Casts

Link to this function

create_cast_double_precision()

View Source
@spec create_cast_double_precision() :: :ok

Creates a native cast for:

rational AS double precision

Link to this section Pg Operators

@spec create_op_add() :: :ok

Creates a custom :rational, :rational + operator.

@spec create_op_div() :: :ok

Creates a custom :rational, :rational / operator.

@spec create_op_eq() :: :ok

Creates a custom :rational, :rational = operator.

@spec create_op_gt() :: :ok

Creates a custom :rational, :rational < operator.

@spec create_op_gte() :: :ok

Creates a custom :rational, :rational < operator.

@spec create_op_lt() :: :ok

Creates a custom :rational, :rational < operator.

@spec create_op_lte() :: :ok

Creates a custom :rational, :rational < operator.

@spec create_op_modulo() :: :ok

Creates a custom :rational, :rational % operator.

@spec create_op_mult() :: :ok

Creates a custom :rational, :rational * operator.

@spec create_op_neq2() :: :ok

Creates a custom :rational, :rational != operator.

@spec create_op_neq() :: :ok

Creates a custom :rational, :rational <> operator.

@spec create_op_sub() :: :ok

Creates a custom :rational, :rational - operator.

Link to this section Pg Operator Classes

@spec create_op_class_btree() :: :ok

Creates a B-tree operator class to support indexing on comparison operations.

Link to this section Pg Functions

@spec create_func_abs() :: :ok

Creates rational.abs(rat) function that returns the absolute value of the rational value.

@spec create_func_minus() :: :ok

Creates rational.minus(rat) function that flips the sign of the input value -- makes a positive value negative and a negative value positive.

@spec create_func_round() :: :ok

Creates rational.round(rat) function that returns the rational input, rounded to the nearest :bigint

Link to this section Pg Private Functions

@spec create_func_add() :: :ok

Creates rational_private.add(a, b) backing function for the + operator between two rationals.

Link to this function

create_func_cast_to_double_precison()

View Source
@spec create_func_cast_to_double_precison() :: :ok

Creates a native CAST from rational to double precision.

@spec create_func_cmp() :: :ok

Creates Creates rational_private.cmp(a, b) that returns:

  • 1 if a > b
  • 0 if a == b
  • -1 if a < b

Used to back equality operators.

@spec create_func_div() :: :ok

Creates Creates rational_private.div(a, b) backing function for the / operator between two rationals.

@spec create_func_eq() :: :ok

Creates Creates rational_private.eq(a, b) that backs the = operator.

Link to this function

create_func_greatest_common_denominator()

View Source
@spec create_func_greatest_common_denominator() :: :ok

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

@spec create_func_gt() :: :ok

Creates Creates rational_private.eq(a, b) that backs the = operator.

@spec create_func_gte() :: :ok

Creates Creates rational_private.eq(a, b) that backs the = operator.

@spec create_func_lt() :: :ok

Creates Creates rational_private.eq(a, b) that backs the = operator.

@spec create_func_lte() :: :ok

Creates Creates rational_private.eq(a, b) that backs the = operator.

@spec create_func_modulo() :: :ok

Creates Creates rational_private.modulo(a, b) backing function for the % operator between two rationals.

@spec create_func_mult() :: :ok

Creates Creates rational_private.mult(a, b) backing function for the * operator between two rationals.

@spec create_func_neq() :: :ok

Creates Creates rational_private.eq(a, b) that backs the = operator.

@spec create_func_simplify() :: :ok

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

@spec create_func_sub() :: :ok

Creates Creates rational_private.sub(a, b) backing function for the - operator between two rationals.

Link to this section Functions

@spec function(atom(), Ecto.Repo.t()) :: String.t()

Returns the config-qualified name of the function for this type.