Skip to content

Commit

Permalink
Address comments, add MongoDB and Postgres Support
Browse files Browse the repository at this point in the history
  • Loading branch information
PineappleIOnic committed Sep 17, 2024
1 parent 79e65da commit b079870
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
30 changes: 15 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1661,8 +1661,6 @@ public function deleteDocuments(string $collection, array $queries): bool

$sqlWhere = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';

$selections = $this->getAttributeSelections($queries);

$sql = "
DELETE FROM {$this->getSQLTable($name)}
{$sqlWhere};
Expand Down
27 changes: 26 additions & 1 deletion src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,32 @@ public function deleteDocument(string $collection, string $id): bool
*/
public function deleteDocuments(string $collection, array $queries): bool
{
throw new \Exception('Not Implemented');
$name = $this->getNamespace() . '_' . $this->filter($collection);
$queries = array_map(fn ($query) => clone $query, $queries);

$filters = $this->buildFilters($queries);

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

$filters = $this->replaceInternalIdsKeys($filters, '$', '_', $this->operators);
$filters = $this->timeFilter($filters);

$options = [];

try {
$res = $this->client->delete(
collection: $name,
filters: $filters,
options: $options,
limit: 0
);
} catch (MongoException $e) {
$this->processException($e);
}

return true;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1585,11 +1585,8 @@ public function deleteDocuments(string $collection, array $queries): bool

$sqlWhere = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';

$selections = $this->getAttributeSelections($queries);

$sql = "
SELECT {$this->getAttributeProjection($selections, 'table_main')}
FROM {$this->getSQLTable($name)} as table_main
DELETE FROM {$this->getSQLTable($name)}
{$sqlWhere}
";

Expand Down
10 changes: 6 additions & 4 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -4951,29 +4951,31 @@ private function deleteCascade(Document $collection, Document $relatedCollection
*
* @param string $collection
* @param array<Query> $queries
* @param int $batchSize
*
* @return bool
*
* @throws AuthorizationException
* @throws DatabaseException
* @throws RestrictedException
*/
public function deleteDocuments(string $collection, array $queries = []): bool
public function deleteDocuments(string $collection, array $queries = [], int $batchSize = 100): bool
{
if ($this->adapter->getSharedTables() && empty($this->adapter->getTenant())) {
throw new DatabaseException('Missing tenant. Tenant must be set when table sharing is enabled.');
}

$queries = Query::groupByType($queries)['filters'];
$collection = $this->silent(fn () => $this->getCollection($collection));

$deleted = $this->withTransaction(function () use ($collection, $queries) {
$deleted = $this->withTransaction(function () use ($collection, $queries, $batchSize) {
$lastDocument = null;
while (true) {
$affectedDocuments = $this->find($collection->getId(), array_merge(
empty($lastDocument) ? [
Query::limit(100),
Query::limit($batchSize),
] : [
Query::limit(100),
Query::limit($batchSize),
Query::cursorAfter($lastDocument),
],
$queries,
Expand Down
14 changes: 0 additions & 14 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -15612,20 +15612,6 @@ public function testEvents(): void
public function propegateBulkDocuments(bool $documentSecurity = false): void
{
for ($i = 0; $i < 10; $i++) {
var_dump(new Document(
array_merge([
'$id' => 'doc' . $i,
'text' => 'value' . $i,
'integer' => $i
], $documentSecurity ? [
'$permissions' => [
Permission::delete(Role::any()),
Permission::create(Role::any()),
Permission::read(Role::any()),
],
] : [])
));

static::getDatabase()->createDocument('bulk_delete', new Document(
array_merge([
'$id' => 'doc' . $i,
Expand Down

0 comments on commit b079870

Please sign in to comment.