postgres-embedded 0.1.8 → 0.2.0
raw patch · 9 files changed
+91/−31 lines, 9 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Database.PostgreSQL.Embedded: [silentR] :: RuntimeConfig -> Bool
+ Database.PostgreSQL.Embedded: [silent] :: StartupConfig -> Bool
- Database.PostgreSQL.Embedded: RuntimeConfig :: FilePath -> FilePath -> RuntimeConfig
+ Database.PostgreSQL.Embedded: RuntimeConfig :: FilePath -> FilePath -> Bool -> RuntimeConfig
- Database.PostgreSQL.Embedded: StartupConfig :: Bool -> Version -> StartupConfig
+ Database.PostgreSQL.Embedded: StartupConfig :: Bool -> Version -> Bool -> StartupConfig
Files
- CHANGELOG.md +0/−9
- CHANGES.txt +13/−0
- LICENSE +1/−1
- postgres-embedded.cabal +5/−4
- src/Database/PostgreSQL/Embedded/Download.hs +8/−6
- src/Database/PostgreSQL/Embedded/Exec.hs +46/−0
- src/Database/PostgreSQL/Embedded/Postgres.hs +14/−10
- src/Database/PostgreSQL/Embedded/Types.hs +3/−0
- test/Spec.hs +1/−1
− CHANGELOG.md
@@ -1,9 +0,0 @@-# Changes--## 0.1.8--Removed shell-conduit dependency as it causes lots of problems--## 0.1.7--Unstable version for testing
+ CHANGES.txt view
@@ -0,0 +1,13 @@+# Changes++## 0.2.0 (18.02.2018)++Added option to run silently++## 0.1.8 (28.11.2017)++Removed shell-conduit dependency as it causes lots of problems++## 0.1.7 (14.10.2017)++Unstable version for testing
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2017 Ilya Murzinov+Copyright (c) 2017-2018 Ilya Murzinov and contributors All rights reserved.
postgres-embedded.cabal view
@@ -1,5 +1,5 @@ name: postgres-embedded-version: 0.1.8+version: 0.2.0 description: Library for easily running embedded PostgreSQL server for tests. It downloads, runs, checks and stops PostgreSQL Database instance for you. synopsis: Library for easily running embedded PostgreSQL server for tests.@@ -9,13 +9,13 @@ license-file: LICENSE author: Ilya Murzinov maintainer: Ilya Murzinov <murz42@gmail.com>-copyright: 2017 Ilya Murzinov+copyright: 2017-2018 Ilya Murzinov and contributors category: Lib build-type: Simple extra-doc-files: README.md- CHANGELOG.md+ CHANGES.txt cabal-version: >= 1.18-tested-with: GHC==8.2.1, GHC==8.0.2, GHC==7.10.3, GHC==7.8.4+tested-with: GHC==8.2.2, GHC==8.0.2, GHC==7.10.3, GHC==7.8.4 source-repository head type: git@@ -27,6 +27,7 @@ other-modules: Database.PostgreSQL.Embedded.Postgres , Database.PostgreSQL.Embedded.Download , Database.PostgreSQL.Embedded.Types+ , Database.PostgreSQL.Embedded.Exec build-depends: base >= 4 && < 5 , directory , filepath
src/Database/PostgreSQL/Embedded/Download.hs view
@@ -11,17 +11,19 @@ import System.FilePath.Posix ((</>)) import Database.PostgreSQL.Embedded.Types+import Database.PostgreSQL.Embedded.Exec data ArchiveType = Zip | Tar deriving (Eq) -downloadPostgres :: Os -> Version -> IO FilePath-downloadPostgres os_ version_ = do+downloadPostgres :: Bool -> Os -> Version -> IO FilePath+downloadPostgres silent_ os_ version_ = do home <- getHomeDirectory let v = value version_ let workdir = home </> ".postgres-embedded" </> v let tmp = workdir </> "postgres.tmp" let dist = workdir </> "pgsql"+ let execSystem = if silent_ then rawSystemSilent else rawSystem createDirectoryIfMissing True workdir @@ -31,12 +33,12 @@ False -> do setCurrentDirectory workdir let (aType, suffix) = binaries os_- _ <- rawSystem "wget" ["-O", tmp, base_download_url <> v <> suffix]+ _ <- execSystem "wget" ["-O", tmp, base_download_url <> v <> suffix] _ <- case aType of- Zip -> rawSystem "unzip" ["-q", tmp]- Tar -> rawSystem "tar" ["-xzf", tmp]+ Zip -> execSystem "unzip" ["-q", tmp]+ Tar -> execSystem "tar" ["-xzf", tmp] return dist- + where binaries :: Os -> (ArchiveType, String) binaries OSX = (Zip, "-osx-binaries.zip")
+ src/Database/PostgreSQL/Embedded/Exec.hs view
@@ -0,0 +1,46 @@+module Database.PostgreSQL.Embedded.Exec+ ( systemSilent+ , rawSystemSilent+ ) where++import GHC.IO.Exception (IOErrorType (..), ioException)+import System.Exit+import System.IO+import System.IO.Error+import System.Process++{-|+Exactly like 'System.Process.system' but does not capture stdout nor stderr.+-}+systemSilent :: String -> IO ExitCode+systemSilent "" = ioException+ (ioeSetErrorString (mkIOError InvalidArgument "system" Nothing Nothing) "null command")+systemSilent str = do+ devNull <- openFile "/dev/null" WriteMode+ (_, _, _, p) <- createProcess (shell str)+ { delegate_ctlc = True+ , std_err = UseHandle devNull+ , std_out = UseHandle devNull+ }+ waitForProcess p++{-|+Exactly like 'System.Process.rawSystem' but does not capture stdout nor stderr.+-}+rawSystemSilent :: String -> [String] -> IO ExitCode+rawSystemSilent cmd args = do+ devNull <- nullDevice+ (_, _, _, p) <- createProcess (proc cmd args)+ { delegate_ctlc = True+ , std_err = UseHandle devNull+ , std_out = UseHandle devNull+ }+ waitForProcess p++nullDevice :: IO Handle+nullDevice = do+ h <- tryIOError nix+ either (const windows) return h+ where+ nix = openFile "/dev/null" WriteMode+ windows = openFile "nul" WriteMode
src/Database/PostgreSQL/Embedded/Postgres.hs view
@@ -15,6 +15,7 @@ import Database.PostgreSQL.Embedded.Download import Database.PostgreSQL.Embedded.Types+import Database.PostgreSQL.Embedded.Exec {-| Starts PostgreSQL instance with given config.@@ -22,21 +23,22 @@ Returns @RuntimeConfig@ that is required for @stopPostgres@. -} startPostgres :: StartupConfig -> DBConfig -> IO RuntimeConfig-startPostgres (StartupConfig сlean version_) dConfig@(DBConfig p u) = do- e <- downloadPostgres getOS version_+startPostgres (StartupConfig сlean version_ silent_) dConfig@(DBConfig p u) = do+ let execSystem = if silent_ then systemSilent else system+ e <- downloadPostgres silent_ getOS version_ let d = e </> "data" exists <- doesDirectoryExist d _ <- if exists && not сlean then do putStrLn $ "Directory " <> d <> " must not exist" exitFailure- else system $ "rm" <> " -rf " <> d+ else execSystem $ "rm" <> " -rf " <> d _ <- do- _ <- system $ e </> "bin" </> "initdb" <> " -A trust -U " <> u <> " -D " <> d <> " -E UTF-8"- system $ e </> "bin" </> "pg_ctl" <> " -w " <> " -D " <> d <> " -o \"-F -p " <>+ _ <- execSystem $ e </> "bin" </> "initdb" <> " -A trust -U " <> u <> " -D " <> d <> " -E UTF-8"+ execSystem $ e </> "bin" </> "pg_ctl" <> " -w " <> " -D " <> d <> " -o \"-F -p " <> show p <> "\"" <> " -l " <> (e </> "log") <> " start" - let r = RuntimeConfig e d+ let r = RuntimeConfig e d silent_ checkPostgresStarted r dConfig return r @@ -52,16 +54,18 @@ Doesn't remove data directory. -} stopPostgres :: RuntimeConfig -> IO ()-stopPostgres (RuntimeConfig e d) = do- _ <- system $ e </> "bin" </> "pg_ctl" <> " -D " <> d <> " stop" <> " -m fast -t 5 -w"+stopPostgres (RuntimeConfig e d s) = do+ let execSystem = if s then systemSilent else system+ _ <- execSystem $ e </> "bin" </> "pg_ctl" <> " -D " <> d <> " stop" <> " -m fast -t 5 -w" return () checkPostgresStarted :: RuntimeConfig -> DBConfig -> IO ()-checkPostgresStarted (RuntimeConfig e _) (DBConfig p _) = checkPostgresStarted_ 10 >>= \_ -> return ()+checkPostgresStarted (RuntimeConfig e _ s) (DBConfig p _) = checkPostgresStarted_ 10 >>= \_ -> return () where checkPostgresStarted_ :: Int -> IO Bool checkPostgresStarted_ n = do- let check = system (e </> "bin" </> "pg_isready" <> " -p " <> show p <> " -h localhost") >>= \_ -> return True+ let execSystem = if s then systemSilent else system+ let check = execSystem (e </> "bin" </> "pg_isready" <> " -p " <> show p <> " -h localhost") >>= \_ -> return True res <- try check :: IO (Either SomeException Bool) let retry = if n > 0 then checkPostgresStarted_ (n - 1)
src/Database/PostgreSQL/Embedded/Types.hs view
@@ -21,6 +21,8 @@ cleanDir :: Bool -- | See @Version@ , version :: Version+ -- | Silence the output of PG commands+ , silent :: Bool } -- | Config of running instance@@ -29,6 +31,7 @@ execDir :: FilePath -- | Data directory , dataDir :: FilePath+ , silentR :: Bool } -- | Database config
test/Spec.hs view
@@ -10,7 +10,7 @@ main :: IO () main = do- let sConfig = StartupConfig True (Version "9.6.5-1")+ let sConfig = StartupConfig True (Version "9.6.5-1") False let dConfig = DBConfig 46782 "postgres" system $ "rm" <> " -rf " <> ("~/.postgres-embedded/" </> "9.6.5-1")