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

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

Link to this section Summary

Types

Details for an individual migration to support a Postgres type.

The type of object a migration is creating.

Alias for string that indicates the returned value is a raw SQL statement.

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

Creates function schema as described by the Configuring Database Objects section above.

Pg Casts

Creates a native cast for

Creates a native cast for

Pg Operators

Creates a custom unary :rational @ unary 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 unary :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 ABS(rational) function that returns the absolute value of the rational value.

Creates FLOOR(rational) function that returns the rational input as a bigint, rounded towards zero, to match Postgres FLOOR(real) behavior.

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

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

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

Pg Private Functions

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

Creates a native CAST from bigint to rational.

Creates a native CAST from rational to double precision.

Creates rational.__private__cmp(a, b) that returns

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

Creates rational.__private__eq(a, b) that backs the = operator.

Creates DIV(a, b) function, which executed integer floor division on rational values.

Creates rational.__private__gt(a, b) that backs the > operator.

Creates rational.__private__gte(a, b) that backs the >= operator.

Creates rational.__private__lt(a, b) that backs the < operator.

Creates rational.__private__lte(a, b) that backs the <= operator.

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

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

Creates rational.__private__neq(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 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 Types

@type migration_info() :: {migration_type(), raw_sql(), raw_sql()} | :skip

Details for an individual migration to support a Postgres type.

Either {migration_type, up_command, down_command} or :skip if the migration should be skipped.

@type migration_type() ::
  :schema | :type | :function | :operator | :operator_class | :cast | :other

The type of object a migration is creating.

@type raw_sql() :: String.t()

Alias for string that indicates the returned value is a raw SQL statement.

Link to this section Full

@spec run(include: Keyword.t(atom()), exclude: Keyword.t(atom())) :: :ok

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

This migration includes all migrations under the Pg Types, Pg Operators, Pg Operator Classes, Pg Functions, Pg Private Functions, headings, and Pg Casts

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

Individual migration functions return raw sql commands in an {up_command, down_command} tuple.

options

Options

  • include: A list of migration functions to include. If not set, all sub-migrations will be included.

  • exclude: A list of migration functions to exclude. If not set, no sub-migrations will be excluded.

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 configuration:

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

Option definitions are as follows:

  • functions_schema: The postgres schema to store rational-related custom functions.

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

examples

Examples

defmodule MyMigration do
  use Ecto.Migration

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

  def change do
    PgRational.Migrations.run()
  end
end

Link to this section Pg Constraints

Link to this function

create_constraints(table, field_name)

View Source
@spec create_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() :: migration_info()

Creates function schema as described by the Configuring Database Objects section above.

@spec create_type() :: migration_info()

Adds:

  • rational composite type
  • rationals schema
  • rationals_helpers schema

Link to this section Pg Casts

Link to this function

create_cast_bigint_to_rational()

View Source
@spec create_cast_bigint_to_rational() :: migration_info()

Creates a native cast for:

bigint AS rational
Link to this function

create_cast_double_precision()

View Source
@spec create_cast_double_precision() :: migration_info()

Creates a native cast for:

rational AS double precision

Link to this section Pg Operators

@spec create_op_abs() :: migration_info()

Creates a custom unary :rational @ unary operator.

Returns the absolute value of the input.

@spec create_op_add() :: migration_info()

Creates a custom :rational, :rational + operator.

@spec create_op_div() :: migration_info()

Creates a custom :rational, :rational / operator.

@spec create_op_eq() :: migration_info()

Creates a custom :rational, :rational = operator.

@spec create_op_gt() :: migration_info()

Creates a custom :rational, :rational < operator.

@spec create_op_gte() :: migration_info()

Creates a custom :rational, :rational < operator.

@spec create_op_lt() :: migration_info()

Creates a custom :rational, :rational < operator.

@spec create_op_lte() :: migration_info()

Creates a custom :rational, :rational < operator.

@spec create_op_minus() :: migration_info()

Creates a custom unary :rational - operator.

Flips the sign of value. Equivalent to value * -1.

@spec create_op_modulo() :: migration_info()

Creates a custom :rational, :rational % operator.

@spec create_op_mult() :: migration_info()

Creates a custom :rational, :rational * operator.

@spec create_op_neq() :: migration_info()

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

@spec create_op_sub() :: migration_info()

Creates a custom :rational, :rational - operator.

Link to this section Pg Operator Classes

@spec create_op_class_btree() :: migration_info()

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

Link to this section Pg Functions

@spec create_func_abs() :: migration_info()

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

@spec create_func_floor() :: migration_info()

Creates FLOOR(rational) function that returns the rational input as a bigint, rounded towards zero, to match Postgres FLOOR(real) behavior.

@spec create_func_minus() :: migration_info()

Creates rational.__private__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() :: migration_info()

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

@spec create_func_sign() :: migration_info()

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

Link to this section Pg Private Functions

@spec create_func_add() :: migration_info()

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

Link to this function

create_func_cast_bigint_to_rational()

View Source
@spec create_func_cast_bigint_to_rational() :: migration_info()

Creates a native CAST from bigint to rational.

Link to this function

create_func_cast_to_double_precision()

View Source
@spec create_func_cast_to_double_precision() :: migration_info()

Creates a native CAST from rational to double precision.

@spec create_func_cmp() :: migration_info()

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() :: migration_info()

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

@spec create_func_eq() :: migration_info()

Creates rational.__private__eq(a, b) that backs the = operator.

@spec create_func_floor_div() :: migration_info()

Creates DIV(a, b) function, which executed integer floor division on rational values.

Just like DIV(real, real), DIV(rational, rational) floors towards zero.

@spec create_func_gt() :: migration_info()

Creates rational.__private__gt(a, b) that backs the > operator.

@spec create_func_gte() :: migration_info()

Creates rational.__private__gte(a, b) that backs the >= operator.

@spec create_func_lt() :: migration_info()

Creates rational.__private__lt(a, b) that backs the < operator.

@spec create_func_lte() :: migration_info()

Creates rational.__private__lte(a, b) that backs the <= operator.

@spec create_func_modulo() :: migration_info()

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

@spec create_func_mult() :: migration_info()

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

@spec create_func_neq() :: migration_info()

Creates rational.__private__neq(a, b) that backs the <> operator.

@spec create_func_simplify() :: migration_info()

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() :: migration_info()

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.