Skip to content

Commit 6738511

Browse files
committed
CHG upsert starts a transaction.
When an upsert query is executed, a transaction is started to prevent failures from concurrent conetions changing records.
1 parent 0b193d5 commit 6738511

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/DatabaseHandler.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ public function executeUpsert(UpsertQuery $query): UpsertResult
199199
$keys = $query->getKeys();
200200
$keys = array_combine($keys, $keys);
201201

202+
// Creates a transaction to prevent concurrent failures.
203+
$transactionStarted = false;
204+
if (!$this->inTransaction()) {
205+
$this->startTransaction();
206+
$transactionStarted = true;
207+
}
208+
202209
// Finds all rows that can match.
203210
$index = new Index($query, $keys);
204211
$selectQuery = Query::selectFrom($query->getTable());
@@ -245,6 +252,11 @@ public function executeUpsert(UpsertQuery $query): UpsertResult
245252
$success &= $updateResult->success();
246253
}
247254
}
255+
256+
// Commits transaction if this started inside this method.
257+
if ($transactionStarted) {
258+
$this->commit();
259+
}
248260
return new UpsertResult($inserts, $updates, $numInserts, $numUpdates, $insertedId, true);
249261
}
250262

0 commit comments

Comments
 (0)