postgresql-orm-0.4.0: An ORM (Object Relational Mapping) and migrations DSL for PostgreSQL.

Safe HaskellNone
LanguageHaskell2010

Database.PostgreSQL.Migrations

Contents

Description

Functions to help with building database migrations.

Most users will want to create a database migration using defaultMain as follows,

import Database.PostgreSQL.Migrations

main = defaultMain up down

up = migrate $ do
      create_table "posts"
        [ column "title" "VARCHAR(255) NOT NULL"
        , column "author_id" "integer references authors(id)"]

down = migrate $ drop_table "posts"

Synopsis

Utilities

defaultMain Source

Arguments

:: (Connection -> IO ())

Migration function

-> (Connection -> IO ())

Rollback function

-> IO () 

connectEnv :: IO Connection Source

Creates a PostgreSQL Connection using the DATABASE_URL environment variable, if it exists. If it does, it should match the format:

  postgresql://[[USERNAME@PASSWORD]HOSTNAME[:PORT]]/[DBNAME]

If it is not present, the environment variables PG_DBNAME PG_HOST etc, are used.

runSqlFile :: FilePath -> Migration () Source

Runs the SQL file at the given path, relative to the current working directory.

DSL

column Source

Arguments

:: ByteString

name

-> ByteString

type, definition, constraints

-> ByteString 

Returns a column defition by quoting the given name

Adding

create_table Source

Arguments

:: ByteString

Table name

-> [ByteString]

Column definitions

-> Migration Int64 

Creates a table. See column for constructing the column list.

add_column Source

Arguments

:: ByteString

Table name

-> ByteString

Column name

-> ByteString

Column definition

-> Migration Int64 

Adds a column to the given table. For example,

  add_column "posts" "title" "VARCHAR(255)"

adds a varchar column called "title" to the table "posts".

create_index Source

Arguments

:: ByteString

Index name

-> ByteString

Table name

-> [ByteString]

Column names

-> Migration Int64 

Creates an index for efficient lookup.

create_unique_index Source

Arguments

:: ByteString

Index name

-> ByteString

Table name

-> [ByteString]

Column names

-> Migration Int64 

Creates a unique index for efficient lookup.

Removing

drop_column Source

Arguments

:: ByteString

Table name

-> ByteString

Column name

-> Migration Int64 

Drops a column from the given table. For example,

  drop_column "posts" "title"

drops the column "title" from the "posts" table.

drop_index Source

Arguments

:: ByteString

Index name

-> Migration Int64 

Drops an index.

Modifying

rename_column Source

Arguments

:: ByteString

Table name

-> ByteString

Old column name

-> ByteString

New column name

-> Migration Int64 

Renames a column in the given table. For example,

  rename_column "posts" "title" "name"

renames the column "title" in the "posts" table to "name".

change_column Source

Arguments

:: ByteString

Table name

-> ByteString

Column name

-> ByteString

Action

-> Migration Int64 

Alters a column in the given table. For example,

  change_column "posts" "title" "DROP DEFAULT"

drops the default constraint for the "title" column in the "posts" table.

Statements

create_table_stmt Source

Arguments

:: ByteString

Table name

-> [ByteString]

Column definitions

-> Query 

Returns a Query that creates a table, for example:

  create_table "posts"
    [ column "title" "VARCHAR(255) NOT NULL"
    , column "body"  "text"]

add_column_stmt Source

Arguments

:: ByteString

Table name

-> ByteString

Column name

-> ByteString

Column definition

-> Query 

Returns a Query that adds a column to the given table. For example,

  add_column "posts" "title" "VARCHAR(255)"

Returns the query

  ALTER TABLE "posts" add "title" VARCHAR(255);

create_index_stmt Source

Arguments

:: Bool

Unique index?

-> ByteString

Index name

-> ByteString

Table name

-> [ByteString]

Column names

-> Query 

Returns a Query that creates an index for the given columns on the given table. For example,

  create_index_stmt "post_owner_index" "posts" "owner"

Returns the query

  CREATE INDEX "post_owner_index" ON "posts" ("owner")

drop_table_stmt :: ByteString -> Query Source

Returns a Query that drops a table

drop_column_stmt Source

Arguments

:: ByteString

Table name

-> ByteString

Column name

-> Query 

Returns a Query that drops a column from the given table. For example,

  drop_column "posts" "title"

Returns the query

  ALTER TABLE "posts" add "title";

drop_index_stmt Source

Arguments

:: ByteString

Index name

-> Query 

Returns a Query that drops an index.

  drop_index_stmt "post_owner_index"

Returns the query

  DROP INDEX "post_owner_index"

rename_column_stmt Source

Arguments

:: ByteString

Table name

-> ByteString

Old column name

-> ByteString

New column name

-> Query 

Returns a Query that renames a column in the given table. For example,

  rename_column "posts" "title" "name"

Returns the query

  ALTER TABLE "posts" RENAME "title" TO "name";

change_column_stmt Source

Arguments

:: ByteString

Table name

-> ByteString

Column name

-> ByteString

Action

-> Query 

Returns a Query that alters a column in the given table. For example,

  change_column "posts" "title" "DROP DEFAULT"

Returns the query

  ALTER TABLE "posts" ALTER "title" DROP DEFAULT;