dbcleaner (empty) → 0.1.0
raw patch · 6 files changed
+96/−0 lines, 6 filesdep +basedep +hspecdep +postgresql-simplesetup-changed
Dependencies added: base, hspec, postgresql-simple, text
Files
- LICENSE +20/−0
- Setup.hs +2/−0
- dbcleaner.cabal +35/−0
- src/Database/DBCleaner/PostgreSQLSimple.hs +35/−0
- src/Database/DBCleaner/Types.hs +3/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,20 @@+Copyright (c) 2015 Stack Builders++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ dbcleaner.cabal view
@@ -0,0 +1,35 @@+name: dbcleaner+version: 0.1.0+synopsis: Clean database tables automatically around hspec tests+description: A simple database cleaner library for testing that provides+ different cleanup strategies.+license: MIT+license-file: LICENSE+author: Stack Builders+maintainer: hackage@stackbuilders.com+category: Database+build-type: Simple+cabal-version: >= 1.10++library+ exposed-modules: Database.DBCleaner.PostgreSQLSimple+ , Database.DBCleaner.Types+ build-depends: base >=4.4 && <4.9+ , postgresql-simple+ , text+ hs-source-dirs: src+ default-language: Haskell2010++test-suite spec+ type: exitcode-stdio-1.0+ build-depends: base+ , hspec+ , postgresql-simple+ , text+ hs-source-dirs: src, test+ main-is: Spec.hs+ default-language: Haskell2010++source-repository head+ type: git+ location: https://github.com/stackbuilders/dbcleaner
+ src/Database/DBCleaner/PostgreSQLSimple.hs view
@@ -0,0 +1,35 @@+{-# LANGUAGE OverloadedStrings #-}++module Database.DBCleaner.PostgreSQLSimple (withConnection) where++import Control.Exception (bracket, finally)+import Control.Monad (void)+import Data.Monoid (mconcat)+import Data.Text (Text)+import Database.PostgreSQL.Simple (Connection, Only (..), begin,+ execute, query_, rollback)+import Database.PostgreSQL.Simple.Types (Identifier (..))++import Database.DBCleaner.Types (Strategy (..))++withConnection :: Strategy -> (Connection -> IO a) -> Connection -> IO a+withConnection Transaction f c = bracket (begin c >> return c) rollback f+withConnection Truncation f c = finally (f c) $ listTables c >>= mapM_ (truncateTable c)++listTables :: Connection -> IO [Text]+listTables c = map fromOnly `fmap` query_ c q+ where+ q = mconcat [ "SELECT c.relname FROM pg_catalog.pg_class c"+ , " LEFT JOIN pg_catalog.pg_namespace n"+ , " ON c.relnamespace = n.oid"+ , " WHERE c.relkind IN ('r', '')"+ , " AND n.nspname <> 'pg_catalog'"+ , " AND n.nspname <> 'information_schema'"+ , " AND n.nspname !~ '^pg_toast'"+ , " AND pg_catalog.pg_table_is_visible(c.oid)"+ ]++truncateTable :: Connection -> Text -> IO ()+truncateTable c = void . execute c q . Only . Identifier+ where+ q = "TRUNCATE ? CASCADE"
+ src/Database/DBCleaner/Types.hs view
@@ -0,0 +1,3 @@+module Database.DBCleaner.Types where++data Strategy = Transaction | Truncation
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}