Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ColumnType>
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Database/Traits/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions tests/e2e/Adapter/Scopes/AttributeTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Loading