Skip to content

Commit dacf453

Browse files
authored
Merge pull request #23 from event-engine/bugfix/22-sql-constraint-error
Check if doc is null - Close #22
2 parents 92b60a8 + 6162636 commit dacf453

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/PostgresDocumentStore.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public function upsertDoc(string $collectionName, string $docId, array $docOrSub
427427
{
428428
$doc = $this->getDoc($collectionName, $docId);
429429

430-
if($doc) {
430+
if ($doc !== null) {
431431
$this->updateDoc($collectionName, $docId, $docOrSubset);
432432
} else {
433433
$this->addDoc($collectionName, $docId, $docOrSubset);

tests/PostgresDocumentStoreTest.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function it_adds_collection_with_multi_field_index_unique(): void
150150
);
151151
$this->assertStringStartsWith('CREATE UNIQUE INDEX', $indexes[1]['indexdef']);
152152
}
153-
153+
154154
/**
155155
* @test
156156
*/
@@ -216,6 +216,30 @@ public function it_updates_a_subset_of_a_doc()
216216
$this->assertArrayNotHasKey('other', $filteredDocs[0]['some']);
217217
}
218218

219+
/**
220+
* @test
221+
*/
222+
public function it_upserts_empty_array_doc(): void
223+
{
224+
$collectionName = 'test_upserts_empty_doc';
225+
$this->documentStore->addCollection($collectionName);
226+
227+
$doc = [];
228+
229+
$docId = Uuid::uuid4()->toString();
230+
$this->documentStore->addDoc($collectionName, $docId, $doc);
231+
232+
// be aware that this will add the data as an entry to the array which is wrong, because it should be transformed to an object
233+
$this->documentStore->upsertDoc($collectionName, $docId, [
234+
'some' => [
235+
'prop' => 'fuzz',
236+
],
237+
]);
238+
239+
$doc = $this->documentStore->getDoc($collectionName, $docId);
240+
$this->assertArrayHasKey('some', $doc[0], \var_export($doc, true));
241+
}
242+
219243
/**
220244
* @test
221245
*/

0 commit comments

Comments
 (0)