postgres-embedded 0.1.5 → 0.1.6
raw patch · 8 files changed
+107/−42 lines, 8 filesdep +bytestringdep +postgresql-simplePVP: major bump suggested
API removals or changes: PVP suggests a major version bump
Dependencies added: bytestring, postgresql-simple
API changes (from Hackage documentation)
- Database.PostgreSQL.Embedded: [startupTimeout] :: StartupConfig -> Int
+ Database.PostgreSQL.Embedded: [cleanDir] :: StartupConfig -> Bool
- Database.PostgreSQL.Embedded: StartupConfig :: Version -> Int -> StartupConfig
+ Database.PostgreSQL.Embedded: StartupConfig :: Bool -> Version -> StartupConfig
Files
- CHANGELOG.md +7/−0
- LICENSE +1/−1
- README.md +2/−1
- postgres-embedded.cabal +16/−9
- src/Database/PostgreSQL/Embedded.hs +2/−0
- src/Database/PostgreSQL/Embedded/Postgres.hs +34/−18
- src/Database/PostgreSQL/Embedded/Types.hs +26/−12
- test/Spec.hs +19/−1
+ CHANGELOG.md view
@@ -0,0 +1,7 @@+## 0.1.6++Still unstable version for testing++## 0.1.5++Unstable version for testing
LICENSE view
@@ -1,4 +1,4 @@-Copyright Ilya Murzinov (c) 2017+Copyright (c) 2017 Ilya Murzinov All rights reserved.
README.md view
@@ -1,3 +1,4 @@-# postgres-embedded+# postgres-embedded [](https://hackage.haskell.org/package/postgres-embedded) [](https://travis-ci.org/ilya-murzinov/postgres-embedded) +[](https://ci.appveyor.com/project/ilya-murzinov/postgres-embedded) Haskell API for starting/stopping embedded PostgreSQL instance.
postgres-embedded.cabal view
@@ -1,17 +1,25 @@ name: postgres-embedded-version: 0.1.5+version: 0.1.6 description: Library for easily running embedded PostgreSQL server for tests+synopsis: Library for easily running embedded PostgreSQL server for tests homepage: https://github.com/ilya-murzinov/postgres-embedded+bug-reports: https://github.com/ilya-murzinov/postgres-embedded/issues license: MIT license-file: LICENSE author: Ilya Murzinov-maintainer: murz42@gmail.com+maintainer: Ilya Murzinov <murz42@gmail.com> copyright: 2017 Ilya Murzinov category: Lib build-type: Simple-extra-source-files: README.md-cabal-version: >=1.10+extra-doc-files: README.md+ CHANGELOG.md+cabal-version: >=1.18+tested-with: GHC==8.2.1, GHC==8.0.2, GHC==7.10.3, GHC==7.8.4, GHC==7.6.3, GHC==7.4.2 +source-repository head+ type: git+ location: https://github.com/ilya-murzinov/postgres-embedded.git+ library hs-source-dirs: src exposed-modules: Database.PostgreSQL.Embedded@@ -31,11 +39,10 @@ hs-source-dirs: test main-is: Spec.hs build-depends: base- , postgres-embedded+ , bytestring , filepath+ , postgres-embedded+ , postgresql-simple+ , shell-conduit ghc-options: -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010--source-repository head- type: git- location: git://github.com/ilya-murzinov/postgres-embedded.git
src/Database/PostgreSQL/Embedded.hs view
@@ -1,3 +1,5 @@+-- | Library for easily running embedded PostgreSQL server for tests+ module Database.PostgreSQL.Embedded ( startPostgres , stopPostgres
src/Database/PostgreSQL/Embedded/Postgres.hs view
@@ -4,52 +4,68 @@ , checkPostgresStarted ) where -import Control.Concurrent (threadDelay) import Control.Exception (SomeException, try) import Data.Conduit.Shell (rm, run, shell) import Data.List (isInfixOf) import Data.Monoid ((<>))+import System.Directory (doesPathExist)+import System.Exit (exitFailure) import System.FilePath.Posix ((</>)) import System.Info (os) import Database.PostgreSQL.Embedded.Download import Database.PostgreSQL.Embedded.Types +{-|+Starts PostgreSQL instance with given config.++Returns @RuntimeConfig@ that is required for @stopPostgres@.+-} startPostgres :: StartupConfig -> DBConfig -> IO RuntimeConfig-startPostgres (StartupConfig version_ t) dConfig@(DBConfig p u) = do+startPostgres (StartupConfig сlean version_) dConfig@(DBConfig p u) = do e <- downloadPostgres getOS version_ let d = e </> "data"+ exists <- doesPathExist d+ if exists && not сlean then do+ putStrLn $ "Directory " <> d <> " must not exist"+ exitFailure+ else run $ rm "-rf" d+ run $ do shell $ e </> "bin" </> "initdb" <> " -A trust -U " <> u <> " -D " <> d <> " -E UTF-8" shell $ e </> "bin" </> "pg_ctl" <> " -D " <> d <> " -o \"-F -p " <>- (show p) <> "\"" <> " -l " <> (e </> "log") <> " start"+ show p <> "\"" <> " -l " <> (e </> "log") <> " start"+ let r = RuntimeConfig e d- checkPostgresStarted r dConfig t- return $ r+ checkPostgresStarted r dConfig+ return r+ where getOS | "darwin" `isInfixOf` os = OSX | "linux" `isInfixOf` os = Linux | "win" `isInfixOf` os = Win | otherwise = error $ "Unsupported platform" <> os +{-|+Stops PostgreSQL instance.++Doesn't remove data directory.+-} stopPostgres :: RuntimeConfig -> IO ()-stopPostgres (RuntimeConfig e d) = run $ do- shell $ e </> "bin" </> "pg_ctl" <> " -D " <> d <> " stop"- rm "-rf" d+stopPostgres (RuntimeConfig e d) = run $+ shell $ e </> "bin" </> "pg_ctl" <> " -D " <> d <> " stop" <>+ " -m fast -t 5 -w" -checkPostgresStarted :: RuntimeConfig -> DBConfig -> Int -> IO ()-checkPostgresStarted (RuntimeConfig e _) (DBConfig p _) secs = checkPostgresStarted_ secs >>= \_ -> return ()+checkPostgresStarted :: RuntimeConfig -> DBConfig -> IO ()+checkPostgresStarted (RuntimeConfig e _) (DBConfig p _) = checkPostgresStarted_ 10 >>= \_ -> return () where checkPostgresStarted_ :: Int -> IO Bool checkPostgresStarted_ n = do- let oneSec = 1000000- let check = run $ shell (e </> "bin" </> "pg_isready" <> " -p " <> (show p) <> " -h localhost") >>= \_ -> return True+ let check = run $ shell (e </> "bin" </> "pg_isready" <> " -p " <> show p <> " -h localhost") >>= \_ -> return True res <- try check :: IO (Either SomeException Bool)- let retry = if n > 0 then threadDelay oneSec >>=- \_ -> checkPostgresStarted_ (n - 1)- else- return False+ let retry = if n > 0 then checkPostgresStarted_ (n - 1)+ else return False case res of- Left err -> print err >>= \_ -> retry- Right res1 -> if (not res1) then retry else return True+ Left err -> print err >>= const retry+ Right res1 -> if not res1 then retry else return True
src/Database/PostgreSQL/Embedded/Types.hs view
@@ -6,21 +6,35 @@ , DBConfig(..) ) where +-- | Type of operation system+-- Doesn't really work for Windows now data Os = Win | OSX | Linux deriving (Eq) +-- | Version of PostgreSQL distribution+-- See https://www.enterprisedb.com/downloads/postgres-postgresql-downloads for supported versions newtype Version = Version { value :: String } -data StartupConfig = StartupConfig {- version :: Version,- startupTimeout :: Int-}+-- | Config for stating up instance+-- Note that startup requeres clean or non-existing directory+data StartupConfig = StartupConfig+ { -- | Should data directory be cleaned up?+ cleanDir :: Bool+ -- | See @Version@+ , version :: Version+ } -data RuntimeConfig = RuntimeConfig {- execDir :: FilePath,- dataDir :: FilePath-}+-- | Config of running instance+data RuntimeConfig = RuntimeConfig+ { -- | Path of extracter PostgreSQL distribution+ execDir :: FilePath+ -- | Data directory+ , dataDir :: FilePath+ } -data DBConfig = DBConfig {- port :: Integer,- username :: String-}+-- | Database config+data DBConfig = DBConfig+ { -- | Port+ port :: Integer+ -- | Username+ , username :: String+ }
test/Spec.hs view
@@ -1,14 +1,32 @@+import Data.ByteString.Char8 (pack)+import Data.Conduit.Shell (rm, run)+import Data.Monoid ((<>))+import Data.String (fromString)+import Database.PostgreSQL.Simple (Only (..), connectPostgreSQL,+ query_)+import System.FilePath.Posix ((</>))+ import Database.PostgreSQL.Embedded main :: IO () main = do- let sConfig = StartupConfig (Version "9.6.5-1") 10+ let sConfig = StartupConfig True (Version "9.6.5-1") let dConfig = DBConfig 46782 "postgres" + run $ rm "-rf" $ "~/.postgres-embedded/" </> "9.6.5-1"+ -- Start Postgres downloading distribution rc <- startPostgres sConfig dConfig+ runQuery dConfig stopPostgres rc -- Start Postgres with cached distribution rc1 <- startPostgres sConfig dConfig+ runQuery dConfig stopPostgres rc1++runQuery :: DBConfig -> IO ()+runQuery (DBConfig p u) = do+ conn <- connectPostgreSQL $ pack $ "host=127.0.0.1 port=" <> show p <> " user=" <> u+ _ <- (query_ conn $ fromString "select 1") :: IO [Only Int]+ return ()