HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulepersistent-2.14.6.3Haskell2010

Database.Persist.Sql.Migration

This module documents tools and utilities for running SQL migrations.

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

  • 4 types
  • 16 values

Types

3 declarations
typetype CautiousMigration = [(Bool, Sql)]
#

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.

Using a Migration

11 declarations

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!

Utilities for constructing migrations

5 declarations

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, Database.Persist.TH.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
valueaddMigration
  1. :: 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.

  2. -> Sql

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

  3. -> Migration
#

Add a migration to the migration plan.

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";" []

If something goes wrong...

1 declaration

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.

Instances2Show, Exception
  • Show PersistUnsafeMigrationExceptionDefined in persistent-2.14.6.3 · Database.Persist.Sql.Migration

    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.

  • Exception PersistUnsafeMigrationExceptionDefined in persistent-2.14.6.3 · Database.Persist.Sql.Migration