module Database.Persist.Sql.Migration
(
Migration
, CautiousMigration
, Sql
, showMigration
, parseMigration
, parseMigration'
, printMigration
, getMigration
, runMigration
, runMigrationQuiet
, runMigrationSilent
, runMigrationUnsafe
, runMigrationUnsafeQuiet
, migrate
, reportErrors
, reportError
, addMigrations
, addMigration
, runSqlCommand
, PersistUnsafeMigrationException(..)
) where
import Control.Exception (throwIO)
import Control.Monad (liftM, unless)
import Control.Monad.IO.Unlift
import Control.Monad.Trans.Class (MonadTrans(..))
import Control.Monad.Trans.Reader (ReaderT(..), ask)
import Control.Monad.Trans.Writer
import Data.Text (Text, isPrefixOf, pack, snoc, unpack)
import qualified Data.Text.IO
import GHC.Stack
import System.IO
import System.IO.Silently (hSilence)
import Database.Persist.Sql.Orphan.PersistStore ()
import Database.Persist.Sql.Raw
import Database.Persist.Sql.Types
import Database.Persist.Sql.Types.Internal
import Database.Persist.Types
import Control.Exception (Exception(..))
type Sql = Text
type CautiousMigration = [(Bool, Sql)]
type Migration = WriterT [Text] (WriterT CautiousMigration (ReaderT SqlBackend IO)) ()
allSql :: CautiousMigration -> [Sql]
allSql :: CautiousMigration -> [Text]
allSql = ((Bool, Text) -> Text) -> CautiousMigration -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Bool, Text) -> Text
forall a b. (a, b) -> b
snd
safeSql :: CautiousMigration -> [Sql]
safeSql :: CautiousMigration -> [Text]
safeSql = CautiousMigration -> [Text]
allSql (CautiousMigration -> [Text])
-> (CautiousMigration -> CautiousMigration)
-> CautiousMigration
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool, Text) -> Bool) -> CautiousMigration -> CautiousMigration
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> ((Bool, Text) -> Bool) -> (Bool, Text) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool, Text) -> Bool
forall a b. (a, b) -> a
fst)
parseMigration :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m (Either [Text] CautiousMigration)
parseMigration :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m (Either [Text] CautiousMigration)
parseMigration =
ReaderT SqlBackend IO (Either [Text] CautiousMigration)
-> ReaderT SqlBackend m (Either [Text] CautiousMigration)
forall {m :: * -> *} {r} {a}.
MonadIO m =>
ReaderT r IO a -> ReaderT r m a
liftIOReader (ReaderT SqlBackend IO (Either [Text] CautiousMigration)
-> ReaderT SqlBackend m (Either [Text] CautiousMigration))
-> (Migration
-> ReaderT SqlBackend IO (Either [Text] CautiousMigration))
-> Migration
-> ReaderT SqlBackend m (Either [Text] CautiousMigration)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (([Text], CautiousMigration) -> Either [Text] CautiousMigration)
-> ReaderT SqlBackend IO ([Text], CautiousMigration)
-> ReaderT SqlBackend IO (Either [Text] CautiousMigration)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ([Text], CautiousMigration) -> Either [Text] CautiousMigration
forall {a} {b}. ([a], b) -> Either [a] b
go (ReaderT SqlBackend IO ([Text], CautiousMigration)
-> ReaderT SqlBackend IO (Either [Text] CautiousMigration))
-> (Migration -> ReaderT SqlBackend IO ([Text], CautiousMigration))
-> Migration
-> ReaderT SqlBackend IO (Either [Text] CautiousMigration)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT CautiousMigration (ReaderT SqlBackend IO) [Text]
-> ReaderT SqlBackend IO ([Text], CautiousMigration)
forall w (m :: * -> *) a. WriterT w m a -> m (a, w)
runWriterT (WriterT CautiousMigration (ReaderT SqlBackend IO) [Text]
-> ReaderT SqlBackend IO ([Text], CautiousMigration))
-> (Migration
-> WriterT CautiousMigration (ReaderT SqlBackend IO) [Text])
-> Migration
-> ReaderT SqlBackend IO ([Text], CautiousMigration)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Migration
-> WriterT CautiousMigration (ReaderT SqlBackend IO) [Text]
forall (m :: * -> *) w a. Monad m => WriterT w m a -> m w
execWriterT
where
go :: ([a], b) -> Either [a] b
go ([], b
sql) = b -> Either [a] b
forall a b. b -> Either a b
Right b
sql
go ([a]
errs, b
_) = [a] -> Either [a] b
forall a b. a -> Either a b
Left [a]
errs
liftIOReader :: ReaderT r IO a -> ReaderT r m a
liftIOReader (ReaderT r -> IO a
m) = (r -> m a) -> ReaderT r m a
forall r (m :: * -> *) a. (r -> m a) -> ReaderT r m a
ReaderT ((r -> m a) -> ReaderT r m a) -> (r -> m a) -> ReaderT r m a
forall a b. (a -> b) -> a -> b
$ IO a -> m a
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> (r -> IO a) -> r -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> IO a
m
parseMigration' :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m CautiousMigration
parseMigration' :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m CautiousMigration
parseMigration' Migration
m = do
Either [Text] CautiousMigration
x <- Migration -> ReaderT SqlBackend m (Either [Text] CautiousMigration)
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m (Either [Text] CautiousMigration)
parseMigration Migration
m
case Either [Text] CautiousMigration
x of
Left [Text]
errs -> [Char] -> ReaderT SqlBackend m CautiousMigration
forall a. HasCallStack => [Char] -> a
error ([Char] -> ReaderT SqlBackend m CautiousMigration)
-> [Char] -> ReaderT SqlBackend m CautiousMigration
forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ (Text -> [Char]) -> [Text] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Text -> [Char]
unpack [Text]
errs
Right CautiousMigration
sql -> CautiousMigration -> ReaderT SqlBackend m CautiousMigration
forall a. a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. Monad m => a -> m a
return CautiousMigration
sql
printMigration :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m ()
printMigration :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m ()
printMigration Migration
m = Migration -> ReaderT SqlBackend m [Text]
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m [Text]
showMigration Migration
m
ReaderT SqlBackend m [Text]
-> ([Text] -> ReaderT SqlBackend m ()) -> ReaderT SqlBackend m ()
forall a b.
ReaderT SqlBackend m a
-> (a -> ReaderT SqlBackend m b) -> ReaderT SqlBackend m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Text -> ReaderT SqlBackend m ())
-> [Text] -> ReaderT SqlBackend m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (IO () -> ReaderT SqlBackend m ()
forall a. IO a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT SqlBackend m ())
-> (Text -> IO ()) -> Text -> ReaderT SqlBackend m ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> IO ()
Data.Text.IO.putStrLn)
showMigration :: (HasCallStack, MonadIO m) => Migration -> ReaderT SqlBackend m [Text]
showMigration :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m [Text]
showMigration Migration
m = (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ((Text -> Char -> Text) -> Char -> Text -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Char -> Text
snoc Char
';') ([Text] -> [Text])
-> ReaderT SqlBackend m [Text] -> ReaderT SqlBackend m [Text]
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Migration -> ReaderT SqlBackend m [Text]
forall (m :: * -> *).
(MonadIO m, HasCallStack) =>
Migration -> ReaderT SqlBackend m [Text]
getMigration Migration
m
getMigration :: (MonadIO m, HasCallStack) => Migration -> ReaderT SqlBackend m [Sql]
getMigration :: forall (m :: * -> *).
(MonadIO m, HasCallStack) =>
Migration -> ReaderT SqlBackend m [Text]
getMigration Migration
m = do
CautiousMigration
mig <- Migration -> ReaderT SqlBackend m CautiousMigration
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m CautiousMigration
parseMigration' Migration
m
[Text] -> ReaderT SqlBackend m [Text]
forall a. a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Text] -> ReaderT SqlBackend m [Text])
-> [Text] -> ReaderT SqlBackend m [Text]
forall a b. (a -> b) -> a -> b
$ CautiousMigration -> [Text]
allSql CautiousMigration
mig
runMigration :: MonadIO m
=> Migration
-> ReaderT SqlBackend m ()
runMigration :: forall (m :: * -> *).
MonadIO m =>
Migration -> ReaderT SqlBackend m ()
runMigration Migration
m = Migration -> Bool -> ReaderT SqlBackend m [Text]
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> Bool -> ReaderT SqlBackend m [Text]
runMigration' Migration
m Bool
False ReaderT SqlBackend m [Text]
-> ReaderT SqlBackend m () -> ReaderT SqlBackend m ()
forall a b.
ReaderT SqlBackend m a
-> ReaderT SqlBackend m b -> ReaderT SqlBackend m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> ReaderT SqlBackend m ()
forall a. a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
runMigrationQuiet :: MonadIO m
=> Migration
-> ReaderT SqlBackend m [Text]
runMigrationQuiet :: forall (m :: * -> *).
MonadIO m =>
Migration -> ReaderT SqlBackend m [Text]
runMigrationQuiet Migration
m = Migration -> Bool -> ReaderT SqlBackend m [Text]
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> Bool -> ReaderT SqlBackend m [Text]
runMigration' Migration
m Bool
True
runMigrationSilent :: MonadUnliftIO m
=> Migration
-> ReaderT SqlBackend m [Text]
runMigrationSilent :: forall (m :: * -> *).
MonadUnliftIO m =>
Migration -> ReaderT SqlBackend m [Text]
runMigrationSilent Migration
m = ((forall a. ReaderT SqlBackend m a -> IO a) -> IO [Text])
-> ReaderT SqlBackend m [Text]
forall b.
((forall a. ReaderT SqlBackend m a -> IO a) -> IO b)
-> ReaderT SqlBackend m b
forall (m :: * -> *) b.
MonadUnliftIO m =>
((forall a. m a -> IO a) -> IO b) -> m b
withRunInIO (((forall a. ReaderT SqlBackend m a -> IO a) -> IO [Text])
-> ReaderT SqlBackend m [Text])
-> ((forall a. ReaderT SqlBackend m a -> IO a) -> IO [Text])
-> ReaderT SqlBackend m [Text]
forall a b. (a -> b) -> a -> b
$ \forall a. ReaderT SqlBackend m a -> IO a
run ->
[Handle] -> IO [Text] -> IO [Text]
forall a. [Handle] -> IO a -> IO a
hSilence [Handle
stderr] (IO [Text] -> IO [Text]) -> IO [Text] -> IO [Text]
forall a b. (a -> b) -> a -> b
$ ReaderT SqlBackend m [Text] -> IO [Text]
forall a. ReaderT SqlBackend m a -> IO a
run (ReaderT SqlBackend m [Text] -> IO [Text])
-> ReaderT SqlBackend m [Text] -> IO [Text]
forall a b. (a -> b) -> a -> b
$ Migration -> Bool -> ReaderT SqlBackend m [Text]
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> Bool -> ReaderT SqlBackend m [Text]
runMigration' Migration
m Bool
True
runMigration'
:: (HasCallStack, MonadIO m)
=> Migration
-> Bool
-> ReaderT SqlBackend m [Text]
runMigration' :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> Bool -> ReaderT SqlBackend m [Text]
runMigration' Migration
m Bool
silent = do
CautiousMigration
mig <- Migration -> ReaderT SqlBackend m CautiousMigration
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m CautiousMigration
parseMigration' Migration
m
if ((Bool, Text) -> Bool) -> CautiousMigration -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (Bool, Text) -> Bool
forall a b. (a, b) -> a
fst CautiousMigration
mig
then IO [Text] -> ReaderT SqlBackend m [Text]
forall a. IO a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Text] -> ReaderT SqlBackend m [Text])
-> (PersistUnsafeMigrationException -> IO [Text])
-> PersistUnsafeMigrationException
-> ReaderT SqlBackend m [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PersistUnsafeMigrationException -> IO [Text]
forall e a. Exception e => e -> IO a
throwIO (PersistUnsafeMigrationException -> ReaderT SqlBackend m [Text])
-> PersistUnsafeMigrationException -> ReaderT SqlBackend m [Text]
forall a b. (a -> b) -> a -> b
$ CautiousMigration -> PersistUnsafeMigrationException
PersistUnsafeMigrationException CautiousMigration
mig
else (Text -> ReaderT SqlBackend m Text)
-> [Text] -> ReaderT SqlBackend m [Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Bool -> Text -> ReaderT SqlBackend m Text
forall (m :: * -> *).
MonadIO m =>
Bool -> Text -> ReaderT SqlBackend m Text
executeMigrate Bool
silent) ([Text] -> ReaderT SqlBackend m [Text])
-> [Text] -> ReaderT SqlBackend m [Text]
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
sortMigrations ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ CautiousMigration -> [Text]
safeSql CautiousMigration
mig
runMigrationUnsafe :: MonadIO m
=> Migration
-> ReaderT SqlBackend m ()
runMigrationUnsafe :: forall (m :: * -> *).
MonadIO m =>
Migration -> ReaderT SqlBackend m ()
runMigrationUnsafe Migration
m = Bool -> Migration -> ReaderT SqlBackend m [Text]
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Bool -> Migration -> ReaderT SqlBackend m [Text]
runMigrationUnsafe' Bool
False Migration
m ReaderT SqlBackend m [Text]
-> ReaderT SqlBackend m () -> ReaderT SqlBackend m ()
forall a b.
ReaderT SqlBackend m a
-> ReaderT SqlBackend m b -> ReaderT SqlBackend m b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> ReaderT SqlBackend m ()
forall a. a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
runMigrationUnsafeQuiet :: (HasCallStack, MonadIO m)
=> Migration
-> ReaderT SqlBackend m [Text]
runMigrationUnsafeQuiet :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m [Text]
runMigrationUnsafeQuiet = Bool -> Migration -> ReaderT SqlBackend m [Text]
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Bool -> Migration -> ReaderT SqlBackend m [Text]
runMigrationUnsafe' Bool
True
runMigrationUnsafe' :: (HasCallStack, MonadIO m)
=> Bool
-> Migration
-> ReaderT SqlBackend m [Text]
runMigrationUnsafe' :: forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Bool -> Migration -> ReaderT SqlBackend m [Text]
runMigrationUnsafe' Bool
silent Migration
m = do
CautiousMigration
mig <- Migration -> ReaderT SqlBackend m CautiousMigration
forall (m :: * -> *).
(HasCallStack, MonadIO m) =>
Migration -> ReaderT SqlBackend m CautiousMigration
parseMigration' Migration
m
(Text -> ReaderT SqlBackend m Text)
-> [Text] -> ReaderT SqlBackend m [Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (Bool -> Text -> ReaderT SqlBackend m Text
forall (m :: * -> *).
MonadIO m =>
Bool -> Text -> ReaderT SqlBackend m Text
executeMigrate Bool
silent) ([Text] -> ReaderT SqlBackend m [Text])
-> [Text] -> ReaderT SqlBackend m [Text]
forall a b. (a -> b) -> a -> b
$ [Text] -> [Text]
sortMigrations ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ CautiousMigration -> [Text]
allSql CautiousMigration
mig
executeMigrate :: MonadIO m => Bool -> Text -> ReaderT SqlBackend m Text
executeMigrate :: forall (m :: * -> *).
MonadIO m =>
Bool -> Text -> ReaderT SqlBackend m Text
executeMigrate Bool
silent Text
s = do
Bool -> ReaderT SqlBackend m () -> ReaderT SqlBackend m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
silent (ReaderT SqlBackend m () -> ReaderT SqlBackend m ())
-> ReaderT SqlBackend m () -> ReaderT SqlBackend m ()
forall a b. (a -> b) -> a -> b
$ IO () -> ReaderT SqlBackend m ()
forall a. IO a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ReaderT SqlBackend m ())
-> IO () -> ReaderT SqlBackend m ()
forall a b. (a -> b) -> a -> b
$ Handle -> [Char] -> IO ()
hPutStrLn Handle
stderr ([Char] -> IO ()) -> [Char] -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char]
"Migrating: " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
unpack Text
s
Text -> [PersistValue] -> ReaderT SqlBackend m ()
forall (m :: * -> *) backend.
(MonadIO m, BackendCompatible SqlBackend backend) =>
Text -> [PersistValue] -> ReaderT backend m ()
rawExecute Text
s []
Text -> ReaderT SqlBackend m Text
forall a. a -> ReaderT SqlBackend m a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
s
sortMigrations :: [Sql] -> [Sql]
sortMigrations :: [Text] -> [Text]
sortMigrations [Text]
x =
(Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter Text -> Bool
isCreate [Text]
x [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ (Text -> Bool) -> [Text] -> [Text]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Text -> Bool) -> Text -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Bool
isCreate) [Text]
x
where
isCreate :: Text -> Bool
isCreate Text
t = [Char] -> Text
pack [Char]
"CREATe " Text -> Text -> Bool
`isPrefixOf` Text
t
migrate :: [EntityDef]
-> EntityDef
-> Migration
migrate :: [EntityDef] -> EntityDef -> Migration
migrate [EntityDef]
allDefs EntityDef
val = do
SqlBackend
conn <- WriterT CautiousMigration (ReaderT SqlBackend IO) SqlBackend
-> WriterT
[Text]
(WriterT CautiousMigration (ReaderT SqlBackend IO))
SqlBackend
forall (m :: * -> *) a. Monad m => m a -> WriterT [Text] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WriterT CautiousMigration (ReaderT SqlBackend IO) SqlBackend
-> WriterT
[Text]
(WriterT CautiousMigration (ReaderT SqlBackend IO))
SqlBackend)
-> WriterT CautiousMigration (ReaderT SqlBackend IO) SqlBackend
-> WriterT
[Text]
(WriterT CautiousMigration (ReaderT SqlBackend IO))
SqlBackend
forall a b. (a -> b) -> a -> b
$ ReaderT SqlBackend IO SqlBackend
-> WriterT CautiousMigration (ReaderT SqlBackend IO) SqlBackend
forall (m :: * -> *) a.
Monad m =>
m a -> WriterT CautiousMigration m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift ReaderT SqlBackend IO SqlBackend
forall (m :: * -> *) r. Monad m => ReaderT r m r
ask
Either [Text] CautiousMigration
res <- IO (Either [Text] CautiousMigration)
-> WriterT
[Text]
(WriterT CautiousMigration (ReaderT SqlBackend IO))
(Either [Text] CautiousMigration)
forall a.
IO a
-> WriterT
[Text] (WriterT CautiousMigration (ReaderT SqlBackend IO)) a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Either [Text] CautiousMigration)
-> WriterT
[Text]
(WriterT CautiousMigration (ReaderT SqlBackend IO))
(Either [Text] CautiousMigration))
-> IO (Either [Text] CautiousMigration)
-> WriterT
[Text]
(WriterT CautiousMigration (ReaderT SqlBackend IO))
(Either [Text] CautiousMigration)
forall a b. (a -> b) -> a -> b
$ SqlBackend
-> [EntityDef]
-> (Text -> IO Statement)
-> EntityDef
-> IO (Either [Text] CautiousMigration)
connMigrateSql SqlBackend
conn [EntityDef]
allDefs (SqlBackend -> Text -> IO Statement
getStmtConn SqlBackend
conn) EntityDef
val
([Text] -> Migration)
-> (CautiousMigration -> Migration)
-> Either [Text] CautiousMigration
-> Migration
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either [Text] -> Migration
reportErrors CautiousMigration -> Migration
addMigrations Either [Text] CautiousMigration
res
reportError :: Text -> Migration
reportError :: Text -> Migration
reportError = [Text] -> Migration
forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
tell ([Text] -> Migration) -> (Text -> [Text]) -> Text -> Migration
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure
reportErrors :: [Text] -> Migration
reportErrors :: [Text] -> Migration
reportErrors = [Text] -> Migration
forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
tell
addMigration
:: Bool
-> Sql
-> Migration
addMigration :: Bool -> Text -> Migration
addMigration Bool
isUnsafe Text
sql = WriterT CautiousMigration (ReaderT SqlBackend IO) () -> Migration
forall (m :: * -> *) a. Monad m => m a -> WriterT [Text] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (CautiousMigration
-> WriterT CautiousMigration (ReaderT SqlBackend IO) ()
forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
tell [(Bool
isUnsafe, Text
sql)])
addMigrations
:: CautiousMigration
-> Migration
addMigrations :: CautiousMigration -> Migration
addMigrations = WriterT CautiousMigration (ReaderT SqlBackend IO) () -> Migration
forall (m :: * -> *) a. Monad m => m a -> WriterT [Text] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WriterT CautiousMigration (ReaderT SqlBackend IO) () -> Migration)
-> (CautiousMigration
-> WriterT CautiousMigration (ReaderT SqlBackend IO) ())
-> CautiousMigration
-> Migration
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CautiousMigration
-> WriterT CautiousMigration (ReaderT SqlBackend IO) ()
forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
tell
runSqlCommand :: SqlPersistT IO () -> Migration
runSqlCommand :: SqlPersistT IO () -> Migration
runSqlCommand = WriterT CautiousMigration (ReaderT SqlBackend IO) () -> Migration
forall (m :: * -> *) a. Monad m => m a -> WriterT [Text] m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WriterT CautiousMigration (ReaderT SqlBackend IO) () -> Migration)
-> (SqlPersistT IO ()
-> WriterT CautiousMigration (ReaderT SqlBackend IO) ())
-> SqlPersistT IO ()
-> Migration
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SqlPersistT IO ()
-> WriterT CautiousMigration (ReaderT SqlBackend IO) ()
forall (m :: * -> *) a.
Monad m =>
m a -> WriterT CautiousMigration m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift
newtype PersistUnsafeMigrationException
= PersistUnsafeMigrationException [(Bool, Sql)]
instance Show PersistUnsafeMigrationException where
show :: PersistUnsafeMigrationException -> [Char]
show (PersistUnsafeMigrationException CautiousMigration
mig) =
[[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat
[ [Char]
"\n\nDatabase migration: manual intervention required.\n"
, [Char]
"The unsafe actions are prefixed by '***' below:\n\n"
, [[Char]] -> [Char]
unlines ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ ((Bool, Text) -> [Char]) -> CautiousMigration -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (Bool, Text) -> [Char]
displayMigration CautiousMigration
mig
]
where
displayMigration :: (Bool, Sql) -> String
displayMigration :: (Bool, Text) -> [Char]
displayMigration (Bool
True, Text
s) = [Char]
"*** " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
unpack Text
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
";"
displayMigration (Bool
False, Text
s) = [Char]
" " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Text -> [Char]
unpack Text
s [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
";"
instance Exception PersistUnsafeMigrationException