Skip to content

Commit 471d31f

Browse files
Address Comments
1 parent 8adb126 commit 471d31f

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,17 @@ public function createDocuments(string $collection, array $documents, int $batch
982982
$name = $this->filter($collection);
983983

984984
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
985+
986+
$hasInternalId = null;
985987
foreach ($documents as $document) {
986988
$attributes = $document->getAttributes();
987989
$attributeKeys = array_merge($attributeKeys, array_keys($attributes));
990+
991+
if ($hasInternalId === null) {
992+
$hasInternalId = !empty($document->getInternalId());
993+
} elseif ($hasInternalId == empty($document->getInternalId())) {
994+
throw new DatabaseException('All documents must have an internalId if one is set');
995+
}
988996
}
989997
$attributeKeys = array_unique($attributeKeys);
990998

src/Database/Adapter/Postgres.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,11 +1041,20 @@ public function createDocuments(string $collection, array $documents, int $batch
10411041
$name = $this->filter($collection);
10421042

10431043
$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;
1044+
1045+
$hasInternalId = null;
10441046
foreach ($documents as $document) {
10451047
$attributes = $document->getAttributes();
10461048
$attributeKeys = array_merge($attributeKeys, array_keys($attributes));
1049+
1050+
if ($hasInternalId === null) {
1051+
$hasInternalId = !empty($document->getInternalId());
1052+
} elseif ($hasInternalId == empty($document->getInternalId())) {
1053+
throw new DatabaseException('All documents must have an internalId if one is set');
1054+
}
10471055
}
10481056
$attributeKeys = array_unique($attributeKeys);
1057+
10491058
if ($this->sharedTables) {
10501059
$attributeKeys[] = '_tenant';
10511060
}

tests/e2e/Adapter/Base.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,36 @@ public function testCreateDocumentsWithDifferentAttributes(): void
22732273
$this->assertNull($documents[1]->getAttribute('integer'));
22742274
$this->assertEquals('default', $documents[1]->getAttribute('string_default'));
22752275

2276+
/**
2277+
* Expect fail, mix of internalId and no internalId
2278+
*/
2279+
$documents = [
2280+
new Document([
2281+
'$id' => 'third',
2282+
'$internalId' => 'third',
2283+
'string' => 'text📝',
2284+
]),
2285+
new Document([
2286+
'$id' => 'fourth',
2287+
'string' => 'text📝',
2288+
]),
2289+
];
2290+
2291+
try {
2292+
static::getDatabase()->createDocuments($collection, $documents);
2293+
$this->fail('Failed to throw exception');
2294+
} catch (Throwable $e) {
2295+
$this->assertTrue($e instanceof DatabaseException);
2296+
}
2297+
2298+
$documents = array_reverse($documents);
2299+
try {
2300+
static::getDatabase()->createDocuments($collection, $documents);
2301+
$this->fail('Failed to throw exception');
2302+
} catch (Throwable $e) {
2303+
$this->assertTrue($e instanceof DatabaseException);
2304+
}
2305+
22762306
static::getDatabase()->deleteCollection($collection);
22772307
}
22782308

0 commit comments

Comments
 (0)