Skip to content

Commit dbe3537

Browse files
Refactor attribute size handling in database adapters to support both int and string types
- Updated `createAttribute`, `updateAttribute`, and `getColumnType` methods across various database adapters to accept `int|string` for the size parameter. - Introduced a new private method `getSize` in the `Database` class to normalize size handling based on attribute type. - Enhanced tests to ensure proper functionality with the updated size handling.
1 parent cafb353 commit dbe3537

10 files changed

Lines changed: 44 additions & 28 deletions

File tree

src/Database/Adapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ abstract public function analyzeCollection(string $collection): bool;
576576
* @throws TimeoutException
577577
* @throws DuplicateException
578578
*/
579-
abstract public function createAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): bool;
579+
abstract public function createAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): bool;
580580

581581
/**
582582
* Create Attributes
@@ -603,7 +603,7 @@ abstract public function createAttributes(string $collection, array $attributes)
603603
*
604604
* @return bool
605605
*/
606-
abstract public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool;
606+
abstract public function updateAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool;
607607

608608
/**
609609
* Delete Attribute
@@ -1405,7 +1405,7 @@ abstract public function getSchemaIndexes(string $collection): array;
14051405
* @return string
14061406
* @throws \Utopia\Database\Exception For unknown types on adapters that support column-type resolution.
14071407
*/
1408-
public function getColumnType(string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): string
1408+
public function getColumnType(string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): string
14091409
{
14101410
return '';
14111411
}

