Last active
September 21, 2019 22:15
-
-
Save nikita-volkov/57212aedbabeed0b5ff2 to your computer and use it in GitHub Desktop.
Hasql Database Migration Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | | |
-- Migrate from any version to any other higher version. | |
migrate :: (Word, Word) -> EitherT Text (Tx s) () | |
migrate = | |
\case | |
(0, 1) -> -- Execute statements, which create the DB in initial state. | |
(1, 2) -> -- Execute statements, which alter the DB to migrate from version 1 to 2. | |
(2, 3) -> -- Execute statements, which alter the DB to migrate from version 2 to 3. | |
(3, 4) -> throwError $ "Trying to migrate to an inexistent version" | |
(from, to) | |
| from == to -> return () | |
| from < to -> migrate (from, succ from) >> migrate (succ from, to) | |
| from > to -> throwError $ "Trying to migrate to an older version" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment