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

Commit 6fe9957

Browse files
committed
Add MigrationCommands
Closes #13
1 parent f249619 commit 6fe9957

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
@@ -22,6 +22,8 @@ module Database.PostgreSQL.Simple.Migration
2222
(
2323
-- * Migration actions
2424
runMigration
25+
, runMigrations
26+
, sequenceMigrations
2527

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

84116
-- | Executes all SQL-file based migrations located in the provided 'dir'
85117
-- in alphabetical order.
@@ -145,6 +177,7 @@ initializeSchema con verbose = do
145177
-- * 'MigrationScript': validate the presence and checksum of the given script.
146178
-- * 'MigrationFile': validate the presence and checksum of the given file.
147179
-- * 'MigrationValidation': always succeeds.
180+
-- * 'MigrationCommands': validates all the sub-commands stopping at the first failure.
148181
executeValidation :: Connection -> Bool -> MigrationCommand -> IO (MigrationResult String)
149182
executeValidation con verbose cmd = case cmd of
150183
MigrationInitialization ->
@@ -159,6 +192,8 @@ executeValidation con verbose cmd = case cmd of
159192
validate name =<< BS.readFile path
160193
MigrationValidation _ ->
161194
return MigrationSuccess
195+
MigrationCommands cs ->
196+
sequenceMigrations (executeValidation con verbose <$> cs)
162197
where
163198
validate name contents =
164199
checkScript con name (md5Hash contents) >>= \case
@@ -227,8 +262,17 @@ data MigrationCommand
227262
-- ^ Executes a migration based on the provided bytestring.
228263
| MigrationValidation MigrationCommand
229264
-- ^ Validates the provided MigrationCommand.
265+
| MigrationCommands [MigrationCommand]
266+
-- ^ Performs a series of 'MigrationCommand's in sequence.
230267
deriving (Show, Eq, Read, Ord)
231268

269+
instance Monoid MigrationCommand where
270+
mempty = MigrationCommands []
271+
mappend (MigrationCommands xs) (MigrationCommands ys) = MigrationCommands (xs ++ ys)
272+
mappend (MigrationCommands xs) y = MigrationCommands (xs ++ [y])
273+
mappend x (MigrationCommands ys) = MigrationCommands (x : ys)
274+
mappend x y = MigrationCommands [x, y]
275+
232276
-- | A sum-type denoting the result of a single migration.
233277
data CheckScriptResult
234278
= ScriptOk

0 commit comments

Comments
 (0)