tmp-postgres 1.17.0.0 → 1.18.0.0
raw patch · 5 files changed
+6/−209 lines, 5 filesdep −random
Dependencies removed: random
Files
- CHANGELOG.md +4/−1
- benchmark/Main.hs +1/−8
- src/Database/Postgres/Temp.hs +0/−6
- src/Database/Postgres/Temp/Internal.hs +0/−192
- tmp-postgres.cabal +1/−2
CHANGELOG.md view
@@ -1,6 +1,9 @@ Changelog for tmp-postgres -1.7.0.0+1.18.0.0+ #143 Remove the the withNewDb API++1.17.0.0 #156 Deprecate `NewDb` functions. #155 Better monoids for `initDbConfig` and `createDbConfig`. #142 Cluster save points.
benchmark/Main.hs view
@@ -91,16 +91,9 @@ , bench "with migrate 10x" $ whnfIO $ replicateM 10 $ withConfig silentConfig $ \db -> migrateDb db >> testQuery db - , bench "withNewDb migrate 10x" $ whnfIO $ withConfig silentConfig $ \db -> do- migrateDb db- replicateM 10 $ withNewDb db testQuery -}- setupWithCache $ \cacheConfig -> bench "withNewDbConfig migrate 10x and cache" $ whnfIO $ withConfig cacheConfig- $ \db -> do- migrateDb db- replicateM 10 $ withNewDb db testQuery - , setupWithCache $ \cacheConfig -> do+ setupWithCache $ \cacheConfig -> do bench "with migrate 10x and cache" $ whnfIO $ withConfig cacheConfig $ \_ -> do replicateM_ 10 $ withConfig cacheConfig $ \db -> migrateDb db >> testQuery db
src/Database/Postgres/Temp.hs view
@@ -90,12 +90,6 @@ , optionsToDefaultConfig -- ** Configuration Types , module Database.Postgres.Temp.Config- -- * Deprecated NewDb Functions. Use 'withSnapshot'.- , withNewDb- , withNewDbConfig- , startNewDb- , startNewDbConfig- , stopNewDb ) where import Database.Postgres.Temp.Internal import Database.Postgres.Temp.Internal.Core
src/Database/Postgres/Temp/Internal.hs view
@@ -13,15 +13,10 @@ import Control.Monad.Trans.Cont import Data.ByteString (ByteString) import qualified Data.Map.Strict as Map-import Data.Maybe-import Data.Monoid-import Data.String import qualified Database.PostgreSQL.Simple as PG import qualified Database.PostgreSQL.Simple.Options as Client-import System.Environment import System.Exit (ExitCode(..)) import System.Process-import System.Random import Text.PrettyPrint.ANSI.Leijen hiding ((<$>)) -- | Handle for holding temporary resources, the @postgres@ process handle@@ -439,193 +434,6 @@ -- @since 1.12.0.0 prettyPrintDB :: DB -> String prettyPrintDB = show . pretty------------------------------------------------------------------------------------ withNewDb----------------------------------------------------------------------------------- | Drop the db if it exists. Terminates all connections to the db first.------ @since 1.12.0.0-dropDbIfExists :: Client.Options -> String -> IO ()-dropDbIfExists options dbName = do- let theConnectionString = Client.toConnectionString options- dropDbQuery = fromString $ "DROP DATABASE IF EXISTS " <> dbName <> ";"-- terminateConnections $ options- { Client.dbname = pure dbName- }-- mapException DeleteDbError $- bracket (PG.connectPostgreSQL theConnectionString) PG.close $- \conn -> void $ PG.execute_ conn dropDbQuery--{-|-Use the current database as a template and make a copy. Give the-copy a random name.--Equivalent to:--@- 'withNewDb' = 'withNewDbConfig' mempty-@--See 'startNewDbConfig' for more details.--@since 1.12.0.0--}-withNewDb- :: DB- -- ^ The original 'DB' handle. The database name specified in the- -- connection options- -- is used as the template for the @generated@ 'ProcessConfig'- -> (DB -> IO a)- -- ^ The modified 'DB' handle that has the new database name- -- in it's connection options.- -> IO (Either StartError a)-withNewDb = withNewDbConfig mempty-{-# DEPRECATED withNewDb "Use withSnapshot" #-}-{-|-Exception safe version of 'startNewDbConfig'. Creates a-temporary database using the current database as a template.--See 'startNewDbConfig' for more details.--@since 1.12.0.0--}-withNewDbConfig- :: ProcessConfig- -- ^ @extra@ @createdb@ 'ProcessConfig'.- -> DB- -- ^ The original 'DB' handle. The database name specified in the- -- connection options- -- is used as the template for the @generated@ 'ProcessConfig'.- -> (DB -> IO a)- -- ^ The modified 'DB' handle that has the new database name- -- in it's connection options.- -> IO (Either StartError a)-withNewDbConfig extra db f =- bracket (startNewDbConfig extra db) (either mempty stopNewDb) $- either (pure . Left) (fmap Right . f)-{-# DEPRECATED withNewDbConfig "Use withSnapshot" #-}--{-|-Use the current database as a template and make a copy. Give the-copy a random name.--Equivalent to:--@- 'startNewDb' = 'startNewDbConfig' mempty-@--See 'startNewDbConfig' for more details.--@since 1.13.0.0--}-startNewDb- :: DB- -- ^ The original 'DB' handle. The database name specified in the- -- connection options- -- is used as the template for the @generated@ 'ProcessConfig'.- -> IO (Either StartError DB)-startNewDb = startNewDbConfig mempty-{-# DEPRECATED startNewDb "Use takeSnapshot" #-}--{-|-Use the current database as a template and make a copy. Give the-copy a random name.--Copying a database from a template can be faster than creating a new-@postgres@ and migrating a database from scratch. In artifical benchmarks-it appears to be about 2x faster.--To use the current database as a template all connections to the database-must be terminated first.--To override the arguments passed to @createdb@ one can pass in @extra@-'ProcessConfig'. The @combined@ process is created by 'mappend'ed the-@generated@ with the @extra@ 'ProcessConfig', e.g.--@- combined = generated '<>' extra-@--The current implementation has a few known issues.--If a connection is made between the termination command and the @createdb@-call the @createdb@ call will fail.--Additionally the generated name is 32 character random name of characters-\"a\" to \"z\". It is possible, although unlikeily that a duplicate-database name could be generated and this would also cause a failure.--'startNewDbConfig' requires cleanup so it best to use a 'bracket' along-with 'stopNewDb'. This is equivalient to 'withNewDbConfig'.--The only reason to use 'startNewDbConfig' over 'withNewDbConfig' is-if you are unable to use 'withNewDbConfig' for some reason.--@since 1.13.0.0--}-startNewDbConfig- :: ProcessConfig- -- ^ @extra@ @createdb@ 'ProcessConfig'.- -> DB- -- ^ The original 'DB' handle. The database name specified in the- -- connection options- -- is used as the template for the @generated@ 'ProcessConfig'.- -> IO (Either StartError DB)-startNewDbConfig extra@ProcessConfig{..} db = try $ do- stdGen <- getStdGen- let oldOptions@Client.Options {..} = toConnectionOptions db- theDbName = fromMaybe "postgres" $ getLast dbname- newDbName = fromMaybe (take 32 $ randomRs ('a', 'z') stdGen) $- Map.lookup 0 $ indexBased commandLine- newOptions = oldOptions- { Client.dbname = pure newDbName- }- generated = standardProcessConfig- { commandLine = mempty- { keyBased = Map.fromList- [ ("-T", Just theDbName)- , ("-h", Just $ fromMaybe "127.0.0.1" $ getLast host)- , ("-p ", Just $ maybe "5432" show $ getLast port)- ]- , indexBased = Map.singleton 0 newDbName- }- }- combined = generated <> extra- newDb = db- { dbPostgresProcess = (dbPostgresProcess db)- { postgresProcessClientOptions = newOptions- }- }- envs <- getEnvironment- final <- case completeProcessConfig envs combined of- -- Failure case does not look possible- Left errs -> throwIO $ CompleteProcessConfigFailed (show $ pretty combined) errs- Right x -> pure x- terminateConnections oldOptions- executeCreateDb final- pure newDb-{-# DEPRECATED startNewDbConfig "Use takeSnapshot" #-}--- | Cleanup the temporary database created by 'startNewDbConfig'--- or 'startNewDb'.------ @since 1.13.0.0-stopNewDb :: DB -> IO ()-stopNewDb db = do- let oldOptions@Client.Options{..} = toConnectionOptions db- -- This should not happen.- newDbName <- maybe- (throwIO $ userError "stopNewDb: missing db name!")- pure $- getLast dbname- let template1Options = oldOptions- { Client.dbname = pure "template1"- }- dropDbIfExists template1Options newDbName-{-# DEPRECATED stopNewDb "Use cleanupSnapshot" #-} ------------------------------------------------------------------------------- -- initdb cache
tmp-postgres.cabal view
@@ -1,5 +1,5 @@ name: tmp-postgres-version: 1.17.0.0+version: 1.18.0.0 synopsis: Start and stop a temporary postgres description: Start and stop a temporary postgres. See README.md homepage: https://github.com/jfischoff/tmp-postgres#readme@@ -53,7 +53,6 @@ , postgres-options >= 0.2.0.0 , postgresql-simple , process >= 1.2.0.0- , random , stm , temporary , transformers