packages feed

tmp-postgres 1.12.0.1 → 1.13.0.0

raw patch · 4 files changed

+82/−15 lines, 4 files

Files

CHANGELOG.md view
@@ -35,3 +35,6 @@  1.12.0.1   Documentation fixes++1.13.0.0+  #108 'startNewDBConfig' functions added and related
src/Database/Postgres/Temp.hs view
@@ -57,6 +57,9 @@   , stop   , restart   , stopPostgres+  , startNewDb+  , startNewDbConfig+  , stopNewDb   -- * Main resource handle   , DB   -- ** 'DB' accessors
src/Database/Postgres/Temp/Internal.hs view
@@ -6,7 +6,6 @@ module Database.Postgres.Temp.Internal where import Database.Postgres.Temp.Internal.Core import Database.Postgres.Temp.Internal.Config-import           Control.Concurrent.Async import           Control.Exception import           Control.Monad (void) import           Control.Monad.Trans.Cont@@ -434,7 +433,7 @@  'withNewDb' = 'withNewDbConfig' mempty @ -See 'withNewDbConfig' for more details.+See 'startNewDbConfig' for more details.  @since 1.12.0.0 -}@@ -450,9 +449,54 @@ withNewDb = withNewDbConfig mempty  {-|+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)++{-| 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++{-|+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.@@ -477,20 +521,23 @@ \"a\" to \"z\". It is possible, although unlikeily that a duplicate database name could be generated and this would also cause a failure. -@since 1.12.0.0+'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 -}-withNewDbConfig+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'.-  -> (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 = try $ do+  -> IO (Either StartError DB)+startNewDbConfig extra db = try $ do   stdGen <- getStdGen   let oldOptions@Client.Options {..} = toConnectionOptions db       theDbName = fromMaybe "postgres" $ getLast dbname@@ -498,9 +545,6 @@       newOptions = oldOptions         { Client.dbname = pure newDbName         }-      template1Options = oldOptions-        { Client.dbname = pure "template1"-        }       generated = standardProcessConfig         { commandLine = mempty           { keyBased = Map.fromList@@ -522,5 +566,22 @@     Left errs -> throwIO $ CompleteProcessConfigFailed (show $ pretty combined) errs     Right x -> pure x   terminateConnections oldOptions-  bracket_ (wait =<< asyncWithUnmask (\unmask -> unmask (executeCreateDb final))) (dropDbIfExists template1Options newDbName) $-    f newDb+  executeCreateDb final+  pure newDb++-- | 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
tmp-postgres.cabal view
@@ -1,5 +1,5 @@ name:                tmp-postgres-version:             1.12.0.1+version:             1.13.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