Skip to content

Instantly share code, notes, and snippets.

@nikita-volkov
Last active September 21, 2019 22:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikita-volkov/57212aedbabeed0b5ff2 to your computer and use it in GitHub Desktop.
Save nikita-volkov/57212aedbabeed0b5ff2 to your computer and use it in GitHub Desktop.
Hasql Database Migration Algorithm
-- |
-- 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