postgres-tmp 0.1.0.1 → 0.2.0
raw patch · 4 files changed
+57/−20 lines, 4 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Database.PostgreSQL.Tmp: createTmpDB :: ByteString -> IO (Connection, DBInfo)
+ Database.PostgreSQL.Tmp: dropDB :: Connection -> Text -> IO Int64
+ Database.PostgreSQL.Tmp: dropRole :: Connection -> Text -> IO Int64
+ Database.PostgreSQL.Tmp: dropTmpDB :: (Connection, DBInfo) -> IO ()
Files
- CHANGELOG.md +5/−0
- README.md +8/−0
- postgres-tmp.cabal +5/−5
- src/Database/PostgreSQL/Tmp.hs +39/−15
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+## 0.2.0 (2017-12-01)++* Drop support for GHC < 7.10+* Expose low-level API to make it easier to use it in transformers on+ top of IO.
+ README.md view
@@ -0,0 +1,8 @@+# postgres-tmp++[](https://travis-ci.org/cocreature/postgres-tmp)+[](https://hackage.haskell.org/package/postgres-tmp)++Haskell library for creating temporary postgresql databases. This is+especially useful for tests where you don’t want to assume that a+certain database is available.
postgres-tmp.cabal view
@@ -1,5 +1,5 @@ name: postgres-tmp-version: 0.1.0.1+version: 0.2.0 synopsis: Create a temporary database that is deleted after performing some operation description: Please see README.md homepage: https://github.com/cocreature/postgres-tmp#readme@@ -8,16 +8,16 @@ author: Moritz Kiefer maintainer: moritz.kiefer@purelyfunctional.org copyright: 2016-category: Unknown+category: Database build-type: Simple--- extra-source-files:+extra-source-files: README.md CHANGELOG.md cabal-version: >=1.10-tested-with: GHC == 7.8.4, GHC == 7.10.3, GHC == 8.0.1+tested-with: GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.2 library hs-source-dirs: src exposed-modules: Database.PostgreSQL.Tmp- build-depends: base >= 4.7 && < 5+ build-depends: base >= 4.8 && < 5 , bytestring >= 0.10 && < 0.11 , postgresql-simple >= 0.5 && < 0.6 , text >= 1.2 && < 1.3
src/Database/PostgreSQL/Tmp.hs view
@@ -4,13 +4,19 @@ -} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE ScopedTypeVariables #-}-module Database.PostgreSQL.Tmp - (withTmpDB- ,withTmpDB'- ,newRole- ,newDB- ,defaultDB- ,DBInfo(..)) where+module Database.PostgreSQL.Tmp+ ( defaultDB+ , DBInfo(..)+ , withTmpDB+ , withTmpDB'+ -- * Low level APIs+ , createTmpDB+ , dropTmpDB+ , newRole+ , dropRole+ , newDB+ , dropDB+ ) where import Control.Applicative (pure) import Control.Exception@@ -40,17 +46,35 @@ -- callback can operate on. After the action has finished the database -- and the role are destroyed. --+-- This is a `bracket`-style wrapper around 'createTmpDB' and 'dropTmpDB'+-- -- This function assumes that the connection string points to a -- database containing the tables called @pg_roles@ and @pg_database@ -- and that the user has the @CREATEDB@ and @CREATEROLE@ privileges. withTmpDB' :: ByteString -> (DBInfo -> IO a) -> IO a-withTmpDB' conStr f =- bracket (connectPostgreSQL conStr) close $- \conn ->- bracket (newRole conn) (dropRole conn) $ \role -> do- bracket (newDB conn role) (dropDatabase conn) $ \db -> do- f (DBInfo {dbName = db, roleName = role})+withTmpDB' conStr f = bracket (createTmpDB conStr) dropTmpDB (\(_,dbInfo) -> f dbInfo) +-- | Create a temporary database and a temporary role.+--+-- To destroy the database and the role use `dropTmpDB`.+--+-- This function assumes that the connection string points to a+-- database containing the tables called @pg_roles@ and @pg_database@+-- and that the user has the @CREATEDB@ and @CREATEROLE@ privileges.+createTmpDB :: ByteString -> IO (Connection, DBInfo)+createTmpDB conStr = do+ conn <- connectPostgreSQL conStr+ role <- newRole conn+ db <- newDB conn role+ pure (conn, DBInfo {dbName = db,roleName = role})++-- | Destroy the database and the role created by `createTmpDB`.+dropTmpDB :: (Connection, DBInfo) -> IO ()+dropTmpDB (conn, DBInfo db role) = do+ _ <- dropDB conn db+ _ <- dropRole conn role+ close conn+ -- | Create a new role that does not already exist and return its name. -- -- The new role does not have a password and has the @CREATEDB@@@ -76,8 +100,8 @@ pure newName -- | Drop the database.-dropDatabase :: Connection -> T.Text -> IO Int64-dropDatabase conn name =+dropDB :: Connection -> T.Text -> IO Int64+dropDB conn name = execute conn "DROP DATABASE ?" (Only (Identifier name)) -- | Create a fresh name that is not in the list of already existing names.