Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,16 +954,22 @@ public function createDocuments(string $collection, array $documents): array
throw new DatabaseException('All documents must have an sequence if one is set');
}
}

$attributeKeys = array_unique($attributeKeys);

if ($hasSequence) {
$attributeKeys[] = '_id';
}

if ($this->sharedTables) {
$attributeKeys[] = '_tenant';
}

$columns = [];
foreach ($attributeKeys as $key => $attribute) {
$columns[$key] = "`{$this->filter($attribute)}`";
$columns[$key] = $this->quote($this->filter($attribute));
}

$columns = '(' . \implode(', ', $columns) . ')';

$bindIndex = 0;
Expand All @@ -982,7 +988,6 @@ public function createDocuments(string $collection, array $documents): array

if (!empty($document->getSequence())) {
$attributes['_id'] = $document->getSequence();
$attributeKeys[] = '_id';
} else {
$documentIds[] = $document->getId();
}
Expand All @@ -1007,6 +1012,7 @@ public function createDocuments(string $collection, array $documents): array
}

$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';

foreach (Database::PERMISSIONS as $type) {
foreach ($document->getPermissionsByType($type) as $permission) {
$tenantBind = $this->sharedTables ? ", :_tenant_{$index}" : '';
Expand Down
48 changes: 30 additions & 18 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ public function createDocument(string $collection, Document $document): Document
* @return array<Document>
*
* @throws DuplicateException
* @throws \Throwable
*/
public function createDocuments(string $collection, array $documents): array
{
Expand All @@ -1060,37 +1061,44 @@ public function createDocuments(string $collection, array $documents): array

try {
$name = $this->filter($collection);

$attributeKeys = Database::INTERNAL_ATTRIBUTE_KEYS;

$hasSequence = null;
foreach ($documents as $document) {
$attributes = $document->getAttributes();
$attributeKeys = array_merge($attributeKeys, array_keys($attributes));
$attributeKeys = [...$attributeKeys, ...\array_keys($attributes)];

if ($hasSequence === null) {
$hasSequence = !empty($document->getSequence());
} elseif ($hasSequence == empty($document->getSequence())) {
throw new DatabaseException('All documents must have an sequence if one is set');
}
}

$attributeKeys = array_unique($attributeKeys);

if ($hasSequence) {
$attributeKeys[] = '_id';
}

if ($this->sharedTables) {
$attributeKeys[] = '_tenant';
}

$columns = [];
foreach ($attributeKeys as $key => $attribute) {
$columns[$key] = "\"{$this->filter($attribute)}\"";
$columns[$key] = $this->quote($this->filter($attribute));
}
$columns = '(' . \implode(', ', $columns) . ')';

$sequences = [];
$columns = '(' . \implode(', ', $columns) . ')';

$bindIndex = 0;
$batchKeys = [];
$bindValues = [];
$permissions = [];
$documentIds = [];
$documentTenants = [];

foreach ($documents as $index => $document) {
$attributes = $document->getAttributes();
Expand All @@ -1100,13 +1108,14 @@ public function createDocuments(string $collection, array $documents): array
$attributes['_permissions'] = \json_encode($document->getPermissions());

if (!empty($document->getSequence())) {
$sequences[$document->getId()] = true;
$attributes['_id'] = $document->getSequence();
$attributeKeys[] = '_id';
} else {
$documentIds[] = $document->getId();
}

if ($this->sharedTables) {
$attributes['_tenant'] = $document->getTenant();
$documentTenants[] = $document->getTenant();
}

$bindKeys = [];
Expand All @@ -1124,6 +1133,7 @@ public function createDocuments(string $collection, array $documents): array
}

$batchKeys[] = '(' . \implode(', ', $bindKeys) . ')';

foreach (Database::PERMISSIONS as $type) {
foreach ($document->getPermissionsByType($type) as $permission) {
$tenantBind = $this->sharedTables ? ", :_tenant_{$index}" : '';
Expand All @@ -1145,7 +1155,7 @@ public function createDocuments(string $collection, array $documents): array
$stmt->bindValue($key, $value, $this->getPDOType($value));
}

$this->execute($stmt);
$stmt->execute();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the reason we can't move to SQL.php, we need to use the Postgres specific execute method here to handle timeouts

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it .. Fixed
I think we we should abstract this function

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, can you address in another PR? Makes sense to have it adapter level and have only Postgres override the SQL implementation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100%


if (!empty($permissions)) {
$tenantColumn = $this->sharedTables ? ', _tenant' : '';
Expand All @@ -1165,20 +1175,22 @@ public function createDocuments(string $collection, array $documents): array
}
}

$this->execute($stmtPermissions);
$stmtPermissions?->execute();
}
} catch (PDOException $e) {
throw $this->processException($e);
}

foreach ($documents as $document) {
if (!isset($sequences[$document->getId()])) {
$document['$sequence'] = $this->getDocument(
$collection,
$document->getId(),
[Query::select(['$sequence'])]
)->getSequence();
$sequences = $this->getSequences(
$collection,
$documentIds,
$documentTenants
);

foreach ($documents as $document) {
if (isset($sequences[$document->getId()])) {
$document['$sequence'] = $sequences[$document->getId()];
}
}
} catch (PDOException $e) {
throw $this->processException($e);
}

return $documents;
Expand Down
68 changes: 62 additions & 6 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,8 @@ public function testCreateDocument(): Document

return $document;
}
/**
* @return array<Document>
*/
public function testCreateDocuments(): array

public function testCreateDocuments(): void
{
$count = 3;
$collection = 'testCreateDocuments';
Expand Down Expand Up @@ -242,7 +240,27 @@ public function testCreateDocuments(): array
]);
}

