Skip to content

Commit bb419ba

Browse files
committed
Normalize timestamps in race-condition reconciliation for Postgres compatibility
1 parent 9487388 commit bb419ba

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

src/Database/Adapter/SQL.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,21 +2716,26 @@ public function createDocuments(Document $collection, array $documents, bool $ig
27162716
$rows = $verifyStmt->fetchAll();
27172717
$verifyStmt->closeCursor();
27182718

2719-
// Keep only docs whose _createdAt matches what we set (= ours, not racer's)
2719+
// Keep only docs whose _createdAt matches what we set (= ours, not racer's).
2720+
// Normalise timestamps before comparing — Postgres omits .000 for round seconds.
2721+
$normalizeTimestamp = fn (?string $ts): string => $ts !== null
2722+
? (new \DateTime($ts))->format('Y-m-d H:i:s.v')
2723+
: '';
2724+
27202725
$actualTimestamps = [];
27212726
foreach ($rows as $row) {
27222727
$key = ($this->sharedTables && $this->tenantPerDocument)
27232728
? $row['_tenant'] . ':' . $row['_uid']
27242729
: $row['_uid'];
2725-
$actualTimestamps[$key] = $row['_createdAt'];
2730+
$actualTimestamps[$key] = $normalizeTimestamp($row['_createdAt']);
27262731
}
27272732

27282733
$insertedDocs = [];
27292734
foreach ($documents as $doc) {
27302735
$key = ($this->sharedTables && $this->tenantPerDocument)
27312736
? $doc->getTenant() . ':' . $doc->getId()
27322737
: $doc->getId();
2733-
if (isset($actualTimestamps[$key]) && $actualTimestamps[$key] === $expectedTimestamps[$key]) {
2738+
if (isset($actualTimestamps[$key]) && $actualTimestamps[$key] === $normalizeTimestamp($expectedTimestamps[$key] ?? null)) {
27342739
$insertedDocs[] = $doc;
27352740
}
27362741
}

0 commit comments

Comments
 (0)