diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index ec795b30f..37cc03094 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -2622,7 +2622,19 @@ protected function limitsEscalatedStringByteCap(): bool #[\Override] public function getCountOfAttributes(Document $collection): int { - return \count(self::collectionAttributes($collection)) + $this->limits()->defaultAttributes; + return \count(self::collectionColumns($collection)) + $this->limits()->defaultAttributes; + } + + /** + * @return list + */ + private static function collectionColumns(Document $collection): array + { + return \array_values(\array_filter( + self::collectionAttributes($collection), + static fn (Attribute $attribute): bool => $attribute->relationship === null + || $attribute->relationship->hasColumn($attribute->side ?? RelationshipSide::Parent), + )); } #[\Override] @@ -2653,7 +2665,7 @@ public function getAttributeWidth(Document $collection): int */ $total = 1067; - foreach (self::collectionAttributes($collection) as $attribute) { + foreach (self::collectionColumns($collection) as $attribute) { $attributeSize = $attribute->size ?? 0; /** diff --git a/src/Database/Relationship.php b/src/Database/Relationship.php index 6f0e1857a..59aec5865 100644 --- a/src/Database/Relationship.php +++ b/src/Database/Relationship.php @@ -89,6 +89,16 @@ public function inverse(string $collection): self return new self($collection, $this->type, $this->twoWay, $this->twoWayKey, $this->key, $this->onDelete); } + public function hasColumn(RelationshipSide $side): bool + { + return match ($this->type) { + RelationshipType::OneToOne => $side === RelationshipSide::Parent || $this->twoWay, + RelationshipType::OneToMany => $side === RelationshipSide::Child, + RelationshipType::ManyToOne => $side === RelationshipSide::Parent, + RelationshipType::ManyToMany => false, + }; + } + /** * @throws RelationshipException */ diff --git a/tests/unit/Adapter/RelationshipColumnEstimateTest.php b/tests/unit/Adapter/RelationshipColumnEstimateTest.php new file mode 100644 index 000000000..65dc96f30 --- /dev/null +++ b/tests/unit/Adapter/RelationshipColumnEstimateTest.php @@ -0,0 +1,79 @@ +createStub(\PDO::class)); + + $strings = []; + for ($i = 0; $i < 7; $i++) { + $strings[] = Attribute::string(key: 'string'.$i, size: 2200); + } + + $relationships = [ + Attribute::relationship('authors', Relationship::manyToOne(relatedCollection: 'authors', twoWay: true, twoWayKey: 'book'), RelationshipSide::Child), + Attribute::relationship('tags', Relationship::manyToMany(relatedCollection: 'tags', twoWay: true, twoWayKey: 'books'), RelationshipSide::Child), + Attribute::relationship('reviews', Relationship::manyToOne(relatedCollection: 'reviews', twoWay: true, twoWayKey: 'book'), RelationshipSide::Child), + ]; + + $withoutRelationships = Collection::create(id: 'books', attributes: $strings); + $collection = Collection::create(id: 'books', attributes: [...$strings, ...$relationships]); + + $width = $adapter->getAttributeWidth($collection); + + $this->assertSame(62681, $width, 'Seven VARCHAR(2200) columns on top of the internal columns, and nothing for the relationships'); + $this->assertLessThan($adapter->limits()->documentSize, $width); + $this->assertSame($adapter->getAttributeWidth($withoutRelationships), $width); + $this->assertSame($adapter->getCountOfAttributes($withoutRelationships), $adapter->getCountOfAttributes($collection)); + } + + /** + * @return array + */ + public static function sides(): array + { + return [ + 'oneToOne parent' => [Relationship::oneToOne(relatedCollection: 'other'), RelationshipSide::Parent, true], + 'oneToOne two-way child' => [Relationship::oneToOne(relatedCollection: 'other', twoWay: true), RelationshipSide::Child, true], + 'oneToOne one-way child' => [Relationship::oneToOne(relatedCollection: 'other'), RelationshipSide::Child, false], + 'oneToMany parent' => [Relationship::oneToMany(relatedCollection: 'other', twoWay: true), RelationshipSide::Parent, false], + 'oneToMany child' => [Relationship::oneToMany(relatedCollection: 'other'), RelationshipSide::Child, true], + 'manyToOne parent' => [Relationship::manyToOne(relatedCollection: 'other'), RelationshipSide::Parent, true], + 'manyToOne child' => [Relationship::manyToOne(relatedCollection: 'other', twoWay: true), RelationshipSide::Child, false], + 'manyToMany parent' => [Relationship::manyToMany(relatedCollection: 'other', twoWay: true), RelationshipSide::Parent, false], + 'manyToMany child' => [Relationship::manyToMany(relatedCollection: 'other', twoWay: true), RelationshipSide::Child, false], + ]; + } + + #[DataProvider('sides')] + public function testOnlyASideWithAColumnIsCounted(Relationship $relationship, RelationshipSide $side, bool $hasColumn): void + { + $adapter = new MySQL($this->createStub(\PDO::class)); + + $empty = Collection::create(id: 'books'); + $collection = Collection::create(id: 'books', attributes: [Attribute::relationship('related', $relationship, $side)]); + + $this->assertSame($hasColumn, $relationship->hasColumn($side)); + $this->assertSame( + $hasColumn ? self::RELATIONSHIP_WIDTH : 0, + $adapter->getAttributeWidth($collection) - $adapter->getAttributeWidth($empty), + ); + $this->assertSame( + $hasColumn ? 1 : 0, + $adapter->getCountOfAttributes($collection) - $adapter->getCountOfAttributes($empty), + ); + } +}