It is now possible to get the last inserted ID from db_merge().
The accepted resolution was to add a getResult() method to the MergeQuery class.
Example implementation:
$merge = db_merge('block_custom')
->key(array(
'bid' => $delta,
))
->fields(array(
'body' => $edit['body']['value'],
'info' => $edit['info'],
'title' => $edit['title'],
'format' => $edit['body']['format'],
));
$result = $merge->execute();
if ($result === $merge::STATUS_INSERT) {
$delta = $merge->getResult();
}
The return value from $merge->execute() would stay the same, but after execution we could get the result from the $merge object.
Multiple previous reports stated that it was not possible, other than selecting the highest ID from the table, which either requires a table lock or causes a race condition.
Similar reports:
https://www.drupal.org/node/1231730
http://drupal.stackexchange.com/questions/58991/how-to-retrieve-last-mys...
Introduced in branch:
1.0.x
Introduced in version:
1.0.0
Impacts:
Module developers
Related Github Issues: