From 58f5c7e97b669025afe552923a94b9f0594fd02e Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 1 Oct 2023 16:46:28 +0300 Subject: [PATCH 1/3] Same twoWayKey --- composer.lock | 10 +++++----- src/Database/Database.php | 2 +- tests/Database/Base.php | 41 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index 3ab0753d4..fd54a496d 100644 --- a/composer.lock +++ b/composer.lock @@ -839,16 +839,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.35", + "version": "1.10.36", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "e730e5facb75ffe09dfb229795e8c01a459f26c3" + "reference": "ffa3089511121a672e62969404e4fddc753f9b15" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/e730e5facb75ffe09dfb229795e8c01a459f26c3", - "reference": "e730e5facb75ffe09dfb229795e8c01a459f26c3", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/ffa3089511121a672e62969404e4fddc753f9b15", + "reference": "ffa3089511121a672e62969404e4fddc753f9b15", "shasum": "" }, "require": { @@ -897,7 +897,7 @@ "type": "tidelift" } ], - "time": "2023-09-19T15:27:56+00:00" + "time": "2023-09-29T14:07:45+00:00" }, { "name": "phpunit/php-code-coverage", diff --git a/src/Database/Database.php b/src/Database/Database.php index 8292bf6bf..8209bb6e9 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1528,7 +1528,7 @@ public function createRelationship( $id ??= $relatedCollection->getId(); - $twoWayKey ??= $collection->getId(); + $twoWayKey ??= $collection->getId() . '_' . uniqid(); $attributes = $collection->getAttribute('attributes', []); /** @var Document[] $attributes */ diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 34609d113..714167f51 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3429,6 +3429,47 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document): return $document; } + public function testRelationSameKey(): void + { + if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + $this->expectNotToPerformAssertions(); + return; + } + + $permissions = [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::delete(Role::any()), + ]; + + static::getDatabase()->createCollection("c1", [], [], $permissions); + static::getDatabase()->createCollection("c2", [], [], $permissions); + + $res = static::getDatabase()->createRelationship( + collection: "c1", + relatedCollection: "c2", + type: Database::RELATION_ONE_TO_ONE, + id: "c2" + ); + $this->assertTrue($res); + + $res = static::getDatabase()->createRelationship( + collection: "c1", + relatedCollection: "c2", + type: Database::RELATION_ONE_TO_MANY, + id: "c1" + ); + $this->assertTrue($res); + + $res = static::getDatabase()->createRelationship( + collection: "c1", + relatedCollection: "c2", + type: Database::RELATION_MANY_TO_ONE, + id: "c3" + ); + $this->assertTrue($res); + } + public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { From 217babece9b38d62e8a318b7c826ec3925166887 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 2 Oct 2023 10:40:32 +0300 Subject: [PATCH 2/3] Related attribute already exists Try Catch --- src/Database/Database.php | 16 ++++++++++------ tests/Database/Base.php | 36 ++++++++++++------------------------ 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 8209bb6e9..4c9c95793 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1528,7 +1528,7 @@ public function createRelationship( $id ??= $relatedCollection->getId(); - $twoWayKey ??= $collection->getId() . '_' . uniqid(); + $twoWayKey ??= $collection->getId(); $attributes = $collection->getAttribute('attributes', []); /** @var Document[] $attributes */ @@ -1537,11 +1537,15 @@ public function createRelationship( throw new DuplicateException('Attribute already exists'); } - if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP - && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) - && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() - ) { - throw new DuplicateException('Related attribute already exists'); + try { + if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP + && \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey) + && $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId() + ) { + throw new DuplicateException('Related attribute already exists'); + } + } catch (DuplicateException $e) { + $twoWayKey ??= $collection->getId() . '_' . uniqid(); } } diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 714167f51..0ad032577 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3429,7 +3429,7 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document): return $document; } - public function testRelationSameKey(): void + public function testRelationSametwoWayKey(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3442,30 +3442,30 @@ public function testRelationSameKey(): void Permission::delete(Role::any()), ]; - static::getDatabase()->createCollection("c1", [], [], $permissions); - static::getDatabase()->createCollection("c2", [], [], $permissions); + static::getDatabase()->createCollection('c1', [], [], $permissions); + static::getDatabase()->createCollection('c2', [], [], $permissions); $res = static::getDatabase()->createRelationship( - collection: "c1", - relatedCollection: "c2", + collection: 'c1', + relatedCollection: 'c2', type: Database::RELATION_ONE_TO_ONE, - id: "c2" + id: 'c2' ); $this->assertTrue($res); $res = static::getDatabase()->createRelationship( - collection: "c1", - relatedCollection: "c2", + collection: 'c1', + relatedCollection: 'c2', type: Database::RELATION_ONE_TO_MANY, - id: "c1" + id: 'c1' ); $this->assertTrue($res); $res = static::getDatabase()->createRelationship( - collection: "c1", - relatedCollection: "c2", + collection: 'c1', + relatedCollection: 'c2', type: Database::RELATION_MANY_TO_ONE, - id: "c3" + id: 'c3' ); $this->assertTrue($res); } @@ -5626,18 +5626,6 @@ public function testIdenticalTwoWayKeyRelationship(): void id: 'child1' ); - try { - static::getDatabase()->createRelationship( - collection: 'parent', - relatedCollection: 'child', - type: Database::RELATION_ONE_TO_MANY, - id: 'children', - ); - $this->fail('Failed to throw Exception'); - } catch (Exception $e) { - $this->assertEquals('Related attribute already exists', $e->getMessage()); - } - static::getDatabase()->createRelationship( collection: 'parent', relatedCollection: 'child', From 0bc46ecebc47c7afe1dd65bc8ac79fc6fc5aecaf Mon Sep 17 00:00:00 2001 From: Shmuel Fogel Date: Mon, 2 Oct 2023 10:46:52 +0300 Subject: [PATCH 3/3] Update tests/Database/Base.php Co-authored-by: Jake Barnby --- tests/Database/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Base.php b/tests/Database/Base.php index 0ad032577..a8b1c4af1 100644 --- a/tests/Database/Base.php +++ b/tests/Database/Base.php @@ -3431,7 +3431,7 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document): public function testRelationSametwoWayKey(): void { - if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { + if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); return; }