| Safe Haskell | None |
|---|
Database.PostgreSQL.Migrations
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"
- defaultMain :: (Connection -> IO ()) -> (Connection -> IO ()) -> IO ()
- connectEnv :: IO Connection
- runSqlFile :: FilePath -> Migration ()
- type Migration = ReaderT Connection IO
- migrate :: Migration a -> Connection -> IO ()
- column :: ByteString -> ByteString -> ByteString
- create_table :: ByteString -> [ByteString] -> Migration Int64
- add_column :: ByteString -> ByteString -> ByteString -> Migration Int64
- create_index :: ByteString -> ByteString -> [ByteString] -> Migration Int64
- create_unique_index :: ByteString -> ByteString -> [ByteString] -> Migration Int64
- drop_table :: ByteString -> Migration Int64
- drop_column :: ByteString -> ByteString -> Migration Int64
- drop_index :: ByteString -> Migration Int64
- rename_column :: ByteString -> ByteString -> ByteString -> Migration Int64
- change_column :: ByteString -> ByteString -> ByteString -> Migration Int64
- create_table_stmt :: ByteString -> [ByteString] -> Query
- add_column_stmt :: ByteString -> ByteString -> ByteString -> Query
- create_index_stmt :: Bool -> ByteString -> ByteString -> [ByteString] -> Query
- drop_table_stmt :: ByteString -> Query
- drop_column_stmt :: ByteString -> ByteString -> Query
- drop_index_stmt :: ByteString -> Query
- rename_column_stmt :: ByteString -> ByteString -> ByteString -> Query
- change_column_stmt :: ByteString -> ByteString -> ByteString -> Query
Utilities
Arguments
| :: (Connection -> IO ()) | Migration function |
| -> (Connection -> IO ()) | Rollback function |
| -> IO () |
connectEnv :: IO ConnectionSource
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
type Migration = ReaderT Connection IOSource
migrate :: Migration a -> Connection -> IO ()Source
Arguments
| :: ByteString | name |
| -> ByteString | type, definition, constraints |
| -> ByteString |
Returns a column defition by quoting the given name
Adding
Arguments
| :: ByteString | Table name |
| -> [ByteString] | Column definitions |
| -> Migration Int64 |
Creates a table. See column for constructing the column list.
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".
Arguments
| :: ByteString | Index name |
| -> ByteString | Table name |
| -> [ByteString] | Column names |
| -> Migration Int64 |
Creates an index for efficient lookup.
Arguments
| :: ByteString | Index name |
| -> ByteString | Table name |
| -> [ByteString] | Column names |
| -> Migration Int64 |
Creates a unique index for efficient lookup.
Removing
drop_table :: ByteString -> Migration Int64Source
Drops a table
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.
Modifying
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".
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
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"]
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);
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 -> QuerySource
Returns a Query that drops a table
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";
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"
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";
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;