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

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

# Link to this section Summary

## Types

raw_sql()

Indicates returned string is am SQL command.

## Full

create_all(opts \\ [])

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

## Pg Constraints

create_constraints(table, field_name)

Creates basic constraints for a `PgRational` database field.

## Pg Types

create_function_schemas()

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

create_type()

Adds

## Pg Casts

create_cast_bigint_to_rational()

Creates a native cast for

create_cast_double_precision()

Creates a native cast for

## Pg Operators

create_op_add()

Creates a custom :rational, :rational `+` operator.

create_op_div()

Creates a custom :rational, :rational `/` operator.

create_op_eq()

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

create_op_gt()

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

create_op_gte()

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

create_op_lt()

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

create_op_lte()

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

create_op_modulo()

Creates a custom :rational, :rational `%` operator.

create_op_mult()

Creates a custom :rational, :rational `*` operator.

create_op_neq()

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

create_op_sub()

Creates a custom :rational, :rational `-` operator.

## Pg Operator Classes

create_op_class_btree()

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

## Pg Functions

create_func_abs()

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

create_func_floor()

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

create_func_minus()

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

create_func_round()

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

## Pg Private Functions

create_func_add()

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

create_func_cast_bigint_to_rational()

Creates a native CAST from `bigint` to `rational`.

create_func_cast_to_double_precison()

Creates a native CAST from `rational` to `double precision`.

create_func_cmp()

Creates `rational.__private__cmp(a, b)` that returns

create_func_div()

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

create_func_eq()

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

create_func_floor_div()

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

create_func_gt()

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

create_func_gte()

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

create_func_lt()

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

create_func_lte()

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

create_func_modulo()

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

create_func_mult()

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

create_func_neq()

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

create_func_simplify()

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

create_func_sub()

Creates `rational.__private__sub(a, b)` backing function for the `-` operator between two rationals.

## Functions

function(name, repo)

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

# Link to this section Types

Link to this type

# raw_sql()

View Source

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

Indicates returned string is am SQL command.

# Link to this section Full

Link to this function

# create_all(opts \\ [])

View Source

    @spec create_all(include: Keyword.t(), exclude: Keyword.t()) :: :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, 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 inclide. 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 confiugration:

    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.

##

private-functions

Private Functions

Some custom function names are prefaced with `__private__`. These functions should not be called by end-users, as they are not subject to any API staility guarantees.

##

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_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() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_type()

View Source

    @spec create_type() :: {raw_sql(), raw_sql()}

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() :: {raw_sql(), raw_sql()}

Creates a native cast for:

    bigint AS rational

Link to this function

# create_cast_double_precision()

View Source

    @spec create_cast_double_precision() :: {raw_sql(), raw_sql()}

Creates a native cast for:

    rational AS double precision

# Link to this section Pg Operators

Link to this function

# create_op_add()

View Source

    @spec create_op_add() :: {raw_sql(), raw_sql()}

Creates a custom :rational, :rational `+` operator.

Link to this function

# create_op_div()

View Source

    @spec create_op_div() :: {raw_sql(), raw_sql()}

Creates a custom :rational, :rational `/` operator.

Link to this function

# create_op_eq()

View Source

    @spec create_op_eq() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_op_gt()

View Source

    @spec create_op_gt() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_op_gte()

View Source

    @spec create_op_gte() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_op_lt()

View Source

    @spec create_op_lt() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_op_lte()

View Source

    @spec create_op_lte() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_op_modulo()

View Source

    @spec create_op_modulo() :: {raw_sql(), raw_sql()}

Creates a custom :rational, :rational `%` operator.

Link to this function

# create_op_mult()

View Source

    @spec create_op_mult() :: {raw_sql(), raw_sql()}

Creates a custom :rational, :rational `*` operator.

Link to this function

# create_op_neq()

View Source

    @spec create_op_neq() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_op_sub()

View Source

    @spec create_op_sub() :: {raw_sql(), raw_sql()}

Creates a custom :rational, :rational `-` operator.

# Link to this section Pg Operator Classes

Link to this function

# create_op_class_btree()

View Source

    @spec create_op_class_btree() :: {raw_sql(), raw_sql()}

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

# Link to this section Pg Functions

Link to this function

# create_func_abs()

View Source

    @spec create_func_abs() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_floor()

View Source

    @spec create_func_floor() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_minus()

View Source

    @spec create_func_minus() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_round()

View Source

    @spec create_func_round() :: {raw_sql(), raw_sql()}

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

# Link to this section Pg Private Functions

Link to this function

# create_func_add()

View Source

    @spec create_func_add() :: {raw_sql(), raw_sql()}

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() :: {raw_sql(), raw_sql()}

Creates a native CAST from `bigint` to `rational`.

Link to this function

# create_func_cast_to_double_precison()

View Source

    @spec create_func_cast_to_double_precison() :: {raw_sql(), raw_sql()}

Creates a native CAST from `rational` to `double precision`.

Link to this function

# create_func_cmp()

View Source

    @spec create_func_cmp() :: {raw_sql(), raw_sql()}

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.

Link to this function

# create_func_div()

View Source

    @spec create_func_div() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_eq()

View Source

    @spec create_func_eq() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_floor_div()

View Source

    @spec create_func_floor_div() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_gt()

View Source

    @spec create_func_gt() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_gte()

View Source

    @spec create_func_gte() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_lt()

View Source

    @spec create_func_lt() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_lte()

View Source

    @spec create_func_lte() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_modulo()

View Source

    @spec create_func_modulo() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_mult()

View Source

    @spec create_func_mult() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_neq()

View Source

    @spec create_func_neq() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_simplify()

View Source

    @spec create_func_simplify() :: {raw_sql(), raw_sql()}

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

Link to this function

# create_func_sub()

View Source

    @spec create_func_sub() :: {raw_sql(), raw_sql()}

Creates `rational.__private__sub(a, b)` backing function for the `-` operator between two rationals.

# Link to this section Functions

Link to this function

# function(name, repo)

View Source

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

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