-
Notifications
You must be signed in to change notification settings - Fork 301
Closed
Description
In persistent 2.6.1:
-- | Update based on a given uniqueness constraint or insert:
--
-- * insert the new record if it does not exist;
-- * update the existing record that matches the given uniqueness contraint.
upsertBy :: (MonadIO m, PersistRecordBackend record backend)
=> Unique record -- ^ uniqueness constraint to find by
-> record -- ^ new record to insert
-> [Update record]
-- ^ updates to perform if the record already exists (leaving
-- this empty is the equivalent of performing a 'repsert' on a
-- unique key)
-> ReaderT backend m (Entity record)
-- ^ the record in the database after the operation
upsertBy uniqueKey record updates = do
mExists <- getBy uniqueKey
k <- case mExists of
Just (Entity k _) -> do
when (null updates) (replace k record)
return k
Nothing -> insert record
Entity k `liftM` updateGet k updatesIn persistent 2.8.1:
-- | Update based on a given uniqueness constraint or insert:
--
-- * insert the new record if it does not exist;
-- * update the existing record that matches the given uniqueness constraint.
upsertBy
:: (MonadIO m, PersistRecordBackend record backend)
=> Unique record -- ^ uniqueness constraint to find by
-> record -- ^ new record to insert
-> [Update record] -- ^ updates to perform if the record already exists (leaving
-- this empty is the equivalent of performing a 'repsert' on a
-- unique key)
-> ReaderT backend m (Entity record) -- ^ the record in the database after the operation
upsertBy uniqueKey record updates = do
mrecord <- getBy uniqueKey
maybe (insertEntity record) (`updateGetEntity` updates) mrecord
where
updateGetEntity (Entity k _) upds =
(Entity k) `liftM` (updateGet k upds)`As you can see, the code below has completely disappeared:
Just (Entity k _) -> do
when (null updates) (replace k record)
return kAs a result, the current version of upsertBy doesn't replaces the record when the update record is null. Is this intended for some reason?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels