Skip to content
This repository was archived by the owner on Sep 20, 2021. It is now read-only.

Commit ae5b859

Browse files
committed
Add MigrationCommands
1 parent 7bd8d5f commit ae5b859

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

src/Database/PostgreSQL/Simple/Migration.hs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module Database.PostgreSQL.Simple.Migration
1919
(
2020
-- * Migration actions
2121
runMigration
22+
, runMigrations
23+
, sequenceMigrations
2224

2325
-- * Migration types
2426
, MigrationContext(..)
@@ -68,13 +70,43 @@ runMigration (MigrationContext cmd verbose con) = case cmd of
6870
MigrationInitialization ->
6971
initializeSchema con verbose >> return MigrationSuccess
7072
MigrationDirectory path ->
71-
executeDirectoryMigration con verbose path
73+
executeDirectoryMigration con verbose path
7274
MigrationScript name contents ->
7375
executeMigration con verbose name contents
7476
MigrationFile name path ->
7577
executeMigration con verbose name =<< BS.readFile path
7678
MigrationValidation validationCmd ->
7779
executeValidation con verbose validationCmd
80+
MigrationCommands commands ->
81+
runMigrations verbose con commands
82+
83+
-- | Execute a sequence of migrations
84+
--
85+
-- Returns 'MigrationSuccess' if all of the provided 'MigrationCommand's
86+
-- execute without error. If an error occurs, execution is stopped and the
87+
-- 'MigrationError' is returned.
88+
--
89+
-- It is recommended to wrap 'runMigrations' inside a database transaction.
90+
runMigrations
91+
:: Bool
92+
-- ^ Run in verbose mode
93+
-> Connection
94+
-- ^ The postgres connection to use
95+
-> [MigrationCommand]
96+
-- ^ The commands to run
97+
-> IO (MigrationResult String)
98+
runMigrations verbose con commands =
99+
sequenceMigrations [runMigration (MigrationContext c verbose con) | c <- commands]
100+
101+
-- | Run a sequence of contexts, stopping on the first failure
102+
sequenceMigrations :: Monad m => [m (MigrationResult e)] -> m (MigrationResult e)
103+
sequenceMigrations = \case
104+
[] -> pure MigrationSuccess
105+
c:cs -> do
106+
r <- c
107+
case r of
108+
MigrationError s -> pure (MigrationError s)
109+
MigrationSuccess -> sequenceMigrations cs
78110

79111
-- | Executes all SQL-file based migrations located in the provided 'dir'
80112
-- in alphabetical order.
@@ -140,6 +172,7 @@ initializeSchema con verbose = do
140172
-- * 'MigrationScript': validate the presence and checksum of the given script.
141173
-- * 'MigrationFile': validate the presence and checksum of the given file.
142174
-- * 'MigrationValidation': always succeeds.
175+
-- * 'MigrationCommands': validates all the sub-commands stopping at the first failure.
143176
executeValidation :: Connection -> Bool -> MigrationCommand -> IO (MigrationResult String)
144177
executeValidation con verbose cmd = case cmd of
145178
MigrationInitialization ->
@@ -154,6 +187,8 @@ executeValidation con verbose cmd = case cmd of
154187
validate name =<< BS.readFile path
155188
MigrationValidation _ ->
156189
return MigrationSuccess
190+
MigrationCommands cs ->
191+
sequenceMigrations (executeValidation con verbose <$> cs)
157192
where
158193
validate name contents =
159194
checkScript con name (md5Hash contents) >>= \case
@@ -222,8 +257,17 @@ data MigrationCommand
222257
-- ^ Executes a migration based on the provided bytestring.
223258
| MigrationValidation MigrationCommand
224259
-- ^ Validates the provided MigrationCommand.
260+
| MigrationCommands [MigrationCommand]
261+
-- ^ Performs a series of 'MigrationCommand's in sequence.
225262
deriving (Show, Eq, Read, Ord)
226263

264+
instance Monoid MigrationCommand where
265+
mempty = MigrationCommands []
266+
mappend (MigrationCommands xs) (MigrationCommands ys) = MigrationCommands (xs ++ ys)
267+
mappend (MigrationCommands xs) y = MigrationCommands (xs ++ [y])
268+
mappend x (MigrationCommands ys) = MigrationCommands (x : ys)
269+
mappend x y = MigrationCommands [x, y]
270+
227271
-- | A sum-type denoting the result of a single migration.
228272
data CheckScriptResult
229273
= ScriptOk

0 commit comments

Comments
 (0)