From bbb0ce0027b4cdf53a5b1fee831520c0a7565ad3 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Wed, 18 Mar 2015 18:52:57 +0200 Subject: [PATCH] createPostgresqlPoolModified function added --- persistent-postgresql/ChangeLog.md | 5 +-- .../Database/Persist/Postgresql.hs | 33 +++++++++++++++---- .../persistent-postgresql.cabal | 2 +- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/persistent-postgresql/ChangeLog.md b/persistent-postgresql/ChangeLog.md index 40534cd66..4866c8f33 100644 --- a/persistent-postgresql/ChangeLog.md +++ b/persistent-postgresql/ChangeLog.md @@ -1,6 +1,7 @@ -## Master +## 2.1.3 -Added a `Show` instance for `PostgresConf`. +* Added a `Show` instance for `PostgresConf`. +* `createPostgresqlPoolModified` added, see [relevant mailing list discussion](https://groups.google.com/d/msg/yesodweb/qUXrEN_swEo/O0pFwqwQIdcJ) ## 2.1.2.1 diff --git a/persistent-postgresql/Database/Persist/Postgresql.hs b/persistent-postgresql/Database/Persist/Postgresql.hs index ace2ffa6a..31be2fe3b 100644 --- a/persistent-postgresql/Database/Persist/Postgresql.hs +++ b/persistent-postgresql/Database/Persist/Postgresql.hs @@ -12,6 +12,7 @@ module Database.Persist.Postgresql ( withPostgresqlPool , withPostgresqlConn , createPostgresqlPool + , createPostgresqlPoolModified , module Database.Persist.Sql , ConnectionString , PostgresConf (..) @@ -88,7 +89,7 @@ withPostgresqlPool :: (MonadBaseControl IO m, MonadLogger m, MonadIO m) -- ^ Action to be executed that uses the -- connection pool. -> m a -withPostgresqlPool ci = withSqlPool $ open' ci +withPostgresqlPool ci = withSqlPool $ open' (const $ return ()) ci -- | Create a PostgreSQL connection pool. Note that it's your @@ -102,17 +103,37 @@ createPostgresqlPool :: (MonadIO m, MonadBaseControl IO m, MonadLogger m) -- ^ Number of connections to be kept open -- in the pool. -> m ConnectionPool -createPostgresqlPool ci = createSqlPool $ open' ci - +createPostgresqlPool = createPostgresqlPoolModified (const $ return ()) + +-- | Same as 'createPostgresqlPool', but additionally takes a callback function +-- for some connection-specific tweaking to be performed after connection +-- creation. This could be used, for example, to change the schema. For more +-- information, see: +-- +-- +-- +-- Since 2.1.3 +createPostgresqlPoolModified + :: (MonadIO m, MonadBaseControl IO m, MonadLogger m) + => (PG.Connection -> IO ()) -- ^ action to perform after connection is created + -> ConnectionString -- ^ Connection string to the database. + -> Int -- ^ Number of connections to be kept open in the pool. + -> m ConnectionPool +createPostgresqlPoolModified modConn ci = createSqlPool $ open' modConn ci -- | Same as 'withPostgresqlPool', but instead of opening a pool -- of connections, only one connection is opened. withPostgresqlConn :: (MonadIO m, MonadBaseControl IO m, MonadLogger m) => ConnectionString -> (SqlBackend -> m a) -> m a -withPostgresqlConn = withSqlConn . open' +withPostgresqlConn = withSqlConn . open' (const $ return ()) + +open' :: (PG.Connection -> IO ()) + -> ConnectionString -> LogFunc -> IO SqlBackend +open' modConn cstr logFunc = do + conn <- PG.connectPostgreSQL cstr + modConn conn + openSimpleConn logFunc conn -open' :: ConnectionString -> LogFunc -> IO SqlBackend -open' cstr logFunc = PG.connectPostgreSQL cstr >>= openSimpleConn logFunc -- | Generate a 'Connection' from a 'PG.Connection' openSimpleConn :: LogFunc -> PG.Connection -> IO SqlBackend diff --git a/persistent-postgresql/persistent-postgresql.cabal b/persistent-postgresql/persistent-postgresql.cabal index bccedf7c0..b6a95d567 100644 --- a/persistent-postgresql/persistent-postgresql.cabal +++ b/persistent-postgresql/persistent-postgresql.cabal @@ -1,5 +1,5 @@ name: persistent-postgresql -version: 2.1.2.2 +version: 2.1.3 license: MIT license-file: LICENSE author: Felipe Lessa, Michael Snoyman