module Database.Migrant.Run
( migrate
, unsafeMigrate
, executePlan
, plan
, makePlan
, MigrationDirection (..)
)
where
import Database.Migrant.Driver.Class
import Database.Migrant.MigrationName
import Control.Monad (forM_)
data MigrationDirection
= MigrateUp
| MigrateDown
deriving (Int -> MigrationDirection -> ShowS
[MigrationDirection] -> ShowS
MigrationDirection -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MigrationDirection] -> ShowS
$cshowList :: [MigrationDirection] -> ShowS
show :: MigrationDirection -> String
$cshow :: MigrationDirection -> String
showsPrec :: Int -> MigrationDirection -> ShowS
$cshowsPrec :: Int -> MigrationDirection -> ShowS
Show, MigrationDirection -> MigrationDirection -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MigrationDirection -> MigrationDirection -> Bool
$c/= :: MigrationDirection -> MigrationDirection -> Bool
== :: MigrationDirection -> MigrationDirection -> Bool
$c== :: MigrationDirection -> MigrationDirection -> Bool
Eq, Eq MigrationDirection
MigrationDirection -> MigrationDirection -> Bool
MigrationDirection -> MigrationDirection -> Ordering
MigrationDirection -> MigrationDirection -> MigrationDirection
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: MigrationDirection -> MigrationDirection -> MigrationDirection
$cmin :: MigrationDirection -> MigrationDirection -> MigrationDirection
max :: MigrationDirection -> MigrationDirection -> MigrationDirection
$cmax :: MigrationDirection -> MigrationDirection -> MigrationDirection
>= :: MigrationDirection -> MigrationDirection -> Bool
$c>= :: MigrationDirection -> MigrationDirection -> Bool
> :: MigrationDirection -> MigrationDirection -> Bool
$c> :: MigrationDirection -> MigrationDirection -> Bool
<= :: MigrationDirection -> MigrationDirection -> Bool
$c<= :: MigrationDirection -> MigrationDirection -> Bool
< :: MigrationDirection -> MigrationDirection -> Bool
$c< :: MigrationDirection -> MigrationDirection -> Bool
compare :: MigrationDirection -> MigrationDirection -> Ordering
$ccompare :: MigrationDirection -> MigrationDirection -> Ordering
Ord, Int -> MigrationDirection
MigrationDirection -> Int
MigrationDirection -> [MigrationDirection]
MigrationDirection -> MigrationDirection
MigrationDirection -> MigrationDirection -> [MigrationDirection]
MigrationDirection
-> MigrationDirection -> MigrationDirection -> [MigrationDirection]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: MigrationDirection
-> MigrationDirection -> MigrationDirection -> [MigrationDirection]
$cenumFromThenTo :: MigrationDirection
-> MigrationDirection -> MigrationDirection -> [MigrationDirection]
enumFromTo :: MigrationDirection -> MigrationDirection -> [MigrationDirection]
$cenumFromTo :: MigrationDirection -> MigrationDirection -> [MigrationDirection]
enumFromThen :: MigrationDirection -> MigrationDirection -> [MigrationDirection]
$cenumFromThen :: MigrationDirection -> MigrationDirection -> [MigrationDirection]
enumFrom :: MigrationDirection -> [MigrationDirection]
$cenumFrom :: MigrationDirection -> [MigrationDirection]
fromEnum :: MigrationDirection -> Int
$cfromEnum :: MigrationDirection -> Int
toEnum :: Int -> MigrationDirection
$ctoEnum :: Int -> MigrationDirection
pred :: MigrationDirection -> MigrationDirection
$cpred :: MigrationDirection -> MigrationDirection
succ :: MigrationDirection -> MigrationDirection
$csucc :: MigrationDirection -> MigrationDirection
Enum, MigrationDirection
forall a. a -> a -> Bounded a
maxBound :: MigrationDirection
$cmaxBound :: MigrationDirection
minBound :: MigrationDirection
$cminBound :: MigrationDirection
Bounded)
plan :: Driver d
=> [MigrationName]
-> d
-> IO [(MigrationDirection, MigrationName)]
plan :: forall d.
Driver d =>
[MigrationName] -> d -> IO [(MigrationDirection, MigrationName)]
plan [MigrationName]
target d
driver = do
[MigrationName]
current <- forall d. Driver d => d -> IO [MigrationName]
getMigrations d
driver
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ [MigrationName]
-> [MigrationName] -> [(MigrationDirection, MigrationName)]
makePlan [MigrationName]
target [MigrationName]
current
makePlan :: [MigrationName]
-> [MigrationName]
-> [(MigrationDirection, MigrationName)]
makePlan :: [MigrationName]
-> [MigrationName] -> [(MigrationDirection, MigrationName)]
makePlan [] []
= []
makePlan [] [MigrationName]
xs
= [(MigrationDirection
MigrateDown, MigrationName
n) | MigrationName
n <- [MigrationName]
xs]
makePlan [MigrationName]
xs []
= [(MigrationDirection
MigrateUp, MigrationName
n) | MigrationName
n <- [MigrationName]
xs]
makePlan (MigrationName
t:[MigrationName]
ts) (MigrationName
c:[MigrationName]
cs)
| MigrationName
t forall a. Eq a => a -> a -> Bool
== MigrationName
c
= [MigrationName]
-> [MigrationName] -> [(MigrationDirection, MigrationName)]
makePlan [MigrationName]
ts [MigrationName]
cs
| Bool
otherwise
= (MigrationDirection
MigrateDown, MigrationName
c)forall a. a -> [a] -> [a]
:[MigrationName]
-> [MigrationName] -> [(MigrationDirection, MigrationName)]
makePlan (MigrationName
tforall a. a -> [a] -> [a]
:[MigrationName]
ts) [MigrationName]
cs
executePlan :: Driver d
=> [(MigrationDirection, MigrationName)]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
executePlan :: forall d.
Driver d =>
[(MigrationDirection, MigrationName)]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
executePlan [(MigrationDirection, MigrationName)]
migrationPlan MigrationName -> d -> IO ()
up MigrationName -> d -> IO ()
down d
driver = do
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(MigrationDirection, MigrationName)]
migrationPlan forall a b. (a -> b) -> a -> b
$ \(MigrationDirection
direction, MigrationName
name) -> do
let (MigrationName -> d -> IO ()
run, MigrationName -> d -> IO ()
mark) = case MigrationDirection
direction of
MigrationDirection
MigrateUp -> (MigrationName -> d -> IO ()
up, forall d. Driver d => MigrationName -> d -> IO ()
markUp)
MigrationDirection
MigrateDown -> (MigrationName -> d -> IO ()
down, forall d. Driver d => MigrationName -> d -> IO ()
markDown)
MigrationName -> d -> IO ()
run MigrationName
name d
driver
MigrationName -> d -> IO ()
mark MigrationName
name d
driver
migrate :: Driver d
=> [MigrationName]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
migrate :: forall d.
Driver d =>
[MigrationName]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
migrate [MigrationName]
target MigrationName -> d -> IO ()
up MigrationName -> d -> IO ()
down d
driver =
forall d a. Driver d => (d -> IO a) -> d -> IO a
withTransaction (forall d.
Driver d =>
[MigrationName]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
unsafeMigrate [MigrationName]
target MigrationName -> d -> IO ()
up MigrationName -> d -> IO ()
down) d
driver
unsafeMigrate :: Driver d
=> [MigrationName]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
unsafeMigrate :: forall d.
Driver d =>
[MigrationName]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
unsafeMigrate [MigrationName]
target MigrationName -> d -> IO ()
up MigrationName -> d -> IO ()
down d
driver = do
forall d. Driver d => d -> IO ()
initMigrations d
driver
[(MigrationDirection, MigrationName)]
migrationPlan <- forall d.
Driver d =>
[MigrationName] -> d -> IO [(MigrationDirection, MigrationName)]
plan [MigrationName]
target d
driver
forall d.
Driver d =>
[(MigrationDirection, MigrationName)]
-> (MigrationName -> d -> IO ())
-> (MigrationName -> d -> IO ())
-> d
-> IO ()
executePlan [(MigrationDirection, MigrationName)]
migrationPlan MigrationName -> d -> IO ()
up MigrationName -> d -> IO ()
down d
driver