Repository navigation
fix(sql): count a relationship only on the side that holds its column #1083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One-way updates can leave an uncounted child column If an existing two-way one-to-one relationship is changed to one-way, the update changes its metadata but does not drop the already-created child column. This now excludes that column from the estimate, so subsequent schema checks can accept additions that exceed the actual MySQL row-width limit. Prompt To Fix With AIThis is a comment left during a code review.
Path: src/Database/Relationship.php
Line: 95
Comment:
**One-way updates can leave an uncounted child column**
If an existing two-way one-to-one relationship is changed to one-way, the update changes its metadata but does not drop the already-created child column. This now excludes that column from the estimate, so subsequent schema checks can accept additions that exceed the actual MySQL row-width limit.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · bug · Reply if this doesn't apply. |
||
| RelationshipType::OneToMany => $side === RelationshipSide::Child, | ||
| RelationshipType::ManyToOne => $side === RelationshipSide::Parent, | ||
| RelationshipType::ManyToMany => false, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @throws RelationshipException | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\Unit\Adapter; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Database\Adapter\MySQL; | ||
| use Utopia\Database\Attribute; | ||
| use Utopia\Database\Collection; | ||
| use Utopia\Database\Relationship; | ||
| use Utopia\Database\RelationshipSide; | ||
|
|
||
| final class RelationshipColumnEstimateTest extends TestCase | ||
| { | ||
| private const int RELATIONSHIP_WIDTH = 1021; | ||
|
|
||
| public function testSidesWithoutColumnDoNotWidenTheRow(): void | ||
| { | ||
| $adapter = new MySQL($this->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<string, array{Relationship, RelationshipSide, bool}> | ||
| */ | ||
| 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), | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep existing child columns in the estimate after a two-way update.
If a two-way one-to-one relationship changes to
twoWay=false, the update caller changes both attribute definitions without dropping the child column. This filter then excludes that existing column. (raw.githubusercontent.com)The previous estimators still counted it. The new estimators undercount by one column and 1,021 bytes, so they report capacity that the existing table does not have. (raw.githubusercontent.com)
Track physical-column presence separately from
twoWay, or reconcile the child column when that flag changes. Keep newly created one-way child sides excluded.