mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #13441 from appwrite/create-collection-integer-format-options
Column boundaries for Integer attributes
This commit is contained in:
6 files changed
+326
-4
No files matched your search
+4
-1
@@ -94,7 +94,10 @@ class Create extends Action
|
||||
throw new Exception($this->getInvalidValueException(), $validator->getDescription());
|
||||
}
|
||||
|
||||
$size = $max > 2147483647 ? 8 : 4;
|
||||
// The 4 byte column only holds a range that fits INT32. min counts: a
|
||||
// column bounded below -2147483648 has to be able to store that value,
|
||||
// and with min left out the bound is PHP_INT_MIN.
|
||||
$size = $min >= -2147483648 && $max <= 2147483647 ? 4 : 8;
|
||||
|
||||
$attribute = $this->createAttribute($databaseId, $collectionId, new Document([
|
||||
'key' => $key,
|
||||
|
||||
@@ -297,16 +297,24 @@ class Create extends Action
|
||||
$formatOptions = ['elements' => $attribute['elements']];
|
||||
}
|
||||
|
||||
if (isset($attribute['min']) || isset($attribute['max'])) {
|
||||
// The dedicated endpoints store a range on every numeric attribute, falling
|
||||
// back to the full width of the type, so omitting min/max here has to produce
|
||||
// the same document rather than one with no range at all.
|
||||
if (\in_array($type, [Database::VAR_INTEGER, Database::VAR_BIGINT, Database::VAR_FLOAT])) {
|
||||
$isFloat = $type === Database::VAR_FLOAT;
|
||||
|
||||
$format = match($type) {
|
||||
Database::VAR_INTEGER => APP_DATABASE_ATTRIBUTE_INT_RANGE,
|
||||
Database::VAR_BIGINT => APP_DATABASE_ATTRIBUTE_BIGINT_RANGE,
|
||||
default => APP_DATABASE_ATTRIBUTE_FLOAT_RANGE,
|
||||
};
|
||||
|
||||
$min = $attribute['min'] ?? ($isFloat ? -\PHP_FLOAT_MAX : \PHP_INT_MIN);
|
||||
$max = $attribute['max'] ?? ($isFloat ? \PHP_FLOAT_MAX : \PHP_INT_MAX);
|
||||
|
||||
$formatOptions = [
|
||||
'min' => $attribute['min'] ?? ($type === Database::VAR_INTEGER || $type === Database::VAR_BIGINT ? \PHP_INT_MIN : -\PHP_FLOAT_MAX),
|
||||
'max' => $attribute['max'] ?? ($type === Database::VAR_INTEGER || $type === Database::VAR_BIGINT ? \PHP_INT_MAX : \PHP_FLOAT_MAX),
|
||||
'min' => $isFloat ? \floatval($min) : $min,
|
||||
'max' => $isFloat ? \floatval($max) : $max,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@ class Attribute
|
||||
Database::VAR_TEXT => 65535,
|
||||
Database::VAR_MEDIUMTEXT => 16777215,
|
||||
Database::VAR_LONGTEXT => 2147483647,
|
||||
// Bytes, the widths createBigIntColumn and createFloatColumn hardcode.
|
||||
// Every adapter maps these two types without consulting the size.
|
||||
Database::VAR_BIGINT => 8,
|
||||
Database::VAR_FLOAT => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -91,6 +95,18 @@ class Attribute
|
||||
$size = self::FORMAT_SIZES[$format] ?? $size;
|
||||
}
|
||||
|
||||
if ($type === Database::VAR_INTEGER) {
|
||||
// Same width createIntegerColumn picks. That endpoint takes no size at
|
||||
// all, so a size sent inline is ignored rather than left to promise a
|
||||
// range the column cannot hold: the 4 byte column only holds a range
|
||||
// that fits INT32, and a bound left out means the int64 edge, which
|
||||
// does not.
|
||||
$min = $attribute['min'] ?? \PHP_INT_MIN;
|
||||
$max = $attribute['max'] ?? \PHP_INT_MAX;
|
||||
$fitsInt32 = \is_int($min) && \is_int($max) && $min >= -2147483648 && $max <= 2147483647;
|
||||
$size = $fitsInt32 ? 4 : 8;
|
||||
}
|
||||
|
||||
return [
|
||||
'type' => $type,
|
||||
'format' => $format,
|
||||
|
||||
@@ -8,6 +8,8 @@ use Utopia\Database\Validator\Datetime as DatetimeValidator;
|
||||
use Utopia\Database\Validator\Key;
|
||||
use Utopia\Emails\Validator\Email;
|
||||
use Utopia\Validator;
|
||||
use Utopia\Validator\FloatValidator;
|
||||
use Utopia\Validator\Integer;
|
||||
use Utopia\Validator\IP;
|
||||
use Utopia\Validator\Range;
|
||||
use Utopia\Validator\Text;
|
||||
@@ -204,6 +206,21 @@ class Attributes extends Validator
|
||||
return false;
|
||||
}
|
||||
|
||||
// The dedicated endpoints bound these with Integer(false, 64) / FloatValidator.
|
||||
// Without the same check a JSON number past PHP_INT_MAX decodes to a float and is
|
||||
// stored verbatim, so an int64 bound round-tripped through a client that cannot
|
||||
// hold it lands in formatOptions as 9.223372036854776e+18.
|
||||
$boundValidator = $type === Database::VAR_FLOAT
|
||||
? new FloatValidator()
|
||||
: new Integer(false, 64);
|
||||
|
||||
foreach (['min', 'max'] as $bound) {
|
||||
if (isset($attribute[$bound]) && !$boundValidator->isValid($attribute[$bound])) {
|
||||
$this->message = "Attribute '" . $attribute['key'] . "': " . $bound . ' is invalid. ' . $boundValidator->getDescription();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// If both are set, validate ordering
|
||||
if (isset($attribute['min']) && isset($attribute['max']) && $attribute['min'] > $attribute['max']) {
|
||||
$this->message = "Attribute '" . $attribute['key'] . "': minimum value must be less than or equal to maximum value";
|
||||
|
||||
@@ -1223,6 +1223,177 @@ trait DatabasesBase
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An inline numeric definition on create collection has to produce the same
|
||||
* document the dedicated endpoints write: the full width of the type when no
|
||||
* bounds are given, on a column wide enough to hold it.
|
||||
*/
|
||||
public function testCreateCollectionInlineNumericRange(): void
|
||||
{
|
||||
if (!$this->getSupportForAttributes()) {
|
||||
$this->markTestSkipped('Attributes are not supported by this database adapter');
|
||||
}
|
||||
|
||||
$data = $this->setupDatabase();
|
||||
$databaseId = $data['databaseId'];
|
||||
$headers = [
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
'x-appwrite-key' => $this->getProject()['apiKey'],
|
||||
];
|
||||
$schemaResource = $this->getSchemaResource();
|
||||
|
||||
$container = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), $headers, [
|
||||
$this->getContainerIdParam() => ID::unique(),
|
||||
'name' => 'Counters',
|
||||
'permissions' => [
|
||||
Permission::create(Role::any()),
|
||||
Permission::read(Role::any()),
|
||||
],
|
||||
$schemaResource => [
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER],
|
||||
['key' => 'total', 'type' => Database::VAR_BIGINT],
|
||||
['key' => 'ratio', 'type' => Database::VAR_FLOAT],
|
||||
['key' => 'bounded', 'type' => Database::VAR_INTEGER, 'min' => 0, 'max' => 100],
|
||||
// The numeric endpoints take no size, so this one cannot narrow the
|
||||
// column below the int64 range the definition still declares
|
||||
['key' => 'sized', 'type' => Database::VAR_INTEGER, 'size' => 4],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEquals(201, $container['headers']['status-code']);
|
||||
$containerId = $container['body']['$id'];
|
||||
|
||||
$schema = $this->client->call(Client::METHOD_GET, $this->getSchemaUrl($databaseId, $containerId), $headers);
|
||||
|
||||
$this->assertEquals(200, $schema['headers']['status-code']);
|
||||
|
||||
$byKey = [];
|
||||
foreach ($schema['body'][$schemaResource] as $attribute) {
|
||||
$byKey[$attribute['key']] = $attribute;
|
||||
}
|
||||
|
||||
$this->assertSame(\PHP_INT_MIN, $byKey['sized']['min']);
|
||||
$this->assertSame(\PHP_INT_MAX, $byKey['sized']['max']);
|
||||
$this->assertSame(\PHP_INT_MIN, $byKey['counter']['min']);
|
||||
$this->assertSame(\PHP_INT_MAX, $byKey['counter']['max']);
|
||||
$this->assertSame(\PHP_INT_MIN, $byKey['total']['min']);
|
||||
$this->assertSame(\PHP_INT_MAX, $byKey['total']['max']);
|
||||
$this->assertSame(-\PHP_FLOAT_MAX, $byKey['ratio']['min']);
|
||||
$this->assertSame(\PHP_FLOAT_MAX, $byKey['ratio']['max']);
|
||||
|
||||
// A bound that was asked for is kept as asked for
|
||||
$this->assertSame(0, $byKey['bounded']['min']);
|
||||
$this->assertSame(100, $byKey['bounded']['max']);
|
||||
|
||||
// The bounds are only honest if the column is that wide. 5e9 overflows the
|
||||
// 4 byte column an implied INT32 range would have produced.
|
||||
$record = $this->client->call(Client::METHOD_POST, $this->getRecordUrl($databaseId, $containerId), $headers, [
|
||||
$this->getRecordIdParam() => ID::unique(),
|
||||
'data' => [
|
||||
'counter' => 5000000000,
|
||||
'total' => \PHP_INT_MAX,
|
||||
'sized' => 5000000000,
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEquals(201, $record['headers']['status-code']);
|
||||
$this->assertSame(5000000000, $record['body']['counter']);
|
||||
$this->assertSame(\PHP_INT_MAX, $record['body']['total']);
|
||||
$this->assertSame(5000000000, $record['body']['sized']);
|
||||
|
||||
// A value outside a declared bound is still refused
|
||||
$rejected = $this->client->call(Client::METHOD_POST, $this->getRecordUrl($databaseId, $containerId), $headers, [
|
||||
$this->getRecordIdParam() => ID::unique(),
|
||||
'data' => ['bounded' => 101],
|
||||
]);
|
||||
|
||||
$this->assertEquals(400, $rejected['headers']['status-code']);
|
||||
|
||||
// Same type, same collection, through the dedicated endpoint: the two paths
|
||||
// have to agree on what "no bounds" means
|
||||
$dedicated = $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $containerId, 'integer'), $headers, [
|
||||
'key' => 'dedicated',
|
||||
'required' => false,
|
||||
]);
|
||||
|
||||
$this->assertEquals(202, $dedicated['headers']['status-code']);
|
||||
$this->assertSame($byKey['counter']['min'], $dedicated['body']['min']);
|
||||
$this->assertSame($byKey['counter']['max'], $dedicated['body']['max']);
|
||||
}
|
||||
|
||||
/**
|
||||
* A JSON number past PHP_INT_MAX decodes to a float. The dedicated endpoints
|
||||
* refuse it; an inline definition used to store it, leaving formatOptions
|
||||
* holding 9.223372036854776e+18 on a column typed as an integer.
|
||||
*/
|
||||
public function testCreateCollectionInlineBoundBeyondInt64(): void
|
||||
{
|
||||
if (!$this->getSupportForAttributes()) {
|
||||
$this->markTestSkipped('Attributes are not supported by this database adapter');
|
||||
}
|
||||
|
||||
$data = $this->setupDatabase();
|
||||
$databaseId = $data['databaseId'];
|
||||
$headers = [
|
||||
'content-type' => 'application/json',
|
||||
'x-appwrite-project' => $this->getProject()['$id'],
|
||||
'x-appwrite-key' => $this->getProject()['apiKey'],
|
||||
];
|
||||
$schemaResource = $this->getSchemaResource();
|
||||
$containerId = ID::unique();
|
||||
|
||||
$container = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), $headers, [
|
||||
$this->getContainerIdParam() => $containerId,
|
||||
'name' => 'Overflowing Bounds',
|
||||
$schemaResource => [
|
||||
// What a client that cannot hold an int64 sends back after reading
|
||||
// the default bounds off an existing column
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER, 'min' => -9223372036854776000, 'max' => 9223372036854776000],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEquals(400, $container['headers']['status-code']);
|
||||
$this->assertStringContainsString("Attribute 'counter'", (string) $container['body']['message']);
|
||||
|
||||
// The rejected definition must not leave a collection behind
|
||||
$missing = $this->client->call(Client::METHOD_GET, $this->getContainerUrl($databaseId, $containerId), $headers);
|
||||
$this->assertEquals(404, $missing['headers']['status-code']);
|
||||
|
||||
// A bound sent as a string is not an integer either
|
||||
$stringBound = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), $headers, [
|
||||
$this->getContainerIdParam() => ID::unique(),
|
||||
'name' => 'String Bounds',
|
||||
$schemaResource => [
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER, 'max' => '9223372036854776000'],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEquals(400, $stringBound['headers']['status-code']);
|
||||
$this->assertStringContainsString("Attribute 'counter'", (string) $stringBound['body']['message']);
|
||||
|
||||
// The dedicated endpoint is the reference: it refuses the same bound
|
||||
$valid = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), $headers, [
|
||||
$this->getContainerIdParam() => ID::unique(),
|
||||
'name' => 'Dedicated Bounds',
|
||||
]);
|
||||
|
||||
$this->assertEquals(201, $valid['headers']['status-code']);
|
||||
|
||||
$attribute = $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $valid['body']['$id'], 'integer'), $headers, [
|
||||
'key' => 'counter',
|
||||
'required' => false,
|
||||
'max' => 9223372036854776000,
|
||||
]);
|
||||
|
||||
$this->assertEquals(400, $attribute['headers']['status-code']);
|
||||
|
||||
$schema = $this->client->call(Client::METHOD_GET, $this->getSchemaUrl($databaseId, $valid['body']['$id']), $headers);
|
||||
|
||||
$this->assertEquals(200, $schema['headers']['status-code']);
|
||||
$this->assertEquals(0, $schema['body']['total']);
|
||||
}
|
||||
|
||||
public function testListAttributes(): void
|
||||
{
|
||||
if (!$this->getSupportForAttributes()) {
|
||||
|
||||
@@ -120,6 +120,113 @@ final class AttributesTest extends TestCase
|
||||
['type' => Database::VAR_STRING, 'format' => APP_DATABASE_ATTRIBUTE_ENUM, 'size' => Database::LENGTH_KEY],
|
||||
Attribute::resolve(['key' => 'enum', 'type' => APP_DATABASE_ATTRIBUTE_ENUM])
|
||||
);
|
||||
|
||||
// createIntegerColumn sizes the column off max, defaulting to the int64
|
||||
// range, and createBigIntColumn is always 8 bytes
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 8],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 8],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER, 'max' => 3000000000])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 4],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER, 'min' => 0, 'max' => 100])
|
||||
);
|
||||
|
||||
// Both edges of the declared range have to be storable. A max on its own
|
||||
// leaves min at PHP_INT_MIN, and a min below INT32 needs the wide column
|
||||
// however small max is.
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 8],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER, 'max' => 100])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 8],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER, 'min' => -5000000000, 'max' => 100])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 4],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER, 'min' => -2147483648, 'max' => 2147483647])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_BIGINT, 'format' => '', 'size' => 8],
|
||||
Attribute::resolve(['key' => 'total', 'type' => Database::VAR_BIGINT])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_FLOAT, 'format' => '', 'size' => 0],
|
||||
Attribute::resolve(['key' => 'ratio', 'type' => Database::VAR_FLOAT])
|
||||
);
|
||||
|
||||
// None of the numeric endpoints takes a size. A size sent inline must not
|
||||
// narrow the column below the range the same definition declares.
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 8],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER, 'size' => 4])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_INTEGER, 'format' => '', 'size' => 4],
|
||||
Attribute::resolve(['key' => 'counter', 'type' => Database::VAR_INTEGER, 'size' => 8, 'min' => 0, 'max' => 100])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_BIGINT, 'format' => '', 'size' => 8],
|
||||
Attribute::resolve(['key' => 'total', 'type' => Database::VAR_BIGINT, 'size' => 4])
|
||||
);
|
||||
|
||||
$this->assertEquals(
|
||||
['type' => Database::VAR_FLOAT, 'format' => '', 'size' => 0],
|
||||
Attribute::resolve(['key' => 'ratio', 'type' => Database::VAR_FLOAT, 'size' => 4])
|
||||
);
|
||||
}
|
||||
|
||||
public function testNumericBoundsMustFitTheType(): void
|
||||
{
|
||||
$this->assertTrue($this->object->isValid([
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER, 'min' => 0, 'max' => 100],
|
||||
['key' => 'total', 'type' => Database::VAR_BIGINT, 'min' => \PHP_INT_MIN, 'max' => \PHP_INT_MAX],
|
||||
['key' => 'ratio', 'type' => Database::VAR_FLOAT, 'min' => -1.5, 'max' => 1.5],
|
||||
]), $this->object->getDescription());
|
||||
|
||||
// 9223372036854776000 is what a client that rounds an int64 to a double
|
||||
// sends back after reading the default bounds off an existing column. PHP
|
||||
// decodes it as a float, and storing it leaves an integer column bounded
|
||||
// by 9.223372036854776e+18.
|
||||
$this->assertFalse($this->object->isValid([
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER, 'min' => -9223372036854776000, 'max' => 9223372036854776000],
|
||||
]));
|
||||
$this->assertStringContainsString("Attribute 'counter': min is invalid", $this->object->getDescription());
|
||||
|
||||
$this->assertFalse($this->object->isValid([
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER, 'max' => 9223372036854776000],
|
||||
]));
|
||||
$this->assertStringContainsString("Attribute 'counter': max is invalid", $this->object->getDescription());
|
||||
|
||||
$this->assertFalse($this->object->isValid([
|
||||
['key' => 'total', 'type' => Database::VAR_BIGINT, 'max' => 9223372036854776000],
|
||||
]));
|
||||
|
||||
$this->assertFalse($this->object->isValid([
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER, 'max' => 1.5],
|
||||
]));
|
||||
|
||||
$this->assertFalse($this->object->isValid([
|
||||
['key' => 'counter', 'type' => Database::VAR_INTEGER, 'max' => '100'],
|
||||
]));
|
||||
|
||||
// A float column carries the same bound as a double, which is what it is
|
||||
$this->assertTrue($this->object->isValid([
|
||||
['key' => 'ratio', 'type' => Database::VAR_FLOAT, 'max' => 9223372036854776000],
|
||||
]), $this->object->getDescription());
|
||||
}
|
||||
|
||||
public function testResolveKeepsExplicitSize(): void
|
||||
|
||||
Reference in new issue
Block a user