diff --git a/src/Database/Trait/Attributes.php b/src/Database/Trait/Attributes.php index a6334e00c..2f9974fff 100644 --- a/src/Database/Trait/Attributes.php +++ b/src/Database/Trait/Attributes.php @@ -310,14 +310,21 @@ public function updateAttribute(string $collection, string $key, Update $update) /** * Checks that the attribute can be added to the collection without exceeding its limits. * + * Pending attributes are accepted but not yet in the collection, such as ones queued for + * creation, and count towards the limits as if they were already added. + * + * @param list $pending + * * @throws LimitException * @throws NotFoundException */ - public function checkAttribute(string $collection, Attribute $attribute): bool + public function checkAttribute(string $collection, Attribute $attribute, array $pending = []): bool { $definition = clone ($this->silent(fn () => $this->getCollection($collection))); - $definition->setAttribute(self::COLLECTION_ATTRIBUTES, $attribute->toDocument(), SetType::Append); + foreach ([...$pending, $attribute] as $added) { + $definition->setAttribute(self::COLLECTION_ATTRIBUTES, $added->toDocument(), SetType::Append); + } if ( $this->adapter->limits()->attributes > 0 && diff --git a/tests/unit/Attributes/CheckAttributeTest.php b/tests/unit/Attributes/CheckAttributeTest.php index 91940f6a5..22ccad97e 100644 --- a/tests/unit/Attributes/CheckAttributeTest.php +++ b/tests/unit/Attributes/CheckAttributeTest.php @@ -32,6 +32,15 @@ public function testAnAttributeOverTheLimitIsRefused(): void $database->checkAttribute('items', Attribute::string('fifth', 16)); } + public function testPendingAttributesCountTowardsTheLimit(): void + { + $database = $this->database([Attribute::string('first', 16), Attribute::string('second', 16), Attribute::string('third', 16)]); + + $this->expectException(LimitException::class); + + $database->checkAttribute('items', Attribute::string('fifth', 16), [Attribute::string('fourth', 16)]); + } + public function testCheckingDoesNotStoreTheAttribute(): void { $database = $this->database([Attribute::string('first', 16)]);