diff --git a/src/Database/Database.php b/src/Database/Database.php index 5da5573d9..dcffff33e 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -92,9 +92,9 @@ class Database public const DELETE_BATCH_SIZE = 1_000; /** - * Attribute types whose stored value is produced by a filter of the same name. Both public - * creation paths add it, so an attribute made through createCollection() encodes and decodes - * the same way as the identical one made through createAttribute(). + * Attribute types whose stored value is produced by a filter of the same name. Every public + * creation path adds it, so an attribute made through createCollection(), createAttribute() + * or createAttributes() encodes and decodes the same way as any identical one. * * @var list */ diff --git a/src/Database/Traits/Attributes.php b/src/Database/Traits/Attributes.php index 2ea245f63..a4b719bee 100644 --- a/src/Database/Traits/Attributes.php +++ b/src/Database/Traits/Attributes.php @@ -227,6 +227,12 @@ public function createAttributes(string $collection, array $attributes): bool throw new DatabaseException('Missing attribute key'); } + if (in_array($attribute->type, Database::ATTRIBUTE_FILTER_TYPES, true)) { + $attribute->filters = array_values( + array_unique(array_merge($attribute->filters, [$attribute->type->value])) + ); + } + $existsInSchema = false; try { diff --git a/tests/e2e/Adapter/Scopes/AttributeTests.php b/tests/e2e/Adapter/Scopes/AttributeTests.php index e8692aded..b4c66bec0 100644 --- a/tests/e2e/Adapter/Scopes/AttributeTests.php +++ b/tests/e2e/Adapter/Scopes/AttributeTests.php @@ -1935,6 +1935,41 @@ public function testCreateDatetimeAddingAutoFilter(): void $database->deleteCollection($collection); } + public function testCreateAttributesAddingAutoFilter(): void + { + /** @var Database $database */ + $database = $this->getDatabase(); + + if (! $database->getAdapter()->supports(Capability::BatchCreateAttributes)) { + $this->markTestSkipped('Adapter does not support batch attribute creation'); + } + + $collection = 'datetime_batch_auto_filter'; + + $database->createCollection(new Collection( + id: $collection, + permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + ], + documentSecurity: false, + )); + + $database->createAttributes($collection, [Attribute::datetime(key: 'batch')]); + + $database->createDocument($collection, new Document([ + Document::ID => 'offset', + 'batch' => '2024-01-02T03:04:05.000+05:00', + ])); + + $this->assertSame( + '2024-01-01T22:04:05.000+00:00', + $database->getDocument($collection, 'offset')->getAttribute('batch') + ); + + $database->deleteCollection($collection); + } + public function testCreateAttributesBigIntIgnoresSizeMetadata(): void { /** @var Database $database */