Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1398,4 +1398,8 @@ abstract public function setUTCDatetime(string $value): mixed;
*/
abstract public function setSupportForAttributes(bool $support): bool;

/**
* @return int
*/
abstract public function getMaxVarcharLength(): int;
}
12 changes: 12 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1934,4 +1934,16 @@ public function getSupportForOptionalSpatialAttributeWithExistingRows(): bool
{
return true;
}

/**
* @return int
*/
public function getMaxVarcharLength(): int
{
/**
* Max varchar in MySQL:16381 | MariaDB:16382
* Will return the max the index length can use for smaller page size
*/
return $this->getMaxIndexLength();
}
}
5 changes: 5 additions & 0 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3189,4 +3189,9 @@ public function getTenantQuery(string $collection, string $alias = ''): string
{
return '';
}

public function getMaxVarcharLength(): int
{
return 0;
}
}
5 changes: 5 additions & 0 deletions src/Database/Adapter/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,9 @@ public function setSupportForAttributes(bool $support): bool
{
return $this->delegate(__FUNCTION__, \func_get_args());
}

public function getMaxVarcharLength(): int
{
return $this->delegate(__FUNCTION__, \func_get_args());
}
}
8 changes: 8 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -2321,4 +2321,12 @@ public function decodePolygon(string $wkb): array

return $rings; // array of rings, each ring is array of [x,y]
}

/**
* @return int
*/
public function getMaxVarcharLength(): int
{
return 16383;
}
}
8 changes: 0 additions & 8 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1849,14 +1849,6 @@ public function getHostname(): string
}
}

/**
* @return int
*/
public function getMaxVarcharLength(): int
{
return 16381; // Floor value for Postgres:16383 | MySQL:16381 | MariaDB:16382
}

/**
* Size of POINT spatial type
*
Expand Down
36 changes: 20 additions & 16 deletions tests/e2e/Adapter/Scopes/AttributeTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testCreateDeleteAttribute(): void
$database->createCollection('attributes');

$this->assertEquals(true, $database->createAttribute('attributes', 'string1', Database::VAR_STRING, 128, true));
$this->assertEquals(true, $database->createAttribute('attributes', 'string2', Database::VAR_STRING, 16382 + 1, true));
$this->assertEquals(true, $database->createAttribute('attributes', 'string2', Database::VAR_STRING, 20000, true));
$this->assertEquals(true, $database->createAttribute('attributes', 'string3', Database::VAR_STRING, 65535 + 1, true));
$this->assertEquals(true, $database->createAttribute('attributes', 'string4', Database::VAR_STRING, 16777215 + 1, true));
$this->assertEquals(true, $database->createAttribute('attributes', 'integer', Database::VAR_INTEGER, 0, true));
Expand Down Expand Up @@ -979,23 +979,27 @@ public function testExceptionWidthLimit(): void
return;
}

$limit = floor(($database->getAdapter()->getDocumentSizeLimit() / 4) / $database->getAdapter()->getMaxVarcharLength());

$attributes = [];

$attributes[] = new Document([
'$id' => ID::custom('varchar_16000'),
'type' => Database::VAR_STRING,
'size' => 16000,
'required' => true,
'default' => null,
'signed' => true,
'array' => false,
'filters' => [],
]);
for ($i = 1; $i < $limit; $i++) {
$attributes[] = new Document([
'$id' => ID::custom('varchar_'.$i),
'type' => Database::VAR_STRING,
'size' => $database->getAdapter()->getMaxVarcharLength(),
'required' => true,
'default' => null,
'signed' => true,
'array' => false,
'filters' => [],
]);
}

$attributes[] = new Document([
'$id' => ID::custom('varchar_200'),
'$id' => ID::custom('breaking'),
'type' => Database::VAR_STRING,
'size' => 200,
'size' => $database->getAdapter()->getMaxVarcharLength(),
'required' => true,
'default' => null,
'signed' => true,
Expand All @@ -1022,7 +1026,7 @@ public function testExceptionWidthLimit(): void
$attribute = new Document([
'$id' => ID::custom('breaking'),
'type' => Database::VAR_STRING,
'size' => 200,
'size' => $database->getAdapter()->getMaxVarcharLength(),
'required' => true,
'default' => null,
'signed' => true,
Expand All @@ -1039,7 +1043,7 @@ public function testExceptionWidthLimit(): void
}

try {
$database->createAttribute($collection->getId(), 'breaking', Database::VAR_STRING, 200, true);
$database->createAttribute($collection->getId(), 'breaking', Database::VAR_STRING, $database->getAdapter()->getMaxVarcharLength(), true);
$this->fail('Failed to throw exception');
} catch (\Throwable $e) {
$this->assertInstanceOf(LimitException::class, $e);
Expand Down Expand Up @@ -1074,7 +1078,7 @@ public function testUpdateAttributeSize(): void
// Go up in size

// 0-16381 to 16382-65535
$document = $this->updateStringAttributeSize(16382, $document);
$document = $this->updateStringAttributeSize(20000, $document);

// 16382-65535 to 65536-16777215
$document = $this->updateStringAttributeSize(65536, $document);
Expand Down
36 changes: 5 additions & 31 deletions tests/e2e/Adapter/Scopes/CollectionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,45 +605,19 @@ public function testRowSizeToLarge(): void
* 65535 / 4 = 16383 MB4
*/
$collection_1 = $database->createCollection('row_size_1');
$collection_2 = $database->createCollection('row_size_2');

$this->assertEquals(true, $database->createAttribute($collection_1->getId(), 'attr_1', Database::VAR_STRING, 16000, true));
$limit = floor(($database->getAdapter()->getDocumentSizeLimit() / 4) / $database->getAdapter()->getMaxVarcharLength());

try {
$database->createAttribute($collection_1->getId(), 'attr_2', Database::VAR_STRING, Database::LENGTH_KEY, true);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(LimitException::class, $e);
for ($i = 1; $i < $limit; $i++) {
$this->assertEquals(true, $database->createAttribute($collection_1->getId(), 'attr_'.$i, Database::VAR_STRING, $database->getAdapter()->getMaxVarcharLength(), true));
}

/**
* Relation takes length of Database::LENGTH_KEY so exceeding getDocumentSizeLimit
*/

try {
$database->createRelationship(
collection: $collection_2->getId(),
relatedCollection: $collection_1->getId(),
type: Database::RELATION_ONE_TO_ONE,
twoWay: true,
);

$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(LimitException::class, $e);
}

try {
$database->createRelationship(
collection: $collection_1->getId(),
relatedCollection: $collection_2->getId(),
type: Database::RELATION_ONE_TO_ONE,
twoWay: true,
);

$this->assertEquals(true, $database->createAttribute($collection_1->getId(), 'attr_100', Database::VAR_STRING, $database->getAdapter()->getMaxVarcharLength(), true));
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertInstanceOf(LimitException::class, $e);
$this->assertEquals('Row width limit reached. Cannot create new attribute.', $e->getMessage());
}
}

Expand Down