-
Notifications
You must be signed in to change notification settings - Fork 55
Add ignore param to createDocuments for silent duplicate handling #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
287420b
2b4fd1e
37aace4
9653052
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1460,11 +1460,18 @@ public function castingBefore(Document $collection, Document $document): Documen | |
| * @throws DuplicateException | ||
| * @throws DatabaseException | ||
| */ | ||
| public function createDocuments(Document $collection, array $documents): array | ||
| public function createDocuments(Document $collection, array $documents, bool $ignore = false): array | ||
| { | ||
| $name = $this->getNamespace() . '_' . $this->filter($collection->getId()); | ||
|
|
||
| $options = $this->getTransactionOptions(); | ||
| if ($ignore) { | ||
| // Run outside transaction — MongoDB aborts transactions on any write error, | ||
| // so ordered:false + session would roll back even successfully inserted docs. | ||
| $options = ['ordered' => false]; | ||
| } else { | ||
| $options = $this->getTransactionOptions(); | ||
| } | ||
|
|
||
| $records = []; | ||
| $hasSequence = null; | ||
| $documents = \array_map(fn ($doc) => clone $doc, $documents); | ||
|
|
@@ -1490,7 +1497,16 @@ public function createDocuments(Document $collection, array $documents): array | |
| try { | ||
| $documents = $this->client->insertMany($name, $records, $options); | ||
| } catch (MongoException $e) { | ||
| throw $this->processException($e); | ||
| $processed = $this->processException($e); | ||
|
|
||
| if ($ignore && $processed instanceof DuplicateException) { | ||
| // Race condition: a doc was inserted between pre-filter and insertMany. | ||
| // With ordered:false outside transaction, non-duplicate inserts persist. | ||
| // Return empty — we cannot determine which docs succeeded without querying. | ||
| return []; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't report the raced batch as empty. With 🤖 Prompt for AI Agents |
||
|
|
||
| throw $processed; | ||
| } | ||
|
|
||
| foreach ($documents as $index => $document) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a
The comment in the code acknowledges this, but callers of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Known limitation, documented in code comments. This only triggers on a race condition — the pre-fetch already filters known duplicates before they reach the adapter. The
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Known limitation. This only triggers on a race condition — pre-fetch filters known duplicates before reaching the adapter. The |
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1365,6 +1365,35 @@ public function updateDocument(Document $collection, string $id, Document $docum | |||||||||||||||||||||
| return $document; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| protected function getInsertKeyword(bool $ignore): string | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| return 'INSERT INTO'; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| protected function getInsertSuffix(bool $ignore, string $table): string | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| if (!$ignore) { | ||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $conflictTarget = $this->sharedTables ? '("_uid", "_tenant")' : '("_uid")'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return "ON CONFLICT {$conflictTarget} DO NOTHING"; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| protected function getInsertPermissionsSuffix(bool $ignore): string | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| if (!$ignore) { | ||||||||||||||||||||||
| return ''; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| $conflictTarget = $this->sharedTables | ||||||||||||||||||||||
| ? '("_type", "_permission", "_document", "_tenant")' | ||||||||||||||||||||||
| : '("_type", "_permission", "_document")'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return "ON CONFLICT {$conflictTarget} DO NOTHING"; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+1373
to
+1395
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Both
Actual unique indexes vs. conflict targets:
The fixes: // getInsertSuffix – main document table
protected function getInsertSuffix(bool $ignore, string $table): string
{
if (!$ignore) {
return '';
}
$conflictTarget = $this->sharedTables
? '("_uid" COLLATE utf8_ci_ai, "_tenant")'
: '("_uid" COLLATE utf8_ci_ai)';
return "ON CONFLICT {$conflictTarget} DO NOTHING";
}
// getInsertPermissionsSuffix – permissions table
protected function getInsertPermissionsSuffix(bool $ignore): string
{
if (!$ignore) {
return '';
}
$conflictTarget = $this->sharedTables
? '("_type", "_permission", "_document", "_tenant")'
: '("_document" COLLATE utf8_ci_ai, "_type", "_permission")';
return "ON CONFLICT {$conflictTarget} DO NOTHING";
}Without this fix,
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing upsertDocuments at [Postgres.php:1462] uses ("_uid") without COLLATE and works in production |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * @param string $tableName | ||||||||||||||||||||||
| * @param string $columns | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't silently escape the active transaction.
When
$ignoreis true this write no longer carries the current session, so a caller that is already inside a transaction can still end up committing these documents even if the surrounding transaction later rolls back. Failing fast here is safer than silently weakening atomicity.🔒 Suggested guard
📝 Committable suggestion
🤖 Prompt for AI Agents