Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Quiet variants of runMigration, functioning as workaround for the broken runMigrationSilent #971

Merged
merged 3 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions persistent/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog for persistent

## 2.10.2

* Added `runMigrationQuiet` and `runMigrationUnsafeQuiet` to `Database.Persist.Sql.Migration` as safer alternatives to `runMigrationSilent`. [#971](https://github.com/yesodweb/persistent/pull/971)

This functions as workaround/fix for: [#966](https://github.com/yesodweb/persistent/issues/966), [#948](https://github.com/yesodweb/persistent/issues/948), [#640](https://github.com/yesodweb/persistent/issues/640), and [#474](https://github.com/yesodweb/persistent/issues/474)

## 2.10.1

* Added `constraint=` attribute to allow users to specify foreign reference constraint names.
Expand Down
39 changes: 37 additions & 2 deletions persistent/Database/Persist/Sql/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ module Database.Persist.Sql.Migration
, showMigration
, getMigration
, runMigration
, runMigrationQuiet
, runMigrationSilent
, runMigrationUnsafe
, runMigrationUnsafeQuiet
, migrate
-- * Utilities for constructing migrations
, reportErrors
Expand Down Expand Up @@ -80,8 +82,26 @@ runMigration :: MonadIO m
-> ReaderT SqlBackend m ()
runMigration m = runMigration' m False >> return ()

-- | 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
runMigrationQuiet :: MonadIO m
=> Migration
-> ReaderT SqlBackend m [Text]
runMigrationQuiet m = runMigration' m True

-- | 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!
runMigrationSilent :: MonadUnliftIO m
=> Migration
-> ReaderT SqlBackend m [Text]
Expand Down Expand Up @@ -115,9 +135,24 @@ runMigration' m silent = do
runMigrationUnsafe :: MonadIO m
=> Migration
-> ReaderT SqlBackend m ()
runMigrationUnsafe m = do
runMigrationUnsafe m = runMigrationUnsafe' False m >> return ()

-- | Same as 'runMigrationUnsafe', but returns a list of the SQL commands
-- executed instead of printing them to stderr.
--
-- @since 2.10.2
runMigrationUnsafeQuiet :: MonadIO m
=> Migration
-> ReaderT SqlBackend m [Text]
runMigrationUnsafeQuiet = runMigrationUnsafe' True

runMigrationUnsafe' :: MonadIO m
=> Bool
-> Migration
-> ReaderT SqlBackend m [Text]
runMigrationUnsafe' silent m = do
mig <- parseMigration' m
mapM_ (executeMigrate False) $ sortMigrations $ allSql mig
mapM (executeMigrate silent) $ sortMigrations $ allSql mig

executeMigrate :: MonadIO m => Bool -> Text -> ReaderT SqlBackend m Text
executeMigrate silent s = do
Expand Down
2 changes: 1 addition & 1 deletion persistent/persistent.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent
version: 2.10.1
version: 2.10.2
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
Expand Down