From aeb8927ebd456b6d1fc34fb4f8d4381643051e89 Mon Sep 17 00:00:00 2001 From: parsonsmatt Date: Thu, 8 Apr 2021 13:17:42 -0600 Subject: [PATCH 1/3] persistent-2.12.1.0 --- .../persistent-postgresql.cabal | 2 +- persistent/ChangeLog.md | 10 +++++-- persistent/Database/Persist/Sql.hs | 1 - .../Persist/Sql/Orphan/PersistQuery.hs | 27 ++++++++++++++++--- 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/persistent-postgresql/persistent-postgresql.cabal b/persistent-postgresql/persistent-postgresql.cabal index 39bf8c070..1bc6d7027 100644 --- a/persistent-postgresql/persistent-postgresql.cabal +++ b/persistent-postgresql/persistent-postgresql.cabal @@ -16,7 +16,7 @@ extra-source-files: ChangeLog.md library build-depends: base >= 4.9 && < 5 - , persistent >= 2.12 && < 3 + , persistent >= 2.12.1.0 && < 2.13 , aeson >= 1.0 , attoparsec , blaze-builder diff --git a/persistent/ChangeLog.md b/persistent/ChangeLog.md index 87913f4d3..da9c1024d 100644 --- a/persistent/ChangeLog.md +++ b/persistent/ChangeLog.md @@ -1,5 +1,11 @@ # Changelog for persistent +## 2.12.1.0 + +* [#]() + * Expose the `filterClause` and `filterClauseWithValues` functions to support + the `upsertWhere` functionality in `persistent-postgresql`. + ## 2.12.0.2 * [#1123](https://github.com/yesodweb/persistent/pull/1223) @@ -22,10 +28,10 @@ * Added `makeCompatibleInstances` and `makeCompatibleKeyInstances`, TemplateHaskell invocations for auto-generating standalone derivations using `Compatible` and `DerivingVia`. * [#1207](https://github.com/yesodweb/persistent/pull/1207) * @codygman discovered a bug in [issue #1199](https://github.com/yesodweb/persistent/issues/1199) where postgres connections were being returned to the `Pool SqlBackend` in an inconsistent state. - @parsonsmatt debugged the issue and determined that it had something to do with asynchronous exceptions. + @parsonsmatt debugged the issue and determined that it had something to do with asynchronous exceptions. Declaring it to be "out of his pay grade," he ripped the `poolToAcquire` function out and replaced it with `Data.Pool.withResource`, which doesn't exhibit the bug. Fortunately, this doesn't affect the public API, and can be a mere bug release. - * Removed the functions `unsafeAcquireSqlConnFromPool`, `acquireASqlConnFromPool`, and `acquireSqlConnFromPoolWithIsolation`. + * Removed the functions `unsafeAcquireSqlConnFromPool`, `acquireASqlConnFromPool`, and `acquireSqlConnFromPoolWithIsolation`. For a replacement, see `runSqlPoolNoTransaction` and `runSqlPoolWithHooks`. * Renaming values in persistent-template [#1203](https://github.com/yesodweb/persistent/pull/1203) * [#1214](https://github.com/yesodweb/persistent/pull/1214): diff --git a/persistent/Database/Persist/Sql.hs b/persistent/Database/Persist/Sql.hs index 33676da55..b4209cfb3 100644 --- a/persistent/Database/Persist/Sql.hs +++ b/persistent/Database/Persist/Sql.hs @@ -13,7 +13,6 @@ module Database.Persist.Sql , deleteWhereCount , updateWhereCount , filterClause - , filterClauseHelper , filterClauseWithVals , FilterTablePrefix (..) , transactionSave diff --git a/persistent/Database/Persist/Sql/Orphan/PersistQuery.hs b/persistent/Database/Persist/Sql/Orphan/PersistQuery.hs index a593bf4e1..2308ef2ae 100644 --- a/persistent/Database/Persist/Sql/Orphan/PersistQuery.hs +++ b/persistent/Database/Persist/Sql/Orphan/PersistQuery.hs @@ -233,9 +233,21 @@ getFiltsValues conn = snd . filterClauseHelper Nothing False conn OrNullNo data OrNull = OrNullYes | OrNullNo +-- | Used when determining how to prefix a column name in a @WHERE@ clause. +-- +-- @since 2.12.1.0 data FilterTablePrefix - = PrefixTableName - | PrefixExcluded + = PrefixTableName + -- ^ Prefix the column with the table name. This is useful if the column + -- name might be ambiguous. + -- + -- @since 2.12.1.0 + | PrefixExcluded + -- ^ Prefix the column name with the @EXCLUDED@ keyword. This is used with + -- the Postgresql backend when doing @ON CONFLICT DO UPDATE@ clauses - see + -- the documentation on @upsertWhere@ and @upsertManyWhere@. + -- + -- @since 2.12.1.0 filterClauseHelper :: (PersistEntity val) => Maybe FilterTablePrefix -- ^ include table name or PostgresSQL EXCLUDED @@ -397,6 +409,10 @@ filterClauseHelper tablePrefix includeWhere conn orNull filters = showSqlFilter NotIn = " NOT IN " showSqlFilter (BackendSpecificFilter s) = s +-- | Render a @['Filter' record]@ into a 'Text' value suitable for inclusion +-- into a SQL query. +-- +-- @since 2.12.1.0 filterClause :: (PersistEntity val) => Maybe FilterTablePrefix -- ^ include table name or EXCLUDED -> SqlBackend @@ -404,6 +420,11 @@ filterClause :: (PersistEntity val) -> Text filterClause b c = fst . filterClauseHelper b True c OrNullNo +-- | Render a @['Filter' record]@ into a 'Text' value suitable for inclusion +-- into a SQL query, as well as the @['PersistValue']@ to properly fill in the +-- @?@ place holders. +-- +-- @since 2.12.1.0 filterClauseWithVals :: (PersistEntity val) => Maybe FilterTablePrefix -- ^ include table name or EXCLUDED -> SqlBackend @@ -450,4 +471,4 @@ decorateSQLWithLimitOffset nolimit (limit,offset) _ sql = [ sql , lim , off - ] \ No newline at end of file + ] From f63cf274af2a3d5f481f8bbe340651b4518e22e4 Mon Sep 17 00:00:00 2001 From: parsonsmatt Date: Thu, 8 Apr 2021 13:18:50 -0600 Subject: [PATCH 2/3] stylish haskell --- .../Database/Persist/Postgresql.hs | 30 +++++++++---------- persistent/ChangeLog.md | 2 +- persistent/Database/Persist/Sql.hs | 10 +++---- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/persistent-postgresql/Database/Persist/Postgresql.hs b/persistent-postgresql/Database/Persist/Postgresql.hs index 5c676a274..73c967fb9 100644 --- a/persistent-postgresql/Database/Persist/Postgresql.hs +++ b/persistent-postgresql/Database/Persist/Postgresql.hs @@ -1,9 +1,9 @@ {-# LANGUAGE DeriveDataTypeable #-} -{-# LANGUAGE LambdaCase #-} -{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE ScopedTypeVariables #-} @@ -47,29 +47,29 @@ module Database.Persist.Postgresql import qualified Database.PostgreSQL.LibPQ as LibPQ import qualified Database.PostgreSQL.Simple as PG -import qualified Database.PostgreSQL.Simple.Internal as PG import qualified Database.PostgreSQL.Simple.FromField as PGFF +import qualified Database.PostgreSQL.Simple.Internal as PG +import Database.PostgreSQL.Simple.Ok (Ok(..)) import qualified Database.PostgreSQL.Simple.ToField as PGTF import qualified Database.PostgreSQL.Simple.Transaction as PG -import qualified Database.PostgreSQL.Simple.Types as PG import qualified Database.PostgreSQL.Simple.TypeInfo.Static as PS -import Database.PostgreSQL.Simple.Ok (Ok (..)) +import qualified Database.PostgreSQL.Simple.Types as PG import Control.Arrow import Control.Exception (Exception, throw, throwIO) import Control.Monad import Control.Monad.Except -import Control.Monad.IO.Unlift (MonadIO (..), MonadUnliftIO) +import Control.Monad.IO.Unlift (MonadIO(..), MonadUnliftIO) import Control.Monad.Logger (MonadLoggerIO, runNoLoggingT) -import Control.Monad.Trans.Reader (ReaderT(..), runReaderT, asks) +import Control.Monad.Trans.Reader (ReaderT(..), asks, runReaderT) import Control.Monad.Trans.Writer (WriterT(..), runWriterT) import qualified Blaze.ByteString.Builder.Char8 as BBB import Data.Acquire (Acquire, mkAcquire, with) import Data.Aeson import Data.Aeson.Types (modifyFailure) -import qualified Data.Attoparsec.Text as AT import qualified Data.Attoparsec.ByteString.Char8 as P +import qualified Data.Attoparsec.Text as AT import Data.Bits ((.&.)) import Data.ByteString (ByteString) import qualified Data.ByteString.Builder as BB @@ -77,16 +77,16 @@ import qualified Data.ByteString.Char8 as B8 import Data.Char (ord) import Data.Conduit import qualified Data.Conduit.List as CL -import Data.Data ( Data, Typeable ) +import Data.Data (Data, Typeable) import Data.Either (partitionEithers) import Data.Fixed (Fixed(..), Pico) import Data.Function (on) +import Data.IORef import Data.Int (Int64) import qualified Data.IntMap as I -import Data.IORef -import Data.List (find, sort, groupBy, foldl') -import Data.List.NonEmpty (NonEmpty) +import Data.List (find, foldl', groupBy, sort) import qualified Data.List as List +import Data.List.NonEmpty (NonEmpty) import qualified Data.List.NonEmpty as NEL import qualified Data.Map as Map import Data.Maybe @@ -99,7 +99,7 @@ import qualified Data.Text as T import qualified Data.Text.Encoding as T import qualified Data.Text.IO as T import Data.Text.Read (rational) -import Data.Time (utc, NominalDiffTime, localTimeToUTC) +import Data.Time (NominalDiffTime, localTimeToUTC, utc) import System.Environment (getEnvironment) import Database.Persist.Sql @@ -1808,7 +1808,7 @@ copyField = CopyField -- -- Called thusly, this method will insert a new record (if none exists) OR update a recordField with a new value -- assuming the condition in the last block is met. --- +-- -- @since 2.12.1.0 upsertWhere :: ( backend ~ PersistEntityBackend record @@ -1939,7 +1939,7 @@ mkBulkUpsertQuery records conn fieldValues updates filters = fieldSets = map (\n -> T.concat [n, "=EXCLUDED.", n, ""]) updateFieldNames upds = map (Util.mkUpdateText' (escapeF) (\n -> T.concat [nameOfTable, ".", n])) updates updsValues = map (\(Update _ val _) -> toPersistValue val) updates - (wher, whereVals) = if null filters + (wher, whereVals) = if null filters then ("", []) else (filterClauseWithVals (Just PrefixTableName) conn filters) updateText = case fieldSets <> upds <> condFieldSets of diff --git a/persistent/ChangeLog.md b/persistent/ChangeLog.md index da9c1024d..4d1b76097 100644 --- a/persistent/ChangeLog.md +++ b/persistent/ChangeLog.md @@ -2,7 +2,7 @@ ## 2.12.1.0 -* [#]() +* [#1226](https://github.com/yesodweb/persistent/pull/1226) * Expose the `filterClause` and `filterClauseWithValues` functions to support the `upsertWhere` functionality in `persistent-postgresql`. diff --git a/persistent/Database/Persist/Sql.hs b/persistent/Database/Persist/Sql.hs index b4209cfb3..5bb716e98 100644 --- a/persistent/Database/Persist/Sql.hs +++ b/persistent/Database/Persist/Sql.hs @@ -30,13 +30,13 @@ import Control.Monad.IO.Class import Control.Monad.Trans.Reader (ReaderT, ask) import Database.Persist -import Database.Persist.Sql.Types -import Database.Persist.Sql.Types.Internal (IsolationLevel (..)) import Database.Persist.Sql.Class -import Database.Persist.Sql.Run hiding (rawAcquireSqlConn, rawRunSqlPool) -import Database.Persist.Sql.Raw -import Database.Persist.Sql.Migration import Database.Persist.Sql.Internal +import Database.Persist.Sql.Migration +import Database.Persist.Sql.Raw +import Database.Persist.Sql.Run hiding (rawAcquireSqlConn, rawRunSqlPool) +import Database.Persist.Sql.Types +import Database.Persist.Sql.Types.Internal (IsolationLevel(..)) import Database.Persist.Sql.Orphan.PersistQuery import Database.Persist.Sql.Orphan.PersistStore From 00ebec7e2ec7452fb0d774e26483fcdfa02b51e4 Mon Sep 17 00:00:00 2001 From: parsonsmatt Date: Thu, 8 Apr 2021 13:23:53 -0600 Subject: [PATCH 3/3] bump version numbers --- persistent-postgresql/persistent-postgresql.cabal | 2 +- persistent/persistent.cabal | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/persistent-postgresql/persistent-postgresql.cabal b/persistent-postgresql/persistent-postgresql.cabal index 1bc6d7027..158569aee 100644 --- a/persistent-postgresql/persistent-postgresql.cabal +++ b/persistent-postgresql/persistent-postgresql.cabal @@ -1,5 +1,5 @@ name: persistent-postgresql -version: 2.12.0.0 +version: 2.12.1.0 license: MIT license-file: LICENSE author: Felipe Lessa, Michael Snoyman diff --git a/persistent/persistent.cabal b/persistent/persistent.cabal index 26f1c9d27..2719a7983 100644 --- a/persistent/persistent.cabal +++ b/persistent/persistent.cabal @@ -1,5 +1,5 @@ name: persistent -version: 2.12.0.2 +version: 2.12.1.0 license: MIT license-file: LICENSE author: Michael Snoyman