orville-postgresql-1.0.0.0: A Haskell library for PostgreSQL
CopyrightFlipstone Technology Partners 2023
LicenseMIT
StabilityStable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Orville.PostgreSQL.Marshall.DefaultValue

Description

Since: 1.0.0.0

Synopsis

Documentation

data DefaultValue a Source #

A DefaultValue is a SQL expression that can be attached to a field definition to give a default value for a column at the database level. The default value will be used if an insert is done and the column is not provided.

This is useful if you want to add a new column to a table that is already in production without breaking a previous version of your application that is running (e.g. during a zero-down-time deployment) and without needing to make the new column nullable. Default values can also be used to create database-assigned values such as using now() to set a created_at column on a row automatically in the database.

Since: 1.0.0.0

integerDefault :: Int32 -> DefaultValue Int32 Source #

Builds a default value from an Int32 for use with integer fields.

This is a specialization of integerDefault.

Since: 1.0.0.0

smallIntegerDefault :: Int16 -> DefaultValue Int16 Source #

Builds a default value from an Int16 for use with small integer fields.

This is a specialization of integerDefault.

Since: 1.0.0.0

bigIntegerDefault :: Int64 -> DefaultValue Int64 Source #

Builds a default value from an Int16 for use with big integer fields.

This is a specialization of integerDefault.

Since: 1.0.0.0

integralDefault :: Integral n => n -> DefaultValue n Source #

Builds a default value for any Integral type n by converting it to an Integer.

Since: 1.0.0.0

doubleDefault :: Double -> DefaultValue Double Source #

Builds a default value from a Double field for use with double fields.

Since: 1.0.0.0

booleanDefault :: Bool -> DefaultValue Bool Source #

Builds a default value from a Bool, for use with boolean fields.

Since: 1.0.0.0

textDefault :: Text -> DefaultValue Text Source #

Builds a default value from a Text, for use with unbounded, bounded and fixed-length text fields.

Since: 1.0.0.0

dateDefault :: Day -> DefaultValue Day Source #

Builds a default value from a Day for use with date fields.

Since: 1.0.0.0

currentDateDefault :: DefaultValue Day Source #

Builds a default value that will default to the current date (i.e. the date at which the database populates the default value on a given row).

For use with date fields.

Since: 1.0.0.0

utcTimestampDefault :: UTCTime -> DefaultValue UTCTime Source #

Builds a default value from a UTCTime for use with UTC timestamp fields.

Since: 1.0.0.0

currentUTCTimestampDefault :: DefaultValue UTCTime Source #

Builds a default value that will default to the current UTC time (i.e. the time at which the database populates the default value on a given row).

For use with UTC timestamp fields.

Since: 1.0.0.0

localTimestampDefault :: LocalTime -> DefaultValue LocalTime Source #

Builds a default value from a LocalTime for use with local timestamp fields.

Since: 1.0.0.0

currentLocalTimestampDefault :: DefaultValue LocalTime Source #

Builds a default value that will default to the current local time (i.e. the time at which the database populates the default value on a given row).

Note: "local" time here will be determined by the database itself, subject to whatever timezone offset has been configured in its settings.

For use with local timestamp fields.

Since: 1.0.0.0

coerceDefaultValue :: DefaultValue a -> DefaultValue b Source #

Coerces a DefaultValue so that it can be used with field definitions of a different Haskell type. The coercion will always succeed, and is safe as far as Haskell itself is concerned. As long as the DefaultValue is used with a column whose database type is the same as the one the DefaultValue was originally intended for, everything will work as expected.

Since: 1.0.0.0

defaultValueExpression :: DefaultValue a -> ValueExpression Source #

Returns a database value expression for the default value.

Since: 1.0.0.0

rawSqlDefault :: ValueExpression -> DefaultValue a Source #

Constructs a default value from a ValueExpression. You can use this to construct default values for any SQL expression that Orville does not support directly.

Note: If you are using auto-migrations, the ValueExpression that you pass here must match what is returned by the PostgreSQL pg_get_expr function. pg_get_expr decompiles the compiled version of the default experssion back to source text, sometimes in non-obvious ways. Orville's auto-migration compares the expression given in the field definition with the decompiled expression from the database to determine whether the default value needs to be updated in the schema or not. If the expression given by a DefaultValue is logically equivalent but does not match the decompiled form, auto-migration will continue to execute SQL statements to update the schema even when it does not need to.

Since: 1.0.0.0