Skip to content

Commit ea6116c

Browse files
abnegateclaude
andauthored
fix(attributes): attach the auto filter in the batch creation path (#967)
createAttributes() is the third public path that creates attributes, and it was the only one that never attached the type-named filter listed in Database::ATTRIBUTE_FILTER_TYPES. createAttribute() and createCollection() both attach it before validating. The attribute validator requires that filter to be present, so a datetime, point, linestring, polygon, vector or object attribute created through the batch path was rejected outright with "Attribute of type: datetime requires the following filters: datetime". Had validation let it through, the value would have been stored without the encode/decode the other two paths apply — a datetime written as +05:00 would never have been normalised to +00:00. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 002474c commit ea6116c

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎src/Database/Database.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ class Database
9292
public const DELETE_BATCH_SIZE = 1_000;
9393

9494
/**
95-
* Attribute types whose stored value is produced by a filter of the same name. Both public
96-
* creation paths add it, so an attribute made through createCollection() encodes and decodes
97-
* the same way as the identical one made through createAttribute().
95+
* Attribute types whose stored value is produced by a filter of the same name. Every public
96+
* creation path adds it, so an attribute made through createCollection(), createAttribute()
97+
* or createAttributes() encodes and decodes the same way as any identical one.
9898
*
9999
* @var list<ColumnType>
100100
*/

‎src/Database/Traits/Attributes.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public function createAttributes(string $collection, array $attributes): bool
227227
throw new DatabaseException('Missing attribute key');
228228
}
229229

230+
if (in_array($attribute->type, Database::ATTRIBUTE_FILTER_TYPES, true)) {
231+
$attribute->filters = array_values(
232+
array_unique(array_merge($attribute->filters, [$attribute->type->value]))
233+
);
234+
}
235+
230236
$existsInSchema = false;
231237

232238
try {

‎tests/e2e/Adapter/Scopes/AttributeTests.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,41 @@ public function testCreateDatetimeAddingAutoFilter(): void
19351935
$database->deleteCollection($collection);
19361936
}
19371937

1938+
public function testCreateAttributesAddingAutoFilter(): void
1939+
{
1940+
/** @var Database $database */
1941+
$database = $this->getDatabase();
1942+
1943+
if (! $database->getAdapter()->supports(Capability::BatchCreateAttributes)) {
1944+
$this->markTestSkipped('Adapter does not support batch attribute creation');
1945+
}
1946+
1947+
$collection = 'datetime_batch_auto_filter';
1948+
1949+
$database->createCollection(new Collection(
1950+
id: $collection,
1951+
permissions: [
1952+
Permission::create(Role::any()),
1953+
Permission::read(Role::any()),
1954+
],
1955+
documentSecurity: false,
1956+
));
1957+
1958+
$database->createAttributes($collection, [Attribute::datetime(key: 'batch')]);
1959+
1960+
$database->createDocument($collection, new Document([
1961+
Document::ID => 'offset',
1962+
'batch' => '2024-01-02T03:04:05.000+05:00',
1963+
]));
1964+
1965+
$this->assertSame(
1966+
'2024-01-01T22:04:05.000+00:00',
1967+
$database->getDocument($collection, 'offset')->getAttribute('batch')
1968+
);
1969+
1970+
$database->deleteCollection($collection);
1971+
}
1972+
19381973
public function testCreateAttributesBigIntIgnoresSizeMetadata(): void
19391974
{
19401975
/** @var Database $database */

0 commit comments

Comments
 (0)