-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
Feature Request
MySQL/MariaDB supports INSERT ... ON DUPLICATE KEY UPDATE for upsert operations, but Atlas doesn't currently provide a way to generate this syntax.
Proposed API
Add an onDuplicateKeyUpdate() method to the Insert class:
/**
* Add ON DUPLICATE KEY UPDATE clause to INSERT statement
*
* @param array $columns Column names and values to update on duplicate key
* @return static
*/
public function onDuplicateKeyUpdate(array $columns): static
Example Usage
$insert = Insert::new('mysql')
->into('users')
->columns(['id' => 1, 'name' => 'John'])
->onDuplicateKeyUpdate(['name' => 'Jane']);
echo $insert->getQueryString();
// INSERT INTO users (id, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?
print_r($insert->getBindValueArrays());
// [1, 'John', 'Jane']
Use Case
This prevents race condition errors when multiple processes might insert the same record simultaneously -- the first succeeds with INSERT, subsequent ones UPDATE instead of throwing duplicate key errors.
Is this something you'd accept?
Metadata
Metadata
Assignees
Labels
No labels