From b0798700db1d37a4f531b0f005e897f3dfca66a5 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Tue, 17 Sep 2024 14:16:55 +0900 Subject: [PATCH] Address comments, add MongoDB and Postgres Support --- composer.lock | 30 +++++++++++++++--------------- src/Database/Adapter/MariaDB.php | 2 -- src/Database/Adapter/Mongo.php | 27 ++++++++++++++++++++++++++- src/Database/Adapter/Postgres.php | 5 +---- src/Database/Database.php | 10 ++++++---- tests/e2e/Adapter/Base.php | 14 -------------- 6 files changed, 48 insertions(+), 40 deletions(-) diff --git a/composer.lock b/composer.lock index b98610e74..32455f27d 100644 --- a/composer.lock +++ b/composer.lock @@ -136,20 +136,20 @@ }, { "name": "symfony/polyfill-php80", - "version": "v1.30.0", + "version": "v1.31.0", "source": { "type": "git", "url": "https://github.com/symfony/polyfill-php80.git", - "reference": "77fa7995ac1b21ab60769b7323d600a991a90433" + "reference": "60328e362d4c2c802a54fcbf04f9d3fb892b4cf8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/77fa7995ac1b21ab60769b7323d600a991a90433", - "reference": "77fa7995ac1b21ab60769b7323d600a991a90433", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/60328e362d4c2c802a54fcbf04f9d3fb892b4cf8", + "reference": "60328e362d4c2c802a54fcbf04f9d3fb892b4cf8", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.2" }, "type": "library", "extra": { @@ -196,7 +196,7 @@ "shim" ], "support": { - "source": "https://github.com/symfony/polyfill-php80/tree/v1.30.0" + "source": "https://github.com/symfony/polyfill-php80/tree/v1.31.0" }, "funding": [ { @@ -212,7 +212,7 @@ "type": "tidelift" } ], - "time": "2024-05-31T15:07:36+00:00" + "time": "2024-09-09T11:45:10+00:00" }, { "name": "utopia-php/cache", @@ -506,16 +506,16 @@ }, { "name": "laravel/pint", - "version": "v1.17.2", + "version": "v1.17.3", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "e8a88130a25e3f9d4d5785e6a1afca98268ab110" + "reference": "9d77be916e145864f10788bb94531d03e1f7b482" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/e8a88130a25e3f9d4d5785e6a1afca98268ab110", - "reference": "e8a88130a25e3f9d4d5785e6a1afca98268ab110", + "url": "https://api.github.com/repos/laravel/pint/zipball/9d77be916e145864f10788bb94531d03e1f7b482", + "reference": "9d77be916e145864f10788bb94531d03e1f7b482", "shasum": "" }, "require": { @@ -526,13 +526,13 @@ "php": "^8.1.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.61.1", - "illuminate/view": "^10.48.18", + "friendsofphp/php-cs-fixer": "^3.64.0", + "illuminate/view": "^10.48.20", "larastan/larastan": "^2.9.8", "laravel-zero/framework": "^10.4.0", "mockery/mockery": "^1.6.12", "nunomaduro/termwind": "^1.15.1", - "pestphp/pest": "^2.35.0" + "pestphp/pest": "^2.35.1" }, "bin": [ "builds/pint" @@ -568,7 +568,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2024-08-06T15:11:54+00:00" + "time": "2024-09-03T15:00:28+00:00" }, { "name": "myclabs/deep-copy", diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 825ecefdb..7b1537d11 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -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}; diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 62b411ca3..41c3c147e 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -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; } /** diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 7ea145f85..9162ea12e 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -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} "; diff --git a/src/Database/Database.php b/src/Database/Database.php index 58b9d0000..944c3bea0 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4951,6 +4951,7 @@ private function deleteCascade(Document $collection, Document $relatedCollection * * @param string $collection * @param array $queries + * @param int $batchSize * * @return bool * @@ -4958,22 +4959,23 @@ private function deleteCascade(Document $collection, Document $relatedCollection * @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, diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index ff2998633..345605abb 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -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,