Skip to content

Commit 75a7f28

Browse files
Refactor attribute size handling to enforce integer type across database adapters
- Updated `createAttribute`, `updateAttribute`, and `getColumnType` methods in various database adapters to require `int` for the size parameter, removing support for `string`. - Removed the `getSize` method from the `Database` class as it is no longer needed. - Adjusted related tests to validate the new integer-only size handling.
1 parent ba45e22 commit 75a7f28

12 files changed

Lines changed: 45 additions & 69 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|string $size, bool $signed = true, bool $array = false, bool $required = false): bool;
579+
abstract public function createAttribute(string $collection, string $id, string $type, int $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|string $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 $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|string $size, bool $signed = true, bool $array = false, bool $required = false): string
1408+
public function getColumnType(string $type, int $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|string $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 $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|string $size, bool $signed = true, bool $array = false, bool $required = false): string
1681+
protected function getSQLType(string $type, int $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|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
705+
public function createAttribute(string $collection, string $id, string $type, int $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|string $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 $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|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
206+
public function createAttribute(string $collection, string $id, string $type, int $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|string $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 $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|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
473+
public function createAttribute(string $collection, string $id, string $type, int $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|string $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 $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|string $size, bool $signed = true, bool $array = false, bool $required = false): string
1941+
protected function getSQLType(string $type, int $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|string $size, bool $signed = true, bool $array = false, bool $required = false): bool
246+
public function createAttribute(string $collection, string $id, string $type, int $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|string $size,
1840+
int $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|string $size, bool $signed = true, bool $array = false, bool $required = false): string
1849+
public function getColumnType(string $type, int $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|string $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 $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: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,32 +1970,26 @@ private function normalizeCollectionAttributeSizes(Document $collection): Docume
19701970
}
19711971

19721972
$type = $attribute->getAttribute('type');
1973-
$size = $attribute->getAttribute('size', 0);
19741973

19751974
if ($type === self::VAR_BIGINT) {
1976-
// Keep bigint size as string to avoid precision loss.
1977-
$attribute->setAttribute('size', (string)$size);
1978-
continue;
1975+
$formatOptions = $attribute->getAttribute('formatOptions', []);
1976+
if (\is_array($formatOptions)) {
1977+
if (\array_key_exists('min', $formatOptions) && $formatOptions['min'] !== null) {
1978+
$formatOptions['min'] = (string)$formatOptions['min'];
1979+
}
1980+
if (\array_key_exists('max', $formatOptions) && $formatOptions['max'] !== null) {
1981+
$formatOptions['max'] = (string)$formatOptions['max'];
1982+
}
1983+
$attribute->setAttribute('formatOptions', $formatOptions);
1984+
}
19791985
}
1980-
1981-
// Keep legacy behavior for non-bigint metadata.
1982-
$attribute->setAttribute('size', (int)$size);
19831986
}
19841987

19851988
$collection->setAttribute('attributes', $attributes);
19861989

19871990
return $collection;
19881991
}
19891992

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-
19991993
/**
20001994
* Get Collection Size
20011995
*
@@ -2155,9 +2149,8 @@ public function deleteCollection(string $id): bool
21552149
* @throws StructureException
21562150
* @throws Exception
21572151
*/
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
2152+
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
21592153
{
2160-
$size = $this->getSize($type, $size);
21612154
$collection = $this->silent(fn () => $this->getCollection($collection));
21622155

21632156
if ($collection->isEmpty()) {
@@ -2241,7 +2234,7 @@ public function createAttribute(string $collection, string $id, string $type, in
22412234
'$id' => ID::custom($id),
22422235
'key' => $id,
22432236
'type' => $type,
2244-
'size' => $this->getSize($type, $size),
2237+
'size' => $size,
22452238
'required' => $required,
22462239
'default' => $default,
22472240
'signed' => $signed,
@@ -2365,7 +2358,6 @@ public function createAttributes(string $collection, array $attributes): bool
23652358
$existsInSchema = false;
23662359

23672360
try {
2368-
$attribute['size'] = $this->getSize($attribute['type'], $attribute['size']);
23692361
$attributeDocument = $this->validateAttribute(
23702362
$collection,
23712363
$attribute['$id'],
@@ -2422,7 +2414,7 @@ public function createAttributes(string $collection, array $attributes): bool
24222414
'$id' => ID::custom($attribute['$id']),
24232415
'key' => $attribute['$id'],
24242416
'type' => $attribute['type'],
2425-
'size' => $this->getSize($attribute['type'], $attribute['size']),
2417+
'size' => $attribute['size'],
24262418
'required' => $attribute['required'],
24272419
'default' => $attribute['default'],
24282420
'signed' => $attribute['signed'],
@@ -2525,7 +2517,7 @@ private function validateAttribute(
25252517
Document $collection,
25262518
string $id,
25272519
string $type,
2528-
int|string $size,
2520+
int $size,
25292521
bool $required,
25302522
mixed $default,
25312523
bool $signed,
@@ -2539,7 +2531,7 @@ private function validateAttribute(
25392531
'$id' => ID::custom($id),
25402532
'key' => $id,
25412533
'type' => $type,
2542-
'size' => $this->getSize($type, $size),
2534+
'size' => $size,
25432535
'required' => $required,
25442536
'default' => $default,
25452537
'signed' => $signed,
@@ -2881,7 +2873,7 @@ public function updateAttributeDefault(string $collection, string $id, mixed $de
28812873
* @return Document
28822874
* @throws Exception
28832875
*/
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
2876+
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
28852877
{
28862878
$collectionDoc = $this->silent(fn () => $this->getCollection($collection));
28872879

@@ -2917,7 +2909,6 @@ public function updateAttribute(string $collection, string $id, ?string $type =
29172909
|| !\is_null($newKey);
29182910
$type ??= $attribute->getAttribute('type');
29192911
$size ??= $attribute->getAttribute('size');
2920-
$size = $this->getSize($type, $size);
29212912
$signed ??= $attribute->getAttribute('signed');
29222913
$required ??= $attribute->getAttribute('required');
29232914
$default ??= $attribute->getAttribute('default');
@@ -2969,11 +2960,12 @@ public function updateAttribute(string $collection, string $id, ?string $type =
29692960
}
29702961
break;
29712962
case self::VAR_BIGINT:
2972-
$sizeString = (string)$size;
2973-
$limit = ($this->adapter->getSupportForUnsignedBigInt() && !$signed)
2963+
$sizeString = BigIntHelper::normalizeUnsignedString((string)$size);
2964+
$limit = (!$signed && $this->adapter->getSupportForUnsignedBigInt())
29742965
? BigIntHelper::UNSIGNED_MAX
2975-
: (string)$this->adapter->getLimitForBigInt();
2976-
if (!BigIntHelper::fitsBigIntRange($sizeString, false, $this->adapter->getSupportForUnsignedBigInt() && !$signed)) {
2966+
: BigIntHelper::SIGNED_MAX;
2967+
2968+
if (BigIntHelper::compareUnsignedStrings($sizeString, $limit) > 0) {
29772969
throw new DatabaseException('Max size allowed for bigint is: ' . BigIntHelper::formatIntegerString($limit));
29782970
}
29792971
break;
@@ -3082,7 +3074,7 @@ public function updateAttribute(string $collection, string $id, ?string $type =
30823074
->setAttribute('$id', $newKey ?? $id)
30833075
->setattribute('key', $newKey ?? $id)
30843076
->setAttribute('type', $type)
3085-
->setAttribute('size', $this->getSize($type, $size))
3077+
->setAttribute('size', $size)
30863078
->setAttribute('signed', $signed)
30873079
->setAttribute('array', $array)
30883080
->setAttribute('format', $format)

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|string $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 $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|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
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
403403
{
404404
$document = $this->source->updateAttribute(
405405
$collection,

src/Database/Validator/Attribute.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Utopia\Database\Exception as DatabaseException;
88
use Utopia\Database\Exception\Duplicate as DuplicateException;
99
use Utopia\Database\Exception\Limit as LimitException;
10-
use Utopia\Database\Helpers\BigInt as BigIntHelper;
1110
use Utopia\Validator;
1211

1312
class Attribute extends Validator
@@ -347,21 +346,6 @@ public function checkType(Document $attribute): bool
347346
break;
348347

349348
case Database::VAR_BIGINT:
350-
$sizeString = \is_string($size) ? $size : (string)$size;
351-
if (!BigIntHelper::isIntegerString($sizeString, false)) {
352-
$this->message = 'Size must be a positive integer';
353-
throw new DatabaseException($this->message);
354-
}
355-
356-
$sizeString = BigIntHelper::normalizeUnsignedString($sizeString);
357-
$limit = (!$signed && $this->supportUnsignedBigInt)
358-
? BigIntHelper::UNSIGNED_MAX
359-
: (string)$this->maxBigIntLength;
360-
361-
if (BigIntHelper::compareUnsignedStrings($sizeString, $limit) > 0) {
362-
$this->message = 'Max size allowed for bigint is: ' . BigIntHelper::formatIntegerString($limit);
363-
throw new DatabaseException($this->message);
364-
}
365349
break;
366350

367351
case Database::VAR_FLOAT:

0 commit comments

Comments
 (0)