From b00f87fe8ff923de48e9dfe2708cf0780f97699b Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 12 Jan 2025 20:51:58 +0200 Subject: [PATCH 1/8] RELATION_ONE_TO_ONE --- phpunit.xml | 2 +- src/Database/Adapter/MariaDB.php | 18 ++++++++-- tests/e2e/Adapter/Base.php | 60 +++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index ccdaa969e..783265d80 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false"> + stopOnFailure="true"> ./tests/unit diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 449ac4708..d54525d7d 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -502,7 +502,15 @@ public function createRelationship( $sql = "ALTER TABLE {$table} ADD COLUMN `{$id}` {$sqlType} DEFAULT NULL;"; if ($twoWay) { - $sql .= "ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;"; + //$sql .= "ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;"; + + $x = $this->getPDO() + ->prepare("ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;") + ->execute(); + + var_dump("ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;"); + var_dump($x); + } break; case Database::RELATION_ONE_TO_MANY: @@ -519,9 +527,14 @@ public function createRelationship( $sql = $this->trigger(Database::EVENT_ATTRIBUTE_CREATE, $sql); - return $this->getPDO() + $x = $this->getPDO() ->prepare($sql) ->execute(); + + var_dump($sql); + var_dump($x); + + return $x; } /** @@ -797,6 +810,7 @@ public function createIndex(string $collection, string $id, string $type, array ->prepare($sql) ->execute(); } catch (PDOException $e) { + var_dump($e); $this->processException($e); return false; } diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 90298d472..9b2c66a58 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1364,7 +1364,65 @@ public function testSchemaAttributes(): void } } - public function testCreateDeleteAttribute(): void + public function testRowSizeToLarge(): void + { + if (static::getDatabase()->getAdapter()::getDocumentSizeLimit() === 0) { + $this->expectNotToPerformAssertions(); + return; + } + + /** + * getDocumentSizeLimit = 65535 + * mb4 65535 / 4 = 16383 + */ + if (static::getDatabase()->getAdapter()::getDocumentSizeLimit() > 0) { + $collection = static::getDatabase()->createCollection('row_size'); + + $this->assertEquals(true, static::getDatabase()->createAttribute($collection->getId(), 'attr_1', Database::VAR_STRING, 16000, true)); + // $this->assertEquals(true, static::getDatabase()->createAttribute($collection->getId(), 'attr_1', Database::VAR_STRING, 100, true)); + + $collection = static::getDatabase()->getCollection($collection->getId()); + + var_dump(static::getDatabase()->getAdapter()->getAttributeWidth($collection)); + var_dump(static::getDatabase()->getAdapter()->getDocumentSizeLimit()); + +// $attribute = new Document([ +// '$id' => ID::custom('attr_2'), +// 'type' => Database::VAR_STRING, +// 'size' => 1, +// 'required' => false, +// 'default' => null, +// 'signed' => true, +// 'array' => false, +// 'filters' => [], +// ]); +// +// $this->assertEquals(false, static::getDatabase()->checkAttribute($collection, $attribute)); + + $collection2 = static::getDatabase()->createCollection('row_size2'); +var_dump($collection2); + static::getDatabase()->createRelationship( + collection: $collection2->getId(), + relatedCollection: $collection->getId(), + type: Database::RELATION_ONE_TO_ONE, + id: 'shmuel_id', + twoWay: true, + twoWayKey: 'shmuel_twoWayKey' + ); + + //$this->adapter->getAttributeWidth($collection) >= $this->adapter->getDocumentSizeLimit() + + } + + //$this->adapter->getDocumentSizeLimit() > 0 && + // $this->adapter->getAttributeWidth($collection) >= $this->adapter->getDocumentSizeLimit() + + $this->assertEquals('shmuel', 'fogel'); + + + } + + public function testCreateDeleteAttribute(): void { static::getDatabase()->createCollection('attributes'); From 2ca94ac8d8df157469a0c6deaaaf8e8a796c81f9 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 13 Jan 2025 10:36:35 +0200 Subject: [PATCH 2/8] Fix relationship row size --- src/Database/Adapter/MariaDB.php | 18 +------- src/Database/Adapter/SQL.php | 2 +- src/Database/Database.php | 15 +++---- tests/e2e/Adapter/Base.php | 76 ++++++++++++++++---------------- 4 files changed, 48 insertions(+), 63 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index d54525d7d..449ac4708 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -502,15 +502,7 @@ public function createRelationship( $sql = "ALTER TABLE {$table} ADD COLUMN `{$id}` {$sqlType} DEFAULT NULL;"; if ($twoWay) { - //$sql .= "ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;"; - - $x = $this->getPDO() - ->prepare("ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;") - ->execute(); - - var_dump("ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;"); - var_dump($x); - + $sql .= "ALTER TABLE {$relatedTable} ADD COLUMN `{$twoWayKey}` {$sqlType} DEFAULT NULL;"; } break; case Database::RELATION_ONE_TO_MANY: @@ -527,14 +519,9 @@ public function createRelationship( $sql = $this->trigger(Database::EVENT_ATTRIBUTE_CREATE, $sql); - $x = $this->getPDO() + return $this->getPDO() ->prepare($sql) ->execute(); - - var_dump($sql); - var_dump($x); - - return $x; } /** @@ -810,7 +797,6 @@ public function createIndex(string $collection, string $id, string $type, array ->prepare($sql) ->execute(); } catch (PDOException $e) { - var_dump($e); $this->processException($e); return false; } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 725a91a50..f357652c4 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -519,7 +519,7 @@ public function getAttributeWidth(Document $collection): int case Database::VAR_RELATIONSHIP: // INT(11) - $total += 4; + $total += (Database::LENGTH_KEY * 4) + 2; break; case Database::VAR_DATETIME: diff --git a/src/Database/Database.php b/src/Database/Database.php index 6aba14d0b..ced262b5b 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2177,14 +2177,6 @@ public function createRelationship( throw new LimitException('Column limit reached. Cannot create new attribute.'); } - if ( - $this->adapter->getDocumentSizeLimit() > 0 && - ($this->adapter->getAttributeWidth($collection) >= $this->adapter->getDocumentSizeLimit() - || $this->adapter->getAttributeWidth($relatedCollection) >= $this->adapter->getDocumentSizeLimit()) - ) { - throw new LimitException('Row width limit reached. Cannot create new attribute.'); - } - $relationship = new Document([ '$id' => ID::custom($id), 'key' => $id, @@ -2217,6 +2209,13 @@ public function createRelationship( ], ]); + var_dump('getAttributeWidth ++++++++'); + var_dump($this->adapter->getAttributeWidth($collection)); + var_dump($this->adapter->getAttributeWidth($relatedCollection)); + + $this->checkAttribute($collection, $relationship); + $this->checkAttribute($relatedCollection, $twoWayRelationship); + $collection->setAttribute('attributes', $relationship, Document::SET_TYPE_APPEND); $relatedCollection->setAttribute('attributes', $twoWayRelationship, Document::SET_TYPE_APPEND); diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 9b2c66a58..ffa54d3ed 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1376,50 +1376,50 @@ public function testRowSizeToLarge(): void * mb4 65535 / 4 = 16383 */ if (static::getDatabase()->getAdapter()::getDocumentSizeLimit() > 0) { - $collection = static::getDatabase()->createCollection('row_size'); - - $this->assertEquals(true, static::getDatabase()->createAttribute($collection->getId(), 'attr_1', Database::VAR_STRING, 16000, true)); - // $this->assertEquals(true, static::getDatabase()->createAttribute($collection->getId(), 'attr_1', Database::VAR_STRING, 100, true)); - - $collection = static::getDatabase()->getCollection($collection->getId()); - - var_dump(static::getDatabase()->getAdapter()->getAttributeWidth($collection)); - var_dump(static::getDatabase()->getAdapter()->getDocumentSizeLimit()); - -// $attribute = new Document([ -// '$id' => ID::custom('attr_2'), -// 'type' => Database::VAR_STRING, -// 'size' => 1, -// 'required' => false, -// 'default' => null, -// 'signed' => true, -// 'array' => false, -// 'filters' => [], -// ]); -// -// $this->assertEquals(false, static::getDatabase()->checkAttribute($collection, $attribute)); - - $collection2 = static::getDatabase()->createCollection('row_size2'); -var_dump($collection2); - static::getDatabase()->createRelationship( - collection: $collection2->getId(), - relatedCollection: $collection->getId(), - type: Database::RELATION_ONE_TO_ONE, - id: 'shmuel_id', - twoWay: true, - twoWayKey: 'shmuel_twoWayKey' - ); + $collection_1 = static::getDatabase()->createCollection('row_size_1'); + $collection_2 = static::getDatabase()->createCollection('row_size_2'); - //$this->adapter->getAttributeWidth($collection) >= $this->adapter->getDocumentSizeLimit() + $this->assertEquals(true, static::getDatabase()->createAttribute($collection_1->getId(), 'attr_1', Database::VAR_STRING, 16000, true)); - } + try { + $this->assertEquals(true, static::getDatabase()->createAttribute($collection_1->getId(), 'attr_2', Database::VAR_STRING, Database::LENGTH_KEY, true)); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } - //$this->adapter->getDocumentSizeLimit() > 0 && - // $this->adapter->getAttributeWidth($collection) >= $this->adapter->getDocumentSizeLimit() + $collection_1 = static::getDatabase()->getCollection($collection_1->getId()); - $this->assertEquals('shmuel', 'fogel'); + /** + * Relation takes length of Database::LENGTH_KEY so exceeding getDocumentSizeLimit + */ + try { + static::getDatabase()->createRelationship( + collection: $collection_2->getId(), + relatedCollection: $collection_1->getId(), + type: Database::RELATION_ONE_TO_ONE, + twoWay: true, + ); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } + + try { + static::getDatabase()->createRelationship( + collection: $collection_1->getId(), + relatedCollection: $collection_2->getId(), + type: Database::RELATION_ONE_TO_ONE, + twoWay: true, + ); + + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } + } } public function testCreateDeleteAttribute(): void From be5f03845b4825383fea28dc41521eb26bdef47a Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 13 Jan 2025 10:40:17 +0200 Subject: [PATCH 3/8] formatting --- src/Database/Database.php | 4 ---- tests/e2e/Adapter/Base.php | 4 +--- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index ced262b5b..0dfc1479e 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2209,10 +2209,6 @@ public function createRelationship( ], ]); - var_dump('getAttributeWidth ++++++++'); - var_dump($this->adapter->getAttributeWidth($collection)); - var_dump($this->adapter->getAttributeWidth($relatedCollection)); - $this->checkAttribute($collection, $relationship); $this->checkAttribute($relatedCollection, $twoWayRelationship); diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index ffa54d3ed..09dc8fc99 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1388,8 +1388,6 @@ public function testRowSizeToLarge(): void $this->assertInstanceOf(LimitException::class, $e); } - $collection_1 = static::getDatabase()->getCollection($collection_1->getId()); - /** * Relation takes length of Database::LENGTH_KEY so exceeding getDocumentSizeLimit */ @@ -1422,7 +1420,7 @@ public function testRowSizeToLarge(): void } } - public function testCreateDeleteAttribute(): void + public function testCreateDeleteAttribute(): void { static::getDatabase()->createCollection('attributes'); From 29a1065fcc71821dc1710afa8183c2b1d068d8a5 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 13 Jan 2025 10:41:04 +0200 Subject: [PATCH 4/8] stopOnFailure --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 783265d80..ccdaa969e 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true"> + stopOnFailure="false"> ./tests/unit From bc42c9ddee939ec8ae8dcc280b1c9bcc56e00fb0 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 13 Jan 2025 10:45:29 +0200 Subject: [PATCH 5/8] No need for brackets --- src/Database/Adapter/SQL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index f357652c4..e3c9e4054 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -519,7 +519,7 @@ public function getAttributeWidth(Document $collection): int case Database::VAR_RELATIONSHIP: // INT(11) - $total += (Database::LENGTH_KEY * 4) + 2; + $total += Database::LENGTH_KEY * 4 + 2; break; case Database::VAR_DATETIME: From 070d44b86bb5c77099990b897695c01816d94787 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 13 Jan 2025 10:59:03 +0200 Subject: [PATCH 6/8] Address comments --- src/Database/Adapter/SQL.php | 2 +- src/Database/Database.php | 8 ---- tests/e2e/Adapter/Base.php | 75 ++++++++++++++++-------------------- 3 files changed, 35 insertions(+), 50 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index e3c9e4054..4ab86c32f 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -518,7 +518,7 @@ public function getAttributeWidth(Document $collection): int break; case Database::VAR_RELATIONSHIP: - // INT(11) + // VARCHAR(255) $total += Database::LENGTH_KEY * 4 + 2; break; diff --git a/src/Database/Database.php b/src/Database/Database.php index 0dfc1479e..1b7693a24 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -2169,14 +2169,6 @@ public function createRelationship( } } - if ( - $this->adapter->getLimitForAttributes() > 0 && - ($this->adapter->getCountOfAttributes($collection) >= $this->adapter->getLimitForAttributes() - || $this->adapter->getCountOfAttributes($relatedCollection) >= $this->adapter->getLimitForAttributes()) - ) { - throw new LimitException('Column limit reached. Cannot create new attribute.'); - } - $relationship = new Document([ '$id' => ID::custom($id), 'key' => $id, diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 09dc8fc99..5a02e7d80 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1366,57 +1366,50 @@ public function testSchemaAttributes(): void public function testRowSizeToLarge(): void { - if (static::getDatabase()->getAdapter()::getDocumentSizeLimit() === 0) { - $this->expectNotToPerformAssertions(); - return; - } - /** * getDocumentSizeLimit = 65535 - * mb4 65535 / 4 = 16383 + * 65535 / 4 = 16383 MB4 */ - if (static::getDatabase()->getAdapter()::getDocumentSizeLimit() > 0) { - $collection_1 = static::getDatabase()->createCollection('row_size_1'); - $collection_2 = static::getDatabase()->createCollection('row_size_2'); + $collection_1 = static::getDatabase()->createCollection('row_size_1'); + $collection_2 = static::getDatabase()->createCollection('row_size_2'); - $this->assertEquals(true, static::getDatabase()->createAttribute($collection_1->getId(), 'attr_1', Database::VAR_STRING, 16000, true)); + $this->assertEquals(true, static::getDatabase()->createAttribute($collection_1->getId(), 'attr_1', Database::VAR_STRING, 16000, true)); - try { - $this->assertEquals(true, static::getDatabase()->createAttribute($collection_1->getId(), 'attr_2', Database::VAR_STRING, Database::LENGTH_KEY, true)); - $this->fail('Failed to throw exception'); - } catch (Exception $e) { - $this->assertInstanceOf(LimitException::class, $e); - } + try { + static::getDatabase()->createAttribute($collection_1->getId(), 'attr_2', Database::VAR_STRING, Database::LENGTH_KEY, true); + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } - /** - * Relation takes length of Database::LENGTH_KEY so exceeding getDocumentSizeLimit - */ + /** + * Relation takes length of Database::LENGTH_KEY so exceeding getDocumentSizeLimit + */ - try { - static::getDatabase()->createRelationship( - collection: $collection_2->getId(), - relatedCollection: $collection_1->getId(), - type: Database::RELATION_ONE_TO_ONE, - twoWay: true, - ); + try { + static::getDatabase()->createRelationship( + collection: $collection_2->getId(), + relatedCollection: $collection_1->getId(), + type: Database::RELATION_ONE_TO_ONE, + twoWay: true, + ); - $this->fail('Failed to throw exception'); - } catch (Exception $e) { - $this->assertInstanceOf(LimitException::class, $e); - } + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); + } - try { - static::getDatabase()->createRelationship( - collection: $collection_1->getId(), - relatedCollection: $collection_2->getId(), - type: Database::RELATION_ONE_TO_ONE, - twoWay: true, - ); + try { + static::getDatabase()->createRelationship( + collection: $collection_1->getId(), + relatedCollection: $collection_2->getId(), + type: Database::RELATION_ONE_TO_ONE, + twoWay: true, + ); - $this->fail('Failed to throw exception'); - } catch (Exception $e) { - $this->assertInstanceOf(LimitException::class, $e); - } + $this->fail('Failed to throw exception'); + } catch (Exception $e) { + $this->assertInstanceOf(LimitException::class, $e); } } From d89943ba7645fb4e9423a97cf7c6d47d4e7f5b83 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 13 Jan 2025 11:08:03 +0200 Subject: [PATCH 7/8] Fix mongo tests --- tests/e2e/Adapter/Base.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 5a02e7d80..99585ef7a 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1366,6 +1366,10 @@ public function testSchemaAttributes(): void public function testRowSizeToLarge(): void { + if (static::getDatabase()->getAdapter()->getDocumentSizeLimit() > 0) { + $this->expectNotToPerformAssertions(); + return; + } /** * getDocumentSizeLimit = 65535 * 65535 / 4 = 16383 MB4 From 9c08018fb752b054bdc7d1f85a27ff05d047c56e Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 13 Jan 2025 11:08:23 +0200 Subject: [PATCH 8/8] Fix mongo tests --- tests/e2e/Adapter/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 99585ef7a..15fc39ad5 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1366,7 +1366,7 @@ public function testSchemaAttributes(): void public function testRowSizeToLarge(): void { - if (static::getDatabase()->getAdapter()->getDocumentSizeLimit() > 0) { + if (static::getDatabase()->getAdapter()->getDocumentSizeLimit() === 0) { $this->expectNotToPerformAssertions(); return; }