persistent-2.13.2.1: Type-safe, multi-backend data serialization.
Safe HaskellNone
LanguageHaskell2010

Database.Persist.Sql.Migration

Description

This module documents tools and utilities for running SQL migrations.

A Migration is (currently) an alias for a WriterT of

Synopsis

Types

type Migration = WriterT [Text] (WriterT CautiousMigration (ReaderT SqlBackend IO)) () Source #

A Migration is a four level monad stack consisting of:

type CautiousMigration = [(Bool, Sql)] Source #

A list of SQL operations, marked with a safety flag. If the Bool is True, then the operation is *unsafe* - it might be destructive, or otherwise not idempotent. If the Bool is False, then the operation is *safe*, and can be run repeatedly without issues.

type Sql = Text Source #

Using a Migration

showMigration :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m [Text] Source #

Convert a Migration to a list of Text values corresponding to their Sql statements.

parseMigration :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m (Either [Text] CautiousMigration) Source #

Given a Migration, this parses it and returns either a list of errors associated with the migration or a list of migrations to do.

parseMigration' :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m CautiousMigration Source #

Like parseMigration, but instead of returning the value in an Either value, it calls error on the error values.

printMigration :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m () Source #

Prints a migration.

getMigration :: (MonadIO m, HasCallStack) => Migration -> ReaderT SqlBackend m [Sql] Source #

Return all of the Sql values associated with the given migration. Calls error if there's a parse error on any migration.

runMigration :: MonadIO m => Migration -> ReaderT SqlBackend m () Source #

Runs a migration. If the migration fails to parse or if any of the migrations are unsafe, then this throws a PersistUnsafeMigrationException.

runMigrationQuiet :: MonadIO m => Migration -> ReaderT SqlBackend m [Text] Source #

Same as runMigration, but does not report the individual migrations on stderr. Instead it returns a list of the executed SQL commands.

This is a safer/more robust alternative to runMigrationSilent, but may be less silent for some persistent implementations, most notably persistent-postgresql

Since: 2.10.2

runMigrationSilent :: MonadUnliftIO m => Migration -> ReaderT SqlBackend m [Text] Source #

Same as runMigration, but returns a list of the SQL commands executed instead of printing them to stderr.

This function silences the migration by remapping stderr. As a result, it is not thread-safe and can clobber output from other parts of the program. This implementation method was chosen to also silence postgresql migration output on stderr, but is not recommended!

runMigrationUnsafe :: MonadIO m => Migration -> ReaderT SqlBackend m () Source #

Like runMigration, but this will perform the unsafe database migrations instead of erroring out.

runMigrationUnsafeQuiet :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m [Text] Source #

Same as runMigrationUnsafe, but returns a list of the SQL commands executed instead of printing them to stderr.

Since: 2.10.2

migrate :: [EntityDef] -> EntityDef -> Migration Source #

Given a list of old entity definitions and a new EntityDef in val, this creates a Migration to update the old list of definitions with the new one.

Utilities for constructing migrations

While migrate is capable of creating a Migration for you, it's not the only way you can write migrations. You can use these utilities to write extra steps in your migrations.

As an example, let's say we want to enable the citext extension on postgres as part of our migrations.

share [mkPersist sqlSettings, mkMigration "migrateAll"] ...

migration :: Migration
migration = do
    runSqlCommand $
        rawExecute_ "CREATE EXTENSION IF NOT EXISTS "citext";"
    migrateAll

For raw commands, you can also just write addMigration:

migration :: Migration
migration = do
    addMigration "CREATE EXTENSION IF NOT EXISTS "citext";"
    migrateAll

reportErrors :: [Text] -> Migration Source #

Report multiple errors in a Migration.

Since: 2.9.2

reportError :: Text -> Migration Source #

Report a single error in a Migration.

Since: 2.9.2

addMigrations :: CautiousMigration -> Migration Source #

Add a CautiousMigration (aka a [(Bool, Text)]) to the migration plan.

Since: 2.9.2

addMigration Source #

Arguments

:: Bool

Is the migration unsafe to run? (eg a destructive or non-idempotent update on the schema). If True, the migration is *unsafe*, and will need to be run manually later. If False, the migration is *safe*, and can be run any number of times.

-> Sql

A Text value representing the command to run on the database.

-> Migration 

Add a migration to the migration plan.

Since: 2.9.2

runSqlCommand :: SqlPersistT IO () -> Migration Source #

Run an action against the database during a migration. Can be useful for eg creating Postgres extensions:

runSqlCommand $ rawExecute "CREATE EXTENSION IF NOT EXISTS "uuid-ossp";" []

Since: 2.13.0.0

If something goes wrong...

newtype PersistUnsafeMigrationException Source #

An exception indicating that Persistent refused to run some unsafe migrations. Contains a list of pairs where the Bool tracks whether the migration was unsafe (True means unsafe), and the Sql is the sql statement for the migration.

Since: 2.11.1.0

Instances

Instances details
Show PersistUnsafeMigrationException Source #

This Show instance renders an error message suitable for printing to the console. This is a little dodgy, but since GHC uses Show instances when displaying uncaught exceptions, we have little choice.

Instance details

Defined in Database.Persist.Sql.Migration

Exception PersistUnsafeMigrationException Source # 
Instance details

Defined in Database.Persist.Sql.Migration