$count = $database->createDocuments($collection, $documents, 3);
$results = [];

$count = $database->createDocuments($collection, $documents, 3, onNext: function ($doc) use (&$results) {
$results[] = $doc;
});

$this->assertEquals($count, \count($results));

foreach ($results as $document) {
$this->assertNotEmpty(true, $document->getId());
$this->assertIsString($document->getAttribute('string'));
$this->assertEquals('text📝', $document->getAttribute('string')); // Also makes sure an emoji is working
$this->assertIsInt($document->getAttribute('integer'));
$this->assertEquals(5, $document->getAttribute('integer'));
$this->assertIsInt($document->getAttribute('bigint'));
$this->assertEquals(9223372036854775807, $document->getAttribute('bigint'));
}

$documents = $database->find($collection, [
Query::orderAsc()
]);

$this->assertEquals($count, \count($documents));

Expand All @@ -255,8 +273,46 @@ public function testCreateDocuments(): array
$this->assertIsInt($document->getAttribute('bigint'));
$this->assertEquals(9223372036854775807, $document->getAttribute('bigint'));
}
}

return $documents;
public function testCreateDocumentsWithAutoIncrement(): void
{
/** @var Database $database */
$database = static::getDatabase();

$database->createCollection(__FUNCTION__);

$this->assertEquals(true, $database->createAttribute(__FUNCTION__, 'string', Database::VAR_STRING, 128, true));

/** @var array<Document> $documents */
$documents = [];
$count = 10;
$sequence = 1_000_000;

for ($i = $sequence; $i <= ($sequence + $count); $i++) {
$documents[] = new Document([
'$sequence' => (string)$i,
'$permissions' => [
Permission::read(Role::any()),
Permission::create(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'string' => 'text',
]);
}

$count = $database->createDocuments(__FUNCTION__, $documents, 6);
$this->assertEquals($count, \count($documents));

$documents = $database->find(__FUNCTION__, [
Query::orderAsc()
]);
foreach ($documents as $index => $document) {
$this->assertEquals($sequence + $index, $document->getSequence());
$this->assertNotEmpty(true, $document->getId());
$this->assertEquals('text', $document->getAttribute('string'));
}
}

public function testCreateDocumentsWithDifferentAttributes(): void
Expand Down
Loading