pg-harness 0.1.2 → 0.2.0
raw patch · 2 files changed
+85/−5 lines, 2 filesdep +HTTPdep +bytestring
Dependencies added: HTTP, bytestring
Files
- pg-harness.cabal +14/−5
- src/Data/PostgreSQL/Harness/Client.hs +71/−0
pg-harness.cabal view
@@ -1,12 +1,12 @@ Name: pg-harness-Version: 0.1.2-Synopsis: REST service for creating temporary PostgreSQL databases+Version: 0.2.0+Synopsis: REST service and library for creating/consuming temporary PostgreSQL databases Description:- REST service for conveniently creating temporary PostgreSQL databases.- It is intended for use from tests.+ REST service and client library for conveniently creating and consuming temporary+ PostgreSQL databases. This is intended for use from tests. . See <https://github.com/BardurArantsson/pg-harness/blob/master/README.md README.md> on- GitHub for detailed usage instructions.+ GitHub for detailed usage and setup instructions for the REST service. License: AGPL-3 License-file: LICENSE.txt Author: Bardur Arantsson@@ -43,3 +43,12 @@ PgHarness.Configuration PgHarness.Mutex PgHarness.DatabaseId++Library+ Build-depends: base == 4.*+ , bytestring >= 0.9.0.1+ , HTTP >= 4000.2.17 && < 4001+ Default-language: Haskell2010+ Ghc-options: -Wall+ Hs-source-dirs: src+ Exposed-modules: Data.PostgreSQL.Harness.Client
+ src/Data/PostgreSQL/Harness/Client.hs view
@@ -0,0 +1,71 @@+{-+ pg-harness, REST service for creating temporary PostgreSQL databases.+ Copyright (C) 2014, 2015 Bardur Arantsson++ This program is free software: you can redistribute it and/or modify+ it under the terms of the GNU Affero General Public License as published by+ the Free Software Foundation, either version 3 of the License, or+ (at your option) any later version.++ This program is distributed in the hope that it will be useful,+ but WITHOUT ANY WARRANTY; without even the implied warranty of+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ GNU Affero General Public License for more details.++ You should have received a copy of the GNU Affero General Public License+ along with this program. If not, see <http://www.gnu.org/licenses/>.+-}+{-|++ Client library for @pg-harness@ REST service.++ Use the 'createTemporaryDatabase' function to create the database.++-}+module Data.PostgreSQL.Harness.Client+ ( ConnectionInformation(..)+ , createTemporaryDatabase+ , toConnectionString+ ) where++import Data.ByteString (ByteString)+import qualified Data.ByteString.Char8 as B8+import Network.HTTP (simpleHTTP, postRequest, getResponseBody)++-- | Connection information to use for connecting to a database.+data ConnectionInformation = ConnectionInformation+ { ciHost :: String+ , ciPort :: String+ , ciDatabaseName :: String+ , ciUser :: String+ , ciPassword :: String+ }++-- | Create temporary database using the given URL to a+-- running @pg-harness@ REST service. Returns the connection+-- information for connecting to the newly created temporary+-- database.+createTemporaryDatabase :: String -> IO ConnectionInformation+createTemporaryDatabase url = do+ rsp <- simpleHTTP (postRequest url)+ body <- getResponseBody rsp+ return $ parse body+ where+ parse :: String -> ConnectionInformation+ parse s =+ let (userPass, (_:hostPortDatabase)) = break ((==) '@') s in+ let (user, (_:password)) = break ((==) ':') userPass in+ let (hostPort, (_:databaseName)) = break ((==) '/') hostPortDatabase in+ let (host, (_:port)) = break ((==) ':') hostPort in+ ConnectionInformation host port databaseName user password++-- | Convert connection information to a @libpq@- or+-- @postgresql-simple@-compatible connection string.+toConnectionString :: ConnectionInformation -> ByteString+toConnectionString (ConnectionInformation host port databaseName user password) =+ B8.pack $+ "host=" ++ host ++ " " +++ "port=" ++ port ++ " " +++ "dbname=" ++ databaseName ++ " " +++ "user=" ++ user ++ " " +++ "password=" ++ password