From 7363148d2572e4109bde8a9fec093385d34e6c48 Mon Sep 17 00:00:00 2001 From: Dylan Martin Date: Tue, 20 Apr 2021 11:50:22 -0700 Subject: [PATCH 1/4] trying my bset --- .../Database/Persist/Postgresql.hs | 30 +++++++++---------- persistent-postgresql/test/UpsertWhere.hs | 3 +- persistent/Database/Persist/Sql/Raw.hs | 4 ++- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/persistent-postgresql/Database/Persist/Postgresql.hs b/persistent-postgresql/Database/Persist/Postgresql.hs index 73c967fb9..44f3dfec1 100644 --- a/persistent-postgresql/Database/Persist/Postgresql.hs +++ b/persistent-postgresql/Database/Persist/Postgresql.hs @@ -84,7 +84,7 @@ import Data.Function (on) import Data.IORef import Data.Int (Int64) import qualified Data.IntMap as I -import Data.List (find, foldl', groupBy, sort) +import Data.List (find, foldl', groupBy, sort, nub) import qualified Data.List as List import Data.List.NonEmpty (NonEmpty) import qualified Data.List.NonEmpty as NEL @@ -1817,6 +1817,7 @@ upsertWhere , MonadIO m , PersistStore backend , BackendCompatible SqlBackend backend + , OnlyOneUniqueKey record ) => record -> [Update record] @@ -1829,7 +1830,7 @@ upsertWhere record updates filts = -- `upsertManyWhere` -- -- @since 2.12.1.0 --- TODO: we could probably make a sum type for the `Filter` record that's passed into the `upserWhere` and +-- TODO: we could probably make a sum type for the `Filter` record that's passed into the `upsertWhere` and -- `upsertManyWhere` methods that has similar behavior to the HandleCollisionUpdate type. excludeNotEqualToOriginal :: (PersistField typ @@ -1860,29 +1861,26 @@ excludeNotEqualToOriginal field = -- it will only do this update on a user-supplied condition. -- For example, here's how this method could be called like such: -- --- upsertManyWhere [record] [recordField =. newValue] [recordField /= newValue] +-- upsertManyWhere [record] [recordField =. newValue] [recordField !=. newValue] -- -- 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 +-- @since 2.12.1.0 upsertManyWhere :: forall record backend m. ( backend ~ PersistEntityBackend record, BackendCompatible SqlBackend backend, PersistEntityBackend record ~ SqlBackend, PersistEntity record, + OnlyOneUniqueKey record, MonadIO m - ) => - -- | A list of the records you want to insert, or update - [record] -> - -- | A list of the fields you want to copy over. - [HandleUpdateCollision record] -> - -- | A list of the updates to apply that aren't dependent on the record being inserted. - [Update record] -> - -- | A filter condition that dictates the scope of the updates - [Filter record] -> - ReaderT backend m () + ) + => [record] -- ^ A list of the records you want to insert, or update + -> [HandleUpdateCollision record] -- ^ A list of the fields you want to copy over. + -> [Update record] -- ^ A list of the updates to apply that aren't dependent on the record being inserted. + -> [Filter record] -- ^ A filter condition that dictates the scope of the updates + -> ReaderT backend m () upsertManyWhere [] _ _ _ = return () upsertManyWhere records fieldValues updates filters = do conn <- asks projectBackend @@ -1894,7 +1892,7 @@ upsertManyWhere records fieldValues updates filters = do -- a dummy/no-op update using the first field of the record. This avoids -- duplicate key exceptions. mkBulkUpsertQuery - :: (PersistEntity record, PersistEntityBackend record ~ SqlBackend) + :: (PersistEntity record, PersistEntityBackend record ~ SqlBackend, OnlyOneUniqueKey record) => [record] -- ^ A list of the records you want to insert, or update -> SqlBackend -> [HandleUpdateCollision record] -- ^ A list of the fields you want to copy over. @@ -1910,7 +1908,7 @@ mkBulkUpsertQuery records conn fieldValues updates filters = (fieldsToMaybeCopy, updateFieldNames) = partitionEithers $ map mfieldDef fieldValues fieldDbToText = escapeF . fieldDB entityDef' = entityDef records - conflictColumns = escapeF . fieldDB <$> entityKeyFields entityDef' + conflictColumns = nub (escapeF . fieldDB <$> entityKeyFields entityDef') ++ concatMap (map (escapeF . snd) . uniqueFields) (entityUniques entityDef') firstField = case entityFieldNames of [] -> error "The entity you're trying to insert does not have any fields." (field:_) -> field diff --git a/persistent-postgresql/test/UpsertWhere.hs b/persistent-postgresql/test/UpsertWhere.hs index 626f83443..7dd8b1d62 100644 --- a/persistent-postgresql/test/UpsertWhere.hs +++ b/persistent-postgresql/test/UpsertWhere.hs @@ -18,6 +18,7 @@ import PgInit share [mkPersist sqlSettings, mkMigrate "upsertWhereMigrate"] [persistLowerCase| Item name Text sqltype=varchar(80) + UniqueName name description Text price Double Maybe quantity Int Maybe @@ -44,7 +45,7 @@ specs = describe "UpsertWhere" $ do let newDescription = "I am a new description" insert_ item1 upsertWhere - (Item "item1" "i am inserted description" (Just 1) (Just 2)) + (Item "item1" "i am an inserted description" (Just 1) (Just 2)) [ItemDescription =. newDescription] [] Just item <- get (ItemKey "item1") diff --git a/persistent/Database/Persist/Sql/Raw.hs b/persistent/Database/Persist/Sql/Raw.hs index b3bd2b72e..3b5128ddc 100644 --- a/persistent/Database/Persist/Sql/Raw.hs +++ b/persistent/Database/Persist/Sql/Raw.hs @@ -48,7 +48,9 @@ rawExecute :: (MonadIO m, BackendCompatible SqlBackend backend) => Text -- ^ SQL statement, possibly with placeholders. -> [PersistValue] -- ^ Values to fill the placeholders. -> ReaderT backend m () -rawExecute x y = liftM (const ()) $ rawExecuteCount x y +rawExecute x y = do + liftIO $ putStrLn $ show x + liftM (const ()) $ rawExecuteCount x y -- | Execute a raw SQL statement and return the number of -- rows it has modified. From 3b397de48daf4cfa9d8e170e24680988e2111a00 Mon Sep 17 00:00:00 2001 From: Dylan Martin Date: Tue, 20 Apr 2021 12:30:49 -0700 Subject: [PATCH 2/4] idk maybe this works --- persistent-postgresql/Database/Persist/Postgresql.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/persistent-postgresql/Database/Persist/Postgresql.hs b/persistent-postgresql/Database/Persist/Postgresql.hs index 1206fa0d9..abfdf0843 100644 --- a/persistent-postgresql/Database/Persist/Postgresql.hs +++ b/persistent-postgresql/Database/Persist/Postgresql.hs @@ -84,7 +84,7 @@ import Data.Function (on) import Data.IORef import Data.Int (Int64) import qualified Data.IntMap as I -import Data.List (find, foldl', groupBy, sort, nub) +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 @@ -1908,7 +1908,7 @@ mkBulkUpsertQuery records conn fieldValues updates filters = (fieldsToMaybeCopy, updateFieldNames) = partitionEithers $ map mfieldDef fieldValues fieldDbToText = escapeF . fieldDB entityDef' = entityDef records - conflictColumns = nub (escapeF . fieldDB <$> entityKeyFields entityDef') ++ concatMap (map (escapeF . snd) . uniqueFields) (entityUniques entityDef') + conflictColumns = (escapeF . fieldDB <$> entityKeyFields entityDef') ++ concatMap (map (escapeF . snd) . uniqueFields) (entityUniques entityDef') firstField = case entityFieldNames of [] -> error "The entity you're trying to insert does not have any fields." (field:_) -> field @@ -1949,7 +1949,7 @@ mkBulkUpsertQuery records conn fieldValues updates filters = , Util.parenWrapped . Util.commaSeparated $ entityFieldNames , " VALUES " , recordPlaceholders - , " ON CONFLICT " + , " ON CONFLICT" , Util.parenWrapped $ Util.commaSeparated $ conflictColumns , " DO UPDATE SET " , updateText From 21a0672eba495eb57b066c4337c044418ca6ae2d Mon Sep 17 00:00:00 2001 From: Dylan Martin Date: Tue, 20 Apr 2021 12:31:32 -0700 Subject: [PATCH 3/4] remove logs --- persistent/Database/Persist/Sql/Raw.hs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/persistent/Database/Persist/Sql/Raw.hs b/persistent/Database/Persist/Sql/Raw.hs index 3b5128ddc..b3bd2b72e 100644 --- a/persistent/Database/Persist/Sql/Raw.hs +++ b/persistent/Database/Persist/Sql/Raw.hs @@ -48,9 +48,7 @@ rawExecute :: (MonadIO m, BackendCompatible SqlBackend backend) => Text -- ^ SQL statement, possibly with placeholders. -> [PersistValue] -- ^ Values to fill the placeholders. -> ReaderT backend m () -rawExecute x y = do - liftIO $ putStrLn $ show x - liftM (const ()) $ rawExecuteCount x y +rawExecute x y = liftM (const ()) $ rawExecuteCount x y -- | Execute a raw SQL statement and return the number of -- rows it has modified. From b735620312d980270cc1ec740f1fb2bcef124594 Mon Sep 17 00:00:00 2001 From: Dylan Martin Date: Tue, 20 Apr 2021 12:31:50 -0700 Subject: [PATCH 4/4] spacing --- persistent-postgresql/Database/Persist/Postgresql.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/persistent-postgresql/Database/Persist/Postgresql.hs b/persistent-postgresql/Database/Persist/Postgresql.hs index abfdf0843..957a31a31 100644 --- a/persistent-postgresql/Database/Persist/Postgresql.hs +++ b/persistent-postgresql/Database/Persist/Postgresql.hs @@ -1949,7 +1949,7 @@ mkBulkUpsertQuery records conn fieldValues updates filters = , Util.parenWrapped . Util.commaSeparated $ entityFieldNames , " VALUES " , recordPlaceholders - , " ON CONFLICT" + , " ON CONFLICT " , Util.parenWrapped $ Util.commaSeparated $ conflictColumns , " DO UPDATE SET " , updateText