Replace a record if it exists and gracefully fails if it is not?

Resistant comes with two operations for the wholesale replacement of records: replace and repsert . With repsert new record is inserted into the database if the record to be replaced did not initially exist. If replace , if the record to be replaced does not exist, the behavior is undefined.

Both of these options are not enough for my use case: I want to replace the record if (and only if) a record already exists for this key. I could check if the record exists in the first place, and only try to insert if it returns the record, but it seems that it will have potential for the conditions of the race (although, admittedly, such conditions of the race are unlikely to have harmful consequences in this case).

Is there any function that tries to perform a replacement and returns a useful result depending on success or failure?

+6
source share

All Articles