src/Database/Adapter/MariaDB.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ public function getSchemaAttributes(string $collection): array
418418
* @return bool
419419
* @throws DatabaseException
420420
*/
421-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
421+
public function updateAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
422422
{
423423
$name = $this->filter($collection);
424424
$id = $this->filter($id);
@@ -1678,7 +1678,7 @@ protected function getSQLCondition(Query $query, array &$binds): string
16781678
* @return string
16791679
* @throws DatabaseException
16801680
*/
1681-
protected function getSQLType(string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): string
1681+
protected function getSQLType(string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): string
16821682
{
16831683
if (in_array($type, Database::SPATIAL_TYPES)) {
16841684
return $this->getSpatialSQLType($type, $required);

src/Database/Adapter/Mongo.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ public function analyzeCollection(string $collection): bool
702702
* @param bool $array
703703
* @return bool
704704
*/
705-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): bool
705+
public function createAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
706706
{
707707
return true;
708708
}
@@ -1983,7 +1983,7 @@ public function deleteDocuments(string $collection, array $sequences, array $per
19831983
*
19841984
* @return bool
19851985
*/
1986-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
1986+
public function updateAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
19871987
{
19881988
if (!empty($newKey) && $newKey !== $id) {
19891989
return $this->renameAttribute($collection, $id, $newKey);

src/Database/Adapter/Pool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function analyzeCollection(string $collection): bool
203203
return $this->delegate(__FUNCTION__, \func_get_args());
204204
}
205205

206-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): bool
206+
public function createAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
207207
{
208208
return $this->delegate(__FUNCTION__, \func_get_args());
209209
}
@@ -213,7 +213,7 @@ public function createAttributes(string $collection, array $attributes): bool
213213
return $this->delegate(__FUNCTION__, \func_get_args());
214214
}
215215

216-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
216+
public function updateAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
217217
{
218218
return $this->delegate(__FUNCTION__, \func_get_args());
219219
}

src/Database/Adapter/Postgres.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public function analyzeCollection(string $collection): bool
470470
* @return bool
471471
* @throws DatabaseException
472472
*/
473-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): bool
473+
public function createAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
474474
{
475475
// Ensure pgvector extension is installed for vector types
476476
if ($type === Database::VAR_VECTOR) {
@@ -577,7 +577,7 @@ public function renameAttribute(string $collection, string $old, string $new): b
577577
* @throws Exception
578578
* @throws PDOException
579579
*/
580-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
580+
public function updateAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
581581
{
582582
$name = $this->filter($collection);
583583
$id = $this->filter($id);
@@ -1938,7 +1938,7 @@ protected function getFulltextValue(string $value): string
19381938
* @return string
19391939
* @throws DatabaseException
19401940
*/
1941-
protected function getSQLType(string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): string
1941+
protected function getSQLType(string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): string
19421942
{
19431943
if ($array === true) {
19441944
return 'JSONB';

src/Database/Adapter/SQL.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public function list(): array
243243
* @throws Exception
244244
* @throws PDOException
245245
*/
246-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): bool
246+
public function createAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
247247
{
248248
$id = $this->quote($this->filter($id));
249249
$type = $this->getSQLType($type, $size, $signed, $array, $required);
@@ -1837,7 +1837,7 @@ protected function getSQLOperator(string $method): string
18371837

18381838
abstract protected function getSQLType(
18391839
string $type,
1840-
int $size,
1840+
int|string $size,
18411841
bool $signed = true,
18421842
bool $array = false,
18431843
bool $required = false
@@ -1846,7 +1846,7 @@ abstract protected function getSQLType(
18461846
/**
18471847
* @throws DatabaseException For unknown type values.
18481848
*/
1849-
public function getColumnType(string $type, int $size, bool $signed = true, bool $array = false, bool $required = false): string
1849+
public function getColumnType(string $type, int|string $size, bool $signed = true, bool $array = false, bool $required = false): string
18501850
{
18511851
return $this->getSQLType($type, $size, $signed, $array, $required);
18521852
}

src/Database/Adapter/SQLite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public function analyzeCollection(string $collection): bool
336336
* @throws Exception
337337
* @throws PDOException
338338
*/
339-
public function updateAttribute(string $collection, string $id, string $type, int $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
339+
public function updateAttribute(string $collection, string $id, string $type, int|string $size, bool $signed = true, bool $array = false, ?string $newKey = null, bool $required = false): bool
340340
{
341341
if (!empty($newKey) && $newKey !== $id) {
342342
return $this->renameAttribute($collection, $id, $newKey);

src/Database/Database.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,15 @@ private function normalizeCollectionAttributeSizes(Document $collection): Docume
19871987
return $collection;
19881988
}
19891989

1990+
private function getSize(string $type, int|string|null $size): int|string
1991+
{
1992+
if ($type === self::VAR_BIGINT) {
1993+
return (string)($size ?? 0);
1994+
}
1995+
1996+
return (int)($size ?? 0);
1997+
}
1998+
19901999
/**
19912000
* Get Collection Size
19922001
*
@@ -2146,8 +2155,9 @@ public function deleteCollection(string $id): bool
21462155
* @throws StructureException
21472156
* @throws Exception
21482157
*/
2149-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $required, mixed $default = null, bool $signed = true, bool $array = false, ?string $format = null, array $formatOptions = [], array $filters = []): bool
2158+
public function createAttribute(string $collection, string $id, string $type, int|string $size, bool $required, mixed $default = null, bool $signed = true, bool $array = false, ?string $format = null, array $formatOptions = [], array $filters = []): bool
21502159
{
2160+
$size = $this->getSize($type, $size);
21512161
$collection = $this->silent(fn () => $this->getCollection($collection));
21522162

21532163
if ($collection->isEmpty()) {
@@ -2231,7 +2241,7 @@ public function createAttribute(string $collection, string $id, string $type, in
22312241
'$id' => ID::custom($id),
22322242
'key' => $id,
22332243
'type' => $type,
2234-
'size' => $type === self::VAR_BIGINT ? (string)$size : $size,
2244+
'size' => $this->getSize($type, $size),
22352245
'required' => $required,
22362246
'default' => $default,
22372247
'signed' => $signed,
@@ -2355,6 +2365,7 @@ public function createAttributes(string $collection, array $attributes): bool
23552365
$existsInSchema = false;
23562366

23572367
try {
2368+
$attribute['size'] = $this->getSize($attribute['type'], $attribute['size']);
23582369
$attributeDocument = $this->validateAttribute(
23592370
$collection,
23602371
$attribute['$id'],
@@ -2411,7 +2422,7 @@ public function createAttributes(string $collection, array $attributes): bool
24112422
'$id' => ID::custom($attribute['$id']),
24122423
'key' => $attribute['$id'],
24132424
'type' => $attribute['type'],
2414-
'size' => $attribute['size'],
2425+
'size' => $this->getSize($attribute['type'], $attribute['size']),
24152426
'required' => $attribute['required'],
24162427
'default' => $attribute['default'],
24172428
'signed' => $attribute['signed'],
@@ -2514,7 +2525,7 @@ private function validateAttribute(
25142525
Document $collection,
25152526
string $id,
25162527
string $type,
2517-
int $size,
2528+
int|string $size,
25182529
bool $required,
25192530
mixed $default,
25202531
bool $signed,
@@ -2528,7 +2539,7 @@ private function validateAttribute(
25282539
'$id' => ID::custom($id),
25292540
'key' => $id,
25302541
'type' => $type,
2531-
'size' => $type === self::VAR_BIGINT ? (string)$size : $size,
2542+
'size' => $this->getSize($type, $size),
25322543
'required' => $required,
25332544
'default' => $default,
25342545
'signed' => $signed,
@@ -2870,7 +2881,7 @@ public function updateAttributeDefault(string $collection, string $id, mixed $de
28702881
* @return Document
28712882
* @throws Exception
28722883
*/
2873-
public function updateAttribute(string $collection, string $id, ?string $type = null, ?int $size = null, ?bool $required = null, mixed $default = null, ?bool $signed = null, ?bool $array = null, ?string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
2884+
public function updateAttribute(string $collection, string $id, ?string $type = null, int|string|null $size = null, ?bool $required = null, mixed $default = null, ?bool $signed = null, ?bool $array = null, ?string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
28742885
{
28752886
$collectionDoc = $this->silent(fn () => $this->getCollection($collection));
28762887

@@ -2906,6 +2917,7 @@ public function updateAttribute(string $collection, string $id, ?string $type =
29062917
|| !\is_null($newKey);
29072918
$type ??= $attribute->getAttribute('type');
29082919
$size ??= $attribute->getAttribute('size');
2920+
$size = $this->getSize($type, $size);
29092921
$signed ??= $attribute->getAttribute('signed');
29102922
$required ??= $attribute->getAttribute('required');
29112923
$default ??= $attribute->getAttribute('default');
@@ -3070,7 +3082,7 @@ public function updateAttribute(string $collection, string $id, ?string $type =
30703082
->setAttribute('$id', $newKey ?? $id)
30713083
->setattribute('key', $newKey ?? $id)
30723084
->setAttribute('type', $type)
3073-
->setAttribute('size', $type === self::VAR_BIGINT ? (string)$size : $size)
3085+
->setAttribute('size', $this->getSize($type, $size))
30743086
->setAttribute('signed', $signed)
30753087
->setAttribute('array', $array)
30763088
->setAttribute('format', $format)
@@ -3194,7 +3206,7 @@ public function updateAttribute(string $collection, string $id, ?string $type =
31943206
$collection,
31953207
$newKey ?? $id,
31963208
$originalType,
3197-
$originalSize,
3209+
(int)$originalSize,
31983210
$originalSigned,
31993211
$originalArray,
32003212
$originalKey,

src/Database/Mirror.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ public function deleteCollection(string $id): bool
301301
return $result;
302302
}
303303

304-
public function createAttribute(string $collection, string $id, string $type, int $size, bool $required, $default = null, bool $signed = true, bool $array = false, ?string $format = null, array $formatOptions = [], array $filters = []): bool
304+
public function createAttribute(string $collection, string $id, string $type, int|string $size, bool $required, $default = null, bool $signed = true, bool $array = false, ?string $format = null, array $formatOptions = [], array $filters = []): bool
305305
{
306306
$result = $this->source->createAttribute(
307307
$collection,
@@ -399,7 +399,7 @@ public function createAttributes(string $collection, array $attributes): bool
399399
return $result;
400400
}
401401

402-
public function updateAttribute(string $collection, string $id, ?string $type = null, ?int $size = null, ?bool $required = null, mixed $default = null, ?bool $signed = null, ?bool $array = null, ?string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
402+
public function updateAttribute(string $collection, string $id, ?string $type = null, int|string|null $size = null, ?bool $required = null, mixed $default = null, ?bool $signed = null, ?bool $array = null, ?string $format = null, ?array $formatOptions = null, ?array $filters = null, ?string $newKey = null): Document
403403
{
404404
$document = $this->source->updateAttribute(
405405
$collection,

tests/e2e/Adapter/Scopes/DocumentTests.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ public function testBigIntScenariosWithFiltering(): void
187187
$this->assertEquals($unsignedValue, (string)$document->getAttribute('unsigned_bigint'));
188188
$this->assertTrue(\is_string($document->getAttribute('unsigned_bigint')));
189189

190+
// Read path: fetch document and ensure unsigned bigint round-trips unchanged.
191+
$fetchedDocument = $database->getDocument($collection, $document->getId());
192+
$this->assertEquals($unsignedValue, (string)$fetchedDocument->getAttribute('unsigned_bigint'));
193+
190194
// Update path should apply the same normalization for signed bigint numeric strings.
191195
$updated = $database->updateDocument($collection, $document->getId(), new Document([
192196
'signed_bigint' => $signedMax,
@@ -230,8 +234,8 @@ public function testWithSingedBigInt(): void
230234
$database->createCollection($collection);
231235
$this->assertEquals(true, $database->createAttribute($collection, 'signed_bigint', Database::VAR_BIGINT, 0, true));
232236

233-
$signedMin = (string)\PHP_INT_MIN;
234-
$signedMax = (string)\PHP_INT_MAX;
237+
$signedMin = \PHP_INT_MIN;
238+
$signedMax = \PHP_INT_MAX;
235239

236240
$document = $database->createDocument($collection, new Document([
237241
'$id' => 'signed-bigint-doc',

0 commit comments

Comments
 (0)