-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ian Ross
committed
Oct 18, 2017
1 parent
7a29919
commit 011c6a3
Showing
5 changed files
with
189 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ author: Amit Levy and David Mazieres | |
maintainer: [email protected] | ||
category: Database | ||
synopsis: An ORM (Object Relational Mapping) and migrations DSL for PostgreSQL. | ||
data-files: man/man1/pg_migrate.1 man/man5/pg_migrate.5 static/migration.hs | ||
data-files: man/man1/pg_migrate.1 man/man5/pg_migrate.5 static/migration.hs static/CompilerUtils.hs | ||
description: | ||
An ORM (Object Relational Mapping) and migrations DSL for PostgreSQL. See | ||
"Database.PostgreSQL.ORM" for documentation. | ||
|
@@ -33,10 +33,12 @@ library | |
, directory | ||
, filepath | ||
, ghc-prim | ||
, haskell-src-exts | ||
, mtl | ||
, old-locale | ||
, postgresql-simple | ||
, process | ||
, temporary | ||
, text | ||
, time | ||
, transformers | ||
|
@@ -72,4 +74,3 @@ library | |
source-repository head | ||
type: git | ||
location: https://github.com/alevy/postgresql-orm.git | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-} | ||
module CompilerUtils ( | ||
fromList, Migration(..), MigrationMap, compiledMain | ||
) where | ||
|
||
import Control.Exception (SomeException, catch) | ||
import Control.Monad (forM_, void) | ||
import Data.Map (Map, fromList) | ||
import qualified Data.Map as M | ||
import Database.PostgreSQL.Simple | ||
(Connection, Only(..), execute, query_, begin, commit) | ||
import Database.PostgreSQL.Migrations (connectEnv) | ||
import System.Environment (getArgs) | ||
|
||
type Version = String | ||
|
||
data Migration = Migration { migName :: String | ||
, migUp :: Connection -> IO () | ||
, migDown :: Connection -> IO () | ||
} | ||
|
||
type MigrationMap = Map Version Migration | ||
|
||
compiledMain :: MigrationMap -> IO () | ||
compiledMain migrations = do | ||
args <- getArgs | ||
case args of | ||
"list":[] -> listMigrations migrations | ||
"migrate":[] -> runMigrations migrations | ||
"rollback":[] -> runRollback migrations | ||
|
||
listMigrations :: MigrationMap -> IO () | ||
listMigrations migrations = | ||
forM_ (M.toAscList migrations) $ \(_, Migration name _ _) -> putStrLn name | ||
|
||
-- | Runs all new migrations in a given directory and dumps the | ||
-- resulting schema to a file \"schema.sql\" in the migrations | ||
-- directory. | ||
-- | ||
-- Determining which migrations to run is done by querying the database for the | ||
-- largest version in the /schema_migrations/ table, and choosing all | ||
-- migrations in the given directory with higher versions. | ||
runMigrations :: MigrationMap -> IO () | ||
runMigrations migrationsIn = do | ||
conn <- connectEnv | ||
res <- query_ conn | ||
"select version from schema_migrations order by version desc limit 1" | ||
let latestVersion = case res of | ||
[] -> "" | ||
(Only latest):_ -> latest | ||
let migrations = M.toAscList $ | ||
M.filterWithKey (\k _ -> k > latestVersion) migrationsIn | ||
forM_ migrations (doone conn) | ||
where doone conn (version, Migration name up down) = do | ||
putStrLn $ "=== Running Migration " ++ name | ||
ok <- catch | ||
(do | ||
begin conn | ||
void $ execute conn "insert into schema_migrations values(?)" | ||
(Only version) | ||
up conn | ||
commit conn | ||
return True) | ||
(\(e :: SomeException) -> return False) | ||
if ok | ||
then putStrLn "=== Success" | ||
else putStrLn "=== Migration Failed!" | ||
|
||
runRollback :: MigrationMap -> IO () | ||
runRollback migrations = do | ||
conn <- connectEnv | ||
res <- query_ conn | ||
"select version from schema_migrations order by version desc limit 1" | ||
case res of | ||
[] -> putStrLn "=== DB Fully Rolled Back!" | ||
(Only latest):_ -> do | ||
let (Migration name _ down) = migrations M.! latest | ||
putStrLn $ "=== Running Rollback " ++ name | ||
ok <- catch | ||
(do | ||
begin conn | ||
down conn | ||
void $ execute conn "delete from schema_migrations where version = ?" | ||
(Only latest) | ||
commit conn | ||
return True) | ||
(\(e :: SomeException) -> return False) | ||
if ok | ||
then putStrLn "=== Success" | ||
else putStrLn "=== Migration Failed!" |