From 76a4604d78607c45acdc870cab138ca1a590890f Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 1 Nov 2023 01:09:31 +0000 Subject: [PATCH 01/15] update cache namespace and skip structure validation on create. --- src/Database/Database.php | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 42046db4d..becf5a5cf 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -132,6 +132,8 @@ class Database protected Cache $cache; + protected string $name; + /** * @var array */ @@ -316,7 +318,7 @@ class Database * @param Cache $cache * @param array $filters */ - public function __construct(Adapter $adapter, Cache $cache, array $filters = []) + public function __construct(Adapter $adapter, Cache $cache, array $filters = [], string $name = 'default') { $this->adapter = $adapter; $this->cache = $cache; @@ -2319,7 +2321,7 @@ public function getDocument(string $collection, string $id, array $queries = []) $validator = new Authorization(self::PERMISSION_READ); $documentSecurity = $collection->getAttribute('documentSecurity', false); - $cacheKey = 'cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id; + $cacheKey = $this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id; if (!empty($selections)) { $cacheKey .= ':' . \md5(\implode($selections)); @@ -2390,7 +2392,7 @@ public function getDocument(string $collection, string $id, array $queries = []) */ foreach ($this->map as $key => $value) { list($k, $v) = explode('=>', $key); - $ck = 'cache-' . $this->getNamespace() . ':map:' . $k; + $ck = $this->name . '-cache-' . $this->getNamespace() . ':map:' . $k; $cache = $this->cache->load($ck, self::TTL); if (empty($cache)) { $cache = []; @@ -2696,10 +2698,10 @@ public function createDocument(string $collection, Document $document): Document throw new InvalidArgumentException($validator->getDescription()); } - $structure = new Structure($collection); - if (!$structure->isValid($document)) { - throw new StructureException($structure->getDescription()); - } + // $structure = new Structure($collection); + // if (!$structure->isValid($document)) { + // throw new StructureException($structure->getDescription()); + // } if ($this->resolveRelationships) { $document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document)); @@ -3152,7 +3154,7 @@ public function updateDocument(string $collection, string $id, Document $documen $this->purgeRelatedDocuments($collection, $id); - $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); + $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); $this->trigger(self::EVENT_DOCUMENT_UPDATE, $document); @@ -3598,7 +3600,7 @@ public function increaseDocumentAttribute(string $collection, string $id, string $max = $max ? $max - $value : null; $result = $this->adapter->increaseDocumentAttribute($collection->getId(), $id, $attribute, $value, null, $max); - $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); + $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); $this->trigger(self::EVENT_DOCUMENT_INCREASE, $document); @@ -3665,7 +3667,7 @@ public function decreaseDocumentAttribute(string $collection, string $id, string $min = $min ? $min + $value : null; $result = $this->adapter->increaseDocumentAttribute($collection->getId(), $id, $attribute, $value * -1, $min); - $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); + $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); $this->trigger(self::EVENT_DOCUMENT_DECREASE, $document); return $result; @@ -3718,7 +3720,7 @@ public function deleteDocument(string $collection, string $id): bool } $this->purgeRelatedDocuments($collection, $id); - $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); + $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id . ':*'); $deleted = $this->adapter->deleteDocument($collection->getId(), $id); @@ -4103,7 +4105,7 @@ private function deleteCascade(Document $collection, Document $relatedCollection */ public function deleteCachedCollection(string $collection): bool { - return $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection . ':*'); + return $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection . ':*'); } /** @@ -4117,7 +4119,7 @@ public function deleteCachedCollection(string $collection): bool */ public function deleteCachedDocument(string $collection, string $id): bool { - return $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection . ':' . $id . ':*'); + return $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection . ':' . $id . ':*'); } /** @@ -4822,7 +4824,7 @@ private function purgeRelatedDocuments(Document $collection, string $id): void return; } - $key = 'cache-' . $this->getNamespace() . ':map:' . $collection->getId() . ':' . $id; + $key = $this->name . '-cache-' . $this->getNamespace() . ':map:' . $collection->getId() . ':' . $id; $cache = $this->cache->load($key, self::TTL); if (!empty($cache)) { foreach ($cache as $v) { From fdc6e975be0888736486467571e9bb251dd67368 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 1 Nov 2023 01:36:22 +0000 Subject: [PATCH 02/15] fix name initialization --- composer.lock | 24 ++++++++++++------------ src/Database/Database.php | 1 + 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/composer.lock b/composer.lock index 91cb09a27..573407f5b 100644 --- a/composer.lock +++ b/composer.lock @@ -513,16 +513,16 @@ }, { "name": "laravel/pint", - "version": "v1.13.3", + "version": "v1.13.5", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "93b2d0d49719bc6e444ba21cd4dbbccec935413d" + "reference": "df105cf8ce7a8f0b8a9425ff45cd281a5448e423" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/93b2d0d49719bc6e444ba21cd4dbbccec935413d", - "reference": "93b2d0d49719bc6e444ba21cd4dbbccec935413d", + "url": "https://api.github.com/repos/laravel/pint/zipball/df105cf8ce7a8f0b8a9425ff45cd281a5448e423", + "reference": "df105cf8ce7a8f0b8a9425ff45cd281a5448e423", "shasum": "" }, "require": { @@ -534,12 +534,12 @@ }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.34.1", - "illuminate/view": "^10.23.1", + "illuminate/view": "^10.26.2", "laravel-zero/framework": "^10.1.2", "mockery/mockery": "^1.6.6", "nunomaduro/larastan": "^2.6.4", "nunomaduro/termwind": "^1.15.1", - "pestphp/pest": "^2.18.2" + "pestphp/pest": "^2.20.0" }, "bin": [ "builds/pint" @@ -575,7 +575,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2023-10-10T15:39:09+00:00" + "time": "2023-10-26T09:26:10+00:00" }, { "name": "myclabs/deep-copy", @@ -839,16 +839,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.39", + "version": "1.10.40", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "d9dedb0413f678b4d03cbc2279a48f91592c97c4" + "reference": "93c84b5bf7669920d823631e39904d69b9c7dc5d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/d9dedb0413f678b4d03cbc2279a48f91592c97c4", - "reference": "d9dedb0413f678b4d03cbc2279a48f91592c97c4", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/93c84b5bf7669920d823631e39904d69b9c7dc5d", + "reference": "93c84b5bf7669920d823631e39904d69b9c7dc5d", "shasum": "" }, "require": { @@ -897,7 +897,7 @@ "type": "tidelift" } ], - "time": "2023-10-17T15:46:26+00:00" + "time": "2023-10-30T14:48:31+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Database/Database.php b/src/Database/Database.php index becf5a5cf..189acd540 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -323,6 +323,7 @@ public function __construct(Adapter $adapter, Cache $cache, array $filters = [], $this->adapter = $adapter; $this->cache = $cache; $this->instanceFilters = $filters; + $this->name = $name; self::addFilter( 'json', From 220fcbfe79ae84b1531b0a806635cfd6d32f01e7 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 1 Nov 2023 10:33:45 +0000 Subject: [PATCH 03/15] skip structure validation in createDocuments --- src/Database/Database.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 34cf24e5f..19ef13be9 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2757,10 +2757,10 @@ public function createDocuments(string $collection, array $documents, int $batch $document = $this->encode($collection, $document); - $validator = new Structure($collection); - if (!$validator->isValid($document)) { - throw new StructureException($validator->getDescription()); - } + // $validator = new Structure($collection); + // if (!$validator->isValid($document)) { + // throw new StructureException($validator->getDescription()); + // } $documents[$key] = $document; } From 67fb43f0fe369283450fad125814e60b6eff7e49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Nov 2023 08:53:25 +0000 Subject: [PATCH 04/15] Add preserveDates param --- composer.lock | 46 +++++++++++++++++++-------------------- src/Database/Database.php | 32 ++++++++++++++++++--------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/composer.lock b/composer.lock index 8f3d208b3..e2fa0a161 100644 --- a/composer.lock +++ b/composer.lock @@ -513,16 +513,16 @@ }, { "name": "laravel/pint", - "version": "v1.13.5", + "version": "v1.13.6", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "df105cf8ce7a8f0b8a9425ff45cd281a5448e423" + "reference": "3e3d2ab01c7d8b484c18e6100ecf53639c744fa7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/df105cf8ce7a8f0b8a9425ff45cd281a5448e423", - "reference": "df105cf8ce7a8f0b8a9425ff45cd281a5448e423", + "url": "https://api.github.com/repos/laravel/pint/zipball/3e3d2ab01c7d8b484c18e6100ecf53639c744fa7", + "reference": "3e3d2ab01c7d8b484c18e6100ecf53639c744fa7", "shasum": "" }, "require": { @@ -533,13 +533,13 @@ "php": "^8.1.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.34.1", - "illuminate/view": "^10.26.2", - "laravel-zero/framework": "^10.1.2", + "friendsofphp/php-cs-fixer": "^3.38.0", + "illuminate/view": "^10.30.1", + "laravel-zero/framework": "^10.3.0", "mockery/mockery": "^1.6.6", "nunomaduro/larastan": "^2.6.4", "nunomaduro/termwind": "^1.15.1", - "pestphp/pest": "^2.20.0" + "pestphp/pest": "^2.24.2" }, "bin": [ "builds/pint" @@ -575,7 +575,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2023-10-26T09:26:10+00:00" + "time": "2023-11-07T17:59:57+00:00" }, { "name": "myclabs/deep-copy", @@ -839,16 +839,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.40", + "version": "1.10.44", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "93c84b5bf7669920d823631e39904d69b9c7dc5d" + "reference": "bf84367c53a23f759513985c54ffe0d0c249825b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/93c84b5bf7669920d823631e39904d69b9c7dc5d", - "reference": "93c84b5bf7669920d823631e39904d69b9c7dc5d", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/bf84367c53a23f759513985c54ffe0d0c249825b", + "reference": "bf84367c53a23f759513985c54ffe0d0c249825b", "shasum": "" }, "require": { @@ -897,7 +897,7 @@ "type": "tidelift" } ], - "time": "2023-10-30T14:48:31+00:00" + "time": "2023-11-21T16:30:46+00:00" }, { "name": "phpunit/php-code-coverage", @@ -2428,7 +2428,7 @@ }, { "name": "symfony/deprecation-contracts", - "version": "v3.3.0", + "version": "v3.4.0", "source": { "type": "git", "url": "https://github.com/symfony/deprecation-contracts.git", @@ -2475,7 +2475,7 @@ "description": "A generic function and convention to trigger deprecation notices", "homepage": "https://symfony.com", "support": { - "source": "https://github.com/symfony/deprecation-contracts/tree/v3.3.0" + "source": "https://github.com/symfony/deprecation-contracts/tree/v3.4.0" }, "funding": [ { @@ -2495,16 +2495,16 @@ }, { "name": "theseer/tokenizer", - "version": "1.2.1", + "version": "1.2.2", "source": { "type": "git", "url": "https://github.com/theseer/tokenizer.git", - "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e" + "reference": "b2ad5003ca10d4ee50a12da31de12a5774ba6b96" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/theseer/tokenizer/zipball/34a41e998c2183e22995f158c581e7b5e755ab9e", - "reference": "34a41e998c2183e22995f158c581e7b5e755ab9e", + "url": "https://api.github.com/repos/theseer/tokenizer/zipball/b2ad5003ca10d4ee50a12da31de12a5774ba6b96", + "reference": "b2ad5003ca10d4ee50a12da31de12a5774ba6b96", "shasum": "" }, "require": { @@ -2533,7 +2533,7 @@ "description": "A small library for converting tokenized PHP source code into XML and potentially other formats", "support": { "issues": "https://github.com/theseer/tokenizer/issues", - "source": "https://github.com/theseer/tokenizer/tree/1.2.1" + "source": "https://github.com/theseer/tokenizer/tree/1.2.2" }, "funding": [ { @@ -2541,7 +2541,7 @@ "type": "github" } ], - "time": "2021-07-28T10:34:58+00:00" + "time": "2023-11-20T00:12:19+00:00" }, { "name": "utopia-php/cli", @@ -2608,5 +2608,5 @@ "php": ">=8.0" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.6.0" } diff --git a/src/Database/Database.php b/src/Database/Database.php index f6bd3cdec..a78585bc8 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2667,6 +2667,7 @@ private function populateDocumentRelationships(Document $collection, Document $d * * @param string $collection * @param Document $document + * @param bool $preserveDates If true, createdAt and updatedAt will not be overwritten * * @return Document * @@ -2674,7 +2675,7 @@ private function populateDocumentRelationships(Document $collection, Document $d * @throws DatabaseException * @throws StructureException */ - public function createDocument(string $collection, Document $document): Document + public function createDocument(string $collection, Document $document, bool $preserveDates = false): Document { $collection = $this->silent(fn () => $this->getCollection($collection)); @@ -2687,11 +2688,14 @@ public function createDocument(string $collection, Document $document): Document $time = DateTime::now(); + $createdAt = $document->getCreatedAt(); + $updatedAt = $document->getUpdatedAt(); + $document ->setAttribute('$id', empty($document->getId()) ? ID::unique() : $document->getId()) ->setAttribute('$collection', $collection->getId()) - ->setAttribute('$createdAt', $time) - ->setAttribute('$updatedAt', $time); + ->setAttribute('$createdAt', empty($createdAt) || !$preserveDates ? $time : $createdAt) + ->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt); $document = $this->encode($collection, $document); @@ -2728,6 +2732,7 @@ public function createDocument(string $collection, Document $document): Document * @param string $collection * @param array $documents * @param int $batchSize + * @param bool $preserveDates If true, createdAt and updatedAt will not be overwritten * * @return array * @@ -2735,7 +2740,7 @@ public function createDocument(string $collection, Document $document): Document * @throws StructureException * @throws Exception */ - public function createDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE): array + public function createDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE, bool $preserveDates = false): array { if (empty($documents)) { return []; @@ -2746,11 +2751,14 @@ public function createDocuments(string $collection, array $documents, int $batch $time = DateTime::now(); foreach ($documents as $key => $document) { + $createdAt = $document->getCreatedAt(); + $updatedAt = $document->getUpdatedAt(); + $document ->setAttribute('$id', empty($document->getId()) ? ID::unique() : $document->getId()) ->setAttribute('$collection', $collection->getId()) - ->setAttribute('$createdAt', $time) - ->setAttribute('$updatedAt', $time); + ->setAttribute('$createdAt', empty($createdAt) || !$preserveDates ? $time : $createdAt) + ->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt); $document = $this->encode($collection, $document); @@ -3055,6 +3063,7 @@ private function relateDocumentsById( * @param string $collection * @param string $id * @param Document $document + * @param bool $preserveDates If true, updatedAt will not be overwritten * @return Document * * @throws AuthorizationException @@ -3062,7 +3071,7 @@ private function relateDocumentsById( * @throws DatabaseException * @throws StructureException */ - public function updateDocument(string $collection, string $id, Document $document): Document + public function updateDocument(string $collection, string $id, Document $document, bool $preserveDates = false): Document { if (!$document->getId() || !$id) { throw new DatabaseException('Must define $id attribute'); @@ -3176,7 +3185,8 @@ public function updateDocument(string $collection, string $id, Document $documen } if ($shouldUpdate) { - $document->setAttribute('$updatedAt', $time); + $updatedAt = $document->getUpdatedAt(); + $document->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt); } // Check if document was updated after the request timestamp @@ -3220,6 +3230,7 @@ public function updateDocument(string $collection, string $id, Document $documen * @param string $collection * @param array $documents * @param int $batchSize + * @param bool $preserveDates If true, updatedAt will not be overwritten * * @return array * @@ -3227,7 +3238,7 @@ public function updateDocument(string $collection, string $id, Document $documen * @throws Exception * @throws StructureException */ - public function updateDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE): array + public function updateDocuments(string $collection, array $documents, int $batchSize = self::INSERT_BATCH_SIZE, bool $preserveDates = false): array { if (empty($documents)) { return []; @@ -3241,7 +3252,8 @@ public function updateDocuments(string $collection, array $documents, int $batch throw new Exception('Must define $id attribute for each document'); } - $document->setAttribute('$updatedAt', $time); + $updatedAt = $document->getUpdatedAt(); + $document->setAttribute('$updatedAt', empty($updatedAt) || !$preserveDates ? $time : $updatedAt); $document = $this->encode($collection, $document); $old = Authorization::skip(fn () => $this->silent( From 0d01c296873d38c22e01918c084b3c754289700d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Thu, 23 Nov 2023 10:36:18 +0000 Subject: [PATCH 05/15] Add date preserve tests --- tests/Database/Base.php | 91 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 55c5b9688..c2727ef59 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -75,6 +75,97 @@ public function testCreateExistsDelete(): void $this->assertEquals(true, static::getDatabase()->create()); } + public function testPreserveDatesUpdate(): void + { + Authorization::disable(); + + static::getDatabase()->createCollection('preserve_update_dates'); + + static::getDatabase()->createAttribute('preserve_update_dates', 'attr1', Database::VAR_STRING, 10, false); + + $doc1 = static::getDatabase()->createDocument('preserve_update_dates', new Document([ + '$id' => 'doc1', + '$permissions' => [], + 'attr1' => 'value1', + ])); + + $doc2 = static::getDatabase()->createDocument('preserve_update_dates', new Document([ + '$id' => 'doc2', + '$permissions' => [], + 'attr1' => 'value2', + ])); + + $doc3 = static::getDatabase()->createDocument('preserve_update_dates', new Document([ + '$id' => 'doc3', + '$permissions' => [], + 'attr1' => 'value3', + ])); + + $newDate = '2000-01-01T10:00:00.000+00:00'; + + $doc1->setAttribute('$updatedAt', $newDate); + static::getDatabase()->updateDocument('preserve_update_dates', 'doc1', $doc1, true); + $doc1 = static::getDatabase()->getDocument('preserve_update_dates', 'doc1'); + $this->assertEquals($newDate, $doc1->getAttribute('$updatedAt')); + + $doc2->setAttribute('$updatedAt', $newDate); + $doc3->setAttribute('$updatedAt', $newDate); + static::getDatabase()->updateDocuments('preserve_update_dates', [$doc2, $doc3], 2, true); + + $doc2 = static::getDatabase()->getDocument('preserve_update_dates', 'doc2'); + $doc3 = static::getDatabase()->getDocument('preserve_update_dates', 'doc3'); + $this->assertEquals($newDate, $doc2->getAttribute('$updatedAt')); + $this->assertEquals($newDate, $doc3->getAttribute('$updatedAt')); + + static::getDatabase()->deleteCollection('preserve_update_dates'); + + Authorization::reset(); + } + + public function testPreserveDatesCreate(): void + { + Authorization::disable(); + + static::getDatabase()->createCollection('preserve_create_dates'); + + static::getDatabase()->createAttribute('preserve_create_dates', 'attr1', Database::VAR_STRING, 10, false); + + $date = '2000-01-01T10:00:00.000+00:00'; + + static::getDatabase()->createDocument('preserve_create_dates', new Document([ + '$id' => 'doc1', + '$permissions' => [], + 'attr1' => 'value1', + '$createdAt' => $date + ]), true); + + static::getDatabase()->createDocuments('preserve_create_dates', [ + new Document([ + '$id' => 'doc2', + '$permissions' => [], + 'attr1' => 'value2', + '$createdAt' => $date + ]), + new Document([ + '$id' => 'doc3', + '$permissions' => [], + 'attr1' => 'value3', + '$createdAt' => $date + ]), + ], 2, true); + + $doc1 = static::getDatabase()->getDocument('preserve_create_dates', 'doc1'); + $doc2 = static::getDatabase()->getDocument('preserve_create_dates', 'doc2'); + $doc3 = static::getDatabase()->getDocument('preserve_create_dates', 'doc3'); + $this->assertEquals($date, $doc1->getAttribute('$createdAt')); + $this->assertEquals($date, $doc2->getAttribute('$createdAt')); + $this->assertEquals($date, $doc3->getAttribute('$createdAt')); + + static::getDatabase()->deleteCollection('preserve_create_dates'); + + Authorization::reset(); + } + /** * @throws Exception|Throwable */ From 4dedbf48420a9ff2dd66c7a6fe1a387228bf4b83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 1 Dec 2023 15:09:35 +0000 Subject: [PATCH 06/15] Add filters toggle --- src/Database/Database.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index 8c78833be..593f712f2 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -299,6 +299,8 @@ class Database protected bool $resolveRelationships = true; + protected bool $filter = false; + private int $relationshipFetchDepth = 1; /** @@ -646,6 +648,30 @@ public function clearTimeout(string $event = Database::EVENT_ALL): void $this->adapter->clearTimeout($event); } + /** + * Enable filters + * + * @return $this + */ + public function enableFilters(): self + { + $this->filter = true; + + return $this; + } + + /** + * Disable filters + * + * @return $this + */ + public function disableFilters(): self + { + $this->filter = false; + + return $this; + } + /** * Ping Database * From 42ec5ebe2029b625733829e58738c825613c7883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 1 Dec 2023 15:13:54 +0000 Subject: [PATCH 07/15] Implement functionality for filter var --- src/Database/Database.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index 593f712f2..526111b3e 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4800,6 +4800,10 @@ protected function encodeAttribute(string $name, $value, Document $document): mi */ protected function decodeAttribute(string $name, mixed $value, Document $document): mixed { + if(!$this->filter) { + return $value; + } + if (!array_key_exists($name, self::$filters) && !array_key_exists($name, $this->instanceFilters)) { throw new DatabaseException('Filter not found'); } From 41450939b8ca75289d05c13e7746dcd9b064353d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Fri, 1 Dec 2023 15:14:19 +0000 Subject: [PATCH 08/15] Enable filter by default --- src/Database/Database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 526111b3e..187802d8c 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -299,7 +299,7 @@ class Database protected bool $resolveRelationships = true; - protected bool $filter = false; + protected bool $filter = true; private int $relationshipFetchDepth = 1; @@ -4803,7 +4803,7 @@ protected function decodeAttribute(string $name, mixed $value, Document $documen if(!$this->filter) { return $value; } - + if (!array_key_exists($name, self::$filters) && !array_key_exists($name, $this->instanceFilters)) { throw new DatabaseException('Filter not found'); } From 0b8d2df1580f9902adce8d99604feb97a873b44a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 19 Dec 2023 11:25:42 +0100 Subject: [PATCH 09/15] Add setCache --- src/Database/Database.php | 53 +++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 187802d8c..40e251cf3 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1651,7 +1651,8 @@ public function createRelationship( throw new DuplicateException('Attribute already exists'); } - if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP + if ( + $attribute->getAttribute('type') === self::VAR_RELATIONSHIP && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() ) { @@ -3139,15 +3140,16 @@ public function updateDocument(string $collection, string $id, Document $documen $relationType = (string) $relationships[$key]['options']['relationType']; $side = (string) $relationships[$key]['options']['side']; - switch($relationType) { + switch ($relationType) { case Database::RELATION_ONE_TO_ONE: $oldValue = $old->getAttribute($key) instanceof Document ? $old->getAttribute($key)->getId() : $old->getAttribute($key); if ((\is_null($value) !== \is_null($oldValue)) - || (\is_string($value) && $value !== $oldValue) - || ($value instanceof Document && $value->getId() !== $oldValue)) { + || (\is_string($value) && $value !== $oldValue) + || ($value instanceof Document && $value->getId() !== $oldValue) + ) { $shouldUpdate = true; } break; @@ -3163,15 +3165,17 @@ public function updateDocument(string $collection, string $id, Document $documen : $old->getAttribute($key); if ((\is_null($value) !== \is_null($oldValue)) - || (\is_string($value) && $value !== $oldValue) - || ($value instanceof Document && $value->getId() !== $oldValue)) { + || (\is_string($value) && $value !== $oldValue) + || ($value instanceof Document && $value->getId() !== $oldValue) + ) { $shouldUpdate = true; } break; } if ((\is_null($old->getAttribute($key)) !== \is_null($value)) - || \count($old->getAttribute($key)) !== \count($value)) { + || \count($old->getAttribute($key)) !== \count($value) + ) { $shouldUpdate = true; break; } @@ -3181,7 +3185,8 @@ public function updateDocument(string $collection, string $id, Document $documen : $old->getAttribute($key)[$index]; if ((\is_string($relation) && $relation !== $oldValue) - || ($relation instanceof Document && $relation->getId() !== $oldValue)) { + || ($relation instanceof Document && $relation->getId() !== $oldValue) + ) { $shouldUpdate = true; break; } @@ -3293,8 +3298,10 @@ public function updateDocuments(string $collection, array $documents, int $batch )); $validator = new Authorization(self::PERMISSION_UPDATE); - if ($collection->getId() !== self::METADATA - && !$validator->isValid($old->getUpdate())) { + if ( + $collection->getId() !== self::METADATA + && !$validator->isValid($old->getUpdate()) + ) { throw new AuthorizationException($validator->getDescription()); } @@ -3353,7 +3360,7 @@ private function updateDocumentRelationships(Document $collection, Document $old if ($oldValue == $value) { if ( ($relationType === Database::RELATION_ONE_TO_ONE || - ($relationType === Database::RELATION_MANY_TO_ONE && $side === Database::RELATION_SIDE_PARENT)) && + ($relationType === Database::RELATION_MANY_TO_ONE && $side === Database::RELATION_SIDE_PARENT)) && $value instanceof Document ) { $document->setAttribute($key, $value->getId()); @@ -3950,8 +3957,8 @@ private function deleteDocumentRelationships(Document $collection, Document $doc // collection as an existing relationship, but a different two-way key (the third condition), // or the same two-way key as an existing relationship, but a different key (the fourth condition). $transitive = (($existingKey === $twoWayKey - && $existingCollection === $relatedCollection->getId() - && $existingSide !== $side) + && $existingCollection === $relatedCollection->getId() + && $existingSide !== $side) || ($existingTwoWayKey === $key && $existingRelatedCollection === $collection->getId() && $existingSide !== $side) @@ -4079,7 +4086,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection Query::equal($twoWayKey, [$document->getId()]) ]); } else { - if(empty($value)) { + if (empty($value)) { return; } $related = $this->getDocument($relatedCollection->getId(), $value->getId()); @@ -4298,7 +4305,7 @@ public function find(string $collection, array $queries = []): array $collection = $this->silent(fn () => $this->getCollection($collection)); if ($collection->isEmpty()) { - throw new DatabaseException('Collection "'. $originalName .'" not found'); + throw new DatabaseException('Collection "' . $originalName . '" not found'); } $attributes = $collection->getAttribute('attributes', []); @@ -4800,7 +4807,7 @@ protected function encodeAttribute(string $name, $value, Document $document): mi */ protected function decodeAttribute(string $name, mixed $value, Document $document): mixed { - if(!$this->filter) { + if (!$this->filter) { return $value; } @@ -4928,6 +4935,20 @@ public function getCache(): Cache return $this->cache; } + /** + * Set cache + * + * @param Cache $cache + * + * @return $this + */ + public function setCache(Cache $cache): self + { + $this->cache = $cache; + return $this; + } + + /** * Get instance filters * From 8c365d6c6c669fe8df12b7313b1afcf828c4b648 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 26 Dec 2023 18:04:23 +0200 Subject: [PATCH 10/15] remove structure validation --- src/Database/Database.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 40e251cf3..7e57442d3 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2734,10 +2734,10 @@ public function createDocument(string $collection, Document $document, bool $pre throw new InvalidArgumentException($validator->getDescription()); } - // $structure = new Structure($collection); - // if (!$structure->isValid($document)) { - // throw new StructureException($structure->getDescription()); - // } + $structure = new Structure($collection); + if (!$structure->isValid($document)) { + throw new StructureException($structure->getDescription()); + } if ($this->resolveRelationships) { $document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document)); @@ -2792,10 +2792,10 @@ public function createDocuments(string $collection, array $documents, int $batch $document = $this->encode($collection, $document); - // $validator = new Structure($collection); - // if (!$validator->isValid($document)) { - // throw new StructureException($validator->getDescription()); - // } + $validator = new Structure($collection); + if (!$validator->isValid($document)) { + throw new StructureException($validator->getDescription()); + } $documents[$key] = $document; } From bc2159a6c3f35e047f3f9a0f988e45acece285f8 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 26 Dec 2023 18:20:00 +0200 Subject: [PATCH 11/15] composer.lock --- composer.lock | 98 +++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/composer.lock b/composer.lock index e2fa0a161..1a6b42b8a 100644 --- a/composer.lock +++ b/composer.lock @@ -268,16 +268,16 @@ }, { "name": "utopia-php/framework", - "version": "0.31.0", + "version": "0.32.0", "source": { "type": "git", "url": "https://github.com/utopia-php/framework.git", - "reference": "207f77378965fca9a9bc3783ea379d3549f86bc0" + "reference": "ad6f7e6d6b38cf5bed4e3af9a1394c59d4bb9225" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/framework/zipball/207f77378965fca9a9bc3783ea379d3549f86bc0", - "reference": "207f77378965fca9a9bc3783ea379d3549f86bc0", + "url": "https://api.github.com/repos/utopia-php/framework/zipball/ad6f7e6d6b38cf5bed4e3af9a1394c59d4bb9225", + "reference": "ad6f7e6d6b38cf5bed4e3af9a1394c59d4bb9225", "shasum": "" }, "require": { @@ -307,9 +307,9 @@ ], "support": { "issues": "https://github.com/utopia-php/framework/issues", - "source": "https://github.com/utopia-php/framework/tree/0.31.0" + "source": "https://github.com/utopia-php/framework/tree/0.32.0" }, - "time": "2023-08-30T16:10:04+00:00" + "time": "2023-12-26T14:18:36+00:00" }, { "name": "utopia-php/mongo", @@ -513,16 +513,16 @@ }, { "name": "laravel/pint", - "version": "v1.13.6", + "version": "v1.13.7", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "3e3d2ab01c7d8b484c18e6100ecf53639c744fa7" + "reference": "4157768980dbd977f1c4b4cc94997416d8b30ece" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/3e3d2ab01c7d8b484c18e6100ecf53639c744fa7", - "reference": "3e3d2ab01c7d8b484c18e6100ecf53639c744fa7", + "url": "https://api.github.com/repos/laravel/pint/zipball/4157768980dbd977f1c4b4cc94997416d8b30ece", + "reference": "4157768980dbd977f1c4b4cc94997416d8b30ece", "shasum": "" }, "require": { @@ -575,7 +575,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2023-11-07T17:59:57+00:00" + "time": "2023-12-05T19:43:12+00:00" }, { "name": "myclabs/deep-copy", @@ -638,16 +638,16 @@ }, { "name": "nikic/php-parser", - "version": "v4.17.1", + "version": "v4.18.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d" + "reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", - "reference": "a6303e50c90c355c7eeee2c4a8b27fe8dc8fef1d", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/1bcbb2179f97633e98bbbc87044ee2611c7d7999", + "reference": "1bcbb2179f97633e98bbbc87044ee2611c7d7999", "shasum": "" }, "require": { @@ -688,9 +688,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.17.1" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.18.0" }, - "time": "2023-08-13T19:53:39+00:00" + "time": "2023-12-10T21:03:43+00:00" }, { "name": "pcov/clobber", @@ -839,16 +839,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.44", + "version": "1.10.50", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "bf84367c53a23f759513985c54ffe0d0c249825b" + "reference": "06a98513ac72c03e8366b5a0cb00750b487032e4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/bf84367c53a23f759513985c54ffe0d0c249825b", - "reference": "bf84367c53a23f759513985c54ffe0d0c249825b", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/06a98513ac72c03e8366b5a0cb00750b487032e4", + "reference": "06a98513ac72c03e8366b5a0cb00750b487032e4", "shasum": "" }, "require": { @@ -897,27 +897,27 @@ "type": "tidelift" } ], - "time": "2023-11-21T16:30:46+00:00" + "time": "2023-12-13T10:59:42+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.29", + "version": "9.2.30", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "6a3a87ac2bbe33b25042753df8195ba4aa534c76" + "reference": "ca2bd87d2f9215904682a9cb9bb37dda98e76089" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/6a3a87ac2bbe33b25042753df8195ba4aa534c76", - "reference": "6a3a87ac2bbe33b25042753df8195ba4aa534c76", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/ca2bd87d2f9215904682a9cb9bb37dda98e76089", + "reference": "ca2bd87d2f9215904682a9cb9bb37dda98e76089", "shasum": "" }, "require": { "ext-dom": "*", "ext-libxml": "*", "ext-xmlwriter": "*", - "nikic/php-parser": "^4.15", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3", "phpunit/php-file-iterator": "^3.0.3", "phpunit/php-text-template": "^2.0.2", @@ -967,7 +967,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", "security": "https://github.com/sebastianbergmann/php-code-coverage/security/policy", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.29" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.30" }, "funding": [ { @@ -975,7 +975,7 @@ "type": "github" } ], - "time": "2023-09-19T04:57:46+00:00" + "time": "2023-12-22T06:47:57+00:00" }, { "name": "phpunit/php-file-iterator", @@ -1220,16 +1220,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.13", + "version": "9.6.15", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "f3d767f7f9e191eab4189abe41ab37797e30b1be" + "reference": "05017b80304e0eb3f31d90194a563fd53a6021f1" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/f3d767f7f9e191eab4189abe41ab37797e30b1be", - "reference": "f3d767f7f9e191eab4189abe41ab37797e30b1be", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/05017b80304e0eb3f31d90194a563fd53a6021f1", + "reference": "05017b80304e0eb3f31d90194a563fd53a6021f1", "shasum": "" }, "require": { @@ -1303,7 +1303,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.13" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.15" }, "funding": [ { @@ -1319,7 +1319,7 @@ "type": "tidelift" } ], - "time": "2023-09-19T05:39:22+00:00" + "time": "2023-12-01T16:55:19+00:00" }, { "name": "psr/container", @@ -1663,20 +1663,20 @@ }, { "name": "sebastian/complexity", - "version": "2.0.2", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/complexity.git", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88" + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/739b35e53379900cc9ac327b2147867b8b6efd88", - "reference": "739b35e53379900cc9ac327b2147867b8b6efd88", + "url": "https://api.github.com/repos/sebastianbergmann/complexity/zipball/25f207c40d62b8b7aa32f5ab026c53561964053a", + "reference": "25f207c40d62b8b7aa32f5ab026c53561964053a", "shasum": "" }, "require": { - "nikic/php-parser": "^4.7", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -1708,7 +1708,7 @@ "homepage": "https://github.com/sebastianbergmann/complexity", "support": { "issues": "https://github.com/sebastianbergmann/complexity/issues", - "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.2" + "source": "https://github.com/sebastianbergmann/complexity/tree/2.0.3" }, "funding": [ { @@ -1716,7 +1716,7 @@ "type": "github" } ], - "time": "2020-10-26T15:52:27+00:00" + "time": "2023-12-22T06:19:30+00:00" }, { "name": "sebastian/diff", @@ -1990,20 +1990,20 @@ }, { "name": "sebastian/lines-of-code", - "version": "1.0.3", + "version": "1.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/lines-of-code.git", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc" + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/c1c2e997aa3146983ed888ad08b15470a2e22ecc", - "reference": "c1c2e997aa3146983ed888ad08b15470a2e22ecc", + "url": "https://api.github.com/repos/sebastianbergmann/lines-of-code/zipball/e1e4a170560925c26d424b6a03aed157e7dcc5c5", + "reference": "e1e4a170560925c26d424b6a03aed157e7dcc5c5", "shasum": "" }, "require": { - "nikic/php-parser": "^4.6", + "nikic/php-parser": "^4.18 || ^5.0", "php": ">=7.3" }, "require-dev": { @@ -2035,7 +2035,7 @@ "homepage": "https://github.com/sebastianbergmann/lines-of-code", "support": { "issues": "https://github.com/sebastianbergmann/lines-of-code/issues", - "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.3" + "source": "https://github.com/sebastianbergmann/lines-of-code/tree/1.0.4" }, "funding": [ { @@ -2043,7 +2043,7 @@ "type": "github" } ], - "time": "2020-11-28T06:42:11+00:00" + "time": "2023-12-22T06:20:34+00:00" }, { "name": "sebastian/object-enumerator", From 145965b26c1fe08bafb7cfb3e01b899b3ec754b9 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 19 Jan 2024 19:57:03 +1300 Subject: [PATCH 12/15] Use setter for cache name --- src/Database/Database.php | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index b28285326..92688b1dd 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -135,7 +135,7 @@ class Database protected Cache $cache; - protected string $name; + protected string $cacheName = 'default'; /** * @var array @@ -342,12 +342,11 @@ class Database * @param Cache $cache * @param array $filters */ - public function __construct(Adapter $adapter, Cache $cache, array $filters = [], string $name = 'default') + public function __construct(Adapter $adapter, Cache $cache, array $filters = []) { $this->adapter = $adapter; $this->cache = $cache; $this->instanceFilters = $filters; - $this->name = $name; self::addFilter( 'json', @@ -606,6 +605,29 @@ public function getDatabase(): string return $this->adapter->getDatabase(); } + /** + * Set the name to use for cache + * + * @param string $name + * @return $this + */ + public function setCacheName(string $name): self + { + $this->cacheName = $name; + + return $this; + } + + /** + * Get the cache name + * + * @return string + */ + public function getCacheName(): string + { + return $this->cacheName; + } + /** * Set a metadata value to be printed in the query comments * @@ -2598,7 +2620,7 @@ public function getDocument(string $collection, string $id, array $queries = []) $validator = new Authorization(self::PERMISSION_READ); $documentSecurity = $collection->getAttribute('documentSecurity', false); - $cacheKey = $this->name . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection->getId() . ':' . $id; + $cacheKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection->getId() . ':' . $id; if (!empty($selections)) { $cacheKey .= ':' . \md5(\implode($selections)); @@ -2669,7 +2691,7 @@ public function getDocument(string $collection, string $id, array $queries = []) */ foreach ($this->map as $key => $value) { [$k, $v] = \explode('=>', $key); - $ck = $this->name . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':map:' . $k; + $ck = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':map:' . $k; $cache = $this->cache->load($ck, self::TTL); if (empty($cache)) { $cache = []; @@ -4562,7 +4584,7 @@ private function deleteCascade(Document $collection, Document $relatedCollection */ public function purgeCachedCollection(string $collection): bool { - return $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':*'); + return $this->cache->purge($this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':*'); } /** @@ -4576,7 +4598,7 @@ public function purgeCachedCollection(string $collection): bool */ public function purgeCachedDocument(string $collection, string $id): bool { - return $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':' . $id . ':*'); + return $this->cache->purge($this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':' . $collection . ':' . $id . ':*'); } /** @@ -5312,7 +5334,7 @@ private function purgeRelatedDocuments(Document $collection, string $id): void return; } - $key = $this->name . '-cache-' . $this->getNamespace() . ':map:' . $collection->getId() . ':' . $id; + $key = $this->cacheName . '-cache-' . $this->getNamespace() . ':map:' . $collection->getId() . ':' . $id; $cache = $this->cache->load($key, self::TTL); if (!empty($cache)) { foreach ($cache as $v) { From 897f5cdfc7a88ac9811b0d76a9ee989c5defb30a Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 19 Jan 2024 19:57:31 +1300 Subject: [PATCH 13/15] Format --- src/Database/Database.php | 44 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 92688b1dd..4f69df0a5 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -605,28 +605,28 @@ public function getDatabase(): string return $this->adapter->getDatabase(); } - /** - * Set the name to use for cache - * - * @param string $name - * @return $this - */ - public function setCacheName(string $name): self - { - $this->cacheName = $name; - - return $this; - } - - /** - * Get the cache name - * - * @return string - */ - public function getCacheName(): string - { - return $this->cacheName; - } + /** + * Set the name to use for cache + * + * @param string $name + * @return $this + */ + public function setCacheName(string $name): self + { + $this->cacheName = $name; + + return $this; + } + + /** + * Get the cache name + * + * @return string + */ + public function getCacheName(): string + { + return $this->cacheName; + } /** * Set a metadata value to be printed in the query comments From 10e13f06b8c1617c2a3b5df4b5a0911bdb6a1389 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 19 Jan 2024 20:07:02 +1300 Subject: [PATCH 14/15] Fix method order --- src/Database/Database.php | 107 +++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 54 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 4f69df0a5..1801346d0 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -605,6 +605,29 @@ public function getDatabase(): string return $this->adapter->getDatabase(); } + /** + * Set the cache instance + * + * @param Cache $cache + * + * @return $this + */ + public function setCache(Cache $cache): self + { + $this->cache = $cache; + return $this; + } + + /** + * Get the cache instance + * + * @return Cache + */ + public function getCache(): Cache + { + return $this->cache; + } + /** * Set the name to use for cache * @@ -712,6 +735,16 @@ public function disableFilters(): self return $this; } + /** + * Get instance filters + * + * @return array + */ + public function getInstanceFilters(): array + { + return $this->instanceFilters; + } + /** * Enable validation * @@ -773,6 +806,26 @@ public function setPreserveDates(bool $preserve): self return $this; } + /** + * Get list of keywords that cannot be used + * + * @return string[] + */ + public function getKeywords(): array + { + return $this->adapter->getKeywords(); + } + + /** + * Get Database Adapter + * + * @return Adapter + */ + public function getAdapter(): Adapter + { + return $this->adapter; + } + /** * Ping Database * @@ -5225,60 +5278,6 @@ public function getLimitForIndexes(): int return $this->adapter->getLimitForIndexes() - $this->adapter->getCountOfDefaultIndexes(); } - /** - * Get list of keywords that cannot be used - * - * @return string[] - */ - public function getKeywords(): array - { - return $this->adapter->getKeywords(); - } - - /** - * Get Database Adapter - * - * @return Adapter - */ - public function getAdapter(): Adapter - { - return $this->adapter; - } - - /** - * Get Cache - * - * @return Cache - */ - public function getCache(): Cache - { - return $this->cache; - } - - /** - * Set cache - * - * @param Cache $cache - * - * @return $this - */ - public function setCache(Cache $cache): self - { - $this->cache = $cache; - return $this; - } - - - /** - * Get instance filters - * - * @return array - */ - public function getInstanceFilters(): array - { - return $this->instanceFilters; - } - /** * @param Document $collection * @param array $queries From 761fbe89ea0ad492cac09bf63075d6dd1b5cca71 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Fri, 19 Jan 2024 21:05:56 +1300 Subject: [PATCH 15/15] Fix tests --- src/Database/Adapter/Mongo.php | 2 - src/Database/Database.php | 104 ++++++++++++++++----------------- 2 files changed, 51 insertions(+), 55 deletions(-) diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 12cd7a9fa..f79a5a14a 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -815,8 +815,6 @@ public function updateDocuments(string $collection, array $documents, int $batch } $this->client->update($name, $filters, $document); - - $documents[$index] = new Document($document); } return $documents; diff --git a/src/Database/Database.php b/src/Database/Database.php index 1801346d0..72864e2cc 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -605,28 +605,28 @@ public function getDatabase(): string return $this->adapter->getDatabase(); } - /** - * Set the cache instance - * - * @param Cache $cache - * - * @return $this - */ - public function setCache(Cache $cache): self - { - $this->cache = $cache; - return $this; - } - - /** - * Get the cache instance - * - * @return Cache - */ - public function getCache(): Cache - { - return $this->cache; - } + /** + * Set the cache instance + * + * @param Cache $cache + * + * @return $this + */ + public function setCache(Cache $cache): self + { + $this->cache = $cache; + return $this; + } + + /** + * Get the cache instance + * + * @return Cache + */ + public function getCache(): Cache + { + return $this->cache; + } /** * Set the name to use for cache @@ -735,15 +735,15 @@ public function disableFilters(): self return $this; } - /** - * Get instance filters - * - * @return array - */ - public function getInstanceFilters(): array - { - return $this->instanceFilters; - } + /** + * Get instance filters + * + * @return array + */ + public function getInstanceFilters(): array + { + return $this->instanceFilters; + } /** * Enable validation @@ -806,25 +806,25 @@ public function setPreserveDates(bool $preserve): self return $this; } - /** - * Get list of keywords that cannot be used - * - * @return string[] - */ - public function getKeywords(): array - { - return $this->adapter->getKeywords(); - } - - /** - * Get Database Adapter - * - * @return Adapter - */ - public function getAdapter(): Adapter - { - return $this->adapter; - } + /** + * Get list of keywords that cannot be used + * + * @return string[] + */ + public function getKeywords(): array + { + return $this->adapter->getKeywords(); + } + + /** + * Get Database Adapter + * + * @return Adapter + */ + public function getAdapter(): Adapter + { + return $this->adapter; + } /** * Ping Database @@ -4667,8 +4667,6 @@ public function purgeCachedDocument(string $collection, string $id): bool */ public function find(string $collection, array $queries = []): array { - $collectionId = $collection; - if ($this->adapter->getShareTables() && empty($this->adapter->getTenant())) { throw new DatabaseException('Missing tenant. Tenant must be set when table sharing is enabled.'); } @@ -4676,7 +4674,7 @@ public function find(string $collection, array $queries = []): array $collection = $this->silent(fn () => $this->getCollection($collection)); if ($collection->isEmpty()) { - throw new DatabaseException('Collection "' . $collectionId . '" not found'); + throw new DatabaseException('Collection not found'); } $attributes = $collection->getAttribute('attributes', []);