View Source Vtc.Ecto.Postgres.PgRational.Migrations (vtc v0.14.3)
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
Creates function schema as described by the Configuring Database Objects section above.
Adds
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.
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.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.
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 raw_sql() :: String.t()
Indicates returned string is an SQL command.
Link to this section Full
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.
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 stability 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
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
Creates function schema as described by the Configuring Database Objects section above.
Adds:
rational
composite typerationals
schemarationals_helpers
schema
Link to this section Pg Casts
Creates a native cast for:
bigint AS rational
Creates a native cast for:
rational AS double precision
Link to this section 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.
Link to this section Pg Operator Classes
Creates a B-tree operator class to support indexing on comparison operations.
Link to this section 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.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.
Link to this section 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:
1
if a > b0
if a == b-1
if a < b
Used to back equality operators.
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.
Just like DIV(real, real)
, DIV(rational, rational)
floors towards zero.
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.
Link to this section Functions
@spec function(atom(), Ecto.Repo.t()) :: String.t()
Returns the config-qualified name of the function for this type.