Skip to content

upsertBy doesn't updates existing record #782

@pythonissam

Description

@pythonissam

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 updates

In 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 k

As a result, the current version of upsertBy doesn't replaces the record when the update record is null. Is this intended for some reason?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions