Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- type Migration = WriterT [ Text ] ( WriterT CautiousMigration ( ReaderT SqlBackend IO )) ()
- type CautiousMigration = [( Bool , Sql )]
- type Sql = Text
- showMigration :: ( HasCallStack , MonadIO m) => Migration -> ReaderT SqlBackend m [ Text ]
- parseMigration :: ( HasCallStack , MonadIO m) => Migration -> ReaderT SqlBackend m ( Either [ Text ] CautiousMigration )
- parseMigration' :: ( HasCallStack , MonadIO m) => Migration -> ReaderT SqlBackend m CautiousMigration
- printMigration :: ( HasCallStack , MonadIO m) => Migration -> ReaderT SqlBackend m ()
- getMigration :: ( MonadIO m, HasCallStack ) => Migration -> ReaderT SqlBackend m [ Sql ]
- runMigration :: MonadIO m => Migration -> ReaderT SqlBackend m ()
- runMigrationQuiet :: MonadIO m => Migration -> ReaderT SqlBackend m [ Text ]
- runMigrationSilent :: MonadUnliftIO m => Migration -> ReaderT SqlBackend m [ Text ]
- runMigrationUnsafe :: MonadIO m => Migration -> ReaderT SqlBackend m ()
- runMigrationUnsafeQuiet :: ( HasCallStack , MonadIO m) => Migration -> ReaderT SqlBackend m [ Text ]
- migrate :: [ EntityDef ] -> EntityDef -> Migration
- reportErrors :: [ Text ] -> Migration
- reportError :: Text -> Migration
- addMigrations :: CautiousMigration -> Migration
- addMigration :: Bool -> Sql -> Migration
- runSqlCommand :: SqlPersistT IO () -> Migration
- newtype PersistUnsafeMigrationException = PersistUnsafeMigrationException [( Bool , Sql )]
Types
type Migration = WriterT [ Text ] ( WriterT CautiousMigration ( ReaderT SqlBackend IO )) () Source #
A
Migration
is a four level monad stack consisting of:
-
WriterT
[Text
] -
WriterT
CautiousMigration
-
ReaderT
SqlBackend
SqlPersistT
transformer for database interop. -
IO
type CautiousMigration = [( Bool , Sql )] Source #
Using a
Migration
showMigration :: ( HasCallStack , MonadIO m) => Migration -> ReaderT SqlBackend m [ Text ] Source #
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 #
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
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 = dorunSqlCommand
$rawExecute_
"CREATE EXTENSION IF NOT EXISTS "citext";" migrateAll
For raw commands, you can also just write
addMigration
:
migration ::Migration
migration = doaddMigration
"CREATE EXTENSION IF NOT EXISTS "citext";" migrateAll
addMigrations :: CautiousMigration -> Migration Source #
Add a
CautiousMigration
(aka a
[(
) to the
migration plan.
Bool
,
Text
)]
Since: 2.9.2
:: Bool |
Is the migration unsafe to run? (eg a destructive or non-idempotent
update on the schema). If
|
-> Sql |
A
|
-> 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
PersistUnsafeMigrationException [( Bool , Sql )] |
Instances
Show PersistUnsafeMigrationException Source # |
This
|
Defined in Database.Persist.Sql.Migration |
|
Exception PersistUnsafeMigrationException Source # | |